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