1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Loopback soundcard 4 * 5 * Original code: 6 * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 7 * 8 * More accurate positioning and full-duplex support: 9 * Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de> 10 * 11 * Major (almost complete) rewrite: 12 * Copyright (c) by Takashi Iwai <tiwai@suse.de> 13 * 14 * A next major update in 2010 (separate timers for playback and capture): 15 * Copyright (c) Jaroslav Kysela <perex@perex.cz> 16 */ 17 18 #include <linux/init.h> 19 #include <linux/jiffies.h> 20 #include <linux/slab.h> 21 #include <linux/string.h> 22 #include <linux/time.h> 23 #include <linux/wait.h> 24 #include <linux/module.h> 25 #include <linux/platform_device.h> 26 #include <sound/core.h> 27 #include <sound/control.h> 28 #include <sound/pcm.h> 29 #include <sound/pcm_params.h> 30 #include <sound/info.h> 31 #include <sound/initval.h> 32 #include <sound/timer.h> 33 34 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); 35 MODULE_DESCRIPTION("A loopback soundcard"); 36 MODULE_LICENSE("GPL"); 37 38 #define MAX_PCM_SUBSTREAMS 8 39 40 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ 41 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ 42 static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0}; 43 static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8}; 44 static int pcm_notify[SNDRV_CARDS]; 45 static char *timer_source[SNDRV_CARDS]; 46 47 module_param_array(index, int, NULL, 0444); 48 MODULE_PARM_DESC(index, "Index value for loopback soundcard."); 49 module_param_array(id, charp, NULL, 0444); 50 MODULE_PARM_DESC(id, "ID string for loopback soundcard."); 51 module_param_array(enable, bool, NULL, 0444); 52 MODULE_PARM_DESC(enable, "Enable this loopback soundcard."); 53 module_param_array(pcm_substreams, int, NULL, 0444); 54 MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver."); 55 module_param_array(pcm_notify, int, NULL, 0444); 56 MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes."); 57 module_param_array(timer_source, charp, NULL, 0444); 58 MODULE_PARM_DESC(timer_source, "Sound card name or number and device/subdevice number of timer to be used. Empty string for jiffies timer [default]."); 59 60 #define NO_PITCH 100000 61 62 #define CABLE_VALID_PLAYBACK BIT(SNDRV_PCM_STREAM_PLAYBACK) 63 #define CABLE_VALID_CAPTURE BIT(SNDRV_PCM_STREAM_CAPTURE) 64 #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK | CABLE_VALID_CAPTURE) 65 66 struct loopback_cable; 67 struct loopback_pcm; 68 69 struct loopback_ops { 70 /* optional 71 * call in loopback->cable_lock 72 */ 73 int (*open)(struct loopback_pcm *dpcm); 74 /* required 75 * call in cable->lock 76 */ 77 int (*start)(struct loopback_pcm *dpcm); 78 /* required 79 * call in cable->lock 80 */ 81 int (*stop)(struct loopback_pcm *dpcm); 82 /* optional */ 83 int (*stop_sync)(struct loopback_pcm *dpcm); 84 /* optional */ 85 int (*close_substream)(struct loopback_pcm *dpcm); 86 /* optional 87 * call in loopback->cable_lock 88 */ 89 int (*close_cable)(struct loopback_pcm *dpcm); 90 /* optional 91 * call in cable->lock 92 */ 93 unsigned int (*pos_update)(struct loopback_cable *cable); 94 /* optional */ 95 void (*dpcm_info)(struct loopback_pcm *dpcm, 96 struct snd_info_buffer *buffer); 97 }; 98 99 struct loopback_cable { 100 spinlock_t lock; 101 struct loopback_pcm *streams[2]; 102 /* in-flight peer stops running outside cable->lock */ 103 atomic_t stop_count; 104 wait_queue_head_t stop_wait; 105 struct snd_pcm_hardware hw; 106 /* flags */ 107 unsigned int valid; 108 unsigned int running; 109 unsigned int pause; 110 /* timer specific */ 111 const struct loopback_ops *ops; 112 /* If sound timer is used */ 113 struct { 114 int stream; 115 struct snd_timer_id id; 116 struct work_struct event_work; 117 struct snd_timer_instance *instance; 118 } snd_timer; 119 }; 120 121 struct loopback_setup { 122 unsigned int notify: 1; 123 unsigned int rate_shift; 124 snd_pcm_format_t format; 125 unsigned int rate; 126 snd_pcm_access_t access; 127 unsigned int channels; 128 struct snd_ctl_elem_id active_id; 129 struct snd_ctl_elem_id format_id; 130 struct snd_ctl_elem_id rate_id; 131 struct snd_ctl_elem_id channels_id; 132 struct snd_ctl_elem_id access_id; 133 }; 134 135 struct loopback { 136 struct snd_card *card; 137 struct mutex cable_lock; 138 struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2]; 139 struct snd_pcm *pcm[2]; 140 struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2]; 141 const char *timer_source; 142 }; 143 144 struct loopback_pcm { 145 struct loopback *loopback; 146 struct snd_pcm_substream *substream; 147 struct loopback_cable *cable; 148 unsigned int pcm_buffer_size; 149 unsigned int buf_pos; /* position in buffer */ 150 unsigned int silent_size; 151 /* PCM parameters */ 152 unsigned int pcm_period_size; 153 unsigned int pcm_bps; /* bytes per second */ 154 unsigned int pcm_salign; /* bytes per sample * channels */ 155 unsigned int pcm_rate_shift; /* rate shift value */ 156 /* flags */ 157 unsigned int period_update_pending :1; 158 /* timer stuff */ 159 unsigned int irq_pos; /* fractional IRQ position in jiffies 160 * ticks 161 */ 162 unsigned int period_size_frac; /* period size in jiffies ticks */ 163 unsigned int last_drift; 164 unsigned long last_jiffies; 165 /* If jiffies timer is used */ 166 struct timer_list timer; 167 168 /* size of per channel buffer in case of non-interleaved access */ 169 unsigned int channel_buf_n; 170 }; 171 172 static struct platform_device *devices[SNDRV_CARDS]; 173 174 static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x) 175 { 176 if (dpcm->pcm_rate_shift == NO_PITCH) { 177 x /= HZ; 178 } else { 179 x = div_u64(NO_PITCH * (unsigned long long)x, 180 HZ * (unsigned long long)dpcm->pcm_rate_shift); 181 } 182 return x - (x % dpcm->pcm_salign); 183 } 184 185 static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x) 186 { 187 if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */ 188 return x * HZ; 189 } else { 190 x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ, 191 NO_PITCH); 192 } 193 return x; 194 } 195 196 static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm) 197 { 198 int device = dpcm->substream->pstr->pcm->device; 199 200 if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 201 device ^= 1; 202 return &dpcm->loopback->setup[dpcm->substream->number][device]; 203 } 204 205 static inline unsigned int get_notify(struct loopback_pcm *dpcm) 206 { 207 return get_setup(dpcm)->notify; 208 } 209 210 static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm) 211 { 212 return get_setup(dpcm)->rate_shift; 213 } 214 215 /* call in cable->lock */ 216 static int loopback_jiffies_timer_start(struct loopback_pcm *dpcm) 217 { 218 unsigned long tick; 219 unsigned int rate_shift = get_rate_shift(dpcm); 220 221 if (rate_shift != dpcm->pcm_rate_shift) { 222 dpcm->pcm_rate_shift = rate_shift; 223 dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size); 224 } 225 if (dpcm->period_size_frac <= dpcm->irq_pos) { 226 dpcm->irq_pos %= dpcm->period_size_frac; 227 dpcm->period_update_pending = 1; 228 } 229 tick = dpcm->period_size_frac - dpcm->irq_pos; 230 tick = DIV_ROUND_UP(tick, dpcm->pcm_bps); 231 mod_timer(&dpcm->timer, jiffies + tick); 232 233 return 0; 234 } 235 236 /* call in cable->lock */ 237 static int loopback_snd_timer_start(struct loopback_pcm *dpcm) 238 { 239 struct loopback_cable *cable = dpcm->cable; 240 int err; 241 242 /* Loopback device has to use same period as timer card. Therefore 243 * wake up for each snd_pcm_period_elapsed() call of timer card. 244 */ 245 err = snd_timer_start(cable->snd_timer.instance, 1); 246 if (err < 0) { 247 /* do not report error if trying to start but already 248 * running. For example called by opposite substream 249 * of the same cable 250 */ 251 if (err == -EBUSY) 252 return 0; 253 254 pcm_err(dpcm->substream->pcm, 255 "snd_timer_start(%d,%d,%d) failed with %d", 256 cable->snd_timer.id.card, 257 cable->snd_timer.id.device, 258 cable->snd_timer.id.subdevice, 259 err); 260 } 261 262 return err; 263 } 264 265 /* call in cable->lock */ 266 static inline int loopback_jiffies_timer_stop(struct loopback_pcm *dpcm) 267 { 268 timer_delete(&dpcm->timer); 269 dpcm->timer.expires = 0; 270 271 return 0; 272 } 273 274 /* call in cable->lock */ 275 static int loopback_snd_timer_stop(struct loopback_pcm *dpcm) 276 { 277 struct loopback_cable *cable = dpcm->cable; 278 int err; 279 280 /* only stop if both devices (playback and capture) are not running */ 281 if (cable->running ^ cable->pause) 282 return 0; 283 284 err = snd_timer_stop(cable->snd_timer.instance); 285 if (err < 0) { 286 pcm_err(dpcm->substream->pcm, 287 "snd_timer_stop(%d,%d,%d) failed with %d", 288 cable->snd_timer.id.card, 289 cable->snd_timer.id.device, 290 cable->snd_timer.id.subdevice, 291 err); 292 } 293 294 return err; 295 } 296 297 static inline int loopback_jiffies_timer_stop_sync(struct loopback_pcm *dpcm) 298 { 299 timer_delete_sync(&dpcm->timer); 300 301 return 0; 302 } 303 304 /* call in loopback->cable_lock */ 305 static int loopback_snd_timer_close_cable(struct loopback_pcm *dpcm) 306 { 307 struct loopback_cable *cable = dpcm->cable; 308 309 /* snd_timer was not opened */ 310 if (!cable->snd_timer.instance) 311 return 0; 312 313 /* will only be called from free_cable() when other stream was 314 * already closed. Other stream cannot be reopened as long as 315 * loopback->cable_lock is locked. Therefore no need to lock 316 * cable->lock; 317 */ 318 snd_timer_close(cable->snd_timer.instance); 319 320 /* wait till drain work has finished if requested */ 321 cancel_work_sync(&cable->snd_timer.event_work); 322 323 snd_timer_instance_free(cable->snd_timer.instance); 324 memset(&cable->snd_timer, 0, sizeof(cable->snd_timer)); 325 326 return 0; 327 } 328 329 static bool is_access_interleaved(snd_pcm_access_t access) 330 { 331 switch (access) { 332 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED: 333 case SNDRV_PCM_ACCESS_RW_INTERLEAVED: 334 return true; 335 default: 336 return false; 337 } 338 }; 339 340 static int loopback_check_format(struct loopback_cable *cable, int stream) 341 { 342 struct loopback_pcm *dpcm_play, *dpcm_capt; 343 struct snd_pcm_runtime *runtime, *cruntime; 344 struct loopback_setup *setup; 345 struct snd_card *card; 346 bool stop_capture = false; 347 int check; 348 349 scoped_guard(spinlock_irqsave, &cable->lock) { 350 dpcm_play = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]; 351 dpcm_capt = cable->streams[SNDRV_PCM_STREAM_CAPTURE]; 352 353 if (cable->valid != CABLE_VALID_BOTH) { 354 if (stream == SNDRV_PCM_STREAM_CAPTURE || !dpcm_play) 355 return 0; 356 } else { 357 if (!dpcm_play || !dpcm_capt) 358 return -EIO; 359 runtime = dpcm_play->substream->runtime; 360 cruntime = dpcm_capt->substream->runtime; 361 if (!runtime || !cruntime) 362 return -EIO; 363 check = runtime->format != cruntime->format || 364 runtime->rate != cruntime->rate || 365 runtime->channels != cruntime->channels || 366 is_access_interleaved(runtime->access) != 367 is_access_interleaved(cruntime->access); 368 if (!check) 369 return 0; 370 if (stream == SNDRV_PCM_STREAM_CAPTURE) 371 return -EIO; 372 else if (cruntime->state == SNDRV_PCM_STATE_RUNNING) { 373 /* close must not free the peer runtime below */ 374 atomic_inc(&cable->stop_count); 375 stop_capture = true; 376 } 377 } 378 379 setup = get_setup(dpcm_play); 380 card = dpcm_play->loopback->card; 381 runtime = dpcm_play->substream->runtime; 382 if (setup->format != runtime->format) { 383 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 384 &setup->format_id); 385 setup->format = runtime->format; 386 } 387 if (setup->rate != runtime->rate) { 388 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 389 &setup->rate_id); 390 setup->rate = runtime->rate; 391 } 392 if (setup->channels != runtime->channels) { 393 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 394 &setup->channels_id); 395 setup->channels = runtime->channels; 396 } 397 if (is_access_interleaved(setup->access) != 398 is_access_interleaved(runtime->access)) { 399 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 400 &setup->access_id); 401 setup->access = runtime->access; 402 } 403 } 404 405 if (stop_capture) { 406 snd_pcm_stop(dpcm_capt->substream, SNDRV_PCM_STATE_DRAINING); 407 if (atomic_dec_and_test(&cable->stop_count)) 408 wake_up(&cable->stop_wait); 409 } 410 411 return 0; 412 } 413 414 static void loopback_active_notify(struct loopback_pcm *dpcm) 415 { 416 snd_ctl_notify(dpcm->loopback->card, 417 SNDRV_CTL_EVENT_MASK_VALUE, 418 &get_setup(dpcm)->active_id); 419 } 420 421 static int loopback_trigger(struct snd_pcm_substream *substream, int cmd) 422 { 423 struct snd_pcm_runtime *runtime = substream->runtime; 424 struct loopback_pcm *dpcm = runtime->private_data; 425 struct loopback_cable *cable = dpcm->cable; 426 int err = 0, stream = 1 << substream->stream; 427 428 switch (cmd) { 429 case SNDRV_PCM_TRIGGER_START: 430 err = loopback_check_format(cable, substream->stream); 431 if (err < 0) 432 return err; 433 dpcm->last_jiffies = jiffies; 434 dpcm->pcm_rate_shift = 0; 435 dpcm->last_drift = 0; 436 scoped_guard(spinlock, &cable->lock) { 437 cable->running |= stream; 438 cable->pause &= ~stream; 439 err = cable->ops->start(dpcm); 440 } 441 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 442 loopback_active_notify(dpcm); 443 break; 444 case SNDRV_PCM_TRIGGER_STOP: 445 scoped_guard(spinlock, &cable->lock) { 446 cable->running &= ~stream; 447 cable->pause &= ~stream; 448 err = cable->ops->stop(dpcm); 449 } 450 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 451 loopback_active_notify(dpcm); 452 break; 453 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 454 case SNDRV_PCM_TRIGGER_SUSPEND: 455 scoped_guard(spinlock, &cable->lock) { 456 cable->pause |= stream; 457 err = cable->ops->stop(dpcm); 458 } 459 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 460 loopback_active_notify(dpcm); 461 break; 462 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 463 case SNDRV_PCM_TRIGGER_RESUME: 464 scoped_guard(spinlock, &cable->lock) { 465 dpcm->last_jiffies = jiffies; 466 cable->pause &= ~stream; 467 err = cable->ops->start(dpcm); 468 } 469 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 470 loopback_active_notify(dpcm); 471 break; 472 default: 473 return -EINVAL; 474 } 475 return err; 476 } 477 478 static void params_change(struct snd_pcm_substream *substream) 479 { 480 struct snd_pcm_runtime *runtime = substream->runtime; 481 struct loopback_pcm *dpcm = runtime->private_data; 482 struct loopback_cable *cable = dpcm->cable; 483 484 cable->hw.formats = pcm_format_to_bits(runtime->format); 485 cable->hw.rate_min = runtime->rate; 486 cable->hw.rate_max = runtime->rate; 487 cable->hw.channels_min = runtime->channels; 488 cable->hw.channels_max = runtime->channels; 489 490 if (cable->snd_timer.instance) { 491 cable->hw.period_bytes_min = 492 frames_to_bytes(runtime, runtime->period_size); 493 cable->hw.period_bytes_max = cable->hw.period_bytes_min; 494 } 495 496 } 497 498 static int loopback_prepare(struct snd_pcm_substream *substream) 499 { 500 struct snd_pcm_runtime *runtime = substream->runtime; 501 struct loopback_pcm *dpcm = runtime->private_data; 502 struct loopback_cable *cable = dpcm->cable; 503 int err, bps, salign; 504 505 if (cable->ops->stop_sync) { 506 err = cable->ops->stop_sync(dpcm); 507 if (err < 0) 508 return err; 509 } 510 511 salign = (snd_pcm_format_physical_width(runtime->format) * 512 runtime->channels) / 8; 513 bps = salign * runtime->rate; 514 if (bps <= 0 || salign <= 0) 515 return -EINVAL; 516 517 dpcm->buf_pos = 0; 518 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size); 519 dpcm->channel_buf_n = dpcm->pcm_buffer_size / runtime->channels; 520 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { 521 /* clear capture buffer */ 522 dpcm->silent_size = dpcm->pcm_buffer_size; 523 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, 524 runtime->buffer_size * runtime->channels); 525 } 526 527 dpcm->irq_pos = 0; 528 dpcm->period_update_pending = 0; 529 dpcm->pcm_bps = bps; 530 dpcm->pcm_salign = salign; 531 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size); 532 533 guard(mutex)(&dpcm->loopback->cable_lock); 534 if (!(cable->valid & ~(1 << substream->stream)) || 535 (get_setup(dpcm)->notify && 536 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)) 537 params_change(substream); 538 cable->valid |= 1 << substream->stream; 539 540 return 0; 541 } 542 543 static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes) 544 { 545 struct snd_pcm_runtime *runtime = dpcm->substream->runtime; 546 char *dst = runtime->dma_area; 547 unsigned int dst_off = dpcm->buf_pos; 548 549 if (dpcm->silent_size >= dpcm->pcm_buffer_size) 550 return; 551 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size) 552 bytes = dpcm->pcm_buffer_size - dpcm->silent_size; 553 554 for (;;) { 555 unsigned int size = bytes; 556 if (dst_off + size > dpcm->pcm_buffer_size) 557 size = dpcm->pcm_buffer_size - dst_off; 558 snd_pcm_format_set_silence(runtime->format, dst + dst_off, 559 bytes_to_frames(runtime, size) * 560 runtime->channels); 561 dpcm->silent_size += size; 562 bytes -= size; 563 if (!bytes) 564 break; 565 dst_off = 0; 566 } 567 } 568 569 static void copy_play_buf_part_n(struct loopback_pcm *play, struct loopback_pcm *capt, 570 unsigned int size, unsigned int src_off, unsigned int dst_off) 571 { 572 unsigned int channels = capt->substream->runtime->channels; 573 unsigned int size_p_ch = size / channels; 574 unsigned int src_off_ch = src_off / channels; 575 unsigned int dst_off_ch = dst_off / channels; 576 int i; 577 578 for (i = 0; i < channels; i++) { 579 memcpy(capt->substream->runtime->dma_area + capt->channel_buf_n * i + dst_off_ch, 580 play->substream->runtime->dma_area + play->channel_buf_n * i + src_off_ch, 581 size_p_ch); 582 } 583 } 584 585 static void copy_play_buf(struct loopback_pcm *play, 586 struct loopback_pcm *capt, 587 unsigned int bytes) 588 { 589 struct snd_pcm_runtime *runtime = play->substream->runtime; 590 char *src = runtime->dma_area; 591 char *dst = capt->substream->runtime->dma_area; 592 unsigned int src_off = play->buf_pos; 593 unsigned int dst_off = capt->buf_pos; 594 unsigned int clear_bytes = 0; 595 596 /* check if playback is draining, trim the capture copy size 597 * when our pointer is at the end of playback ring buffer */ 598 if (runtime->state == SNDRV_PCM_STATE_DRAINING && 599 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) { 600 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff; 601 appl_ptr = appl_ptr1 = runtime->control->appl_ptr; 602 appl_ptr1 -= appl_ptr1 % runtime->buffer_size; 603 appl_ptr1 += play->buf_pos / play->pcm_salign; 604 if (appl_ptr < appl_ptr1) 605 appl_ptr1 -= runtime->buffer_size; 606 diff = (appl_ptr - appl_ptr1) * play->pcm_salign; 607 if (diff < bytes) { 608 clear_bytes = bytes - diff; 609 bytes = diff; 610 } 611 } 612 613 for (;;) { 614 unsigned int size = bytes; 615 if (src_off + size > play->pcm_buffer_size) 616 size = play->pcm_buffer_size - src_off; 617 if (dst_off + size > capt->pcm_buffer_size) 618 size = capt->pcm_buffer_size - dst_off; 619 if (!is_access_interleaved(runtime->access)) 620 copy_play_buf_part_n(play, capt, size, src_off, dst_off); 621 else 622 memcpy(dst + dst_off, src + src_off, size); 623 capt->silent_size = 0; 624 bytes -= size; 625 if (!bytes) 626 break; 627 src_off = (src_off + size) % play->pcm_buffer_size; 628 dst_off = (dst_off + size) % capt->pcm_buffer_size; 629 } 630 631 if (clear_bytes > 0) { 632 clear_capture_buf(capt, clear_bytes); 633 capt->silent_size = 0; 634 } 635 } 636 637 static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm, 638 unsigned int jiffies_delta) 639 { 640 unsigned long last_pos; 641 unsigned int delta; 642 643 last_pos = byte_pos(dpcm, dpcm->irq_pos); 644 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps; 645 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos; 646 if (delta >= dpcm->last_drift) 647 delta -= dpcm->last_drift; 648 dpcm->last_drift = 0; 649 if (dpcm->irq_pos >= dpcm->period_size_frac) { 650 dpcm->irq_pos %= dpcm->period_size_frac; 651 dpcm->period_update_pending = 1; 652 } 653 return delta; 654 } 655 656 static inline void bytepos_finish(struct loopback_pcm *dpcm, 657 unsigned int delta) 658 { 659 dpcm->buf_pos += delta; 660 dpcm->buf_pos %= dpcm->pcm_buffer_size; 661 } 662 663 /* call in cable->lock */ 664 static unsigned int loopback_jiffies_timer_pos_update 665 (struct loopback_cable *cable) 666 { 667 struct loopback_pcm *dpcm_play = 668 cable->streams[SNDRV_PCM_STREAM_PLAYBACK]; 669 struct loopback_pcm *dpcm_capt = 670 cable->streams[SNDRV_PCM_STREAM_CAPTURE]; 671 unsigned long delta_play = 0, delta_capt = 0, cur_jiffies; 672 unsigned int running, count1, count2; 673 674 cur_jiffies = jiffies; 675 running = cable->running ^ cable->pause; 676 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) { 677 delta_play = cur_jiffies - dpcm_play->last_jiffies; 678 dpcm_play->last_jiffies += delta_play; 679 } 680 681 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) { 682 delta_capt = cur_jiffies - dpcm_capt->last_jiffies; 683 dpcm_capt->last_jiffies += delta_capt; 684 } 685 686 if (delta_play == 0 && delta_capt == 0) 687 goto unlock; 688 689 if (delta_play > delta_capt) { 690 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt); 691 bytepos_finish(dpcm_play, count1); 692 delta_play = delta_capt; 693 } else if (delta_play < delta_capt) { 694 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play); 695 clear_capture_buf(dpcm_capt, count1); 696 bytepos_finish(dpcm_capt, count1); 697 delta_capt = delta_play; 698 } 699 700 if (delta_play == 0 && delta_capt == 0) 701 goto unlock; 702 703 /* note delta_capt == delta_play at this moment */ 704 count1 = bytepos_delta(dpcm_play, delta_play); 705 count2 = bytepos_delta(dpcm_capt, delta_capt); 706 if (count1 < count2) { 707 dpcm_capt->last_drift = count2 - count1; 708 count1 = count2; 709 } else if (count1 > count2) { 710 dpcm_play->last_drift = count1 - count2; 711 } 712 copy_play_buf(dpcm_play, dpcm_capt, count1); 713 bytepos_finish(dpcm_play, count1); 714 bytepos_finish(dpcm_capt, count1); 715 unlock: 716 return running; 717 } 718 719 static void loopback_jiffies_timer_function(struct timer_list *t) 720 { 721 struct loopback_pcm *dpcm = timer_container_of(dpcm, t, timer); 722 bool period_elapsed = false; 723 724 scoped_guard(spinlock_irqsave, &dpcm->cable->lock) { 725 if (loopback_jiffies_timer_pos_update(dpcm->cable) & 726 (1 << dpcm->substream->stream)) { 727 loopback_jiffies_timer_start(dpcm); 728 if (dpcm->period_update_pending) { 729 dpcm->period_update_pending = 0; 730 period_elapsed = true; 731 break; 732 } 733 } 734 } 735 736 if (period_elapsed) 737 snd_pcm_period_elapsed(dpcm->substream); 738 } 739 740 /* call in cable->lock */ 741 static int loopback_snd_timer_check_resolution(struct snd_pcm_runtime *runtime, 742 unsigned long resolution) 743 { 744 if (resolution != runtime->timer_resolution) { 745 struct loopback_pcm *dpcm = runtime->private_data; 746 struct loopback_cable *cable = dpcm->cable; 747 /* Worst case estimation of possible values for resolution 748 * resolution <= (512 * 1024) frames / 8kHz in nsec 749 * resolution <= 65.536.000.000 nsec 750 * 751 * period_size <= 65.536.000.000 nsec / 1000nsec/usec * 192kHz + 752 * 500.000 753 * period_size <= 12.582.912.000.000 <64bit 754 * / 1.000.000 usec/sec 755 */ 756 snd_pcm_uframes_t period_size_usec = 757 resolution / 1000 * runtime->rate; 758 /* round to nearest sample rate */ 759 snd_pcm_uframes_t period_size = 760 (period_size_usec + 500 * 1000) / (1000 * 1000); 761 762 pcm_err(dpcm->substream->pcm, 763 "Period size (%lu frames) of loopback device is not corresponding to timer resolution (%lu nsec = %lu frames) of card timer %d,%d,%d. Use period size of %lu frames for loopback device.", 764 runtime->period_size, resolution, period_size, 765 cable->snd_timer.id.card, 766 cable->snd_timer.id.device, 767 cable->snd_timer.id.subdevice, 768 period_size); 769 return -EINVAL; 770 } 771 return 0; 772 } 773 774 static void loopback_snd_timer_period_elapsed(struct loopback_cable *cable, 775 int event, 776 unsigned long resolution) 777 { 778 struct loopback_pcm *dpcm_play, *dpcm_capt; 779 struct snd_pcm_substream *substream_play, *substream_capt; 780 struct snd_pcm_runtime *valid_runtime; 781 unsigned int running, elapsed_bytes; 782 bool xrun = false; 783 784 scoped_guard(spinlock_irqsave, &cable->lock) { 785 running = cable->running ^ cable->pause; 786 /* no need to do anything if no stream is running */ 787 if (!running) 788 return; 789 790 dpcm_play = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]; 791 dpcm_capt = cable->streams[SNDRV_PCM_STREAM_CAPTURE]; 792 793 if (event == SNDRV_TIMER_EVENT_MSTOP) { 794 if (!dpcm_play || 795 dpcm_play->substream->runtime->state != 796 SNDRV_PCM_STATE_DRAINING) 797 return; 798 } 799 800 substream_play = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 801 dpcm_play->substream : NULL; 802 substream_capt = (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) ? 803 dpcm_capt->substream : NULL; 804 valid_runtime = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 805 dpcm_play->substream->runtime : 806 dpcm_capt->substream->runtime; 807 808 /* resolution is only valid for SNDRV_TIMER_EVENT_TICK events */ 809 if (event == SNDRV_TIMER_EVENT_TICK) { 810 /* The hardware rules guarantee that playback and capture period 811 * are the same. Therefore only one device has to be checked 812 * here. 813 */ 814 if (loopback_snd_timer_check_resolution(valid_runtime, 815 resolution) < 0) { 816 xrun = true; 817 break; 818 } 819 } 820 821 elapsed_bytes = frames_to_bytes(valid_runtime, 822 valid_runtime->period_size); 823 /* The same timer interrupt is used for playback and capture device */ 824 if ((running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) && 825 (running & (1 << SNDRV_PCM_STREAM_CAPTURE))) { 826 copy_play_buf(dpcm_play, dpcm_capt, elapsed_bytes); 827 bytepos_finish(dpcm_play, elapsed_bytes); 828 bytepos_finish(dpcm_capt, elapsed_bytes); 829 } else if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) { 830 bytepos_finish(dpcm_play, elapsed_bytes); 831 } else if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) { 832 clear_capture_buf(dpcm_capt, elapsed_bytes); 833 bytepos_finish(dpcm_capt, elapsed_bytes); 834 } 835 } 836 837 if (xrun) { 838 if (substream_play) 839 snd_pcm_stop_xrun(substream_play); 840 if (substream_capt) 841 snd_pcm_stop_xrun(substream_capt); 842 return; 843 } 844 845 if (substream_play) 846 snd_pcm_period_elapsed(substream_play); 847 if (substream_capt) 848 snd_pcm_period_elapsed(substream_capt); 849 } 850 851 static void loopback_snd_timer_function(struct snd_timer_instance *timeri, 852 unsigned long resolution, 853 unsigned long ticks) 854 { 855 struct loopback_cable *cable = timeri->callback_data; 856 857 loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_TICK, 858 resolution); 859 } 860 861 static void loopback_snd_timer_work(struct work_struct *work) 862 { 863 struct loopback_cable *cable; 864 865 cable = container_of(work, struct loopback_cable, snd_timer.event_work); 866 loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_MSTOP, 0); 867 } 868 869 static void loopback_snd_timer_event(struct snd_timer_instance *timeri, 870 int event, 871 struct timespec64 *tstamp, 872 unsigned long resolution) 873 { 874 /* Do not lock cable->lock here because timer->lock is already hold. 875 * There are other functions which first lock cable->lock and than 876 * timer->lock e.g. 877 * loopback_trigger() 878 * spin_lock(&cable->lock) 879 * loopback_snd_timer_start() 880 * snd_timer_start() 881 * spin_lock(&timer->lock) 882 * Therefore when using the oposit order of locks here it could result 883 * in a deadlock. 884 */ 885 886 if (event == SNDRV_TIMER_EVENT_MSTOP) { 887 struct loopback_cable *cable = timeri->callback_data; 888 889 /* sound card of the timer was stopped. Therefore there will not 890 * be any further timer callbacks. Due to this forward audio 891 * data from here if in draining state. When still in running 892 * state the streaming will be aborted by the usual timeout. It 893 * should not be aborted here because may be the timer sound 894 * card does only a recovery and the timer is back soon. 895 * This work triggers loopback_snd_timer_work() 896 */ 897 schedule_work(&cable->snd_timer.event_work); 898 } 899 } 900 901 static void loopback_jiffies_timer_dpcm_info(struct loopback_pcm *dpcm, 902 struct snd_info_buffer *buffer) 903 { 904 snd_iprintf(buffer, " update_pending:\t%u\n", 905 dpcm->period_update_pending); 906 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos); 907 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac); 908 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n", 909 dpcm->last_jiffies, jiffies); 910 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires); 911 } 912 913 static void loopback_snd_timer_dpcm_info(struct loopback_pcm *dpcm, 914 struct snd_info_buffer *buffer) 915 { 916 struct loopback_cable *cable = dpcm->cable; 917 918 snd_iprintf(buffer, " sound timer:\thw:%d,%d,%d\n", 919 cable->snd_timer.id.card, 920 cable->snd_timer.id.device, 921 cable->snd_timer.id.subdevice); 922 snd_iprintf(buffer, " timer open:\t\t%s\n", 923 snd_pcm_direction_name(cable->snd_timer.stream)); 924 } 925 926 static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream) 927 { 928 struct snd_pcm_runtime *runtime = substream->runtime; 929 struct loopback_pcm *dpcm = runtime->private_data; 930 snd_pcm_uframes_t pos; 931 932 guard(spinlock)(&dpcm->cable->lock); 933 if (dpcm->cable->ops->pos_update) 934 dpcm->cable->ops->pos_update(dpcm->cable); 935 pos = dpcm->buf_pos; 936 return bytes_to_frames(runtime, pos); 937 } 938 939 static const struct snd_pcm_hardware loopback_pcm_hardware = 940 { 941 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP | 942 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE | 943 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_NONINTERLEAVED), 944 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | 945 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE | 946 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | 947 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE | 948 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE | 949 SNDRV_PCM_FMTBIT_DSD_U8 | 950 SNDRV_PCM_FMTBIT_DSD_U16_LE | SNDRV_PCM_FMTBIT_DSD_U16_BE | 951 SNDRV_PCM_FMTBIT_DSD_U32_LE | SNDRV_PCM_FMTBIT_DSD_U32_BE), 952 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_768000, 953 .rate_min = 8000, 954 .rate_max = 768000, 955 .channels_min = 1, 956 .channels_max = 32, 957 .buffer_bytes_max = 2 * 1024 * 1024, 958 .period_bytes_min = 64, 959 /* note check overflow in frac_pos() using pcm_rate_shift before 960 changing period_bytes_max value */ 961 .period_bytes_max = 1024 * 1024, 962 .periods_min = 1, 963 .periods_max = 1024, 964 .fifo_size = 0, 965 }; 966 967 static void loopback_runtime_free(struct snd_pcm_runtime *runtime) 968 { 969 struct loopback_pcm *dpcm = runtime->private_data; 970 kfree(dpcm); 971 } 972 973 static int loopback_hw_free(struct snd_pcm_substream *substream) 974 { 975 struct snd_pcm_runtime *runtime = substream->runtime; 976 struct loopback_pcm *dpcm = runtime->private_data; 977 struct loopback_cable *cable = dpcm->cable; 978 979 guard(mutex)(&dpcm->loopback->cable_lock); 980 cable->valid &= ~(1 << substream->stream); 981 return 0; 982 } 983 984 static unsigned int get_cable_index(struct snd_pcm_substream *substream) 985 { 986 if (!substream->pcm->device) 987 return substream->stream; 988 else 989 return !substream->stream; 990 } 991 992 static int rule_format(struct snd_pcm_hw_params *params, 993 struct snd_pcm_hw_rule *rule) 994 { 995 struct loopback_pcm *dpcm = rule->private; 996 struct loopback_cable *cable = dpcm->cable; 997 struct snd_mask m; 998 999 snd_mask_none(&m); 1000 scoped_guard(mutex, &dpcm->loopback->cable_lock) { 1001 m.bits[0] = (u_int32_t)cable->hw.formats; 1002 m.bits[1] = (u_int32_t)(cable->hw.formats >> 32); 1003 } 1004 return snd_mask_refine(hw_param_mask(params, rule->var), &m); 1005 } 1006 1007 static int rule_rate(struct snd_pcm_hw_params *params, 1008 struct snd_pcm_hw_rule *rule) 1009 { 1010 struct loopback_pcm *dpcm = rule->private; 1011 struct loopback_cable *cable = dpcm->cable; 1012 struct snd_interval t; 1013 1014 scoped_guard(mutex, &dpcm->loopback->cable_lock) { 1015 t.min = cable->hw.rate_min; 1016 t.max = cable->hw.rate_max; 1017 } 1018 t.openmin = t.openmax = 0; 1019 t.integer = 0; 1020 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 1021 } 1022 1023 static int rule_channels(struct snd_pcm_hw_params *params, 1024 struct snd_pcm_hw_rule *rule) 1025 { 1026 struct loopback_pcm *dpcm = rule->private; 1027 struct loopback_cable *cable = dpcm->cable; 1028 struct snd_interval t; 1029 1030 scoped_guard(mutex, &dpcm->loopback->cable_lock) { 1031 t.min = cable->hw.channels_min; 1032 t.max = cable->hw.channels_max; 1033 } 1034 t.openmin = t.openmax = 0; 1035 t.integer = 0; 1036 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 1037 } 1038 1039 static int rule_period_bytes(struct snd_pcm_hw_params *params, 1040 struct snd_pcm_hw_rule *rule) 1041 { 1042 struct loopback_pcm *dpcm = rule->private; 1043 struct loopback_cable *cable = dpcm->cable; 1044 struct snd_interval t; 1045 1046 scoped_guard(mutex, &dpcm->loopback->cable_lock) { 1047 t.min = cable->hw.period_bytes_min; 1048 t.max = cable->hw.period_bytes_max; 1049 } 1050 t.openmin = 0; 1051 t.openmax = 0; 1052 t.integer = 0; 1053 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 1054 } 1055 1056 static void free_cable(struct snd_pcm_substream *substream) 1057 { 1058 struct loopback *loopback = substream->private_data; 1059 int dev = get_cable_index(substream); 1060 struct loopback_cable *cable; 1061 struct loopback_pcm *dpcm; 1062 bool other_alive; 1063 1064 cable = loopback->cables[substream->number][dev]; 1065 if (!cable) 1066 return; 1067 1068 scoped_guard(spinlock_irq, &cable->lock) { 1069 cable->streams[substream->stream] = NULL; 1070 other_alive = cable->streams[!substream->stream]; 1071 } 1072 1073 /* Pair with the stop_count increment in loopback_check_format(). */ 1074 wait_event(cable->stop_wait, !atomic_read(&cable->stop_count)); 1075 if (other_alive) 1076 return; 1077 1078 dpcm = substream->runtime->private_data; 1079 if (cable->ops && cable->ops->close_cable && dpcm) 1080 cable->ops->close_cable(dpcm); 1081 /* free the cable */ 1082 loopback->cables[substream->number][dev] = NULL; 1083 kfree(cable); 1084 } 1085 1086 static int loopback_jiffies_timer_open(struct loopback_pcm *dpcm) 1087 { 1088 timer_setup(&dpcm->timer, loopback_jiffies_timer_function, 0); 1089 1090 return 0; 1091 } 1092 1093 static const struct loopback_ops loopback_jiffies_timer_ops = { 1094 .open = loopback_jiffies_timer_open, 1095 .start = loopback_jiffies_timer_start, 1096 .stop = loopback_jiffies_timer_stop, 1097 .stop_sync = loopback_jiffies_timer_stop_sync, 1098 .close_substream = loopback_jiffies_timer_stop_sync, 1099 .pos_update = loopback_jiffies_timer_pos_update, 1100 .dpcm_info = loopback_jiffies_timer_dpcm_info, 1101 }; 1102 1103 static int loopback_parse_timer_id(const char *str, 1104 struct snd_timer_id *tid) 1105 { 1106 /* [<pref>:](<card name>|<card idx>)[{.,}<dev idx>[{.,}<subdev idx>]] */ 1107 const char * const sep_dev = ".,"; 1108 const char * const sep_pref = ":"; 1109 const char *name = str; 1110 char *sep, save = '\0'; 1111 int card_idx = 0, dev = 0, subdev = 0; 1112 int err; 1113 1114 sep = strpbrk(str, sep_pref); 1115 if (sep) 1116 name = sep + 1; 1117 sep = strpbrk(name, sep_dev); 1118 if (sep) { 1119 save = *sep; 1120 *sep = '\0'; 1121 } 1122 err = kstrtoint(name, 0, &card_idx); 1123 if (err == -EINVAL) { 1124 /* Must be the name, not number */ 1125 for (card_idx = 0; card_idx < snd_ecards_limit; card_idx++) { 1126 struct snd_card *card = snd_card_ref(card_idx); 1127 1128 if (card) { 1129 if (!strcmp(card->id, name)) 1130 err = 0; 1131 snd_card_unref(card); 1132 } 1133 if (!err) 1134 break; 1135 } 1136 } 1137 if (sep) { 1138 *sep = save; 1139 if (!err) { 1140 char *sep2, save2 = '\0'; 1141 1142 sep2 = strpbrk(sep + 1, sep_dev); 1143 if (sep2) { 1144 save2 = *sep2; 1145 *sep2 = '\0'; 1146 } 1147 err = kstrtoint(sep + 1, 0, &dev); 1148 if (sep2) { 1149 *sep2 = save2; 1150 if (!err) 1151 err = kstrtoint(sep2 + 1, 0, &subdev); 1152 } 1153 } 1154 } 1155 if (card_idx == -1) 1156 tid->dev_class = SNDRV_TIMER_CLASS_GLOBAL; 1157 if (!err && tid) { 1158 tid->card = card_idx; 1159 tid->device = dev; 1160 tid->subdevice = subdev; 1161 } 1162 return err; 1163 } 1164 1165 /* call in loopback->cable_lock */ 1166 static int loopback_snd_timer_open(struct loopback_pcm *dpcm) 1167 { 1168 int err = 0; 1169 struct snd_timer_id tid = { 1170 .dev_class = SNDRV_TIMER_CLASS_PCM, 1171 .dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION, 1172 }; 1173 struct snd_timer_instance *timeri; 1174 struct loopback_cable *cable = dpcm->cable; 1175 1176 /* check if timer was already opened. It is only opened once 1177 * per playback and capture subdevice (aka cable). 1178 */ 1179 if (cable->snd_timer.instance) 1180 goto exit; 1181 1182 err = loopback_parse_timer_id(dpcm->loopback->timer_source, &tid); 1183 if (err < 0) { 1184 pcm_err(dpcm->substream->pcm, 1185 "Parsing timer source \'%s\' failed with %d", 1186 dpcm->loopback->timer_source, err); 1187 goto exit; 1188 } 1189 1190 cable->snd_timer.stream = dpcm->substream->stream; 1191 cable->snd_timer.id = tid; 1192 1193 timeri = snd_timer_instance_new(dpcm->loopback->card->id); 1194 if (!timeri) { 1195 err = -ENOMEM; 1196 goto exit; 1197 } 1198 /* The callback has to be called from another work. If 1199 * SNDRV_TIMER_IFLG_FAST is specified it will be called from the 1200 * snd_pcm_period_elapsed() call of the selected sound card. 1201 * snd_pcm_period_elapsed() helds snd_pcm_stream_lock_irqsave(). 1202 * Due to our callback loopback_snd_timer_function() also calls 1203 * snd_pcm_period_elapsed() which calls snd_pcm_stream_lock_irqsave(). 1204 * This would end up in a dead lock. 1205 */ 1206 timeri->flags |= SNDRV_TIMER_IFLG_AUTO; 1207 timeri->callback = loopback_snd_timer_function; 1208 timeri->callback_data = (void *)cable; 1209 timeri->ccallback = loopback_snd_timer_event; 1210 1211 /* initialise a work used for draining */ 1212 INIT_WORK(&cable->snd_timer.event_work, loopback_snd_timer_work); 1213 1214 /* The mutex loopback->cable_lock is kept locked. 1215 * Therefore snd_timer_open() cannot be called a second time 1216 * by the other device of the same cable. 1217 * Therefore the following issue cannot happen: 1218 * [proc1] Call loopback_timer_open() -> 1219 * Unlock cable->lock for snd_timer_close/open() call 1220 * [proc2] Call loopback_timer_open() -> snd_timer_open(), 1221 * snd_timer_start() 1222 * [proc1] Call snd_timer_open() and overwrite running timer 1223 * instance 1224 */ 1225 err = snd_timer_open(timeri, &cable->snd_timer.id, current->pid); 1226 if (err < 0) { 1227 pcm_err(dpcm->substream->pcm, 1228 "snd_timer_open (%d,%d,%d) failed with %d", 1229 cable->snd_timer.id.card, 1230 cable->snd_timer.id.device, 1231 cable->snd_timer.id.subdevice, 1232 err); 1233 snd_timer_instance_free(timeri); 1234 goto exit; 1235 } 1236 1237 cable->snd_timer.instance = timeri; 1238 1239 exit: 1240 return err; 1241 } 1242 1243 /* stop_sync() is not required for sound timer because it does not need to be 1244 * restarted in loopback_prepare() on Xrun recovery 1245 */ 1246 static const struct loopback_ops loopback_snd_timer_ops = { 1247 .open = loopback_snd_timer_open, 1248 .start = loopback_snd_timer_start, 1249 .stop = loopback_snd_timer_stop, 1250 .close_cable = loopback_snd_timer_close_cable, 1251 .dpcm_info = loopback_snd_timer_dpcm_info, 1252 }; 1253 1254 static int loopback_open(struct snd_pcm_substream *substream) 1255 { 1256 struct snd_pcm_runtime *runtime = substream->runtime; 1257 struct loopback *loopback = substream->private_data; 1258 struct loopback_pcm *dpcm; 1259 struct loopback_cable *cable = NULL; 1260 int err = 0; 1261 int dev = get_cable_index(substream); 1262 1263 guard(mutex)(&loopback->cable_lock); 1264 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); 1265 if (!dpcm) 1266 return -ENOMEM; 1267 dpcm->loopback = loopback; 1268 dpcm->substream = substream; 1269 1270 cable = loopback->cables[substream->number][dev]; 1271 if (!cable) { 1272 cable = kzalloc(sizeof(*cable), GFP_KERNEL); 1273 if (!cable) { 1274 err = -ENOMEM; 1275 goto unlock; 1276 } 1277 spin_lock_init(&cable->lock); 1278 atomic_set(&cable->stop_count, 0); 1279 init_waitqueue_head(&cable->stop_wait); 1280 cable->hw = loopback_pcm_hardware; 1281 if (loopback->timer_source) 1282 cable->ops = &loopback_snd_timer_ops; 1283 else 1284 cable->ops = &loopback_jiffies_timer_ops; 1285 loopback->cables[substream->number][dev] = cable; 1286 } 1287 dpcm->cable = cable; 1288 runtime->private_data = dpcm; 1289 1290 if (cable->ops->open) { 1291 err = cable->ops->open(dpcm); 1292 if (err < 0) 1293 goto unlock; 1294 } 1295 1296 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 1297 1298 /* use dynamic rules based on actual runtime->hw values */ 1299 /* note that the default rules created in the PCM midlevel code */ 1300 /* are cached -> they do not reflect the actual state */ 1301 err = snd_pcm_hw_rule_add(runtime, 0, 1302 SNDRV_PCM_HW_PARAM_FORMAT, 1303 rule_format, dpcm, 1304 SNDRV_PCM_HW_PARAM_FORMAT, -1); 1305 if (err < 0) 1306 goto unlock; 1307 err = snd_pcm_hw_rule_add(runtime, 0, 1308 SNDRV_PCM_HW_PARAM_RATE, 1309 rule_rate, dpcm, 1310 SNDRV_PCM_HW_PARAM_RATE, -1); 1311 if (err < 0) 1312 goto unlock; 1313 err = snd_pcm_hw_rule_add(runtime, 0, 1314 SNDRV_PCM_HW_PARAM_CHANNELS, 1315 rule_channels, dpcm, 1316 SNDRV_PCM_HW_PARAM_CHANNELS, -1); 1317 if (err < 0) 1318 goto unlock; 1319 1320 /* In case of sound timer the period time of both devices of the same 1321 * loop has to be the same. 1322 * This rule only takes effect if a sound timer was chosen 1323 */ 1324 if (cable->snd_timer.instance) { 1325 err = snd_pcm_hw_rule_add(runtime, 0, 1326 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 1327 rule_period_bytes, dpcm, 1328 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, -1); 1329 if (err < 0) 1330 goto unlock; 1331 } 1332 1333 /* loopback_runtime_free() has not to be called if kfree(dpcm) was 1334 * already called here. Otherwise it will end up with a double free. 1335 */ 1336 runtime->private_free = loopback_runtime_free; 1337 if (get_notify(dpcm)) 1338 runtime->hw = loopback_pcm_hardware; 1339 else 1340 runtime->hw = cable->hw; 1341 1342 scoped_guard(spinlock_irq, &cable->lock) { 1343 cable->streams[substream->stream] = dpcm; 1344 } 1345 1346 unlock: 1347 if (err < 0) { 1348 free_cable(substream); 1349 kfree(dpcm); 1350 } 1351 return err; 1352 } 1353 1354 static int loopback_close(struct snd_pcm_substream *substream) 1355 { 1356 struct loopback *loopback = substream->private_data; 1357 struct loopback_pcm *dpcm = substream->runtime->private_data; 1358 int err = 0; 1359 1360 if (dpcm->cable->ops->close_substream) 1361 err = dpcm->cable->ops->close_substream(dpcm); 1362 guard(mutex)(&loopback->cable_lock); 1363 free_cable(substream); 1364 return err; 1365 } 1366 1367 static const struct snd_pcm_ops loopback_pcm_ops = { 1368 .open = loopback_open, 1369 .close = loopback_close, 1370 .hw_free = loopback_hw_free, 1371 .prepare = loopback_prepare, 1372 .trigger = loopback_trigger, 1373 .pointer = loopback_pointer, 1374 }; 1375 1376 static int loopback_pcm_new(struct loopback *loopback, 1377 int device, int substreams) 1378 { 1379 struct snd_pcm *pcm; 1380 int err; 1381 1382 err = snd_pcm_new(loopback->card, "Loopback PCM", device, 1383 substreams, substreams, &pcm); 1384 if (err < 0) 1385 return err; 1386 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_pcm_ops); 1387 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_pcm_ops); 1388 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0); 1389 1390 pcm->private_data = loopback; 1391 pcm->info_flags = 0; 1392 strscpy(pcm->name, "Loopback PCM"); 1393 1394 loopback->pcm[device] = pcm; 1395 return 0; 1396 } 1397 1398 static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol, 1399 struct snd_ctl_elem_info *uinfo) 1400 { 1401 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1402 uinfo->count = 1; 1403 uinfo->value.integer.min = 80000; 1404 uinfo->value.integer.max = 120000; 1405 uinfo->value.integer.step = 1; 1406 return 0; 1407 } 1408 1409 static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol, 1410 struct snd_ctl_elem_value *ucontrol) 1411 { 1412 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 1413 1414 guard(mutex)(&loopback->cable_lock); 1415 ucontrol->value.integer.value[0] = 1416 loopback->setup[kcontrol->id.subdevice] 1417 [kcontrol->id.device].rate_shift; 1418 return 0; 1419 } 1420 1421 static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol, 1422 struct snd_ctl_elem_value *ucontrol) 1423 { 1424 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 1425 unsigned int val; 1426 int change = 0; 1427 1428 val = ucontrol->value.integer.value[0]; 1429 if (val < 80000) 1430 val = 80000; 1431 if (val > 120000) 1432 val = 120000; 1433 guard(mutex)(&loopback->cable_lock); 1434 if (val != loopback->setup[kcontrol->id.subdevice] 1435 [kcontrol->id.device].rate_shift) { 1436 loopback->setup[kcontrol->id.subdevice] 1437 [kcontrol->id.device].rate_shift = val; 1438 change = 1; 1439 } 1440 return change; 1441 } 1442 1443 static int loopback_notify_get(struct snd_kcontrol *kcontrol, 1444 struct snd_ctl_elem_value *ucontrol) 1445 { 1446 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 1447 1448 guard(mutex)(&loopback->cable_lock); 1449 ucontrol->value.integer.value[0] = 1450 loopback->setup[kcontrol->id.subdevice] 1451 [kcontrol->id.device].notify; 1452 return 0; 1453 } 1454 1455 static int loopback_notify_put(struct snd_kcontrol *kcontrol, 1456 struct snd_ctl_elem_value *ucontrol) 1457 { 1458 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 1459 unsigned int val; 1460 int change = 0; 1461 1462 val = ucontrol->value.integer.value[0] ? 1 : 0; 1463 guard(mutex)(&loopback->cable_lock); 1464 if (val != loopback->setup[kcontrol->id.subdevice] 1465 [kcontrol->id.device].notify) { 1466 loopback->setup[kcontrol->id.subdevice] 1467 [kcontrol->id.device].notify = val; 1468 change = 1; 1469 } 1470 return change; 1471 } 1472 1473 static int loopback_active_get(struct snd_kcontrol *kcontrol, 1474 struct snd_ctl_elem_value *ucontrol) 1475 { 1476 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 1477 struct loopback_cable *cable; 1478 1479 unsigned int val = 0; 1480 1481 guard(mutex)(&loopback->cable_lock); 1482 cable = loopback->cables[kcontrol->id.subdevice][kcontrol->id.device ^ 1]; 1483 if (cable != NULL) { 1484 unsigned int running = cable->running ^ cable->pause; 1485 1486 val = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 1 : 0; 1487 } 1488 ucontrol->value.integer.value[0] = val; 1489 return 0; 1490 } 1491 1492 static int loopback_format_info(struct snd_kcontrol *kcontrol, 1493 struct snd_ctl_elem_info *uinfo) 1494 { 1495 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1496 uinfo->count = 1; 1497 uinfo->value.integer.min = 0; 1498 uinfo->value.integer.max = (__force int)SNDRV_PCM_FORMAT_LAST; 1499 uinfo->value.integer.step = 1; 1500 return 0; 1501 } 1502 1503 static int loopback_format_get(struct snd_kcontrol *kcontrol, 1504 struct snd_ctl_elem_value *ucontrol) 1505 { 1506 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 1507 1508 ucontrol->value.integer.value[0] = 1509 (__force int)loopback->setup[kcontrol->id.subdevice] 1510 [kcontrol->id.device].format; 1511 return 0; 1512 } 1513 1514 static int loopback_rate_info(struct snd_kcontrol *kcontrol, 1515 struct snd_ctl_elem_info *uinfo) 1516 { 1517 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1518 uinfo->count = 1; 1519 uinfo->value.integer.min = 0; 1520 uinfo->value.integer.max = 192000; 1521 uinfo->value.integer.step = 1; 1522 return 0; 1523 } 1524 1525 static int loopback_rate_get(struct snd_kcontrol *kcontrol, 1526 struct snd_ctl_elem_value *ucontrol) 1527 { 1528 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 1529 1530 guard(mutex)(&loopback->cable_lock); 1531 ucontrol->value.integer.value[0] = 1532 loopback->setup[kcontrol->id.subdevice] 1533 [kcontrol->id.device].rate; 1534 return 0; 1535 } 1536 1537 static int loopback_channels_info(struct snd_kcontrol *kcontrol, 1538 struct snd_ctl_elem_info *uinfo) 1539 { 1540 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1541 uinfo->count = 1; 1542 uinfo->value.integer.min = 1; 1543 uinfo->value.integer.max = 1024; 1544 uinfo->value.integer.step = 1; 1545 return 0; 1546 } 1547 1548 static int loopback_channels_get(struct snd_kcontrol *kcontrol, 1549 struct snd_ctl_elem_value *ucontrol) 1550 { 1551 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 1552 1553 guard(mutex)(&loopback->cable_lock); 1554 ucontrol->value.integer.value[0] = 1555 loopback->setup[kcontrol->id.subdevice] 1556 [kcontrol->id.device].channels; 1557 return 0; 1558 } 1559 1560 static int loopback_access_info(struct snd_kcontrol *kcontrol, 1561 struct snd_ctl_elem_info *uinfo) 1562 { 1563 const char * const texts[] = {"Interleaved", "Non-interleaved"}; 1564 1565 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts); 1566 } 1567 1568 static int loopback_access_get(struct snd_kcontrol *kcontrol, 1569 struct snd_ctl_elem_value *ucontrol) 1570 { 1571 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 1572 snd_pcm_access_t access; 1573 1574 guard(mutex)(&loopback->cable_lock); 1575 access = loopback->setup[kcontrol->id.subdevice][kcontrol->id.device].access; 1576 1577 ucontrol->value.enumerated.item[0] = !is_access_interleaved(access); 1578 1579 return 0; 1580 } 1581 1582 static const struct snd_kcontrol_new loopback_controls[] = { 1583 { 1584 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1585 .name = "PCM Rate Shift 100000", 1586 .info = loopback_rate_shift_info, 1587 .get = loopback_rate_shift_get, 1588 .put = loopback_rate_shift_put, 1589 }, 1590 { 1591 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1592 .name = "PCM Notify", 1593 .info = snd_ctl_boolean_mono_info, 1594 .get = loopback_notify_get, 1595 .put = loopback_notify_put, 1596 }, 1597 #define ACTIVE_IDX 2 1598 { 1599 .access = SNDRV_CTL_ELEM_ACCESS_READ, 1600 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1601 .name = "PCM Slave Active", 1602 .info = snd_ctl_boolean_mono_info, 1603 .get = loopback_active_get, 1604 }, 1605 #define FORMAT_IDX 3 1606 { 1607 .access = SNDRV_CTL_ELEM_ACCESS_READ, 1608 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1609 .name = "PCM Slave Format", 1610 .info = loopback_format_info, 1611 .get = loopback_format_get 1612 }, 1613 #define RATE_IDX 4 1614 { 1615 .access = SNDRV_CTL_ELEM_ACCESS_READ, 1616 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1617 .name = "PCM Slave Rate", 1618 .info = loopback_rate_info, 1619 .get = loopback_rate_get 1620 }, 1621 #define CHANNELS_IDX 5 1622 { 1623 .access = SNDRV_CTL_ELEM_ACCESS_READ, 1624 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1625 .name = "PCM Slave Channels", 1626 .info = loopback_channels_info, 1627 .get = loopback_channels_get 1628 }, 1629 #define ACCESS_IDX 6 1630 { 1631 .access = SNDRV_CTL_ELEM_ACCESS_READ, 1632 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1633 .name = "PCM Slave Access Mode", 1634 .info = loopback_access_info, 1635 .get = loopback_access_get, 1636 }, 1637 }; 1638 1639 static int loopback_mixer_new(struct loopback *loopback, int notify) 1640 { 1641 struct snd_card *card = loopback->card; 1642 struct snd_pcm *pcm; 1643 struct snd_kcontrol *kctl; 1644 struct loopback_setup *setup; 1645 int err, dev, substr, substr_count, idx; 1646 1647 strscpy(card->mixername, "Loopback Mixer"); 1648 for (dev = 0; dev < 2; dev++) { 1649 pcm = loopback->pcm[dev]; 1650 substr_count = 1651 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count; 1652 for (substr = 0; substr < substr_count; substr++) { 1653 setup = &loopback->setup[substr][dev]; 1654 setup->notify = notify; 1655 setup->rate_shift = NO_PITCH; 1656 setup->format = SNDRV_PCM_FORMAT_S16_LE; 1657 setup->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED; 1658 setup->rate = 48000; 1659 setup->channels = 2; 1660 for (idx = 0; idx < ARRAY_SIZE(loopback_controls); 1661 idx++) { 1662 kctl = snd_ctl_new1(&loopback_controls[idx], 1663 loopback); 1664 if (!kctl) 1665 return -ENOMEM; 1666 kctl->id.device = dev; 1667 kctl->id.subdevice = substr; 1668 1669 /* Add the control before copying the id so that 1670 * the numid field of the id is set in the copy. 1671 */ 1672 err = snd_ctl_add(card, kctl); 1673 if (err < 0) 1674 return err; 1675 1676 switch (idx) { 1677 case ACTIVE_IDX: 1678 setup->active_id = kctl->id; 1679 break; 1680 case FORMAT_IDX: 1681 setup->format_id = kctl->id; 1682 break; 1683 case RATE_IDX: 1684 setup->rate_id = kctl->id; 1685 break; 1686 case CHANNELS_IDX: 1687 setup->channels_id = kctl->id; 1688 break; 1689 case ACCESS_IDX: 1690 setup->access_id = kctl->id; 1691 break; 1692 default: 1693 break; 1694 } 1695 } 1696 } 1697 } 1698 return 0; 1699 } 1700 1701 static void print_dpcm_info(struct snd_info_buffer *buffer, 1702 struct loopback_pcm *dpcm, 1703 const char *id) 1704 { 1705 snd_iprintf(buffer, " %s\n", id); 1706 if (dpcm == NULL) { 1707 snd_iprintf(buffer, " inactive\n"); 1708 return; 1709 } 1710 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size); 1711 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos); 1712 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size); 1713 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size); 1714 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps); 1715 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign); 1716 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift); 1717 if (dpcm->cable->ops->dpcm_info) 1718 dpcm->cable->ops->dpcm_info(dpcm, buffer); 1719 } 1720 1721 static void print_substream_info(struct snd_info_buffer *buffer, 1722 struct loopback *loopback, 1723 int sub, 1724 int num) 1725 { 1726 struct loopback_cable *cable = loopback->cables[sub][num]; 1727 1728 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub); 1729 if (cable == NULL) { 1730 snd_iprintf(buffer, " inactive\n"); 1731 return; 1732 } 1733 snd_iprintf(buffer, " valid: %u\n", cable->valid); 1734 snd_iprintf(buffer, " running: %u\n", cable->running); 1735 snd_iprintf(buffer, " pause: %u\n", cable->pause); 1736 print_dpcm_info(buffer, cable->streams[0], "Playback"); 1737 print_dpcm_info(buffer, cable->streams[1], "Capture"); 1738 } 1739 1740 static void print_cable_info(struct snd_info_entry *entry, 1741 struct snd_info_buffer *buffer) 1742 { 1743 struct loopback *loopback = entry->private_data; 1744 int sub, num; 1745 1746 guard(mutex)(&loopback->cable_lock); 1747 num = entry->name[strlen(entry->name)-1]; 1748 num = num == '0' ? 0 : 1; 1749 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++) 1750 print_substream_info(buffer, loopback, sub, num); 1751 } 1752 1753 static int loopback_cable_proc_new(struct loopback *loopback, int cidx) 1754 { 1755 char name[32]; 1756 1757 snprintf(name, sizeof(name), "cable#%d", cidx); 1758 return snd_card_ro_proc_new(loopback->card, name, loopback, 1759 print_cable_info); 1760 } 1761 1762 static void loopback_set_timer_source(struct loopback *loopback, 1763 const char *value) 1764 { 1765 if (loopback->timer_source) { 1766 devm_kfree(loopback->card->dev, loopback->timer_source); 1767 loopback->timer_source = NULL; 1768 } 1769 if (value && *value) 1770 loopback->timer_source = devm_kstrdup(loopback->card->dev, 1771 value, GFP_KERNEL); 1772 } 1773 1774 static void print_timer_source_info(struct snd_info_entry *entry, 1775 struct snd_info_buffer *buffer) 1776 { 1777 struct loopback *loopback = entry->private_data; 1778 1779 guard(mutex)(&loopback->cable_lock); 1780 snd_iprintf(buffer, "%s\n", 1781 loopback->timer_source ? loopback->timer_source : ""); 1782 } 1783 1784 static void change_timer_source_info(struct snd_info_entry *entry, 1785 struct snd_info_buffer *buffer) 1786 { 1787 struct loopback *loopback = entry->private_data; 1788 char line[64]; 1789 1790 guard(mutex)(&loopback->cable_lock); 1791 if (!snd_info_get_line(buffer, line, sizeof(line))) 1792 loopback_set_timer_source(loopback, strim(line)); 1793 } 1794 1795 static int loopback_timer_source_proc_new(struct loopback *loopback) 1796 { 1797 return snd_card_rw_proc_new(loopback->card, "timer_source", loopback, 1798 print_timer_source_info, 1799 change_timer_source_info); 1800 } 1801 1802 static int loopback_probe(struct platform_device *devptr) 1803 { 1804 struct snd_card *card; 1805 struct loopback *loopback; 1806 int dev = devptr->id; 1807 int err; 1808 1809 err = snd_devm_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE, 1810 sizeof(struct loopback), &card); 1811 if (err < 0) 1812 return err; 1813 loopback = card->private_data; 1814 1815 if (pcm_substreams[dev] < 1) 1816 pcm_substreams[dev] = 1; 1817 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS) 1818 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS; 1819 1820 loopback->card = card; 1821 loopback_set_timer_source(loopback, timer_source[dev]); 1822 1823 mutex_init(&loopback->cable_lock); 1824 1825 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]); 1826 if (err < 0) 1827 return err; 1828 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]); 1829 if (err < 0) 1830 return err; 1831 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0); 1832 if (err < 0) 1833 return err; 1834 loopback_cable_proc_new(loopback, 0); 1835 loopback_cable_proc_new(loopback, 1); 1836 loopback_timer_source_proc_new(loopback); 1837 strscpy(card->driver, "Loopback"); 1838 strscpy(card->shortname, "Loopback"); 1839 sprintf(card->longname, "Loopback %i", dev + 1); 1840 err = snd_card_register(card); 1841 if (err < 0) 1842 return err; 1843 platform_set_drvdata(devptr, card); 1844 return 0; 1845 } 1846 1847 static int loopback_suspend(struct device *pdev) 1848 { 1849 struct snd_card *card = dev_get_drvdata(pdev); 1850 1851 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 1852 return 0; 1853 } 1854 1855 static int loopback_resume(struct device *pdev) 1856 { 1857 struct snd_card *card = dev_get_drvdata(pdev); 1858 1859 snd_power_change_state(card, SNDRV_CTL_POWER_D0); 1860 return 0; 1861 } 1862 1863 static DEFINE_SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume); 1864 1865 #define SND_LOOPBACK_DRIVER "snd_aloop" 1866 1867 static struct platform_driver loopback_driver = { 1868 .probe = loopback_probe, 1869 .driver = { 1870 .name = SND_LOOPBACK_DRIVER, 1871 .pm = &loopback_pm, 1872 }, 1873 }; 1874 1875 static void loopback_unregister_all(void) 1876 { 1877 int i; 1878 1879 for (i = 0; i < ARRAY_SIZE(devices); ++i) 1880 platform_device_unregister(devices[i]); 1881 platform_driver_unregister(&loopback_driver); 1882 } 1883 1884 static int __init alsa_card_loopback_init(void) 1885 { 1886 int i, err, cards; 1887 1888 err = platform_driver_register(&loopback_driver); 1889 if (err < 0) 1890 return err; 1891 1892 1893 cards = 0; 1894 for (i = 0; i < SNDRV_CARDS; i++) { 1895 struct platform_device *device; 1896 if (!enable[i]) 1897 continue; 1898 device = platform_device_register_simple(SND_LOOPBACK_DRIVER, 1899 i, NULL, 0); 1900 if (IS_ERR(device)) 1901 continue; 1902 if (!platform_get_drvdata(device)) { 1903 platform_device_unregister(device); 1904 continue; 1905 } 1906 devices[i] = device; 1907 cards++; 1908 } 1909 if (!cards) { 1910 #ifdef MODULE 1911 pr_err("aloop: No loopback enabled\n"); 1912 #endif 1913 loopback_unregister_all(); 1914 return -ENODEV; 1915 } 1916 return 0; 1917 } 1918 1919 static void __exit alsa_card_loopback_exit(void) 1920 { 1921 loopback_unregister_all(); 1922 } 1923 1924 module_init(alsa_card_loopback_init) 1925 module_exit(alsa_card_loopback_exit) 1926