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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
| 157 | module_param_array(index, int, NULL, 0444); |
| 158 | MODULE_PARM_DESC(index, "Index value for dummy soundcard."); |
| 159 | module_param_array(id, charp, NULL, 0444); |
| 160 | MODULE_PARM_DESC(id, "ID string for dummy soundcard."); |
| 161 | module_param_array(enable, bool, NULL, 0444); |
| 162 | MODULE_PARM_DESC(enable, "Enable this dummy soundcard."); |
| 163 | module_param_array(pcm_devs, int, NULL, 0444); |
| 164 | MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver."); |
| 165 | module_param_array(pcm_substreams, int, NULL, 0444); |
| 166 | MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-16) for dummy driver."); |
| 167 | //module_param_array(midi_devs, int, NULL, 0444); |
| 168 | //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver."); |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 169 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 170 | module_param(hrtimer, bool, 0644); |
| 171 | MODULE_PARM_DESC(hrtimer, "Use hrtimer as the timer source."); |
| 172 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 174 | static struct platform_device *devices[SNDRV_CARDS]; |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | #define MIXER_ADDR_MASTER 0 |
| 177 | #define MIXER_ADDR_LINE 1 |
| 178 | #define MIXER_ADDR_MIC 2 |
| 179 | #define MIXER_ADDR_SYNTH 3 |
| 180 | #define MIXER_ADDR_CD 4 |
| 181 | #define MIXER_ADDR_LAST 4 |
| 182 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 183 | struct dummy_timer_ops { |
| 184 | int (*create)(struct snd_pcm_substream *); |
| 185 | void (*free)(struct snd_pcm_substream *); |
| 186 | int (*prepare)(struct snd_pcm_substream *); |
| 187 | int (*start)(struct snd_pcm_substream *); |
| 188 | int (*stop)(struct snd_pcm_substream *); |
| 189 | snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *); |
| 190 | }; |
| 191 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 192 | struct snd_dummy { |
| 193 | struct snd_card *card; |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 194 | struct snd_pcm *pcm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | spinlock_t mixer_lock; |
| 196 | int mixer_volume[MIXER_ADDR_LAST+1][2]; |
| 197 | int capture_source[MIXER_ADDR_LAST+1][2]; |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 198 | const struct dummy_timer_ops *timer_ops; |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 199 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 201 | /* |
| 202 | * system timer interface |
| 203 | */ |
| 204 | |
| 205 | struct dummy_systimer_pcm { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | spinlock_t lock; |
| 207 | struct timer_list timer; |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 208 | unsigned long base_time; |
| 209 | unsigned int frac_pos; /* fractional sample position (based HZ) */ |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame^] | 210 | unsigned int frac_period_rest; |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 211 | unsigned int frac_buffer_size; /* buffer_size * HZ */ |
| 212 | unsigned int frac_period_size; /* period_size * HZ */ |
| 213 | unsigned int rate; |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame^] | 214 | int elapsed; |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 215 | struct snd_pcm_substream *substream; |
| 216 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 218 | static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm) |
| 219 | { |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 220 | dpcm->timer.expires = jiffies + |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame^] | 221 | (dpcm->frac_period_rest + dpcm->rate - 1) / dpcm->rate; |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 222 | add_timer(&dpcm->timer); |
| 223 | } |
| 224 | |
| 225 | static void dummy_systimer_update(struct dummy_systimer_pcm *dpcm) |
| 226 | { |
| 227 | unsigned long delta; |
| 228 | |
| 229 | delta = jiffies - dpcm->base_time; |
| 230 | if (!delta) |
| 231 | return; |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame^] | 232 | dpcm->base_time += delta; |
| 233 | delta *= dpcm->rate; |
| 234 | dpcm->frac_pos += delta; |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 235 | while (dpcm->frac_pos >= dpcm->frac_buffer_size) |
| 236 | dpcm->frac_pos -= dpcm->frac_buffer_size; |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame^] | 237 | while (dpcm->frac_period_rest <= delta) { |
| 238 | dpcm->elapsed++; |
| 239 | dpcm->frac_period_rest += dpcm->frac_period_size; |
| 240 | } |
| 241 | dpcm->frac_period_rest -= delta; |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 244 | static int dummy_systimer_start(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 246 | struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; |
| 247 | spin_lock(&dpcm->lock); |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 248 | dpcm->base_time = jiffies; |
| 249 | dummy_systimer_rearm(dpcm); |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 250 | spin_unlock(&dpcm->lock); |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 251 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 254 | static int dummy_systimer_stop(struct snd_pcm_substream *substream) |
| 255 | { |
| 256 | struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; |
| 257 | spin_lock(&dpcm->lock); |
| 258 | del_timer(&dpcm->timer); |
| 259 | spin_unlock(&dpcm->lock); |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static int dummy_systimer_prepare(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 265 | struct snd_pcm_runtime *runtime = substream->runtime; |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 266 | struct dummy_systimer_pcm *dpcm = runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 268 | dpcm->frac_pos = 0; |
| 269 | dpcm->rate = runtime->rate; |
| 270 | dpcm->frac_buffer_size = runtime->buffer_size * HZ; |
| 271 | dpcm->frac_period_size = runtime->period_size * HZ; |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame^] | 272 | dpcm->frac_period_rest = dpcm->frac_period_size; |
| 273 | dpcm->elapsed = 0; |
Ahmet Ä°nan | 53463a8 | 2008-02-21 07:55:30 +0100 | [diff] [blame] | 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | return 0; |
| 276 | } |
| 277 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 278 | static void dummy_systimer_callback(unsigned long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 280 | struct dummy_systimer_pcm *dpcm = (struct dummy_systimer_pcm *)data; |
Takashi Iwai | b32425a | 2005-11-18 18:52:14 +0100 | [diff] [blame] | 281 | unsigned long flags; |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame^] | 282 | int elapsed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
Takashi Iwai | b32425a | 2005-11-18 18:52:14 +0100 | [diff] [blame] | 284 | spin_lock_irqsave(&dpcm->lock, flags); |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 285 | dummy_systimer_update(dpcm); |
| 286 | dummy_systimer_rearm(dpcm); |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame^] | 287 | elapsed = dpcm->elapsed; |
| 288 | dpcm->elapsed = 0; |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 289 | spin_unlock_irqrestore(&dpcm->lock, flags); |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame^] | 290 | if (elapsed) |
| 291 | snd_pcm_period_elapsed(dpcm->substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 294 | static snd_pcm_uframes_t |
| 295 | dummy_systimer_pointer(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 297 | struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame^] | 298 | snd_pcm_uframes_t pos; |
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 | spin_lock(&dpcm->lock); |
| 301 | dummy_systimer_update(dpcm); |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame^] | 302 | pos = dpcm->frac_pos / HZ; |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 303 | spin_unlock(&dpcm->lock); |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame^] | 304 | return pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 307 | static int dummy_systimer_create(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 309 | struct dummy_systimer_pcm *dpcm; |
| 310 | |
| 311 | dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); |
| 312 | if (!dpcm) |
| 313 | return -ENOMEM; |
| 314 | substream->runtime->private_data = dpcm; |
| 315 | init_timer(&dpcm->timer); |
| 316 | dpcm->timer.data = (unsigned long) dpcm; |
| 317 | dpcm->timer.function = dummy_systimer_callback; |
| 318 | spin_lock_init(&dpcm->lock); |
| 319 | dpcm->substream = substream; |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | static void dummy_systimer_free(struct snd_pcm_substream *substream) |
| 324 | { |
| 325 | kfree(substream->runtime->private_data); |
| 326 | } |
| 327 | |
| 328 | static struct dummy_timer_ops dummy_systimer_ops = { |
| 329 | .create = dummy_systimer_create, |
| 330 | .free = dummy_systimer_free, |
| 331 | .prepare = dummy_systimer_prepare, |
| 332 | .start = dummy_systimer_start, |
| 333 | .stop = dummy_systimer_stop, |
| 334 | .pointer = dummy_systimer_pointer, |
| 335 | }; |
| 336 | |
| 337 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 338 | /* |
| 339 | * hrtimer interface |
| 340 | */ |
| 341 | |
| 342 | struct dummy_hrtimer_pcm { |
| 343 | ktime_t base_time; |
| 344 | ktime_t period_time; |
| 345 | atomic_t running; |
| 346 | struct hrtimer timer; |
| 347 | struct tasklet_struct tasklet; |
| 348 | struct snd_pcm_substream *substream; |
| 349 | }; |
| 350 | |
| 351 | static void dummy_hrtimer_pcm_elapsed(unsigned long priv) |
| 352 | { |
| 353 | struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv; |
| 354 | if (atomic_read(&dpcm->running)) |
| 355 | snd_pcm_period_elapsed(dpcm->substream); |
| 356 | } |
| 357 | |
| 358 | static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer) |
| 359 | { |
| 360 | struct dummy_hrtimer_pcm *dpcm; |
| 361 | |
| 362 | dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer); |
| 363 | if (!atomic_read(&dpcm->running)) |
| 364 | return HRTIMER_NORESTART; |
| 365 | tasklet_schedule(&dpcm->tasklet); |
| 366 | hrtimer_forward_now(timer, dpcm->period_time); |
| 367 | return HRTIMER_RESTART; |
| 368 | } |
| 369 | |
| 370 | static int dummy_hrtimer_start(struct snd_pcm_substream *substream) |
| 371 | { |
| 372 | struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; |
| 373 | |
| 374 | dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer); |
| 375 | hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL); |
| 376 | atomic_set(&dpcm->running, 1); |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | static int dummy_hrtimer_stop(struct snd_pcm_substream *substream) |
| 381 | { |
| 382 | struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; |
| 383 | |
| 384 | atomic_set(&dpcm->running, 0); |
| 385 | hrtimer_cancel(&dpcm->timer); |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm) |
| 390 | { |
| 391 | tasklet_kill(&dpcm->tasklet); |
| 392 | } |
| 393 | |
| 394 | static snd_pcm_uframes_t |
| 395 | dummy_hrtimer_pointer(struct snd_pcm_substream *substream) |
| 396 | { |
| 397 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 398 | struct dummy_hrtimer_pcm *dpcm = runtime->private_data; |
| 399 | u64 delta; |
| 400 | u32 pos; |
| 401 | |
| 402 | delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer), |
| 403 | dpcm->base_time); |
| 404 | delta = div_u64(delta * runtime->rate + 999999, 1000000); |
| 405 | div_u64_rem(delta, runtime->buffer_size, &pos); |
| 406 | return pos; |
| 407 | } |
| 408 | |
| 409 | static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream) |
| 410 | { |
| 411 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 412 | struct dummy_hrtimer_pcm *dpcm = runtime->private_data; |
| 413 | unsigned int period, rate; |
| 414 | long sec; |
| 415 | unsigned long nsecs; |
| 416 | |
| 417 | dummy_hrtimer_sync(dpcm); |
| 418 | period = runtime->period_size; |
| 419 | rate = runtime->rate; |
| 420 | sec = period / rate; |
| 421 | period %= rate; |
| 422 | nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate); |
| 423 | dpcm->period_time = ktime_set(sec, nsecs); |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | static int dummy_hrtimer_create(struct snd_pcm_substream *substream) |
| 429 | { |
| 430 | struct dummy_hrtimer_pcm *dpcm; |
| 431 | |
| 432 | dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); |
| 433 | if (!dpcm) |
| 434 | return -ENOMEM; |
| 435 | substream->runtime->private_data = dpcm; |
| 436 | hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 437 | dpcm->timer.function = dummy_hrtimer_callback; |
| 438 | dpcm->substream = substream; |
| 439 | atomic_set(&dpcm->running, 0); |
| 440 | tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed, |
| 441 | (unsigned long)dpcm); |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | static void dummy_hrtimer_free(struct snd_pcm_substream *substream) |
| 446 | { |
| 447 | struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; |
| 448 | dummy_hrtimer_sync(dpcm); |
| 449 | kfree(dpcm); |
| 450 | } |
| 451 | |
| 452 | static struct dummy_timer_ops dummy_hrtimer_ops = { |
| 453 | .create = dummy_hrtimer_create, |
| 454 | .free = dummy_hrtimer_free, |
| 455 | .prepare = dummy_hrtimer_prepare, |
| 456 | .start = dummy_hrtimer_start, |
| 457 | .stop = dummy_hrtimer_stop, |
| 458 | .pointer = dummy_hrtimer_pointer, |
| 459 | }; |
| 460 | |
| 461 | #endif /* CONFIG_HIGH_RES_TIMERS */ |
| 462 | |
| 463 | /* |
| 464 | * PCM interface |
| 465 | */ |
| 466 | |
| 467 | static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 468 | { |
| 469 | struct snd_dummy *dummy = snd_pcm_substream_chip(substream); |
| 470 | |
| 471 | switch (cmd) { |
| 472 | case SNDRV_PCM_TRIGGER_START: |
| 473 | case SNDRV_PCM_TRIGGER_RESUME: |
| 474 | return dummy->timer_ops->start(substream); |
| 475 | case SNDRV_PCM_TRIGGER_STOP: |
| 476 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 477 | return dummy->timer_ops->stop(substream); |
| 478 | } |
| 479 | return -EINVAL; |
| 480 | } |
| 481 | |
| 482 | static int dummy_pcm_prepare(struct snd_pcm_substream *substream) |
| 483 | { |
| 484 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 485 | struct snd_dummy *dummy = snd_pcm_substream_chip(substream); |
| 486 | |
| 487 | snd_pcm_format_set_silence(runtime->format, runtime->dma_area, |
| 488 | bytes_to_samples(runtime, runtime->dma_bytes)); |
| 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 | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 521 | return snd_pcm_lib_malloc_pages(substream, |
| 522 | params_buffer_bytes(hw_params)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 525 | static int dummy_pcm_hw_free(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | { |
| 527 | return snd_pcm_lib_free_pages(substream); |
| 528 | } |
| 529 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 530 | static int dummy_pcm_open(struct snd_pcm_substream *substream) |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 531 | { |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 532 | struct snd_dummy *dummy = snd_pcm_substream_chip(substream); |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 533 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | int err; |
| 535 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 536 | dummy->timer_ops = &dummy_systimer_ops; |
| 537 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 538 | if (hrtimer) |
| 539 | dummy->timer_ops = &dummy_hrtimer_ops; |
| 540 | #endif |
| 541 | |
| 542 | err = dummy->timer_ops->create(substream); |
| 543 | if (err < 0) |
| 544 | return err; |
| 545 | |
| 546 | runtime->hw = dummy_pcm_hardware; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | if (substream->pcm->device & 1) { |
| 548 | runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED; |
| 549 | runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED; |
| 550 | } |
| 551 | if (substream->pcm->device & 2) |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 552 | runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP | |
| 553 | SNDRV_PCM_INFO_MMAP_VALID); |
| 554 | |
| 555 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 556 | err = add_playback_constraints(substream->runtime); |
| 557 | else |
| 558 | err = add_capture_constraints(substream->runtime); |
| 559 | if (err < 0) { |
| 560 | dummy->timer_ops->free(substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | return 0; |
| 564 | } |
| 565 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 566 | static int dummy_pcm_close(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | { |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 568 | struct snd_dummy *dummy = snd_pcm_substream_chip(substream); |
| 569 | dummy->timer_ops->free(substream); |
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 struct snd_pcm_ops dummy_pcm_ops = { |
| 574 | .open = dummy_pcm_open, |
| 575 | .close = dummy_pcm_close, |
| 576 | .ioctl = snd_pcm_lib_ioctl, |
| 577 | .hw_params = dummy_pcm_hw_params, |
| 578 | .hw_free = dummy_pcm_hw_free, |
| 579 | .prepare = dummy_pcm_prepare, |
| 580 | .trigger = dummy_pcm_trigger, |
| 581 | .pointer = dummy_pcm_pointer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | }; |
| 583 | |
Prarit Bhargava | 788c604 | 2007-02-13 13:11:11 +0100 | [diff] [blame] | 584 | static int __devinit snd_card_dummy_pcm(struct snd_dummy *dummy, int device, |
| 585 | int substreams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 587 | struct snd_pcm *pcm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | int err; |
| 589 | |
Jaroslav Kysela | 1a11cb6 | 2008-08-15 12:59:02 +0200 | [diff] [blame] | 590 | err = snd_pcm_new(dummy->card, "Dummy PCM", device, |
| 591 | substreams, substreams, &pcm); |
| 592 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | return err; |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 594 | dummy->pcm = pcm; |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 595 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &dummy_pcm_ops); |
| 596 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &dummy_pcm_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | pcm->private_data = dummy; |
| 598 | pcm->info_flags = 0; |
| 599 | strcpy(pcm->name, "Dummy PCM"); |
| 600 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, |
| 601 | snd_dma_continuous_data(GFP_KERNEL), |
| 602 | 0, 64*1024); |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | #define DUMMY_VOLUME(xname, xindex, addr) \ |
Takashi Iwai | fb567a8 | 2006-08-23 13:07:19 +0200 | [diff] [blame] | 607 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ |
| 608 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ |
| 609 | .name = xname, .index = xindex, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | .info = snd_dummy_volume_info, \ |
| 611 | .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \ |
Takashi Iwai | fb567a8 | 2006-08-23 13:07:19 +0200 | [diff] [blame] | 612 | .private_value = addr, \ |
| 613 | .tlv = { .p = db_scale_dummy } } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 615 | static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol, |
| 616 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | { |
| 618 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 619 | uinfo->count = 2; |
| 620 | uinfo->value.integer.min = -50; |
| 621 | uinfo->value.integer.max = 100; |
| 622 | return 0; |
| 623 | } |
| 624 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 625 | static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol, |
| 626 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 628 | struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | int addr = kcontrol->private_value; |
| 630 | |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 631 | spin_lock_irq(&dummy->mixer_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0]; |
| 633 | ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1]; |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 634 | spin_unlock_irq(&dummy->mixer_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | return 0; |
| 636 | } |
| 637 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 638 | static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol, |
| 639 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 641 | struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | int change, addr = kcontrol->private_value; |
| 643 | int left, right; |
| 644 | |
| 645 | left = ucontrol->value.integer.value[0]; |
| 646 | if (left < -50) |
| 647 | left = -50; |
| 648 | if (left > 100) |
| 649 | left = 100; |
| 650 | right = ucontrol->value.integer.value[1]; |
| 651 | if (right < -50) |
| 652 | right = -50; |
| 653 | if (right > 100) |
| 654 | right = 100; |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 655 | spin_lock_irq(&dummy->mixer_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | change = dummy->mixer_volume[addr][0] != left || |
| 657 | dummy->mixer_volume[addr][1] != right; |
| 658 | dummy->mixer_volume[addr][0] = left; |
| 659 | dummy->mixer_volume[addr][1] = right; |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 660 | spin_unlock_irq(&dummy->mixer_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | return change; |
| 662 | } |
| 663 | |
Takashi Iwai | 0cb29ea | 2007-01-29 15:33:49 +0100 | [diff] [blame] | 664 | static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0); |
Takashi Iwai | fb567a8 | 2006-08-23 13:07:19 +0200 | [diff] [blame] | 665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | #define DUMMY_CAPSRC(xname, xindex, addr) \ |
| 667 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ |
| 668 | .info = snd_dummy_capsrc_info, \ |
| 669 | .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \ |
| 670 | .private_value = addr } |
| 671 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 672 | #define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 674 | static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol, |
| 675 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 677 | struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | int addr = kcontrol->private_value; |
| 679 | |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 680 | spin_lock_irq(&dummy->mixer_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | ucontrol->value.integer.value[0] = dummy->capture_source[addr][0]; |
| 682 | ucontrol->value.integer.value[1] = dummy->capture_source[addr][1]; |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 683 | spin_unlock_irq(&dummy->mixer_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | return 0; |
| 685 | } |
| 686 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 687 | 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] | 688 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 689 | struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | int change, addr = kcontrol->private_value; |
| 691 | int left, right; |
| 692 | |
| 693 | left = ucontrol->value.integer.value[0] & 1; |
| 694 | right = ucontrol->value.integer.value[1] & 1; |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 695 | spin_lock_irq(&dummy->mixer_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | change = dummy->capture_source[addr][0] != left && |
| 697 | dummy->capture_source[addr][1] != right; |
| 698 | dummy->capture_source[addr][0] = left; |
| 699 | dummy->capture_source[addr][1] = right; |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 700 | spin_unlock_irq(&dummy->mixer_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | return change; |
| 702 | } |
| 703 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 704 | static struct snd_kcontrol_new snd_dummy_controls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER), |
| 706 | DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER), |
| 707 | DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH), |
Takashi Iwai | e05d696 | 2006-08-17 17:12:19 +0200 | [diff] [blame] | 708 | DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE), |
Takashi Iwai | e05d696 | 2006-08-17 17:12:19 +0200 | [diff] [blame] | 710 | DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC), |
Takashi Iwai | e05d696 | 2006-08-17 17:12:19 +0200 | [diff] [blame] | 712 | DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD), |
Takashi Iwai | e05d696 | 2006-08-17 17:12:19 +0200 | [diff] [blame] | 714 | DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | }; |
| 716 | |
Prarit Bhargava | 788c604 | 2007-02-13 13:11:11 +0100 | [diff] [blame] | 717 | static int __devinit snd_card_dummy_new_mixer(struct snd_dummy *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 719 | struct snd_card *card = dummy->card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | unsigned int idx; |
| 721 | int err; |
| 722 | |
Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 723 | if (snd_BUG_ON(!dummy)) |
| 724 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | spin_lock_init(&dummy->mixer_lock); |
| 726 | strcpy(card->mixername, "Dummy Mixer"); |
| 727 | |
| 728 | for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) { |
Jaroslav Kysela | 1a11cb6 | 2008-08-15 12:59:02 +0200 | [diff] [blame] | 729 | err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy)); |
| 730 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | return err; |
| 732 | } |
| 733 | return 0; |
| 734 | } |
| 735 | |
Prarit Bhargava | 788c604 | 2007-02-13 13:11:11 +0100 | [diff] [blame] | 736 | static int __devinit snd_dummy_probe(struct platform_device *devptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 738 | struct snd_card *card; |
| 739 | struct snd_dummy *dummy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | int idx, err; |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 741 | int dev = devptr->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
Takashi Iwai | bd7dd77 | 2008-12-28 16:45:02 +0100 | [diff] [blame] | 743 | err = snd_card_create(index[dev], id[dev], THIS_MODULE, |
| 744 | sizeof(struct snd_dummy), &card); |
| 745 | if (err < 0) |
| 746 | return err; |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 747 | dummy = card->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | dummy->card = card; |
| 749 | for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) { |
| 750 | if (pcm_substreams[dev] < 1) |
| 751 | pcm_substreams[dev] = 1; |
| 752 | if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS) |
| 753 | pcm_substreams[dev] = MAX_PCM_SUBSTREAMS; |
Jaroslav Kysela | 1a11cb6 | 2008-08-15 12:59:02 +0200 | [diff] [blame] | 754 | err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]); |
| 755 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | goto __nodev; |
| 757 | } |
Jaroslav Kysela | 1a11cb6 | 2008-08-15 12:59:02 +0200 | [diff] [blame] | 758 | err = snd_card_dummy_new_mixer(dummy); |
| 759 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | goto __nodev; |
| 761 | strcpy(card->driver, "Dummy"); |
| 762 | strcpy(card->shortname, "Dummy"); |
| 763 | sprintf(card->longname, "Dummy %i", dev + 1); |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 764 | |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 765 | snd_card_set_dev(card, &devptr->dev); |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 766 | |
Jaroslav Kysela | 1a11cb6 | 2008-08-15 12:59:02 +0200 | [diff] [blame] | 767 | err = snd_card_register(card); |
| 768 | if (err == 0) { |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 769 | platform_set_drvdata(devptr, card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | return 0; |
| 771 | } |
| 772 | __nodev: |
| 773 | snd_card_free(card); |
| 774 | return err; |
| 775 | } |
| 776 | |
Prarit Bhargava | 788c604 | 2007-02-13 13:11:11 +0100 | [diff] [blame] | 777 | static int __devexit snd_dummy_remove(struct platform_device *devptr) |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 778 | { |
| 779 | snd_card_free(platform_get_drvdata(devptr)); |
| 780 | platform_set_drvdata(devptr, NULL); |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | #ifdef CONFIG_PM |
| 785 | static int snd_dummy_suspend(struct platform_device *pdev, pm_message_t state) |
| 786 | { |
| 787 | struct snd_card *card = platform_get_drvdata(pdev); |
| 788 | struct snd_dummy *dummy = card->private_data; |
| 789 | |
| 790 | snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); |
| 791 | snd_pcm_suspend_all(dummy->pcm); |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | static int snd_dummy_resume(struct platform_device *pdev) |
| 796 | { |
| 797 | struct snd_card *card = platform_get_drvdata(pdev); |
| 798 | |
| 799 | snd_power_change_state(card, SNDRV_CTL_POWER_D0); |
| 800 | return 0; |
| 801 | } |
| 802 | #endif |
| 803 | |
| 804 | #define SND_DUMMY_DRIVER "snd_dummy" |
| 805 | |
| 806 | static struct platform_driver snd_dummy_driver = { |
| 807 | .probe = snd_dummy_probe, |
Prarit Bhargava | 788c604 | 2007-02-13 13:11:11 +0100 | [diff] [blame] | 808 | .remove = __devexit_p(snd_dummy_remove), |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 809 | #ifdef CONFIG_PM |
| 810 | .suspend = snd_dummy_suspend, |
| 811 | .resume = snd_dummy_resume, |
| 812 | #endif |
| 813 | .driver = { |
| 814 | .name = SND_DUMMY_DRIVER |
| 815 | }, |
| 816 | }; |
| 817 | |
Randy Dunlap | c12aad6 | 2007-06-25 12:08:01 +0200 | [diff] [blame] | 818 | static void snd_dummy_unregister_all(void) |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 819 | { |
| 820 | int i; |
| 821 | |
| 822 | for (i = 0; i < ARRAY_SIZE(devices); ++i) |
| 823 | platform_device_unregister(devices[i]); |
| 824 | platform_driver_unregister(&snd_dummy_driver); |
| 825 | } |
| 826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | static int __init alsa_card_dummy_init(void) |
| 828 | { |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 829 | int i, cards, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
Jaroslav Kysela | 1a11cb6 | 2008-08-15 12:59:02 +0200 | [diff] [blame] | 831 | err = platform_driver_register(&snd_dummy_driver); |
| 832 | if (err < 0) |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 833 | return err; |
| 834 | |
| 835 | cards = 0; |
Takashi Iwai | 8278ca8 | 2006-02-20 11:57:34 +0100 | [diff] [blame] | 836 | for (i = 0; i < SNDRV_CARDS; i++) { |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 837 | struct platform_device *device; |
Takashi Iwai | 8278ca8 | 2006-02-20 11:57:34 +0100 | [diff] [blame] | 838 | if (! enable[i]) |
| 839 | continue; |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 840 | device = platform_device_register_simple(SND_DUMMY_DRIVER, |
| 841 | i, NULL, 0); |
Rene Herman | a182ee9 | 2006-04-13 12:57:11 +0200 | [diff] [blame] | 842 | if (IS_ERR(device)) |
| 843 | continue; |
Rene Herman | 7152447 | 2006-04-13 12:58:06 +0200 | [diff] [blame] | 844 | if (!platform_get_drvdata(device)) { |
| 845 | platform_device_unregister(device); |
| 846 | continue; |
| 847 | } |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 848 | devices[i] = device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | cards++; |
| 850 | } |
| 851 | if (!cards) { |
| 852 | #ifdef MODULE |
| 853 | printk(KERN_ERR "Dummy soundcard not found or device busy\n"); |
| 854 | #endif |
Rene Herman | a182ee9 | 2006-04-13 12:57:11 +0200 | [diff] [blame] | 855 | snd_dummy_unregister_all(); |
| 856 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | static void __exit alsa_card_dummy_exit(void) |
| 862 | { |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 863 | snd_dummy_unregister_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | module_init(alsa_card_dummy_init) |
| 867 | module_exit(alsa_card_dummy_exit) |