Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Dummy soundcard |
| 3 | * Copyright (c) by Jaroslav Kysela <perex@suse.cz> |
| 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 | |
| 21 | #include <sound/driver.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/jiffies.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/time.h> |
| 26 | #include <linux/wait.h> |
| 27 | #include <linux/moduleparam.h> |
| 28 | #include <sound/core.h> |
| 29 | #include <sound/control.h> |
| 30 | #include <sound/pcm.h> |
| 31 | #include <sound/rawmidi.h> |
| 32 | #include <sound/initval.h> |
| 33 | |
| 34 | MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>"); |
| 35 | MODULE_DESCRIPTION("Dummy soundcard (/dev/null)"); |
| 36 | MODULE_LICENSE("GPL"); |
| 37 | MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}"); |
| 38 | |
| 39 | #define MAX_PCM_DEVICES 4 |
| 40 | #define MAX_PCM_SUBSTREAMS 16 |
| 41 | #define MAX_MIDI_DEVICES 2 |
| 42 | |
| 43 | #if 0 /* emu10k1 emulation */ |
| 44 | #define MAX_BUFFER_SIZE (128 * 1024) |
| 45 | static int emu10k1_playback_constraints(snd_pcm_runtime_t *runtime) |
| 46 | { |
| 47 | int err; |
| 48 | if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) |
| 49 | return err; |
| 50 | if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) |
| 51 | return err; |
| 52 | return 0; |
| 53 | } |
| 54 | #define add_playback_constraints emu10k1_playback_constraints |
| 55 | #endif |
| 56 | |
| 57 | #if 0 /* RME9652 emulation */ |
| 58 | #define MAX_BUFFER_SIZE (26 * 64 * 1024) |
| 59 | #define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE |
| 60 | #define USE_CHANNELS_MIN 26 |
| 61 | #define USE_CHANNELS_MAX 26 |
| 62 | #define USE_PERIODS_MIN 2 |
| 63 | #define USE_PERIODS_MAX 2 |
| 64 | #endif |
| 65 | |
| 66 | #if 0 /* ICE1712 emulation */ |
| 67 | #define MAX_BUFFER_SIZE (256 * 1024) |
| 68 | #define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE |
| 69 | #define USE_CHANNELS_MIN 10 |
| 70 | #define USE_CHANNELS_MAX 10 |
| 71 | #define USE_PERIODS_MIN 1 |
| 72 | #define USE_PERIODS_MAX 1024 |
| 73 | #endif |
| 74 | |
| 75 | #if 0 /* UDA1341 emulation */ |
| 76 | #define MAX_BUFFER_SIZE (16380) |
| 77 | #define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE |
| 78 | #define USE_CHANNELS_MIN 2 |
| 79 | #define USE_CHANNELS_MAX 2 |
| 80 | #define USE_PERIODS_MIN 2 |
| 81 | #define USE_PERIODS_MAX 255 |
| 82 | #endif |
| 83 | |
| 84 | #if 0 /* simple AC97 bridge (intel8x0) with 48kHz AC97 only codec */ |
| 85 | #define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE |
| 86 | #define USE_CHANNELS_MIN 2 |
| 87 | #define USE_CHANNELS_MAX 2 |
| 88 | #define USE_RATE SNDRV_PCM_RATE_48000 |
| 89 | #define USE_RATE_MIN 48000 |
| 90 | #define USE_RATE_MAX 48000 |
| 91 | #endif |
| 92 | |
| 93 | |
| 94 | /* defaults */ |
| 95 | #ifndef MAX_BUFFER_SIZE |
| 96 | #define MAX_BUFFER_SIZE (64*1024) |
| 97 | #endif |
| 98 | #ifndef USE_FORMATS |
| 99 | #define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE) |
| 100 | #endif |
| 101 | #ifndef USE_RATE |
| 102 | #define USE_RATE SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000 |
| 103 | #define USE_RATE_MIN 5500 |
| 104 | #define USE_RATE_MAX 48000 |
| 105 | #endif |
| 106 | #ifndef USE_CHANNELS_MIN |
| 107 | #define USE_CHANNELS_MIN 1 |
| 108 | #endif |
| 109 | #ifndef USE_CHANNELS_MAX |
| 110 | #define USE_CHANNELS_MAX 2 |
| 111 | #endif |
| 112 | #ifndef USE_PERIODS_MIN |
| 113 | #define USE_PERIODS_MIN 1 |
| 114 | #endif |
| 115 | #ifndef USE_PERIODS_MAX |
| 116 | #define USE_PERIODS_MAX 1024 |
| 117 | #endif |
| 118 | #ifndef add_playback_constraints |
| 119 | #define add_playback_constraints(x) 0 |
| 120 | #endif |
| 121 | #ifndef add_capture_constraints |
| 122 | #define add_capture_constraints(x) 0 |
| 123 | #endif |
| 124 | |
| 125 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ |
| 126 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ |
| 127 | static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0}; |
| 128 | static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; |
| 129 | static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8}; |
| 130 | //static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2}; |
| 131 | |
| 132 | module_param_array(index, int, NULL, 0444); |
| 133 | MODULE_PARM_DESC(index, "Index value for dummy soundcard."); |
| 134 | module_param_array(id, charp, NULL, 0444); |
| 135 | MODULE_PARM_DESC(id, "ID string for dummy soundcard."); |
| 136 | module_param_array(enable, bool, NULL, 0444); |
| 137 | MODULE_PARM_DESC(enable, "Enable this dummy soundcard."); |
| 138 | module_param_array(pcm_devs, int, NULL, 0444); |
| 139 | MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver."); |
| 140 | module_param_array(pcm_substreams, int, NULL, 0444); |
| 141 | MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-16) for dummy driver."); |
| 142 | //module_param_array(midi_devs, int, NULL, 0444); |
| 143 | //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver."); |
| 144 | |
| 145 | #define MIXER_ADDR_MASTER 0 |
| 146 | #define MIXER_ADDR_LINE 1 |
| 147 | #define MIXER_ADDR_MIC 2 |
| 148 | #define MIXER_ADDR_SYNTH 3 |
| 149 | #define MIXER_ADDR_CD 4 |
| 150 | #define MIXER_ADDR_LAST 4 |
| 151 | |
| 152 | typedef struct snd_card_dummy { |
| 153 | snd_card_t *card; |
| 154 | spinlock_t mixer_lock; |
| 155 | int mixer_volume[MIXER_ADDR_LAST+1][2]; |
| 156 | int capture_source[MIXER_ADDR_LAST+1][2]; |
| 157 | } snd_card_dummy_t; |
| 158 | |
| 159 | typedef struct snd_card_dummy_pcm { |
| 160 | snd_card_dummy_t *dummy; |
| 161 | spinlock_t lock; |
| 162 | struct timer_list timer; |
| 163 | unsigned int pcm_size; |
| 164 | unsigned int pcm_count; |
| 165 | unsigned int pcm_bps; /* bytes per second */ |
| 166 | unsigned int pcm_jiffie; /* bytes per one jiffie */ |
| 167 | unsigned int pcm_irq_pos; /* IRQ position */ |
| 168 | unsigned int pcm_buf_pos; /* position in buffer */ |
| 169 | snd_pcm_substream_t *substream; |
| 170 | } snd_card_dummy_pcm_t; |
| 171 | |
| 172 | static snd_card_t *snd_dummy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR; |
| 173 | |
| 174 | |
| 175 | static void snd_card_dummy_pcm_timer_start(snd_pcm_substream_t * substream) |
| 176 | { |
| 177 | snd_pcm_runtime_t *runtime = substream->runtime; |
| 178 | snd_card_dummy_pcm_t *dpcm = runtime->private_data; |
| 179 | |
| 180 | dpcm->timer.expires = 1 + jiffies; |
| 181 | add_timer(&dpcm->timer); |
| 182 | } |
| 183 | |
| 184 | static void snd_card_dummy_pcm_timer_stop(snd_pcm_substream_t * substream) |
| 185 | { |
| 186 | snd_pcm_runtime_t *runtime = substream->runtime; |
| 187 | snd_card_dummy_pcm_t *dpcm = runtime->private_data; |
| 188 | |
| 189 | del_timer(&dpcm->timer); |
| 190 | } |
| 191 | |
| 192 | static int snd_card_dummy_playback_trigger(snd_pcm_substream_t * substream, |
| 193 | int cmd) |
| 194 | { |
| 195 | if (cmd == SNDRV_PCM_TRIGGER_START) { |
| 196 | snd_card_dummy_pcm_timer_start(substream); |
| 197 | } else if (cmd == SNDRV_PCM_TRIGGER_STOP) { |
| 198 | snd_card_dummy_pcm_timer_stop(substream); |
| 199 | } else { |
| 200 | return -EINVAL; |
| 201 | } |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int snd_card_dummy_capture_trigger(snd_pcm_substream_t * substream, |
| 206 | int cmd) |
| 207 | { |
| 208 | if (cmd == SNDRV_PCM_TRIGGER_START) { |
| 209 | snd_card_dummy_pcm_timer_start(substream); |
| 210 | } else if (cmd == SNDRV_PCM_TRIGGER_STOP) { |
| 211 | snd_card_dummy_pcm_timer_stop(substream); |
| 212 | } else { |
| 213 | return -EINVAL; |
| 214 | } |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int snd_card_dummy_pcm_prepare(snd_pcm_substream_t * substream) |
| 219 | { |
| 220 | snd_pcm_runtime_t *runtime = substream->runtime; |
| 221 | snd_card_dummy_pcm_t *dpcm = runtime->private_data; |
| 222 | unsigned int bps; |
| 223 | |
| 224 | bps = runtime->rate * runtime->channels; |
| 225 | bps *= snd_pcm_format_width(runtime->format); |
| 226 | bps /= 8; |
| 227 | if (bps <= 0) |
| 228 | return -EINVAL; |
| 229 | dpcm->pcm_bps = bps; |
| 230 | dpcm->pcm_jiffie = bps / HZ; |
| 231 | dpcm->pcm_size = snd_pcm_lib_buffer_bytes(substream); |
| 232 | dpcm->pcm_count = snd_pcm_lib_period_bytes(substream); |
| 233 | dpcm->pcm_irq_pos = 0; |
| 234 | dpcm->pcm_buf_pos = 0; |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static int snd_card_dummy_playback_prepare(snd_pcm_substream_t * substream) |
| 239 | { |
| 240 | return snd_card_dummy_pcm_prepare(substream); |
| 241 | } |
| 242 | |
| 243 | static int snd_card_dummy_capture_prepare(snd_pcm_substream_t * substream) |
| 244 | { |
| 245 | return snd_card_dummy_pcm_prepare(substream); |
| 246 | } |
| 247 | |
| 248 | static void snd_card_dummy_pcm_timer_function(unsigned long data) |
| 249 | { |
| 250 | snd_card_dummy_pcm_t *dpcm = (snd_card_dummy_pcm_t *)data; |
| 251 | |
| 252 | dpcm->timer.expires = 1 + jiffies; |
| 253 | add_timer(&dpcm->timer); |
| 254 | spin_lock_irq(&dpcm->lock); |
| 255 | dpcm->pcm_irq_pos += dpcm->pcm_jiffie; |
| 256 | dpcm->pcm_buf_pos += dpcm->pcm_jiffie; |
| 257 | dpcm->pcm_buf_pos %= dpcm->pcm_size; |
| 258 | if (dpcm->pcm_irq_pos >= dpcm->pcm_count) { |
| 259 | dpcm->pcm_irq_pos %= dpcm->pcm_count; |
| 260 | snd_pcm_period_elapsed(dpcm->substream); |
| 261 | } |
| 262 | spin_unlock_irq(&dpcm->lock); |
| 263 | } |
| 264 | |
| 265 | static snd_pcm_uframes_t snd_card_dummy_playback_pointer(snd_pcm_substream_t * substream) |
| 266 | { |
| 267 | snd_pcm_runtime_t *runtime = substream->runtime; |
| 268 | snd_card_dummy_pcm_t *dpcm = runtime->private_data; |
| 269 | |
| 270 | return bytes_to_frames(runtime, dpcm->pcm_buf_pos); |
| 271 | } |
| 272 | |
| 273 | static snd_pcm_uframes_t snd_card_dummy_capture_pointer(snd_pcm_substream_t * substream) |
| 274 | { |
| 275 | snd_pcm_runtime_t *runtime = substream->runtime; |
| 276 | snd_card_dummy_pcm_t *dpcm = runtime->private_data; |
| 277 | |
| 278 | return bytes_to_frames(runtime, dpcm->pcm_buf_pos); |
| 279 | } |
| 280 | |
| 281 | static snd_pcm_hardware_t snd_card_dummy_playback = |
| 282 | { |
| 283 | .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | |
| 284 | SNDRV_PCM_INFO_MMAP_VALID), |
| 285 | .formats = USE_FORMATS, |
| 286 | .rates = USE_RATE, |
| 287 | .rate_min = USE_RATE_MIN, |
| 288 | .rate_max = USE_RATE_MAX, |
| 289 | .channels_min = USE_CHANNELS_MIN, |
| 290 | .channels_max = USE_CHANNELS_MAX, |
| 291 | .buffer_bytes_max = MAX_BUFFER_SIZE, |
| 292 | .period_bytes_min = 64, |
| 293 | .period_bytes_max = MAX_BUFFER_SIZE, |
| 294 | .periods_min = USE_PERIODS_MIN, |
| 295 | .periods_max = USE_PERIODS_MAX, |
| 296 | .fifo_size = 0, |
| 297 | }; |
| 298 | |
| 299 | static snd_pcm_hardware_t snd_card_dummy_capture = |
| 300 | { |
| 301 | .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | |
| 302 | SNDRV_PCM_INFO_MMAP_VALID), |
| 303 | .formats = USE_FORMATS, |
| 304 | .rates = USE_RATE, |
| 305 | .rate_min = USE_RATE_MIN, |
| 306 | .rate_max = USE_RATE_MAX, |
| 307 | .channels_min = USE_CHANNELS_MIN, |
| 308 | .channels_max = USE_CHANNELS_MAX, |
| 309 | .buffer_bytes_max = MAX_BUFFER_SIZE, |
| 310 | .period_bytes_min = 64, |
| 311 | .period_bytes_max = MAX_BUFFER_SIZE, |
| 312 | .periods_min = USE_PERIODS_MIN, |
| 313 | .periods_max = USE_PERIODS_MAX, |
| 314 | .fifo_size = 0, |
| 315 | }; |
| 316 | |
| 317 | static void snd_card_dummy_runtime_free(snd_pcm_runtime_t *runtime) |
| 318 | { |
| 319 | snd_card_dummy_pcm_t *dpcm = runtime->private_data; |
| 320 | kfree(dpcm); |
| 321 | } |
| 322 | |
| 323 | static int snd_card_dummy_hw_params(snd_pcm_substream_t * substream, |
| 324 | snd_pcm_hw_params_t * hw_params) |
| 325 | { |
| 326 | return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)); |
| 327 | } |
| 328 | |
| 329 | static int snd_card_dummy_hw_free(snd_pcm_substream_t * substream) |
| 330 | { |
| 331 | return snd_pcm_lib_free_pages(substream); |
| 332 | } |
| 333 | |
| 334 | static int snd_card_dummy_playback_open(snd_pcm_substream_t * substream) |
| 335 | { |
| 336 | snd_pcm_runtime_t *runtime = substream->runtime; |
| 337 | snd_card_dummy_pcm_t *dpcm; |
| 338 | int err; |
| 339 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 340 | dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | if (dpcm == NULL) |
| 342 | return -ENOMEM; |
| 343 | init_timer(&dpcm->timer); |
| 344 | dpcm->timer.data = (unsigned long) dpcm; |
| 345 | dpcm->timer.function = snd_card_dummy_pcm_timer_function; |
| 346 | spin_lock_init(&dpcm->lock); |
| 347 | dpcm->substream = substream; |
| 348 | runtime->private_data = dpcm; |
| 349 | runtime->private_free = snd_card_dummy_runtime_free; |
| 350 | runtime->hw = snd_card_dummy_playback; |
| 351 | if (substream->pcm->device & 1) { |
| 352 | runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED; |
| 353 | runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED; |
| 354 | } |
| 355 | if (substream->pcm->device & 2) |
| 356 | runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID); |
| 357 | if ((err = add_playback_constraints(runtime)) < 0) { |
| 358 | kfree(dpcm); |
| 359 | return err; |
| 360 | } |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static int snd_card_dummy_capture_open(snd_pcm_substream_t * substream) |
| 366 | { |
| 367 | snd_pcm_runtime_t *runtime = substream->runtime; |
| 368 | snd_card_dummy_pcm_t *dpcm; |
| 369 | int err; |
| 370 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 371 | dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | if (dpcm == NULL) |
| 373 | return -ENOMEM; |
| 374 | init_timer(&dpcm->timer); |
| 375 | dpcm->timer.data = (unsigned long) dpcm; |
| 376 | dpcm->timer.function = snd_card_dummy_pcm_timer_function; |
| 377 | spin_lock_init(&dpcm->lock); |
| 378 | dpcm->substream = substream; |
| 379 | runtime->private_data = dpcm; |
| 380 | runtime->private_free = snd_card_dummy_runtime_free; |
| 381 | runtime->hw = snd_card_dummy_capture; |
| 382 | if (substream->pcm->device == 1) { |
| 383 | runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED; |
| 384 | runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED; |
| 385 | } |
| 386 | if (substream->pcm->device & 2) |
| 387 | runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID); |
| 388 | if ((err = add_capture_constraints(runtime)) < 0) { |
| 389 | kfree(dpcm); |
| 390 | return err; |
| 391 | } |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static int snd_card_dummy_playback_close(snd_pcm_substream_t * substream) |
| 397 | { |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | static int snd_card_dummy_capture_close(snd_pcm_substream_t * substream) |
| 402 | { |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static snd_pcm_ops_t snd_card_dummy_playback_ops = { |
| 407 | .open = snd_card_dummy_playback_open, |
| 408 | .close = snd_card_dummy_playback_close, |
| 409 | .ioctl = snd_pcm_lib_ioctl, |
| 410 | .hw_params = snd_card_dummy_hw_params, |
| 411 | .hw_free = snd_card_dummy_hw_free, |
| 412 | .prepare = snd_card_dummy_playback_prepare, |
| 413 | .trigger = snd_card_dummy_playback_trigger, |
| 414 | .pointer = snd_card_dummy_playback_pointer, |
| 415 | }; |
| 416 | |
| 417 | static snd_pcm_ops_t snd_card_dummy_capture_ops = { |
| 418 | .open = snd_card_dummy_capture_open, |
| 419 | .close = snd_card_dummy_capture_close, |
| 420 | .ioctl = snd_pcm_lib_ioctl, |
| 421 | .hw_params = snd_card_dummy_hw_params, |
| 422 | .hw_free = snd_card_dummy_hw_free, |
| 423 | .prepare = snd_card_dummy_capture_prepare, |
| 424 | .trigger = snd_card_dummy_capture_trigger, |
| 425 | .pointer = snd_card_dummy_capture_pointer, |
| 426 | }; |
| 427 | |
| 428 | static int __init snd_card_dummy_pcm(snd_card_dummy_t *dummy, int device, int substreams) |
| 429 | { |
| 430 | snd_pcm_t *pcm; |
| 431 | int err; |
| 432 | |
| 433 | if ((err = snd_pcm_new(dummy->card, "Dummy PCM", device, substreams, substreams, &pcm)) < 0) |
| 434 | return err; |
| 435 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_dummy_playback_ops); |
| 436 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_dummy_capture_ops); |
| 437 | pcm->private_data = dummy; |
| 438 | pcm->info_flags = 0; |
| 439 | strcpy(pcm->name, "Dummy PCM"); |
| 440 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, |
| 441 | snd_dma_continuous_data(GFP_KERNEL), |
| 442 | 0, 64*1024); |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | #define DUMMY_VOLUME(xname, xindex, addr) \ |
| 447 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ |
| 448 | .info = snd_dummy_volume_info, \ |
| 449 | .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \ |
| 450 | .private_value = addr } |
| 451 | |
| 452 | static int snd_dummy_volume_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo) |
| 453 | { |
| 454 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 455 | uinfo->count = 2; |
| 456 | uinfo->value.integer.min = -50; |
| 457 | uinfo->value.integer.max = 100; |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | static int snd_dummy_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol) |
| 462 | { |
| 463 | snd_card_dummy_t *dummy = snd_kcontrol_chip(kcontrol); |
| 464 | unsigned long flags; |
| 465 | int addr = kcontrol->private_value; |
| 466 | |
| 467 | spin_lock_irqsave(&dummy->mixer_lock, flags); |
| 468 | ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0]; |
| 469 | ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1]; |
| 470 | spin_unlock_irqrestore(&dummy->mixer_lock, flags); |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | static int snd_dummy_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol) |
| 475 | { |
| 476 | snd_card_dummy_t *dummy = snd_kcontrol_chip(kcontrol); |
| 477 | unsigned long flags; |
| 478 | int change, addr = kcontrol->private_value; |
| 479 | int left, right; |
| 480 | |
| 481 | left = ucontrol->value.integer.value[0]; |
| 482 | if (left < -50) |
| 483 | left = -50; |
| 484 | if (left > 100) |
| 485 | left = 100; |
| 486 | right = ucontrol->value.integer.value[1]; |
| 487 | if (right < -50) |
| 488 | right = -50; |
| 489 | if (right > 100) |
| 490 | right = 100; |
| 491 | spin_lock_irqsave(&dummy->mixer_lock, flags); |
| 492 | change = dummy->mixer_volume[addr][0] != left || |
| 493 | dummy->mixer_volume[addr][1] != right; |
| 494 | dummy->mixer_volume[addr][0] = left; |
| 495 | dummy->mixer_volume[addr][1] = right; |
| 496 | spin_unlock_irqrestore(&dummy->mixer_lock, flags); |
| 497 | return change; |
| 498 | } |
| 499 | |
| 500 | #define DUMMY_CAPSRC(xname, xindex, addr) \ |
| 501 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ |
| 502 | .info = snd_dummy_capsrc_info, \ |
| 503 | .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \ |
| 504 | .private_value = addr } |
| 505 | |
| 506 | static int snd_dummy_capsrc_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo) |
| 507 | { |
| 508 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 509 | uinfo->count = 2; |
| 510 | uinfo->value.integer.min = 0; |
| 511 | uinfo->value.integer.max = 1; |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | static int snd_dummy_capsrc_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol) |
| 516 | { |
| 517 | snd_card_dummy_t *dummy = snd_kcontrol_chip(kcontrol); |
| 518 | unsigned long flags; |
| 519 | int addr = kcontrol->private_value; |
| 520 | |
| 521 | spin_lock_irqsave(&dummy->mixer_lock, flags); |
| 522 | ucontrol->value.integer.value[0] = dummy->capture_source[addr][0]; |
| 523 | ucontrol->value.integer.value[1] = dummy->capture_source[addr][1]; |
| 524 | spin_unlock_irqrestore(&dummy->mixer_lock, flags); |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | static int snd_dummy_capsrc_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol) |
| 529 | { |
| 530 | snd_card_dummy_t *dummy = snd_kcontrol_chip(kcontrol); |
| 531 | unsigned long flags; |
| 532 | int change, addr = kcontrol->private_value; |
| 533 | int left, right; |
| 534 | |
| 535 | left = ucontrol->value.integer.value[0] & 1; |
| 536 | right = ucontrol->value.integer.value[1] & 1; |
| 537 | spin_lock_irqsave(&dummy->mixer_lock, flags); |
| 538 | change = dummy->capture_source[addr][0] != left && |
| 539 | dummy->capture_source[addr][1] != right; |
| 540 | dummy->capture_source[addr][0] = left; |
| 541 | dummy->capture_source[addr][1] = right; |
| 542 | spin_unlock_irqrestore(&dummy->mixer_lock, flags); |
| 543 | return change; |
| 544 | } |
| 545 | |
| 546 | static snd_kcontrol_new_t snd_dummy_controls[] = { |
| 547 | DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER), |
| 548 | DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER), |
| 549 | DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH), |
| 550 | DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_MASTER), |
| 551 | DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE), |
| 552 | DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_MASTER), |
| 553 | DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC), |
| 554 | DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MASTER), |
| 555 | DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD), |
| 556 | DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_MASTER) |
| 557 | }; |
| 558 | |
| 559 | static int __init snd_card_dummy_new_mixer(snd_card_dummy_t * dummy) |
| 560 | { |
| 561 | snd_card_t *card = dummy->card; |
| 562 | unsigned int idx; |
| 563 | int err; |
| 564 | |
| 565 | snd_assert(dummy != NULL, return -EINVAL); |
| 566 | spin_lock_init(&dummy->mixer_lock); |
| 567 | strcpy(card->mixername, "Dummy Mixer"); |
| 568 | |
| 569 | for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) { |
| 570 | if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy))) < 0) |
| 571 | return err; |
| 572 | } |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | static int __init snd_card_dummy_probe(int dev) |
| 577 | { |
| 578 | snd_card_t *card; |
| 579 | struct snd_card_dummy *dummy; |
| 580 | int idx, err; |
| 581 | |
| 582 | if (!enable[dev]) |
| 583 | return -ENODEV; |
| 584 | card = snd_card_new(index[dev], id[dev], THIS_MODULE, |
| 585 | sizeof(struct snd_card_dummy)); |
| 586 | if (card == NULL) |
| 587 | return -ENOMEM; |
| 588 | dummy = (struct snd_card_dummy *)card->private_data; |
| 589 | dummy->card = card; |
| 590 | for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) { |
| 591 | if (pcm_substreams[dev] < 1) |
| 592 | pcm_substreams[dev] = 1; |
| 593 | if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS) |
| 594 | pcm_substreams[dev] = MAX_PCM_SUBSTREAMS; |
| 595 | if ((err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev])) < 0) |
| 596 | goto __nodev; |
| 597 | } |
| 598 | if ((err = snd_card_dummy_new_mixer(dummy)) < 0) |
| 599 | goto __nodev; |
| 600 | strcpy(card->driver, "Dummy"); |
| 601 | strcpy(card->shortname, "Dummy"); |
| 602 | sprintf(card->longname, "Dummy %i", dev + 1); |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 603 | |
| 604 | if ((err = snd_card_set_generic_dev(card)) < 0) |
| 605 | goto __nodev; |
| 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | if ((err = snd_card_register(card)) == 0) { |
| 608 | snd_dummy_cards[dev] = card; |
| 609 | return 0; |
| 610 | } |
| 611 | __nodev: |
| 612 | snd_card_free(card); |
| 613 | return err; |
| 614 | } |
| 615 | |
| 616 | static int __init alsa_card_dummy_init(void) |
| 617 | { |
| 618 | int dev, cards; |
| 619 | |
| 620 | for (dev = cards = 0; dev < SNDRV_CARDS && enable[dev]; dev++) { |
| 621 | if (snd_card_dummy_probe(dev) < 0) { |
| 622 | #ifdef MODULE |
| 623 | printk(KERN_ERR "Dummy soundcard #%i not found or device busy\n", dev + 1); |
| 624 | #endif |
| 625 | break; |
| 626 | } |
| 627 | cards++; |
| 628 | } |
| 629 | if (!cards) { |
| 630 | #ifdef MODULE |
| 631 | printk(KERN_ERR "Dummy soundcard not found or device busy\n"); |
| 632 | #endif |
| 633 | return -ENODEV; |
| 634 | } |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | static void __exit alsa_card_dummy_exit(void) |
| 639 | { |
| 640 | int idx; |
| 641 | |
| 642 | for (idx = 0; idx < SNDRV_CARDS; idx++) |
| 643 | snd_card_free(snd_dummy_cards[idx]); |
| 644 | } |
| 645 | |
| 646 | module_init(alsa_card_dummy_init) |
| 647 | module_exit(alsa_card_dummy_exit) |