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