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/time.h> 22 #include <linux/wait.h> 23 #include <linux/module.h> 24 #include <linux/platform_device.h> 25 #include <sound/core.h> 26 #include <sound/control.h> 27 #include <sound/pcm.h> 28 #include <sound/pcm_params.h> 29 #include <sound/info.h> 30 #include <sound/initval.h> 31 32 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); 33 MODULE_DESCRIPTION("A loopback soundcard"); 34 MODULE_LICENSE("GPL"); 35 MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}"); 36 37 #define MAX_PCM_SUBSTREAMS 8 38 39 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ 40 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ 41 static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0}; 42 static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8}; 43 static int pcm_notify[SNDRV_CARDS]; 44 45 module_param_array(index, int, NULL, 0444); 46 MODULE_PARM_DESC(index, "Index value for loopback soundcard."); 47 module_param_array(id, charp, NULL, 0444); 48 MODULE_PARM_DESC(id, "ID string for loopback soundcard."); 49 module_param_array(enable, bool, NULL, 0444); 50 MODULE_PARM_DESC(enable, "Enable this loopback soundcard."); 51 module_param_array(pcm_substreams, int, NULL, 0444); 52 MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver."); 53 module_param_array(pcm_notify, int, NULL, 0444); 54 MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes."); 55 56 #define NO_PITCH 100000 57 58 struct loopback_pcm; 59 60 struct loopback_cable { 61 spinlock_t lock; 62 struct loopback_pcm *streams[2]; 63 struct snd_pcm_hardware hw; 64 /* flags */ 65 unsigned int valid; 66 unsigned int running; 67 unsigned int pause; 68 }; 69 70 struct loopback_setup { 71 unsigned int notify: 1; 72 unsigned int rate_shift; 73 unsigned int format; 74 unsigned int rate; 75 unsigned int channels; 76 struct snd_ctl_elem_id active_id; 77 struct snd_ctl_elem_id format_id; 78 struct snd_ctl_elem_id rate_id; 79 struct snd_ctl_elem_id channels_id; 80 }; 81 82 struct loopback { 83 struct snd_card *card; 84 struct mutex cable_lock; 85 struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2]; 86 struct snd_pcm *pcm[2]; 87 struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2]; 88 }; 89 90 struct loopback_pcm { 91 struct loopback *loopback; 92 struct snd_pcm_substream *substream; 93 struct loopback_cable *cable; 94 unsigned int pcm_buffer_size; 95 unsigned int buf_pos; /* position in buffer */ 96 unsigned int silent_size; 97 /* PCM parameters */ 98 unsigned int pcm_period_size; 99 unsigned int pcm_bps; /* bytes per second */ 100 unsigned int pcm_salign; /* bytes per sample * channels */ 101 unsigned int pcm_rate_shift; /* rate shift value */ 102 /* flags */ 103 unsigned int period_update_pending :1; 104 /* timer stuff */ 105 unsigned int irq_pos; /* fractional IRQ position in jiffies 106 * ticks 107 */ 108 unsigned int period_size_frac; /* period size in jiffies ticks */ 109 unsigned int last_drift; 110 unsigned long last_jiffies; 111 struct timer_list timer; 112 }; 113 114 static struct platform_device *devices[SNDRV_CARDS]; 115 116 static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x) 117 { 118 if (dpcm->pcm_rate_shift == NO_PITCH) { 119 x /= HZ; 120 } else { 121 x = div_u64(NO_PITCH * (unsigned long long)x, 122 HZ * (unsigned long long)dpcm->pcm_rate_shift); 123 } 124 return x - (x % dpcm->pcm_salign); 125 } 126 127 static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x) 128 { 129 if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */ 130 return x * HZ; 131 } else { 132 x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ, 133 NO_PITCH); 134 } 135 return x; 136 } 137 138 static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm) 139 { 140 int device = dpcm->substream->pstr->pcm->device; 141 142 if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 143 device ^= 1; 144 return &dpcm->loopback->setup[dpcm->substream->number][device]; 145 } 146 147 static inline unsigned int get_notify(struct loopback_pcm *dpcm) 148 { 149 return get_setup(dpcm)->notify; 150 } 151 152 static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm) 153 { 154 return get_setup(dpcm)->rate_shift; 155 } 156 157 /* call in cable->lock */ 158 static int loopback_timer_start(struct loopback_pcm *dpcm) 159 { 160 unsigned long tick; 161 unsigned int rate_shift = get_rate_shift(dpcm); 162 163 if (rate_shift != dpcm->pcm_rate_shift) { 164 dpcm->pcm_rate_shift = rate_shift; 165 dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size); 166 } 167 if (dpcm->period_size_frac <= dpcm->irq_pos) { 168 dpcm->irq_pos %= dpcm->period_size_frac; 169 dpcm->period_update_pending = 1; 170 } 171 tick = dpcm->period_size_frac - dpcm->irq_pos; 172 tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps; 173 mod_timer(&dpcm->timer, jiffies + tick); 174 175 return 0; 176 } 177 178 /* call in cable->lock */ 179 static inline int loopback_timer_stop(struct loopback_pcm *dpcm) 180 { 181 del_timer(&dpcm->timer); 182 dpcm->timer.expires = 0; 183 184 return 0; 185 } 186 187 static inline int loopback_timer_stop_sync(struct loopback_pcm *dpcm) 188 { 189 del_timer_sync(&dpcm->timer); 190 191 return 0; 192 } 193 194 #define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK) 195 #define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE) 196 #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE) 197 198 static int loopback_check_format(struct loopback_cable *cable, int stream) 199 { 200 struct snd_pcm_runtime *runtime, *cruntime; 201 struct loopback_setup *setup; 202 struct snd_card *card; 203 int check; 204 205 if (cable->valid != CABLE_VALID_BOTH) { 206 if (stream == SNDRV_PCM_STREAM_PLAYBACK) 207 goto __notify; 208 return 0; 209 } 210 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]-> 211 substream->runtime; 212 cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]-> 213 substream->runtime; 214 check = runtime->format != cruntime->format || 215 runtime->rate != cruntime->rate || 216 runtime->channels != cruntime->channels; 217 if (!check) 218 return 0; 219 if (stream == SNDRV_PCM_STREAM_CAPTURE) { 220 return -EIO; 221 } else { 222 snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]-> 223 substream, SNDRV_PCM_STATE_DRAINING); 224 __notify: 225 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]-> 226 substream->runtime; 227 setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]); 228 card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card; 229 if (setup->format != runtime->format) { 230 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 231 &setup->format_id); 232 setup->format = runtime->format; 233 } 234 if (setup->rate != runtime->rate) { 235 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 236 &setup->rate_id); 237 setup->rate = runtime->rate; 238 } 239 if (setup->channels != runtime->channels) { 240 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 241 &setup->channels_id); 242 setup->channels = runtime->channels; 243 } 244 } 245 return 0; 246 } 247 248 static void loopback_active_notify(struct loopback_pcm *dpcm) 249 { 250 snd_ctl_notify(dpcm->loopback->card, 251 SNDRV_CTL_EVENT_MASK_VALUE, 252 &get_setup(dpcm)->active_id); 253 } 254 255 static int loopback_trigger(struct snd_pcm_substream *substream, int cmd) 256 { 257 struct snd_pcm_runtime *runtime = substream->runtime; 258 struct loopback_pcm *dpcm = runtime->private_data; 259 struct loopback_cable *cable = dpcm->cable; 260 int err = 0, stream = 1 << substream->stream; 261 262 switch (cmd) { 263 case SNDRV_PCM_TRIGGER_START: 264 err = loopback_check_format(cable, substream->stream); 265 if (err < 0) 266 return err; 267 dpcm->last_jiffies = jiffies; 268 dpcm->pcm_rate_shift = 0; 269 dpcm->last_drift = 0; 270 spin_lock(&cable->lock); 271 cable->running |= stream; 272 cable->pause &= ~stream; 273 err = loopback_timer_start(dpcm); 274 spin_unlock(&cable->lock); 275 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 276 loopback_active_notify(dpcm); 277 break; 278 case SNDRV_PCM_TRIGGER_STOP: 279 spin_lock(&cable->lock); 280 cable->running &= ~stream; 281 cable->pause &= ~stream; 282 err = loopback_timer_stop(dpcm); 283 spin_unlock(&cable->lock); 284 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 285 loopback_active_notify(dpcm); 286 break; 287 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 288 case SNDRV_PCM_TRIGGER_SUSPEND: 289 spin_lock(&cable->lock); 290 cable->pause |= stream; 291 err = loopback_timer_stop(dpcm); 292 spin_unlock(&cable->lock); 293 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 294 loopback_active_notify(dpcm); 295 break; 296 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 297 case SNDRV_PCM_TRIGGER_RESUME: 298 spin_lock(&cable->lock); 299 dpcm->last_jiffies = jiffies; 300 cable->pause &= ~stream; 301 err = loopback_timer_start(dpcm); 302 spin_unlock(&cable->lock); 303 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 304 loopback_active_notify(dpcm); 305 break; 306 default: 307 return -EINVAL; 308 } 309 return err; 310 } 311 312 static void params_change(struct snd_pcm_substream *substream) 313 { 314 struct snd_pcm_runtime *runtime = substream->runtime; 315 struct loopback_pcm *dpcm = runtime->private_data; 316 struct loopback_cable *cable = dpcm->cable; 317 318 cable->hw.formats = pcm_format_to_bits(runtime->format); 319 cable->hw.rate_min = runtime->rate; 320 cable->hw.rate_max = runtime->rate; 321 cable->hw.channels_min = runtime->channels; 322 cable->hw.channels_max = runtime->channels; 323 } 324 325 static int loopback_prepare(struct snd_pcm_substream *substream) 326 { 327 struct snd_pcm_runtime *runtime = substream->runtime; 328 struct loopback_pcm *dpcm = runtime->private_data; 329 struct loopback_cable *cable = dpcm->cable; 330 int err, bps, salign; 331 332 err = loopback_timer_stop_sync(dpcm); 333 if (err < 0) 334 return err; 335 336 salign = (snd_pcm_format_physical_width(runtime->format) * 337 runtime->channels) / 8; 338 bps = salign * runtime->rate; 339 if (bps <= 0 || salign <= 0) 340 return -EINVAL; 341 342 dpcm->buf_pos = 0; 343 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size); 344 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { 345 /* clear capture buffer */ 346 dpcm->silent_size = dpcm->pcm_buffer_size; 347 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, 348 runtime->buffer_size * runtime->channels); 349 } 350 351 dpcm->irq_pos = 0; 352 dpcm->period_update_pending = 0; 353 dpcm->pcm_bps = bps; 354 dpcm->pcm_salign = salign; 355 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size); 356 357 mutex_lock(&dpcm->loopback->cable_lock); 358 if (!(cable->valid & ~(1 << substream->stream)) || 359 (get_setup(dpcm)->notify && 360 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)) 361 params_change(substream); 362 cable->valid |= 1 << substream->stream; 363 mutex_unlock(&dpcm->loopback->cable_lock); 364 365 return 0; 366 } 367 368 static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes) 369 { 370 struct snd_pcm_runtime *runtime = dpcm->substream->runtime; 371 char *dst = runtime->dma_area; 372 unsigned int dst_off = dpcm->buf_pos; 373 374 if (dpcm->silent_size >= dpcm->pcm_buffer_size) 375 return; 376 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size) 377 bytes = dpcm->pcm_buffer_size - dpcm->silent_size; 378 379 for (;;) { 380 unsigned int size = bytes; 381 if (dst_off + size > dpcm->pcm_buffer_size) 382 size = dpcm->pcm_buffer_size - dst_off; 383 snd_pcm_format_set_silence(runtime->format, dst + dst_off, 384 bytes_to_frames(runtime, size) * 385 runtime->channels); 386 dpcm->silent_size += size; 387 bytes -= size; 388 if (!bytes) 389 break; 390 dst_off = 0; 391 } 392 } 393 394 static void copy_play_buf(struct loopback_pcm *play, 395 struct loopback_pcm *capt, 396 unsigned int bytes) 397 { 398 struct snd_pcm_runtime *runtime = play->substream->runtime; 399 char *src = runtime->dma_area; 400 char *dst = capt->substream->runtime->dma_area; 401 unsigned int src_off = play->buf_pos; 402 unsigned int dst_off = capt->buf_pos; 403 unsigned int clear_bytes = 0; 404 405 /* check if playback is draining, trim the capture copy size 406 * when our pointer is at the end of playback ring buffer */ 407 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING && 408 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) { 409 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff; 410 appl_ptr = appl_ptr1 = runtime->control->appl_ptr; 411 appl_ptr1 -= appl_ptr1 % runtime->buffer_size; 412 appl_ptr1 += play->buf_pos / play->pcm_salign; 413 if (appl_ptr < appl_ptr1) 414 appl_ptr1 -= runtime->buffer_size; 415 diff = (appl_ptr - appl_ptr1) * play->pcm_salign; 416 if (diff < bytes) { 417 clear_bytes = bytes - diff; 418 bytes = diff; 419 } 420 } 421 422 for (;;) { 423 unsigned int size = bytes; 424 if (src_off + size > play->pcm_buffer_size) 425 size = play->pcm_buffer_size - src_off; 426 if (dst_off + size > capt->pcm_buffer_size) 427 size = capt->pcm_buffer_size - dst_off; 428 memcpy(dst + dst_off, src + src_off, size); 429 capt->silent_size = 0; 430 bytes -= size; 431 if (!bytes) 432 break; 433 src_off = (src_off + size) % play->pcm_buffer_size; 434 dst_off = (dst_off + size) % capt->pcm_buffer_size; 435 } 436 437 if (clear_bytes > 0) { 438 clear_capture_buf(capt, clear_bytes); 439 capt->silent_size = 0; 440 } 441 } 442 443 static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm, 444 unsigned int jiffies_delta) 445 { 446 unsigned long last_pos; 447 unsigned int delta; 448 449 last_pos = byte_pos(dpcm, dpcm->irq_pos); 450 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps; 451 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos; 452 if (delta >= dpcm->last_drift) 453 delta -= dpcm->last_drift; 454 dpcm->last_drift = 0; 455 if (dpcm->irq_pos >= dpcm->period_size_frac) { 456 dpcm->irq_pos %= dpcm->period_size_frac; 457 dpcm->period_update_pending = 1; 458 } 459 return delta; 460 } 461 462 static inline void bytepos_finish(struct loopback_pcm *dpcm, 463 unsigned int delta) 464 { 465 dpcm->buf_pos += delta; 466 dpcm->buf_pos %= dpcm->pcm_buffer_size; 467 } 468 469 /* call in cable->lock */ 470 static unsigned int loopback_pos_update(struct loopback_cable *cable) 471 { 472 struct loopback_pcm *dpcm_play = 473 cable->streams[SNDRV_PCM_STREAM_PLAYBACK]; 474 struct loopback_pcm *dpcm_capt = 475 cable->streams[SNDRV_PCM_STREAM_CAPTURE]; 476 unsigned long delta_play = 0, delta_capt = 0; 477 unsigned int running, count1, count2; 478 479 running = cable->running ^ cable->pause; 480 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) { 481 delta_play = jiffies - dpcm_play->last_jiffies; 482 dpcm_play->last_jiffies += delta_play; 483 } 484 485 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) { 486 delta_capt = jiffies - dpcm_capt->last_jiffies; 487 dpcm_capt->last_jiffies += delta_capt; 488 } 489 490 if (delta_play == 0 && delta_capt == 0) 491 goto unlock; 492 493 if (delta_play > delta_capt) { 494 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt); 495 bytepos_finish(dpcm_play, count1); 496 delta_play = delta_capt; 497 } else if (delta_play < delta_capt) { 498 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play); 499 clear_capture_buf(dpcm_capt, count1); 500 bytepos_finish(dpcm_capt, count1); 501 delta_capt = delta_play; 502 } 503 504 if (delta_play == 0 && delta_capt == 0) 505 goto unlock; 506 507 /* note delta_capt == delta_play at this moment */ 508 count1 = bytepos_delta(dpcm_play, delta_play); 509 count2 = bytepos_delta(dpcm_capt, delta_capt); 510 if (count1 < count2) { 511 dpcm_capt->last_drift = count2 - count1; 512 count1 = count2; 513 } else if (count1 > count2) { 514 dpcm_play->last_drift = count1 - count2; 515 } 516 copy_play_buf(dpcm_play, dpcm_capt, count1); 517 bytepos_finish(dpcm_play, count1); 518 bytepos_finish(dpcm_capt, count1); 519 unlock: 520 return running; 521 } 522 523 static void loopback_timer_function(struct timer_list *t) 524 { 525 struct loopback_pcm *dpcm = from_timer(dpcm, t, timer); 526 unsigned long flags; 527 528 spin_lock_irqsave(&dpcm->cable->lock, flags); 529 if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) { 530 loopback_timer_start(dpcm); 531 if (dpcm->period_update_pending) { 532 dpcm->period_update_pending = 0; 533 spin_unlock_irqrestore(&dpcm->cable->lock, flags); 534 /* need to unlock before calling below */ 535 snd_pcm_period_elapsed(dpcm->substream); 536 return; 537 } 538 } 539 spin_unlock_irqrestore(&dpcm->cable->lock, flags); 540 } 541 542 static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream) 543 { 544 struct snd_pcm_runtime *runtime = substream->runtime; 545 struct loopback_pcm *dpcm = runtime->private_data; 546 snd_pcm_uframes_t pos; 547 548 spin_lock(&dpcm->cable->lock); 549 loopback_pos_update(dpcm->cable); 550 pos = dpcm->buf_pos; 551 spin_unlock(&dpcm->cable->lock); 552 return bytes_to_frames(runtime, pos); 553 } 554 555 static const struct snd_pcm_hardware loopback_pcm_hardware = 556 { 557 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP | 558 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE | 559 SNDRV_PCM_INFO_RESUME), 560 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | 561 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE | 562 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | 563 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE | 564 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE), 565 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000, 566 .rate_min = 8000, 567 .rate_max = 192000, 568 .channels_min = 1, 569 .channels_max = 32, 570 .buffer_bytes_max = 2 * 1024 * 1024, 571 .period_bytes_min = 64, 572 /* note check overflow in frac_pos() using pcm_rate_shift before 573 changing period_bytes_max value */ 574 .period_bytes_max = 1024 * 1024, 575 .periods_min = 1, 576 .periods_max = 1024, 577 .fifo_size = 0, 578 }; 579 580 static void loopback_runtime_free(struct snd_pcm_runtime *runtime) 581 { 582 struct loopback_pcm *dpcm = runtime->private_data; 583 kfree(dpcm); 584 } 585 586 static int loopback_hw_params(struct snd_pcm_substream *substream, 587 struct snd_pcm_hw_params *params) 588 { 589 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params)); 590 } 591 592 static int loopback_hw_free(struct snd_pcm_substream *substream) 593 { 594 struct snd_pcm_runtime *runtime = substream->runtime; 595 struct loopback_pcm *dpcm = runtime->private_data; 596 struct loopback_cable *cable = dpcm->cable; 597 598 mutex_lock(&dpcm->loopback->cable_lock); 599 cable->valid &= ~(1 << substream->stream); 600 mutex_unlock(&dpcm->loopback->cable_lock); 601 return snd_pcm_lib_free_pages(substream); 602 } 603 604 static unsigned int get_cable_index(struct snd_pcm_substream *substream) 605 { 606 if (!substream->pcm->device) 607 return substream->stream; 608 else 609 return !substream->stream; 610 } 611 612 static int rule_format(struct snd_pcm_hw_params *params, 613 struct snd_pcm_hw_rule *rule) 614 { 615 struct loopback_pcm *dpcm = rule->private; 616 struct loopback_cable *cable = dpcm->cable; 617 struct snd_mask m; 618 619 snd_mask_none(&m); 620 mutex_lock(&dpcm->loopback->cable_lock); 621 m.bits[0] = (u_int32_t)cable->hw.formats; 622 m.bits[1] = (u_int32_t)(cable->hw.formats >> 32); 623 mutex_unlock(&dpcm->loopback->cable_lock); 624 return snd_mask_refine(hw_param_mask(params, rule->var), &m); 625 } 626 627 static int rule_rate(struct snd_pcm_hw_params *params, 628 struct snd_pcm_hw_rule *rule) 629 { 630 struct loopback_pcm *dpcm = rule->private; 631 struct loopback_cable *cable = dpcm->cable; 632 struct snd_interval t; 633 634 mutex_lock(&dpcm->loopback->cable_lock); 635 t.min = cable->hw.rate_min; 636 t.max = cable->hw.rate_max; 637 mutex_unlock(&dpcm->loopback->cable_lock); 638 t.openmin = t.openmax = 0; 639 t.integer = 0; 640 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 641 } 642 643 static int rule_channels(struct snd_pcm_hw_params *params, 644 struct snd_pcm_hw_rule *rule) 645 { 646 struct loopback_pcm *dpcm = rule->private; 647 struct loopback_cable *cable = dpcm->cable; 648 struct snd_interval t; 649 650 mutex_lock(&dpcm->loopback->cable_lock); 651 t.min = cable->hw.channels_min; 652 t.max = cable->hw.channels_max; 653 mutex_unlock(&dpcm->loopback->cable_lock); 654 t.openmin = t.openmax = 0; 655 t.integer = 0; 656 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 657 } 658 659 static void free_cable(struct snd_pcm_substream *substream) 660 { 661 struct loopback *loopback = substream->private_data; 662 int dev = get_cable_index(substream); 663 struct loopback_cable *cable; 664 665 cable = loopback->cables[substream->number][dev]; 666 if (!cable) 667 return; 668 if (cable->streams[!substream->stream]) { 669 /* other stream is still alive */ 670 spin_lock_irq(&cable->lock); 671 cable->streams[substream->stream] = NULL; 672 spin_unlock_irq(&cable->lock); 673 } else { 674 /* free the cable */ 675 loopback->cables[substream->number][dev] = NULL; 676 kfree(cable); 677 } 678 } 679 680 static int loopback_open(struct snd_pcm_substream *substream) 681 { 682 struct snd_pcm_runtime *runtime = substream->runtime; 683 struct loopback *loopback = substream->private_data; 684 struct loopback_pcm *dpcm; 685 struct loopback_cable *cable = NULL; 686 int err = 0; 687 int dev = get_cable_index(substream); 688 689 mutex_lock(&loopback->cable_lock); 690 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); 691 if (!dpcm) { 692 err = -ENOMEM; 693 goto unlock; 694 } 695 dpcm->loopback = loopback; 696 dpcm->substream = substream; 697 timer_setup(&dpcm->timer, loopback_timer_function, 0); 698 699 cable = loopback->cables[substream->number][dev]; 700 if (!cable) { 701 cable = kzalloc(sizeof(*cable), GFP_KERNEL); 702 if (!cable) { 703 err = -ENOMEM; 704 goto unlock; 705 } 706 spin_lock_init(&cable->lock); 707 cable->hw = loopback_pcm_hardware; 708 loopback->cables[substream->number][dev] = cable; 709 } 710 dpcm->cable = cable; 711 712 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 713 714 /* use dynamic rules based on actual runtime->hw values */ 715 /* note that the default rules created in the PCM midlevel code */ 716 /* are cached -> they do not reflect the actual state */ 717 err = snd_pcm_hw_rule_add(runtime, 0, 718 SNDRV_PCM_HW_PARAM_FORMAT, 719 rule_format, dpcm, 720 SNDRV_PCM_HW_PARAM_FORMAT, -1); 721 if (err < 0) 722 goto unlock; 723 err = snd_pcm_hw_rule_add(runtime, 0, 724 SNDRV_PCM_HW_PARAM_RATE, 725 rule_rate, dpcm, 726 SNDRV_PCM_HW_PARAM_RATE, -1); 727 if (err < 0) 728 goto unlock; 729 err = snd_pcm_hw_rule_add(runtime, 0, 730 SNDRV_PCM_HW_PARAM_CHANNELS, 731 rule_channels, dpcm, 732 SNDRV_PCM_HW_PARAM_CHANNELS, -1); 733 if (err < 0) 734 goto unlock; 735 736 runtime->private_data = dpcm; 737 runtime->private_free = loopback_runtime_free; 738 if (get_notify(dpcm)) 739 runtime->hw = loopback_pcm_hardware; 740 else 741 runtime->hw = cable->hw; 742 743 spin_lock_irq(&cable->lock); 744 cable->streams[substream->stream] = dpcm; 745 spin_unlock_irq(&cable->lock); 746 747 unlock: 748 if (err < 0) { 749 free_cable(substream); 750 kfree(dpcm); 751 } 752 mutex_unlock(&loopback->cable_lock); 753 return err; 754 } 755 756 static int loopback_close(struct snd_pcm_substream *substream) 757 { 758 struct loopback *loopback = substream->private_data; 759 struct loopback_pcm *dpcm = substream->runtime->private_data; 760 761 loopback_timer_stop_sync(dpcm); 762 mutex_lock(&loopback->cable_lock); 763 free_cable(substream); 764 mutex_unlock(&loopback->cable_lock); 765 return 0; 766 } 767 768 static const struct snd_pcm_ops loopback_pcm_ops = { 769 .open = loopback_open, 770 .close = loopback_close, 771 .ioctl = snd_pcm_lib_ioctl, 772 .hw_params = loopback_hw_params, 773 .hw_free = loopback_hw_free, 774 .prepare = loopback_prepare, 775 .trigger = loopback_trigger, 776 .pointer = loopback_pointer, 777 }; 778 779 static int loopback_pcm_new(struct loopback *loopback, 780 int device, int substreams) 781 { 782 struct snd_pcm *pcm; 783 int err; 784 785 err = snd_pcm_new(loopback->card, "Loopback PCM", device, 786 substreams, substreams, &pcm); 787 if (err < 0) 788 return err; 789 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_pcm_ops); 790 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_pcm_ops); 791 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_VMALLOC, 792 NULL, 0, 0); 793 794 pcm->private_data = loopback; 795 pcm->info_flags = 0; 796 strcpy(pcm->name, "Loopback PCM"); 797 798 loopback->pcm[device] = pcm; 799 return 0; 800 } 801 802 static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol, 803 struct snd_ctl_elem_info *uinfo) 804 { 805 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 806 uinfo->count = 1; 807 uinfo->value.integer.min = 80000; 808 uinfo->value.integer.max = 120000; 809 uinfo->value.integer.step = 1; 810 return 0; 811 } 812 813 static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol, 814 struct snd_ctl_elem_value *ucontrol) 815 { 816 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 817 818 mutex_lock(&loopback->cable_lock); 819 ucontrol->value.integer.value[0] = 820 loopback->setup[kcontrol->id.subdevice] 821 [kcontrol->id.device].rate_shift; 822 mutex_unlock(&loopback->cable_lock); 823 return 0; 824 } 825 826 static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol, 827 struct snd_ctl_elem_value *ucontrol) 828 { 829 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 830 unsigned int val; 831 int change = 0; 832 833 val = ucontrol->value.integer.value[0]; 834 if (val < 80000) 835 val = 80000; 836 if (val > 120000) 837 val = 120000; 838 mutex_lock(&loopback->cable_lock); 839 if (val != loopback->setup[kcontrol->id.subdevice] 840 [kcontrol->id.device].rate_shift) { 841 loopback->setup[kcontrol->id.subdevice] 842 [kcontrol->id.device].rate_shift = val; 843 change = 1; 844 } 845 mutex_unlock(&loopback->cable_lock); 846 return change; 847 } 848 849 static int loopback_notify_get(struct snd_kcontrol *kcontrol, 850 struct snd_ctl_elem_value *ucontrol) 851 { 852 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 853 854 mutex_lock(&loopback->cable_lock); 855 ucontrol->value.integer.value[0] = 856 loopback->setup[kcontrol->id.subdevice] 857 [kcontrol->id.device].notify; 858 mutex_unlock(&loopback->cable_lock); 859 return 0; 860 } 861 862 static int loopback_notify_put(struct snd_kcontrol *kcontrol, 863 struct snd_ctl_elem_value *ucontrol) 864 { 865 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 866 unsigned int val; 867 int change = 0; 868 869 val = ucontrol->value.integer.value[0] ? 1 : 0; 870 mutex_lock(&loopback->cable_lock); 871 if (val != loopback->setup[kcontrol->id.subdevice] 872 [kcontrol->id.device].notify) { 873 loopback->setup[kcontrol->id.subdevice] 874 [kcontrol->id.device].notify = val; 875 change = 1; 876 } 877 mutex_unlock(&loopback->cable_lock); 878 return change; 879 } 880 881 static int loopback_active_get(struct snd_kcontrol *kcontrol, 882 struct snd_ctl_elem_value *ucontrol) 883 { 884 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 885 struct loopback_cable *cable; 886 887 unsigned int val = 0; 888 889 mutex_lock(&loopback->cable_lock); 890 cable = loopback->cables[kcontrol->id.subdevice][kcontrol->id.device ^ 1]; 891 if (cable != NULL) { 892 unsigned int running = cable->running ^ cable->pause; 893 894 val = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 1 : 0; 895 } 896 mutex_unlock(&loopback->cable_lock); 897 ucontrol->value.integer.value[0] = val; 898 return 0; 899 } 900 901 static int loopback_format_info(struct snd_kcontrol *kcontrol, 902 struct snd_ctl_elem_info *uinfo) 903 { 904 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 905 uinfo->count = 1; 906 uinfo->value.integer.min = 0; 907 uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST; 908 uinfo->value.integer.step = 1; 909 return 0; 910 } 911 912 static int loopback_format_get(struct snd_kcontrol *kcontrol, 913 struct snd_ctl_elem_value *ucontrol) 914 { 915 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 916 917 ucontrol->value.integer.value[0] = 918 loopback->setup[kcontrol->id.subdevice] 919 [kcontrol->id.device].format; 920 return 0; 921 } 922 923 static int loopback_rate_info(struct snd_kcontrol *kcontrol, 924 struct snd_ctl_elem_info *uinfo) 925 { 926 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 927 uinfo->count = 1; 928 uinfo->value.integer.min = 0; 929 uinfo->value.integer.max = 192000; 930 uinfo->value.integer.step = 1; 931 return 0; 932 } 933 934 static int loopback_rate_get(struct snd_kcontrol *kcontrol, 935 struct snd_ctl_elem_value *ucontrol) 936 { 937 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 938 939 mutex_lock(&loopback->cable_lock); 940 ucontrol->value.integer.value[0] = 941 loopback->setup[kcontrol->id.subdevice] 942 [kcontrol->id.device].rate; 943 mutex_unlock(&loopback->cable_lock); 944 return 0; 945 } 946 947 static int loopback_channels_info(struct snd_kcontrol *kcontrol, 948 struct snd_ctl_elem_info *uinfo) 949 { 950 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 951 uinfo->count = 1; 952 uinfo->value.integer.min = 1; 953 uinfo->value.integer.max = 1024; 954 uinfo->value.integer.step = 1; 955 return 0; 956 } 957 958 static int loopback_channels_get(struct snd_kcontrol *kcontrol, 959 struct snd_ctl_elem_value *ucontrol) 960 { 961 struct loopback *loopback = snd_kcontrol_chip(kcontrol); 962 963 mutex_lock(&loopback->cable_lock); 964 ucontrol->value.integer.value[0] = 965 loopback->setup[kcontrol->id.subdevice] 966 [kcontrol->id.device].channels; 967 mutex_unlock(&loopback->cable_lock); 968 return 0; 969 } 970 971 static struct snd_kcontrol_new loopback_controls[] = { 972 { 973 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 974 .name = "PCM Rate Shift 100000", 975 .info = loopback_rate_shift_info, 976 .get = loopback_rate_shift_get, 977 .put = loopback_rate_shift_put, 978 }, 979 { 980 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 981 .name = "PCM Notify", 982 .info = snd_ctl_boolean_mono_info, 983 .get = loopback_notify_get, 984 .put = loopback_notify_put, 985 }, 986 #define ACTIVE_IDX 2 987 { 988 .access = SNDRV_CTL_ELEM_ACCESS_READ, 989 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 990 .name = "PCM Slave Active", 991 .info = snd_ctl_boolean_mono_info, 992 .get = loopback_active_get, 993 }, 994 #define FORMAT_IDX 3 995 { 996 .access = SNDRV_CTL_ELEM_ACCESS_READ, 997 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 998 .name = "PCM Slave Format", 999 .info = loopback_format_info, 1000 .get = loopback_format_get 1001 }, 1002 #define RATE_IDX 4 1003 { 1004 .access = SNDRV_CTL_ELEM_ACCESS_READ, 1005 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1006 .name = "PCM Slave Rate", 1007 .info = loopback_rate_info, 1008 .get = loopback_rate_get 1009 }, 1010 #define CHANNELS_IDX 5 1011 { 1012 .access = SNDRV_CTL_ELEM_ACCESS_READ, 1013 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1014 .name = "PCM Slave Channels", 1015 .info = loopback_channels_info, 1016 .get = loopback_channels_get 1017 } 1018 }; 1019 1020 static int loopback_mixer_new(struct loopback *loopback, int notify) 1021 { 1022 struct snd_card *card = loopback->card; 1023 struct snd_pcm *pcm; 1024 struct snd_kcontrol *kctl; 1025 struct loopback_setup *setup; 1026 int err, dev, substr, substr_count, idx; 1027 1028 strcpy(card->mixername, "Loopback Mixer"); 1029 for (dev = 0; dev < 2; dev++) { 1030 pcm = loopback->pcm[dev]; 1031 substr_count = 1032 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count; 1033 for (substr = 0; substr < substr_count; substr++) { 1034 setup = &loopback->setup[substr][dev]; 1035 setup->notify = notify; 1036 setup->rate_shift = NO_PITCH; 1037 setup->format = SNDRV_PCM_FORMAT_S16_LE; 1038 setup->rate = 48000; 1039 setup->channels = 2; 1040 for (idx = 0; idx < ARRAY_SIZE(loopback_controls); 1041 idx++) { 1042 kctl = snd_ctl_new1(&loopback_controls[idx], 1043 loopback); 1044 if (!kctl) 1045 return -ENOMEM; 1046 kctl->id.device = dev; 1047 kctl->id.subdevice = substr; 1048 switch (idx) { 1049 case ACTIVE_IDX: 1050 setup->active_id = kctl->id; 1051 break; 1052 case FORMAT_IDX: 1053 setup->format_id = kctl->id; 1054 break; 1055 case RATE_IDX: 1056 setup->rate_id = kctl->id; 1057 break; 1058 case CHANNELS_IDX: 1059 setup->channels_id = kctl->id; 1060 break; 1061 default: 1062 break; 1063 } 1064 err = snd_ctl_add(card, kctl); 1065 if (err < 0) 1066 return err; 1067 } 1068 } 1069 } 1070 return 0; 1071 } 1072 1073 static void print_dpcm_info(struct snd_info_buffer *buffer, 1074 struct loopback_pcm *dpcm, 1075 const char *id) 1076 { 1077 snd_iprintf(buffer, " %s\n", id); 1078 if (dpcm == NULL) { 1079 snd_iprintf(buffer, " inactive\n"); 1080 return; 1081 } 1082 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size); 1083 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos); 1084 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size); 1085 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size); 1086 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps); 1087 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign); 1088 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift); 1089 snd_iprintf(buffer, " update_pending:\t%u\n", 1090 dpcm->period_update_pending); 1091 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos); 1092 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac); 1093 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n", 1094 dpcm->last_jiffies, jiffies); 1095 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires); 1096 } 1097 1098 static void print_substream_info(struct snd_info_buffer *buffer, 1099 struct loopback *loopback, 1100 int sub, 1101 int num) 1102 { 1103 struct loopback_cable *cable = loopback->cables[sub][num]; 1104 1105 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub); 1106 if (cable == NULL) { 1107 snd_iprintf(buffer, " inactive\n"); 1108 return; 1109 } 1110 snd_iprintf(buffer, " valid: %u\n", cable->valid); 1111 snd_iprintf(buffer, " running: %u\n", cable->running); 1112 snd_iprintf(buffer, " pause: %u\n", cable->pause); 1113 print_dpcm_info(buffer, cable->streams[0], "Playback"); 1114 print_dpcm_info(buffer, cable->streams[1], "Capture"); 1115 } 1116 1117 static void print_cable_info(struct snd_info_entry *entry, 1118 struct snd_info_buffer *buffer) 1119 { 1120 struct loopback *loopback = entry->private_data; 1121 int sub, num; 1122 1123 mutex_lock(&loopback->cable_lock); 1124 num = entry->name[strlen(entry->name)-1]; 1125 num = num == '0' ? 0 : 1; 1126 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++) 1127 print_substream_info(buffer, loopback, sub, num); 1128 mutex_unlock(&loopback->cable_lock); 1129 } 1130 1131 static int loopback_proc_new(struct loopback *loopback, int cidx) 1132 { 1133 char name[32]; 1134 1135 snprintf(name, sizeof(name), "cable#%d", cidx); 1136 return snd_card_ro_proc_new(loopback->card, name, loopback, 1137 print_cable_info); 1138 } 1139 1140 static int loopback_probe(struct platform_device *devptr) 1141 { 1142 struct snd_card *card; 1143 struct loopback *loopback; 1144 int dev = devptr->id; 1145 int err; 1146 1147 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE, 1148 sizeof(struct loopback), &card); 1149 if (err < 0) 1150 return err; 1151 loopback = card->private_data; 1152 1153 if (pcm_substreams[dev] < 1) 1154 pcm_substreams[dev] = 1; 1155 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS) 1156 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS; 1157 1158 loopback->card = card; 1159 mutex_init(&loopback->cable_lock); 1160 1161 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]); 1162 if (err < 0) 1163 goto __nodev; 1164 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]); 1165 if (err < 0) 1166 goto __nodev; 1167 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0); 1168 if (err < 0) 1169 goto __nodev; 1170 loopback_proc_new(loopback, 0); 1171 loopback_proc_new(loopback, 1); 1172 strcpy(card->driver, "Loopback"); 1173 strcpy(card->shortname, "Loopback"); 1174 sprintf(card->longname, "Loopback %i", dev + 1); 1175 err = snd_card_register(card); 1176 if (!err) { 1177 platform_set_drvdata(devptr, card); 1178 return 0; 1179 } 1180 __nodev: 1181 snd_card_free(card); 1182 return err; 1183 } 1184 1185 static int loopback_remove(struct platform_device *devptr) 1186 { 1187 snd_card_free(platform_get_drvdata(devptr)); 1188 return 0; 1189 } 1190 1191 #ifdef CONFIG_PM_SLEEP 1192 static int loopback_suspend(struct device *pdev) 1193 { 1194 struct snd_card *card = dev_get_drvdata(pdev); 1195 1196 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 1197 return 0; 1198 } 1199 1200 static int loopback_resume(struct device *pdev) 1201 { 1202 struct snd_card *card = dev_get_drvdata(pdev); 1203 1204 snd_power_change_state(card, SNDRV_CTL_POWER_D0); 1205 return 0; 1206 } 1207 1208 static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume); 1209 #define LOOPBACK_PM_OPS &loopback_pm 1210 #else 1211 #define LOOPBACK_PM_OPS NULL 1212 #endif 1213 1214 #define SND_LOOPBACK_DRIVER "snd_aloop" 1215 1216 static struct platform_driver loopback_driver = { 1217 .probe = loopback_probe, 1218 .remove = loopback_remove, 1219 .driver = { 1220 .name = SND_LOOPBACK_DRIVER, 1221 .pm = LOOPBACK_PM_OPS, 1222 }, 1223 }; 1224 1225 static void loopback_unregister_all(void) 1226 { 1227 int i; 1228 1229 for (i = 0; i < ARRAY_SIZE(devices); ++i) 1230 platform_device_unregister(devices[i]); 1231 platform_driver_unregister(&loopback_driver); 1232 } 1233 1234 static int __init alsa_card_loopback_init(void) 1235 { 1236 int i, err, cards; 1237 1238 err = platform_driver_register(&loopback_driver); 1239 if (err < 0) 1240 return err; 1241 1242 1243 cards = 0; 1244 for (i = 0; i < SNDRV_CARDS; i++) { 1245 struct platform_device *device; 1246 if (!enable[i]) 1247 continue; 1248 device = platform_device_register_simple(SND_LOOPBACK_DRIVER, 1249 i, NULL, 0); 1250 if (IS_ERR(device)) 1251 continue; 1252 if (!platform_get_drvdata(device)) { 1253 platform_device_unregister(device); 1254 continue; 1255 } 1256 devices[i] = device; 1257 cards++; 1258 } 1259 if (!cards) { 1260 #ifdef MODULE 1261 printk(KERN_ERR "aloop: No loopback enabled\n"); 1262 #endif 1263 loopback_unregister_all(); 1264 return -ENODEV; 1265 } 1266 return 0; 1267 } 1268 1269 static void __exit alsa_card_loopback_exit(void) 1270 { 1271 loopback_unregister_all(); 1272 } 1273 1274 module_init(alsa_card_loopback_init) 1275 module_exit(alsa_card_loopback_exit) 1276