1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Dummy soundcard 4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 5 */ 6 7 #include <linux/init.h> 8 #include <linux/err.h> 9 #include <linux/platform_device.h> 10 #include <linux/jiffies.h> 11 #include <linux/slab.h> 12 #include <linux/string.h> 13 #include <linux/time.h> 14 #include <linux/wait.h> 15 #include <linux/hrtimer.h> 16 #include <linux/hrtimer_bases.h> 17 #include <linux/math64.h> 18 #include <linux/module.h> 19 #include <sound/core.h> 20 #include <sound/control.h> 21 #include <sound/tlv.h> 22 #include <sound/pcm.h> 23 #include <sound/rawmidi.h> 24 #include <sound/info.h> 25 #include <sound/initval.h> 26 27 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); 28 MODULE_DESCRIPTION("Dummy soundcard (/dev/null)"); 29 MODULE_LICENSE("GPL"); 30 31 #define MAX_PCM_DEVICES 4 32 #define MAX_PCM_SUBSTREAMS 128 33 #define MAX_MIDI_DEVICES 2 34 35 /* defaults */ 36 #define MAX_BUFFER_SIZE (64*1024) 37 #define MIN_PERIOD_SIZE 64 38 #define MAX_PERIOD_SIZE MAX_BUFFER_SIZE 39 #define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE) 40 #define USE_RATE SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000 41 #define USE_RATE_MIN 5500 42 #define USE_RATE_MAX 48000 43 #define USE_CHANNELS_MIN 1 44 #define USE_CHANNELS_MAX 2 45 #define USE_PERIODS_MIN 1 46 #define USE_PERIODS_MAX 1024 47 #define USE_MIXER_VOLUME_LEVEL_MIN -50 48 #define USE_MIXER_VOLUME_LEVEL_MAX 100 49 50 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ 51 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ 52 static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0}; 53 static char *model[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = NULL}; 54 static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; 55 static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8}; 56 //static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2}; 57 static int mixer_volume_level_min = USE_MIXER_VOLUME_LEVEL_MIN; 58 static int mixer_volume_level_max = USE_MIXER_VOLUME_LEVEL_MAX; 59 #ifdef CONFIG_HIGH_RES_TIMERS 60 static bool hrtimer = 1; 61 #endif 62 static bool fake_buffer = 1; 63 64 module_param_array(index, int, NULL, 0444); 65 MODULE_PARM_DESC(index, "Index value for dummy soundcard."); 66 module_param_array(id, charp, NULL, 0444); 67 MODULE_PARM_DESC(id, "ID string for dummy soundcard."); 68 module_param_array(enable, bool, NULL, 0444); 69 MODULE_PARM_DESC(enable, "Enable this dummy soundcard."); 70 module_param_array(model, charp, NULL, 0444); 71 MODULE_PARM_DESC(model, "Soundcard model."); 72 module_param_array(pcm_devs, int, NULL, 0444); 73 MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver."); 74 module_param_array(pcm_substreams, int, NULL, 0444); 75 MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-128) for dummy driver."); 76 //module_param_array(midi_devs, int, NULL, 0444); 77 //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver."); 78 module_param(mixer_volume_level_min, int, 0444); 79 MODULE_PARM_DESC(mixer_volume_level_min, "Minimum mixer volume level for dummy driver. Default: -50"); 80 module_param(mixer_volume_level_max, int, 0444); 81 MODULE_PARM_DESC(mixer_volume_level_max, "Maximum mixer volume level for dummy driver. Default: 100"); 82 module_param(fake_buffer, bool, 0444); 83 MODULE_PARM_DESC(fake_buffer, "Fake buffer allocations."); 84 #ifdef CONFIG_HIGH_RES_TIMERS 85 module_param(hrtimer, bool, 0644); 86 MODULE_PARM_DESC(hrtimer, "Use hrtimer as the timer source."); 87 #endif 88 89 static struct platform_device *devices[SNDRV_CARDS]; 90 91 #define MIXER_ADDR_MASTER 0 92 #define MIXER_ADDR_LINE 1 93 #define MIXER_ADDR_MIC 2 94 #define MIXER_ADDR_SYNTH 3 95 #define MIXER_ADDR_CD 4 96 #define MIXER_ADDR_LAST 4 97 98 struct dummy_timer_ops { 99 int (*create)(struct snd_pcm_substream *); 100 void (*free)(struct snd_pcm_substream *); 101 int (*prepare)(struct snd_pcm_substream *); 102 int (*start)(struct snd_pcm_substream *); 103 int (*stop)(struct snd_pcm_substream *); 104 int (*sync_stop)(struct snd_pcm_substream *); 105 snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *); 106 }; 107 108 #define get_dummy_ops(substream) \ 109 (*(const struct dummy_timer_ops **)(substream)->runtime->private_data) 110 111 struct dummy_model { 112 const char *name; 113 int (*playback_constraints)(struct snd_pcm_runtime *runtime); 114 int (*capture_constraints)(struct snd_pcm_runtime *runtime); 115 u64 formats; 116 size_t buffer_bytes_max; 117 size_t period_bytes_min; 118 size_t period_bytes_max; 119 unsigned int periods_min; 120 unsigned int periods_max; 121 unsigned int rates; 122 unsigned int rate_min; 123 unsigned int rate_max; 124 unsigned int channels_min; 125 unsigned int channels_max; 126 }; 127 128 struct snd_dummy { 129 struct snd_card *card; 130 const struct dummy_model *model; 131 struct snd_pcm *pcm; 132 struct snd_pcm_hardware pcm_hw; 133 spinlock_t mixer_lock; 134 int mixer_volume[MIXER_ADDR_LAST+1][2]; 135 int capture_source[MIXER_ADDR_LAST+1][2]; 136 int iobox; 137 struct snd_kcontrol *cd_volume_ctl; 138 struct snd_kcontrol *cd_switch_ctl; 139 }; 140 141 /* 142 * card models 143 */ 144 145 static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime) 146 { 147 int err; 148 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 149 if (err < 0) 150 return err; 151 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX); 152 if (err < 0) 153 return err; 154 return 0; 155 } 156 157 static const struct dummy_model model_emu10k1 = { 158 .name = "emu10k1", 159 .playback_constraints = emu10k1_playback_constraints, 160 .buffer_bytes_max = 128 * 1024, 161 }; 162 163 static const struct dummy_model model_rme9652 = { 164 .name = "rme9652", 165 .buffer_bytes_max = 26 * 64 * 1024, 166 .formats = SNDRV_PCM_FMTBIT_S32_LE, 167 .channels_min = 26, 168 .channels_max = 26, 169 .periods_min = 2, 170 .periods_max = 2, 171 }; 172 173 static const struct dummy_model model_ice1712 = { 174 .name = "ice1712", 175 .buffer_bytes_max = 256 * 1024, 176 .formats = SNDRV_PCM_FMTBIT_S32_LE, 177 .channels_min = 10, 178 .channels_max = 10, 179 .periods_min = 1, 180 .periods_max = 1024, 181 }; 182 183 static const struct dummy_model model_uda1341 = { 184 .name = "uda1341", 185 .buffer_bytes_max = 16380, 186 .formats = SNDRV_PCM_FMTBIT_S16_LE, 187 .channels_min = 2, 188 .channels_max = 2, 189 .periods_min = 2, 190 .periods_max = 255, 191 }; 192 193 static const struct dummy_model model_ac97 = { 194 .name = "ac97", 195 .formats = SNDRV_PCM_FMTBIT_S16_LE, 196 .channels_min = 2, 197 .channels_max = 2, 198 .rates = SNDRV_PCM_RATE_48000, 199 .rate_min = 48000, 200 .rate_max = 48000, 201 }; 202 203 static const struct dummy_model model_ca0106 = { 204 .name = "ca0106", 205 .formats = SNDRV_PCM_FMTBIT_S16_LE, 206 .buffer_bytes_max = ((65536-64)*8), 207 .period_bytes_max = (65536-64), 208 .periods_min = 2, 209 .periods_max = 8, 210 .channels_min = 2, 211 .channels_max = 2, 212 .rates = SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000, 213 .rate_min = 48000, 214 .rate_max = 192000, 215 }; 216 217 static const struct dummy_model *dummy_models[] = { 218 &model_emu10k1, 219 &model_rme9652, 220 &model_ice1712, 221 &model_uda1341, 222 &model_ac97, 223 &model_ca0106, 224 NULL 225 }; 226 227 /* 228 * system timer interface 229 */ 230 231 struct dummy_systimer_pcm { 232 /* ops must be the first item */ 233 const struct dummy_timer_ops *timer_ops; 234 spinlock_t lock; 235 struct timer_list timer; 236 unsigned long base_time; 237 unsigned int frac_pos; /* fractional sample position (based HZ) */ 238 unsigned int frac_period_rest; 239 unsigned int frac_buffer_size; /* buffer_size * HZ */ 240 unsigned int frac_period_size; /* period_size * HZ */ 241 unsigned int rate; 242 int elapsed; 243 struct snd_pcm_substream *substream; 244 }; 245 246 static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm) 247 { 248 mod_timer(&dpcm->timer, jiffies + 249 DIV_ROUND_UP(dpcm->frac_period_rest, dpcm->rate)); 250 } 251 252 static void dummy_systimer_update(struct dummy_systimer_pcm *dpcm) 253 { 254 unsigned long delta; 255 256 delta = jiffies - dpcm->base_time; 257 if (!delta) 258 return; 259 dpcm->base_time += delta; 260 delta *= dpcm->rate; 261 dpcm->frac_pos += delta; 262 while (dpcm->frac_pos >= dpcm->frac_buffer_size) 263 dpcm->frac_pos -= dpcm->frac_buffer_size; 264 while (dpcm->frac_period_rest <= delta) { 265 dpcm->elapsed++; 266 dpcm->frac_period_rest += dpcm->frac_period_size; 267 } 268 dpcm->frac_period_rest -= delta; 269 } 270 271 static int dummy_systimer_start(struct snd_pcm_substream *substream) 272 { 273 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; 274 275 guard(spinlock)(&dpcm->lock); 276 dpcm->base_time = jiffies; 277 dummy_systimer_rearm(dpcm); 278 return 0; 279 } 280 281 static int dummy_systimer_stop(struct snd_pcm_substream *substream) 282 { 283 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; 284 285 guard(spinlock)(&dpcm->lock); 286 timer_delete(&dpcm->timer); 287 return 0; 288 } 289 290 static int dummy_systimer_sync_stop(struct snd_pcm_substream *substream) 291 { 292 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; 293 294 timer_delete_sync(&dpcm->timer); 295 return 0; 296 } 297 298 static int dummy_systimer_prepare(struct snd_pcm_substream *substream) 299 { 300 struct snd_pcm_runtime *runtime = substream->runtime; 301 struct dummy_systimer_pcm *dpcm = runtime->private_data; 302 303 dpcm->frac_pos = 0; 304 dpcm->rate = runtime->rate; 305 dpcm->frac_buffer_size = runtime->buffer_size * HZ; 306 dpcm->frac_period_size = runtime->period_size * HZ; 307 dpcm->frac_period_rest = dpcm->frac_period_size; 308 dpcm->elapsed = 0; 309 310 return 0; 311 } 312 313 static void dummy_systimer_callback(struct timer_list *t) 314 { 315 struct dummy_systimer_pcm *dpcm = timer_container_of(dpcm, t, timer); 316 int elapsed = 0; 317 318 scoped_guard(spinlock_irqsave, &dpcm->lock) { 319 dummy_systimer_update(dpcm); 320 dummy_systimer_rearm(dpcm); 321 elapsed = dpcm->elapsed; 322 dpcm->elapsed = 0; 323 } 324 if (elapsed) 325 snd_pcm_period_elapsed(dpcm->substream); 326 } 327 328 static snd_pcm_uframes_t 329 dummy_systimer_pointer(struct snd_pcm_substream *substream) 330 { 331 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; 332 333 guard(spinlock)(&dpcm->lock); 334 dummy_systimer_update(dpcm); 335 return dpcm->frac_pos / HZ; 336 } 337 338 static int dummy_systimer_create(struct snd_pcm_substream *substream) 339 { 340 struct dummy_systimer_pcm *dpcm; 341 342 dpcm = kzalloc_obj(*dpcm); 343 if (!dpcm) 344 return -ENOMEM; 345 substream->runtime->private_data = dpcm; 346 timer_setup(&dpcm->timer, dummy_systimer_callback, 0); 347 spin_lock_init(&dpcm->lock); 348 dpcm->substream = substream; 349 return 0; 350 } 351 352 static void dummy_systimer_free(struct snd_pcm_substream *substream) 353 { 354 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; 355 356 timer_shutdown_sync(&dpcm->timer); 357 kfree(dpcm); 358 } 359 360 static const struct dummy_timer_ops dummy_systimer_ops = { 361 .create = dummy_systimer_create, 362 .free = dummy_systimer_free, 363 .prepare = dummy_systimer_prepare, 364 .start = dummy_systimer_start, 365 .stop = dummy_systimer_stop, 366 .sync_stop = dummy_systimer_sync_stop, 367 .pointer = dummy_systimer_pointer, 368 }; 369 370 #ifdef CONFIG_HIGH_RES_TIMERS 371 /* 372 * hrtimer interface 373 */ 374 375 struct dummy_hrtimer_pcm { 376 /* ops must be the first item */ 377 const struct dummy_timer_ops *timer_ops; 378 ktime_t base_time; 379 ktime_t period_time; 380 atomic_t running; 381 struct hrtimer timer; 382 struct snd_pcm_substream *substream; 383 }; 384 385 static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer) 386 { 387 struct dummy_hrtimer_pcm *dpcm; 388 389 dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer); 390 if (!atomic_read(&dpcm->running)) 391 return HRTIMER_NORESTART; 392 /* 393 * In cases of XRUN and draining, this calls .trigger to stop PCM 394 * substream. 395 */ 396 snd_pcm_period_elapsed(dpcm->substream); 397 if (!atomic_read(&dpcm->running)) 398 return HRTIMER_NORESTART; 399 400 hrtimer_forward_now(timer, dpcm->period_time); 401 return HRTIMER_RESTART; 402 } 403 404 static int dummy_hrtimer_start(struct snd_pcm_substream *substream) 405 { 406 struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; 407 408 dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer); 409 hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL_SOFT); 410 atomic_set(&dpcm->running, 1); 411 return 0; 412 } 413 414 static int dummy_hrtimer_stop(struct snd_pcm_substream *substream) 415 { 416 struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; 417 418 atomic_set(&dpcm->running, 0); 419 if (!hrtimer_callback_running(&dpcm->timer)) 420 hrtimer_cancel(&dpcm->timer); 421 return 0; 422 } 423 424 static int dummy_hrtimer_sync_stop(struct snd_pcm_substream *substream) 425 { 426 struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; 427 428 hrtimer_cancel(&dpcm->timer); 429 return 0; 430 } 431 432 static snd_pcm_uframes_t 433 dummy_hrtimer_pointer(struct snd_pcm_substream *substream) 434 { 435 struct snd_pcm_runtime *runtime = substream->runtime; 436 struct dummy_hrtimer_pcm *dpcm = runtime->private_data; 437 u64 delta; 438 u32 pos; 439 440 delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer), 441 dpcm->base_time); 442 delta = div_u64(delta * runtime->rate + 999999, 1000000); 443 div_u64_rem(delta, runtime->buffer_size, &pos); 444 return pos; 445 } 446 447 static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream) 448 { 449 struct snd_pcm_runtime *runtime = substream->runtime; 450 struct dummy_hrtimer_pcm *dpcm = runtime->private_data; 451 unsigned int period, rate; 452 long sec; 453 unsigned long nsecs; 454 455 period = runtime->period_size; 456 rate = runtime->rate; 457 sec = period / rate; 458 period %= rate; 459 nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate); 460 dpcm->period_time = ktime_set(sec, nsecs); 461 462 return 0; 463 } 464 465 static int dummy_hrtimer_create(struct snd_pcm_substream *substream) 466 { 467 struct dummy_hrtimer_pcm *dpcm; 468 469 dpcm = kzalloc_obj(*dpcm); 470 if (!dpcm) 471 return -ENOMEM; 472 substream->runtime->private_data = dpcm; 473 hrtimer_setup(&dpcm->timer, dummy_hrtimer_callback, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT); 474 dpcm->substream = substream; 475 atomic_set(&dpcm->running, 0); 476 return 0; 477 } 478 479 static void dummy_hrtimer_free(struct snd_pcm_substream *substream) 480 { 481 struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; 482 483 kfree(dpcm); 484 } 485 486 static const struct dummy_timer_ops dummy_hrtimer_ops = { 487 .create = dummy_hrtimer_create, 488 .free = dummy_hrtimer_free, 489 .prepare = dummy_hrtimer_prepare, 490 .start = dummy_hrtimer_start, 491 .stop = dummy_hrtimer_stop, 492 .sync_stop = dummy_hrtimer_sync_stop, 493 .pointer = dummy_hrtimer_pointer, 494 }; 495 496 #endif /* CONFIG_HIGH_RES_TIMERS */ 497 498 /* 499 * PCM interface 500 */ 501 502 static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 503 { 504 switch (cmd) { 505 case SNDRV_PCM_TRIGGER_START: 506 case SNDRV_PCM_TRIGGER_RESUME: 507 return get_dummy_ops(substream)->start(substream); 508 case SNDRV_PCM_TRIGGER_STOP: 509 case SNDRV_PCM_TRIGGER_SUSPEND: 510 return get_dummy_ops(substream)->stop(substream); 511 } 512 return -EINVAL; 513 } 514 515 static int dummy_pcm_sync_stop(struct snd_pcm_substream *substream) 516 { 517 if (get_dummy_ops(substream)->sync_stop) 518 return get_dummy_ops(substream)->sync_stop(substream); 519 return 0; 520 } 521 522 static int dummy_pcm_prepare(struct snd_pcm_substream *substream) 523 { 524 return get_dummy_ops(substream)->prepare(substream); 525 } 526 527 static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream) 528 { 529 return get_dummy_ops(substream)->pointer(substream); 530 } 531 532 static const struct snd_pcm_hardware dummy_pcm_hardware = { 533 .info = (SNDRV_PCM_INFO_MMAP | 534 SNDRV_PCM_INFO_INTERLEAVED | 535 SNDRV_PCM_INFO_RESUME | 536 SNDRV_PCM_INFO_MMAP_VALID), 537 .formats = USE_FORMATS, 538 .rates = USE_RATE, 539 .rate_min = USE_RATE_MIN, 540 .rate_max = USE_RATE_MAX, 541 .channels_min = USE_CHANNELS_MIN, 542 .channels_max = USE_CHANNELS_MAX, 543 .buffer_bytes_max = MAX_BUFFER_SIZE, 544 .period_bytes_min = MIN_PERIOD_SIZE, 545 .period_bytes_max = MAX_PERIOD_SIZE, 546 .periods_min = USE_PERIODS_MIN, 547 .periods_max = USE_PERIODS_MAX, 548 .fifo_size = 0, 549 }; 550 551 static int dummy_pcm_hw_params(struct snd_pcm_substream *substream, 552 struct snd_pcm_hw_params *hw_params) 553 { 554 if (fake_buffer) { 555 /* runtime->dma_bytes has to be set manually to allow mmap */ 556 substream->runtime->dma_bytes = params_buffer_bytes(hw_params); 557 return 0; 558 } 559 return 0; 560 } 561 562 static int dummy_pcm_open(struct snd_pcm_substream *substream) 563 { 564 struct snd_dummy *dummy = snd_pcm_substream_chip(substream); 565 const struct dummy_model *model = dummy->model; 566 struct snd_pcm_runtime *runtime = substream->runtime; 567 const struct dummy_timer_ops *ops; 568 int err; 569 570 ops = &dummy_systimer_ops; 571 #ifdef CONFIG_HIGH_RES_TIMERS 572 if (hrtimer) 573 ops = &dummy_hrtimer_ops; 574 #endif 575 576 err = ops->create(substream); 577 if (err < 0) 578 return err; 579 get_dummy_ops(substream) = ops; 580 581 runtime->hw = dummy->pcm_hw; 582 if (substream->pcm->device & 1) { 583 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED; 584 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED; 585 } 586 if (substream->pcm->device & 2) 587 runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP | 588 SNDRV_PCM_INFO_MMAP_VALID); 589 590 if (model == NULL) 591 return 0; 592 593 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 594 if (model->playback_constraints) 595 err = model->playback_constraints(substream->runtime); 596 } else { 597 if (model->capture_constraints) 598 err = model->capture_constraints(substream->runtime); 599 } 600 if (err < 0) { 601 get_dummy_ops(substream)->free(substream); 602 return err; 603 } 604 return 0; 605 } 606 607 static int dummy_pcm_close(struct snd_pcm_substream *substream) 608 { 609 get_dummy_ops(substream)->free(substream); 610 return 0; 611 } 612 613 /* 614 * dummy buffer handling 615 */ 616 617 static void *dummy_page[2]; 618 619 static void free_fake_buffer(void) 620 { 621 if (fake_buffer) { 622 int i; 623 for (i = 0; i < 2; i++) 624 if (dummy_page[i]) { 625 free_page((unsigned long)dummy_page[i]); 626 dummy_page[i] = NULL; 627 } 628 } 629 } 630 631 static int alloc_fake_buffer(void) 632 { 633 int i; 634 635 if (!fake_buffer) 636 return 0; 637 for (i = 0; i < 2; i++) { 638 dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL); 639 if (!dummy_page[i]) { 640 free_fake_buffer(); 641 return -ENOMEM; 642 } 643 } 644 return 0; 645 } 646 647 static int dummy_pcm_copy(struct snd_pcm_substream *substream, 648 int channel, unsigned long pos, 649 struct iov_iter *iter, unsigned long bytes) 650 { 651 return 0; /* do nothing */ 652 } 653 654 static int dummy_pcm_silence(struct snd_pcm_substream *substream, 655 int channel, unsigned long pos, 656 unsigned long bytes) 657 { 658 return 0; /* do nothing */ 659 } 660 661 static struct page *dummy_pcm_page(struct snd_pcm_substream *substream, 662 unsigned long offset) 663 { 664 return virt_to_page(dummy_page[substream->stream]); /* the same page */ 665 } 666 667 static const struct snd_pcm_ops dummy_pcm_ops = { 668 .open = dummy_pcm_open, 669 .close = dummy_pcm_close, 670 .hw_params = dummy_pcm_hw_params, 671 .prepare = dummy_pcm_prepare, 672 .trigger = dummy_pcm_trigger, 673 .sync_stop = dummy_pcm_sync_stop, 674 .pointer = dummy_pcm_pointer, 675 }; 676 677 static const struct snd_pcm_ops dummy_pcm_ops_no_buf = { 678 .open = dummy_pcm_open, 679 .close = dummy_pcm_close, 680 .hw_params = dummy_pcm_hw_params, 681 .prepare = dummy_pcm_prepare, 682 .trigger = dummy_pcm_trigger, 683 .sync_stop = dummy_pcm_sync_stop, 684 .pointer = dummy_pcm_pointer, 685 .copy = dummy_pcm_copy, 686 .fill_silence = dummy_pcm_silence, 687 .page = dummy_pcm_page, 688 }; 689 690 static int snd_card_dummy_pcm(struct snd_dummy *dummy, int device, 691 int substreams) 692 { 693 struct snd_pcm *pcm; 694 const struct snd_pcm_ops *ops; 695 int err; 696 697 err = snd_pcm_new(dummy->card, "Dummy PCM", device, 698 substreams, substreams, &pcm); 699 if (err < 0) 700 return err; 701 dummy->pcm = pcm; 702 if (fake_buffer) 703 ops = &dummy_pcm_ops_no_buf; 704 else 705 ops = &dummy_pcm_ops; 706 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops); 707 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops); 708 pcm->private_data = dummy; 709 pcm->info_flags = 0; 710 strscpy(pcm->name, "Dummy PCM"); 711 if (!fake_buffer) { 712 snd_pcm_set_managed_buffer_all(pcm, 713 SNDRV_DMA_TYPE_CONTINUOUS, 714 NULL, 715 0, 64*1024); 716 } 717 return 0; 718 } 719 720 /* 721 * mixer interface 722 */ 723 724 #define DUMMY_VOLUME(xname, xindex, addr) \ 725 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ 726 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ 727 .name = xname, .index = xindex, \ 728 .info = snd_dummy_volume_info, \ 729 .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \ 730 .private_value = addr, \ 731 .tlv = { .p = db_scale_dummy } } 732 733 static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol, 734 struct snd_ctl_elem_info *uinfo) 735 { 736 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 737 uinfo->count = 2; 738 uinfo->value.integer.min = mixer_volume_level_min; 739 uinfo->value.integer.max = mixer_volume_level_max; 740 return 0; 741 } 742 743 static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol, 744 struct snd_ctl_elem_value *ucontrol) 745 { 746 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); 747 int addr = kcontrol->private_value; 748 749 guard(spinlock_irq)(&dummy->mixer_lock); 750 ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0]; 751 ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1]; 752 return 0; 753 } 754 755 static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol, 756 struct snd_ctl_elem_value *ucontrol) 757 { 758 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); 759 int change, addr = kcontrol->private_value; 760 int left, right; 761 762 left = ucontrol->value.integer.value[0]; 763 if (left < mixer_volume_level_min) 764 left = mixer_volume_level_min; 765 if (left > mixer_volume_level_max) 766 left = mixer_volume_level_max; 767 right = ucontrol->value.integer.value[1]; 768 if (right < mixer_volume_level_min) 769 right = mixer_volume_level_min; 770 if (right > mixer_volume_level_max) 771 right = mixer_volume_level_max; 772 guard(spinlock_irq)(&dummy->mixer_lock); 773 change = dummy->mixer_volume[addr][0] != left || 774 dummy->mixer_volume[addr][1] != right; 775 dummy->mixer_volume[addr][0] = left; 776 dummy->mixer_volume[addr][1] = right; 777 return change; 778 } 779 780 static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0); 781 782 #define DUMMY_CAPSRC(xname, xindex, addr) \ 783 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ 784 .info = snd_dummy_capsrc_info, \ 785 .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \ 786 .private_value = addr } 787 788 #define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info 789 790 static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol, 791 struct snd_ctl_elem_value *ucontrol) 792 { 793 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); 794 int addr = kcontrol->private_value; 795 796 guard(spinlock_irq)(&dummy->mixer_lock); 797 ucontrol->value.integer.value[0] = dummy->capture_source[addr][0]; 798 ucontrol->value.integer.value[1] = dummy->capture_source[addr][1]; 799 return 0; 800 } 801 802 static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 803 { 804 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); 805 int change, addr = kcontrol->private_value; 806 int left, right; 807 808 left = ucontrol->value.integer.value[0] & 1; 809 right = ucontrol->value.integer.value[1] & 1; 810 guard(spinlock_irq)(&dummy->mixer_lock); 811 change = dummy->capture_source[addr][0] != left && 812 dummy->capture_source[addr][1] != right; 813 dummy->capture_source[addr][0] = left; 814 dummy->capture_source[addr][1] = right; 815 return change; 816 } 817 818 static int snd_dummy_iobox_info(struct snd_kcontrol *kcontrol, 819 struct snd_ctl_elem_info *info) 820 { 821 static const char *const names[] = { "None", "CD Player" }; 822 823 return snd_ctl_enum_info(info, 1, 2, names); 824 } 825 826 static int snd_dummy_iobox_get(struct snd_kcontrol *kcontrol, 827 struct snd_ctl_elem_value *value) 828 { 829 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); 830 831 value->value.enumerated.item[0] = dummy->iobox; 832 return 0; 833 } 834 835 static int snd_dummy_iobox_put(struct snd_kcontrol *kcontrol, 836 struct snd_ctl_elem_value *value) 837 { 838 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); 839 int changed; 840 841 if (value->value.enumerated.item[0] > 1) 842 return -EINVAL; 843 844 changed = value->value.enumerated.item[0] != dummy->iobox; 845 if (changed) { 846 dummy->iobox = value->value.enumerated.item[0]; 847 848 if (dummy->iobox) { 849 dummy->cd_volume_ctl->vd[0].access &= 850 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE; 851 dummy->cd_switch_ctl->vd[0].access &= 852 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE; 853 } else { 854 dummy->cd_volume_ctl->vd[0].access |= 855 SNDRV_CTL_ELEM_ACCESS_INACTIVE; 856 dummy->cd_switch_ctl->vd[0].access |= 857 SNDRV_CTL_ELEM_ACCESS_INACTIVE; 858 } 859 860 snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO, 861 &dummy->cd_volume_ctl->id); 862 snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO, 863 &dummy->cd_switch_ctl->id); 864 } 865 866 return changed; 867 } 868 869 static const struct snd_kcontrol_new snd_dummy_controls[] = { 870 DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER), 871 DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER), 872 DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH), 873 DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH), 874 DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE), 875 DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE), 876 DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC), 877 DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC), 878 DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD), 879 DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD), 880 { 881 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 882 .name = "External I/O Box", 883 .info = snd_dummy_iobox_info, 884 .get = snd_dummy_iobox_get, 885 .put = snd_dummy_iobox_put, 886 }, 887 }; 888 889 static int snd_card_dummy_new_mixer(struct snd_dummy *dummy) 890 { 891 struct snd_card *card = dummy->card; 892 struct snd_kcontrol *kcontrol; 893 unsigned int idx; 894 int err; 895 896 spin_lock_init(&dummy->mixer_lock); 897 strscpy(card->mixername, "Dummy Mixer"); 898 dummy->iobox = 1; 899 900 for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) { 901 kcontrol = snd_ctl_new1(&snd_dummy_controls[idx], dummy); 902 err = snd_ctl_add(card, kcontrol); 903 if (err < 0) 904 return err; 905 if (!strcmp(kcontrol->id.name, "CD Volume")) 906 dummy->cd_volume_ctl = kcontrol; 907 else if (!strcmp(kcontrol->id.name, "CD Capture Switch")) 908 dummy->cd_switch_ctl = kcontrol; 909 910 } 911 return 0; 912 } 913 914 #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_PROC_FS) 915 /* 916 * proc interface 917 */ 918 static void print_formats(struct snd_dummy *dummy, 919 struct snd_info_buffer *buffer) 920 { 921 snd_pcm_format_t i; 922 923 pcm_for_each_format(i) { 924 if (dummy->pcm_hw.formats & pcm_format_to_bits(i)) 925 snd_iprintf(buffer, " %s", snd_pcm_format_name(i)); 926 } 927 } 928 929 static void print_rates(struct snd_dummy *dummy, 930 struct snd_info_buffer *buffer) 931 { 932 static const int rates[] = { 933 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000, 934 64000, 88200, 96000, 176400, 192000, 935 }; 936 int i; 937 938 if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_CONTINUOUS) 939 snd_iprintf(buffer, " continuous"); 940 if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_KNOT) 941 snd_iprintf(buffer, " knot"); 942 for (i = 0; i < ARRAY_SIZE(rates); i++) 943 if (dummy->pcm_hw.rates & (1 << i)) 944 snd_iprintf(buffer, " %d", rates[i]); 945 } 946 947 #define get_dummy_int_ptr(dummy, ofs) \ 948 (unsigned int *)((char *)&((dummy)->pcm_hw) + (ofs)) 949 #define get_dummy_ll_ptr(dummy, ofs) \ 950 (unsigned long long *)((char *)&((dummy)->pcm_hw) + (ofs)) 951 952 struct dummy_hw_field { 953 const char *name; 954 const char *format; 955 unsigned int offset; 956 unsigned int size; 957 }; 958 #define FIELD_ENTRY(item, fmt) { \ 959 .name = #item, \ 960 .format = fmt, \ 961 .offset = offsetof(struct snd_pcm_hardware, item), \ 962 .size = sizeof(dummy_pcm_hardware.item) } 963 964 static const struct dummy_hw_field fields[] = { 965 FIELD_ENTRY(formats, "%#llx"), 966 FIELD_ENTRY(rates, "%#x"), 967 FIELD_ENTRY(rate_min, "%d"), 968 FIELD_ENTRY(rate_max, "%d"), 969 FIELD_ENTRY(channels_min, "%d"), 970 FIELD_ENTRY(channels_max, "%d"), 971 FIELD_ENTRY(buffer_bytes_max, "%ld"), 972 FIELD_ENTRY(period_bytes_min, "%ld"), 973 FIELD_ENTRY(period_bytes_max, "%ld"), 974 FIELD_ENTRY(periods_min, "%d"), 975 FIELD_ENTRY(periods_max, "%d"), 976 }; 977 978 static void dummy_proc_read(struct snd_info_entry *entry, 979 struct snd_info_buffer *buffer) 980 { 981 struct snd_dummy *dummy = entry->private_data; 982 int i; 983 984 for (i = 0; i < ARRAY_SIZE(fields); i++) { 985 snd_iprintf(buffer, "%s ", fields[i].name); 986 if (fields[i].size == sizeof(int)) 987 snd_iprintf(buffer, fields[i].format, 988 *get_dummy_int_ptr(dummy, fields[i].offset)); 989 else 990 snd_iprintf(buffer, fields[i].format, 991 *get_dummy_ll_ptr(dummy, fields[i].offset)); 992 if (!strcmp(fields[i].name, "formats")) 993 print_formats(dummy, buffer); 994 else if (!strcmp(fields[i].name, "rates")) 995 print_rates(dummy, buffer); 996 snd_iprintf(buffer, "\n"); 997 } 998 } 999 1000 static void dummy_proc_write(struct snd_info_entry *entry, 1001 struct snd_info_buffer *buffer) 1002 { 1003 struct snd_dummy *dummy = entry->private_data; 1004 char line[64]; 1005 1006 while (!snd_info_get_line(buffer, line, sizeof(line))) { 1007 char item[20]; 1008 const char *ptr; 1009 unsigned long long val; 1010 int i; 1011 1012 ptr = snd_info_get_str(item, line, sizeof(item)); 1013 for (i = 0; i < ARRAY_SIZE(fields); i++) { 1014 if (!strcmp(item, fields[i].name)) 1015 break; 1016 } 1017 if (i >= ARRAY_SIZE(fields)) 1018 continue; 1019 snd_info_get_str(item, ptr, sizeof(item)); 1020 if (kstrtoull(item, 0, &val)) 1021 continue; 1022 if (fields[i].size == sizeof(int)) 1023 *get_dummy_int_ptr(dummy, fields[i].offset) = val; 1024 else 1025 *get_dummy_ll_ptr(dummy, fields[i].offset) = val; 1026 } 1027 } 1028 1029 static void dummy_proc_init(struct snd_dummy *chip) 1030 { 1031 snd_card_rw_proc_new(chip->card, "dummy_pcm", chip, 1032 dummy_proc_read, dummy_proc_write); 1033 } 1034 #else 1035 #define dummy_proc_init(x) 1036 #endif /* CONFIG_SND_DEBUG && CONFIG_SND_PROC_FS */ 1037 1038 static int snd_dummy_probe(struct platform_device *devptr) 1039 { 1040 struct snd_card *card; 1041 struct snd_dummy *dummy; 1042 const struct dummy_model *m = NULL, **mdl; 1043 int idx, err; 1044 int dev = devptr->id; 1045 1046 if (dev < 0 || dev >= SNDRV_CARDS) { 1047 dev_warn(&devptr->dev, 1048 "Invalid card index %d, using default 0\n", dev); 1049 dev = 0; 1050 } 1051 1052 err = snd_devm_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE, 1053 sizeof(struct snd_dummy), &card); 1054 if (err < 0) 1055 return err; 1056 dummy = card->private_data; 1057 dummy->card = card; 1058 for (mdl = dummy_models; *mdl && model[dev]; mdl++) { 1059 if (strcmp(model[dev], (*mdl)->name) == 0) { 1060 pr_info("snd-dummy: Using model '%s' for card %i\n", 1061 (*mdl)->name, card->number); 1062 m = dummy->model = *mdl; 1063 break; 1064 } 1065 } 1066 for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) { 1067 if (pcm_substreams[dev] < 1) 1068 pcm_substreams[dev] = 1; 1069 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS) 1070 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS; 1071 err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]); 1072 if (err < 0) 1073 return err; 1074 } 1075 1076 dummy->pcm_hw = dummy_pcm_hardware; 1077 if (m) { 1078 if (m->formats) 1079 dummy->pcm_hw.formats = m->formats; 1080 if (m->buffer_bytes_max) 1081 dummy->pcm_hw.buffer_bytes_max = m->buffer_bytes_max; 1082 if (m->period_bytes_min) 1083 dummy->pcm_hw.period_bytes_min = m->period_bytes_min; 1084 if (m->period_bytes_max) 1085 dummy->pcm_hw.period_bytes_max = m->period_bytes_max; 1086 if (m->periods_min) 1087 dummy->pcm_hw.periods_min = m->periods_min; 1088 if (m->periods_max) 1089 dummy->pcm_hw.periods_max = m->periods_max; 1090 if (m->rates) 1091 dummy->pcm_hw.rates = m->rates; 1092 if (m->rate_min) 1093 dummy->pcm_hw.rate_min = m->rate_min; 1094 if (m->rate_max) 1095 dummy->pcm_hw.rate_max = m->rate_max; 1096 if (m->channels_min) 1097 dummy->pcm_hw.channels_min = m->channels_min; 1098 if (m->channels_max) 1099 dummy->pcm_hw.channels_max = m->channels_max; 1100 } 1101 1102 if (mixer_volume_level_min > mixer_volume_level_max) { 1103 pr_warn("snd-dummy: Invalid mixer volume level: min=%d, max=%d. Fall back to default value.\n", 1104 mixer_volume_level_min, mixer_volume_level_max); 1105 mixer_volume_level_min = USE_MIXER_VOLUME_LEVEL_MIN; 1106 mixer_volume_level_max = USE_MIXER_VOLUME_LEVEL_MAX; 1107 } 1108 err = snd_card_dummy_new_mixer(dummy); 1109 if (err < 0) 1110 return err; 1111 strscpy(card->driver, "Dummy"); 1112 strscpy(card->shortname, "Dummy"); 1113 sprintf(card->longname, "Dummy %i", dev + 1); 1114 1115 dummy_proc_init(dummy); 1116 1117 err = snd_card_register(card); 1118 if (err < 0) 1119 return err; 1120 platform_set_drvdata(devptr, card); 1121 return 0; 1122 } 1123 1124 static int snd_dummy_suspend(struct device *pdev) 1125 { 1126 struct snd_card *card = dev_get_drvdata(pdev); 1127 1128 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 1129 return 0; 1130 } 1131 1132 static int snd_dummy_resume(struct device *pdev) 1133 { 1134 struct snd_card *card = dev_get_drvdata(pdev); 1135 1136 snd_power_change_state(card, SNDRV_CTL_POWER_D0); 1137 return 0; 1138 } 1139 1140 static DEFINE_SIMPLE_DEV_PM_OPS(snd_dummy_pm, snd_dummy_suspend, snd_dummy_resume); 1141 1142 #define SND_DUMMY_DRIVER "snd_dummy" 1143 1144 static struct platform_driver snd_dummy_driver = { 1145 .probe = snd_dummy_probe, 1146 .driver = { 1147 .name = SND_DUMMY_DRIVER, 1148 .pm = &snd_dummy_pm, 1149 }, 1150 }; 1151 1152 static void snd_dummy_unregister_all(void) 1153 { 1154 int i; 1155 1156 for (i = 0; i < ARRAY_SIZE(devices); ++i) 1157 platform_device_unregister(devices[i]); 1158 platform_driver_unregister(&snd_dummy_driver); 1159 free_fake_buffer(); 1160 } 1161 1162 static int __init alsa_card_dummy_init(void) 1163 { 1164 int i, cards, err; 1165 1166 err = platform_driver_register(&snd_dummy_driver); 1167 if (err < 0) 1168 return err; 1169 1170 err = alloc_fake_buffer(); 1171 if (err < 0) { 1172 platform_driver_unregister(&snd_dummy_driver); 1173 return err; 1174 } 1175 1176 cards = 0; 1177 for (i = 0; i < SNDRV_CARDS; i++) { 1178 struct platform_device *device; 1179 if (! enable[i]) 1180 continue; 1181 device = platform_device_register_simple(SND_DUMMY_DRIVER, 1182 i, NULL, 0); 1183 if (IS_ERR(device)) 1184 continue; 1185 if (!platform_get_drvdata(device)) { 1186 platform_device_unregister(device); 1187 continue; 1188 } 1189 devices[i] = device; 1190 cards++; 1191 } 1192 if (!cards) { 1193 #ifdef MODULE 1194 pr_err("Dummy soundcard not found or device busy\n"); 1195 #endif 1196 snd_dummy_unregister_all(); 1197 return -ENODEV; 1198 } 1199 return 0; 1200 } 1201 1202 static void __exit alsa_card_dummy_exit(void) 1203 { 1204 snd_dummy_unregister_all(); 1205 } 1206 1207 module_init(alsa_card_dummy_init) 1208 module_exit(alsa_card_dummy_exit) 1209