5 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 * More accurate positioning and full-duplex support:
8 * Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de>
10 * Major (almost complete) rewrite:
11 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
13 * A next major update in 2010 (separate timers for playback and capture):
14 * Copyright (c) Jaroslav Kysela <perex@perex.cz>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <linux/init.h>
33 #include <linux/jiffies.h>
34 #include <linux/slab.h>
35 #include <linux/time.h>
36 #include <linux/wait.h>
37 #include <linux/moduleparam.h>
38 #include <linux/platform_device.h>
39 #include <sound/core.h>
40 #include <sound/control.h>
41 #include <sound/pcm.h>
42 #include <sound/info.h>
43 #include <sound/initval.h>
45 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
46 MODULE_DESCRIPTION("A loopback soundcard");
47 MODULE_LICENSE("GPL");
48 MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}");
50 #define MAX_PCM_SUBSTREAMS 8
52 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
53 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
54 static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
55 static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
56 static int pcm_notify[SNDRV_CARDS];
58 module_param_array(index, int, NULL, 0444);
59 MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
60 module_param_array(id, charp, NULL, 0444);
61 MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
62 module_param_array(enable, bool, NULL, 0444);
63 MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
64 module_param_array(pcm_substreams, int, NULL, 0444);
65 MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
66 module_param_array(pcm_notify, int, NULL, 0444);
67 MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
69 #define NO_PITCH 100000
73 struct loopback_cable {
75 struct loopback_pcm *streams[2];
76 struct snd_pcm_hardware hw;
82 struct loopback_setup {
83 unsigned int notify: 1;
84 unsigned int rate_shift;
87 unsigned int channels;
88 struct snd_ctl_elem_id active_id;
89 struct snd_ctl_elem_id format_id;
90 struct snd_ctl_elem_id rate_id;
91 struct snd_ctl_elem_id channels_id;
95 struct snd_card *card;
96 struct mutex cable_lock;
97 struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
98 struct snd_pcm *pcm[2];
99 struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
102 struct loopback_pcm {
103 struct loopback *loopback;
104 struct snd_pcm_substream *substream;
105 struct loopback_cable *cable;
106 unsigned int pcm_buffer_size;
107 unsigned int buf_pos; /* position in buffer */
108 unsigned int silent_size;
110 unsigned int pcm_period_size;
111 unsigned int pcm_bps; /* bytes per second */
112 unsigned int pcm_salign; /* bytes per sample * channels */
113 unsigned int pcm_rate_shift; /* rate shift value */
115 unsigned int period_update_pending :1;
117 unsigned int irq_pos; /* fractional IRQ position */
118 unsigned int period_size_frac;
119 unsigned long last_jiffies;
120 struct timer_list timer;
123 static struct platform_device *devices[SNDRV_CARDS];
125 static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
127 if (dpcm->pcm_rate_shift == NO_PITCH) {
130 x = div_u64(NO_PITCH * (unsigned long long)x,
131 HZ * (unsigned long long)dpcm->pcm_rate_shift);
133 return x - (x % dpcm->pcm_salign);
136 static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
138 if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
141 x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
147 static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
149 int device = dpcm->substream->pstr->pcm->device;
151 if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
153 return &dpcm->loopback->setup[dpcm->substream->number][device];
156 static inline unsigned int get_notify(struct loopback_pcm *dpcm)
158 return get_setup(dpcm)->notify;
161 static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
163 return get_setup(dpcm)->rate_shift;
166 static void loopback_timer_start(struct loopback_pcm *dpcm)
169 unsigned int rate_shift = get_rate_shift(dpcm);
171 if (rate_shift != dpcm->pcm_rate_shift) {
172 dpcm->pcm_rate_shift = rate_shift;
173 dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
175 if (dpcm->period_size_frac <= dpcm->irq_pos) {
176 dpcm->irq_pos %= dpcm->period_size_frac;
177 dpcm->period_update_pending = 1;
179 tick = dpcm->period_size_frac - dpcm->irq_pos;
180 tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
181 dpcm->timer.expires = jiffies + tick;
182 add_timer(&dpcm->timer);
185 static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
187 del_timer(&dpcm->timer);
188 dpcm->timer.expires = 0;
191 #define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
192 #define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
193 #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
195 static int loopback_check_format(struct loopback_cable *cable, int stream)
197 struct snd_pcm_runtime *runtime, *cruntime;
198 struct loopback_setup *setup;
199 struct snd_card *card;
202 if (cable->valid != CABLE_VALID_BOTH) {
203 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
207 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
209 cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
211 check = runtime->format != cruntime->format ||
212 runtime->rate != cruntime->rate ||
213 runtime->channels != cruntime->channels;
216 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
219 snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
220 substream, SNDRV_PCM_STATE_DRAINING);
222 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
224 setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
225 card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
226 if (setup->format != runtime->format) {
227 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
229 setup->format = runtime->format;
231 if (setup->rate != runtime->rate) {
232 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
234 setup->rate = runtime->rate;
236 if (setup->channels != runtime->channels) {
237 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
238 &setup->channels_id);
239 setup->channels = runtime->channels;
245 static void loopback_active_notify(struct loopback_pcm *dpcm)
247 snd_ctl_notify(dpcm->loopback->card,
248 SNDRV_CTL_EVENT_MASK_VALUE,
249 &get_setup(dpcm)->active_id);
252 static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
254 struct snd_pcm_runtime *runtime = substream->runtime;
255 struct loopback_pcm *dpcm = runtime->private_data;
256 struct loopback_cable *cable = dpcm->cable;
260 case SNDRV_PCM_TRIGGER_START:
261 err = loopback_check_format(cable, substream->stream);
264 dpcm->last_jiffies = jiffies;
265 dpcm->pcm_rate_shift = 0;
266 loopback_timer_start(dpcm);
267 cable->running |= (1 << substream->stream);
268 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
269 loopback_active_notify(dpcm);
271 case SNDRV_PCM_TRIGGER_STOP:
272 cable->running &= ~(1 << substream->stream);
273 loopback_timer_stop(dpcm);
274 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
275 loopback_active_notify(dpcm);
283 static void params_change_substream(struct loopback_pcm *dpcm,
284 struct snd_pcm_runtime *runtime)
286 struct snd_pcm_runtime *dst_runtime;
288 if (dpcm == NULL || dpcm->substream == NULL)
290 dst_runtime = dpcm->substream->runtime;
291 if (dst_runtime == NULL)
293 dst_runtime->hw = dpcm->cable->hw;
296 static void params_change(struct snd_pcm_substream *substream)
298 struct snd_pcm_runtime *runtime = substream->runtime;
299 struct loopback_pcm *dpcm = runtime->private_data;
300 struct loopback_cable *cable = dpcm->cable;
302 cable->hw.formats = (1ULL << runtime->format);
303 cable->hw.rate_min = runtime->rate;
304 cable->hw.rate_max = runtime->rate;
305 cable->hw.channels_min = runtime->channels;
306 cable->hw.channels_max = runtime->channels;
307 params_change_substream(cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
309 params_change_substream(cable->streams[SNDRV_PCM_STREAM_CAPTURE],
313 static int loopback_prepare(struct snd_pcm_substream *substream)
315 struct snd_pcm_runtime *runtime = substream->runtime;
316 struct loopback_pcm *dpcm = runtime->private_data;
317 struct loopback_cable *cable = dpcm->cable;
320 salign = (snd_pcm_format_width(runtime->format) *
321 runtime->channels) / 8;
322 bps = salign * runtime->rate;
323 if (bps <= 0 || salign <= 0)
327 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
328 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
329 /* clear capture buffer */
330 dpcm->silent_size = dpcm->pcm_buffer_size;
331 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
332 runtime->buffer_size * runtime->channels);
336 dpcm->period_update_pending = 0;
338 dpcm->pcm_salign = salign;
339 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
341 mutex_lock(&dpcm->loopback->cable_lock);
342 if (!(cable->valid & ~(1 << substream->stream)) ||
343 (get_setup(dpcm)->notify &&
344 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
345 params_change(substream);
346 cable->valid |= 1 << substream->stream;
347 mutex_unlock(&dpcm->loopback->cable_lock);
352 static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
354 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
355 char *dst = runtime->dma_area;
356 unsigned int dst_off = dpcm->buf_pos;
358 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
360 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
361 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
364 unsigned int size = bytes;
365 if (dst_off + size > dpcm->pcm_buffer_size)
366 size = dpcm->pcm_buffer_size - dst_off;
367 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
368 bytes_to_frames(runtime, size) *
370 dpcm->silent_size += size;
378 static void copy_play_buf(struct loopback_pcm *play,
379 struct loopback_pcm *capt,
382 struct snd_pcm_runtime *runtime = play->substream->runtime;
383 char *src = runtime->dma_area;
384 char *dst = capt->substream->runtime->dma_area;
385 unsigned int src_off = play->buf_pos;
386 unsigned int dst_off = capt->buf_pos;
387 unsigned int clear_bytes = 0;
389 /* check if playback is draining, trim the capture copy size
390 * when our pointer is at the end of playback ring buffer */
391 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
392 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
393 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
394 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
395 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
396 appl_ptr1 += play->buf_pos / play->pcm_salign;
397 if (appl_ptr < appl_ptr1)
398 appl_ptr1 -= runtime->buffer_size;
399 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
401 clear_bytes = bytes - diff;
407 unsigned int size = bytes;
408 if (src_off + size > play->pcm_buffer_size)
409 size = play->pcm_buffer_size - src_off;
410 if (dst_off + size > capt->pcm_buffer_size)
411 size = capt->pcm_buffer_size - dst_off;
412 memcpy(dst + dst_off, src + src_off, size);
413 capt->silent_size = 0;
417 src_off = (src_off + size) % play->pcm_buffer_size;
418 dst_off = (dst_off + size) % capt->pcm_buffer_size;
421 if (clear_bytes > 0) {
422 clear_capture_buf(capt, clear_bytes);
423 capt->silent_size = 0;
427 #define BYTEPOS_UPDATE_POSONLY 0
428 #define BYTEPOS_UPDATE_CLEAR 1
429 #define BYTEPOS_UPDATE_COPY 2
431 static void loopback_bytepos_update(struct loopback_pcm *dpcm,
436 unsigned long last_pos;
438 last_pos = byte_pos(dpcm, dpcm->irq_pos);
439 dpcm->irq_pos += delta * dpcm->pcm_bps;
440 count = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
443 if (cmd == BYTEPOS_UPDATE_CLEAR)
444 clear_capture_buf(dpcm, count);
445 else if (cmd == BYTEPOS_UPDATE_COPY)
446 copy_play_buf(dpcm->cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
447 dpcm->cable->streams[SNDRV_PCM_STREAM_CAPTURE],
449 dpcm->buf_pos += count;
450 dpcm->buf_pos %= dpcm->pcm_buffer_size;
451 if (dpcm->irq_pos >= dpcm->period_size_frac) {
452 dpcm->irq_pos %= dpcm->period_size_frac;
453 dpcm->period_update_pending = 1;
457 static void loopback_pos_update(struct loopback_cable *cable)
459 struct loopback_pcm *dpcm_play =
460 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
461 struct loopback_pcm *dpcm_capt =
462 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
463 unsigned long delta_play = 0, delta_capt = 0;
465 spin_lock(&cable->lock);
466 if (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
467 delta_play = jiffies - dpcm_play->last_jiffies;
468 dpcm_play->last_jiffies += delta_play;
471 if (cable->running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
472 delta_capt = jiffies - dpcm_capt->last_jiffies;
473 dpcm_capt->last_jiffies += delta_capt;
476 if (delta_play == 0 && delta_capt == 0) {
477 spin_unlock(&cable->lock);
481 if (delta_play > delta_capt) {
482 loopback_bytepos_update(dpcm_play, delta_play - delta_capt,
483 BYTEPOS_UPDATE_POSONLY);
484 delta_play = delta_capt;
485 } else if (delta_play < delta_capt) {
486 loopback_bytepos_update(dpcm_capt, delta_capt - delta_play,
487 BYTEPOS_UPDATE_CLEAR);
488 delta_capt = delta_play;
491 if (delta_play == 0 && delta_capt == 0) {
492 spin_unlock(&cable->lock);
495 /* note delta_capt == delta_play at this moment */
496 loopback_bytepos_update(dpcm_capt, delta_capt, BYTEPOS_UPDATE_COPY);
497 loopback_bytepos_update(dpcm_play, delta_play, BYTEPOS_UPDATE_POSONLY);
498 spin_unlock(&cable->lock);
501 static void loopback_timer_function(unsigned long data)
503 struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
506 loopback_pos_update(dpcm->cable);
507 stream = dpcm->substream->stream;
508 if (dpcm->cable->running & (1 << stream))
509 loopback_timer_start(dpcm);
510 if (dpcm->period_update_pending) {
511 dpcm->period_update_pending = 0;
512 if (dpcm->cable->running & (1 << stream))
513 snd_pcm_period_elapsed(dpcm->substream);
517 static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
519 struct snd_pcm_runtime *runtime = substream->runtime;
520 struct loopback_pcm *dpcm = runtime->private_data;
522 loopback_pos_update(dpcm->cable);
523 return bytes_to_frames(runtime, dpcm->buf_pos);
526 static struct snd_pcm_hardware loopback_pcm_hardware =
528 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
529 SNDRV_PCM_INFO_MMAP_VALID),
530 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
531 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
532 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
533 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
538 .buffer_bytes_max = 2 * 1024 * 1024,
539 .period_bytes_min = 64,
540 /* note check overflow in frac_pos() using pcm_rate_shift before
541 changing period_bytes_max value */
542 .period_bytes_max = 1024 * 1024,
548 static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
550 struct loopback_pcm *dpcm = runtime->private_data;
554 static int loopback_hw_params(struct snd_pcm_substream *substream,
555 struct snd_pcm_hw_params *params)
557 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
560 static int loopback_hw_free(struct snd_pcm_substream *substream)
562 struct snd_pcm_runtime *runtime = substream->runtime;
563 struct loopback_pcm *dpcm = runtime->private_data;
564 struct loopback_cable *cable = dpcm->cable;
566 mutex_lock(&dpcm->loopback->cable_lock);
567 cable->valid &= ~(1 << substream->stream);
568 mutex_unlock(&dpcm->loopback->cable_lock);
569 return snd_pcm_lib_free_pages(substream);
572 static unsigned int get_cable_index(struct snd_pcm_substream *substream)
574 if (!substream->pcm->device)
575 return substream->stream;
577 return !substream->stream;
580 static int rule_format(struct snd_pcm_hw_params *params,
581 struct snd_pcm_hw_rule *rule)
584 struct snd_pcm_hardware *hw = rule->private;
585 struct snd_mask *maskp = hw_param_mask(params, rule->var);
587 maskp->bits[0] &= (u_int32_t)hw->formats;
588 maskp->bits[1] &= (u_int32_t)(hw->formats >> 32);
589 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
590 if (! maskp->bits[0] && ! maskp->bits[1])
595 static int rule_rate(struct snd_pcm_hw_params *params,
596 struct snd_pcm_hw_rule *rule)
598 struct snd_pcm_hardware *hw = rule->private;
599 struct snd_interval t;
601 t.min = hw->rate_min;
602 t.max = hw->rate_max;
603 t.openmin = t.openmax = 0;
605 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
608 static int rule_channels(struct snd_pcm_hw_params *params,
609 struct snd_pcm_hw_rule *rule)
611 struct snd_pcm_hardware *hw = rule->private;
612 struct snd_interval t;
614 t.min = hw->channels_min;
615 t.max = hw->channels_max;
616 t.openmin = t.openmax = 0;
618 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
621 static int loopback_open(struct snd_pcm_substream *substream)
623 struct snd_pcm_runtime *runtime = substream->runtime;
624 struct loopback *loopback = substream->private_data;
625 struct loopback_pcm *dpcm;
626 struct loopback_cable *cable;
628 int dev = get_cable_index(substream);
630 mutex_lock(&loopback->cable_lock);
631 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
636 dpcm->loopback = loopback;
637 dpcm->substream = substream;
638 setup_timer(&dpcm->timer, loopback_timer_function,
639 (unsigned long)dpcm);
641 cable = loopback->cables[substream->number][dev];
643 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
649 spin_lock_init(&cable->lock);
650 cable->hw = loopback_pcm_hardware;
651 loopback->cables[substream->number][dev] = cable;
654 cable->streams[substream->stream] = dpcm;
656 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
658 /* use dynamic rules based on actual runtime->hw values */
659 /* note that the default rules created in the PCM midlevel code */
660 /* are cached -> they do not reflect the actual state */
661 err = snd_pcm_hw_rule_add(runtime, 0,
662 SNDRV_PCM_HW_PARAM_FORMAT,
663 rule_format, &runtime->hw,
664 SNDRV_PCM_HW_PARAM_FORMAT, -1);
667 err = snd_pcm_hw_rule_add(runtime, 0,
668 SNDRV_PCM_HW_PARAM_RATE,
669 rule_rate, &runtime->hw,
670 SNDRV_PCM_HW_PARAM_RATE, -1);
673 err = snd_pcm_hw_rule_add(runtime, 0,
674 SNDRV_PCM_HW_PARAM_CHANNELS,
675 rule_channels, &runtime->hw,
676 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
680 runtime->private_data = dpcm;
681 runtime->private_free = loopback_runtime_free;
682 if (get_notify(dpcm))
683 runtime->hw = loopback_pcm_hardware;
685 runtime->hw = cable->hw;
687 mutex_unlock(&loopback->cable_lock);
691 static int loopback_close(struct snd_pcm_substream *substream)
693 struct loopback *loopback = substream->private_data;
694 struct loopback_pcm *dpcm = substream->runtime->private_data;
695 struct loopback_cable *cable;
696 int dev = get_cable_index(substream);
698 loopback_timer_stop(dpcm);
699 mutex_lock(&loopback->cable_lock);
700 cable = loopback->cables[substream->number][dev];
701 if (cable->streams[!substream->stream]) {
702 /* other stream is still alive */
703 cable->streams[substream->stream] = NULL;
706 loopback->cables[substream->number][dev] = NULL;
709 mutex_unlock(&loopback->cable_lock);
713 static struct snd_pcm_ops loopback_playback_ops = {
714 .open = loopback_open,
715 .close = loopback_close,
716 .ioctl = snd_pcm_lib_ioctl,
717 .hw_params = loopback_hw_params,
718 .hw_free = loopback_hw_free,
719 .prepare = loopback_prepare,
720 .trigger = loopback_trigger,
721 .pointer = loopback_pointer,
724 static struct snd_pcm_ops loopback_capture_ops = {
725 .open = loopback_open,
726 .close = loopback_close,
727 .ioctl = snd_pcm_lib_ioctl,
728 .hw_params = loopback_hw_params,
729 .hw_free = loopback_hw_free,
730 .prepare = loopback_prepare,
731 .trigger = loopback_trigger,
732 .pointer = loopback_pointer,
735 static int __devinit loopback_pcm_new(struct loopback *loopback,
736 int device, int substreams)
741 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
742 substreams, substreams, &pcm);
745 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
746 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
748 pcm->private_data = loopback;
750 strcpy(pcm->name, "Loopback PCM");
752 loopback->pcm[device] = pcm;
754 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
755 snd_dma_continuous_data(GFP_KERNEL),
760 static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
761 struct snd_ctl_elem_info *uinfo)
763 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
765 uinfo->value.integer.min = 80000;
766 uinfo->value.integer.max = 120000;
767 uinfo->value.integer.step = 1;
771 static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
772 struct snd_ctl_elem_value *ucontrol)
774 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
776 ucontrol->value.integer.value[0] =
777 loopback->setup[kcontrol->id.subdevice]
778 [kcontrol->id.device].rate_shift;
782 static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
783 struct snd_ctl_elem_value *ucontrol)
785 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
789 val = ucontrol->value.integer.value[0];
794 mutex_lock(&loopback->cable_lock);
795 if (val != loopback->setup[kcontrol->id.subdevice]
796 [kcontrol->id.device].rate_shift) {
797 loopback->setup[kcontrol->id.subdevice]
798 [kcontrol->id.device].rate_shift = val;
801 mutex_unlock(&loopback->cable_lock);
805 static int loopback_notify_get(struct snd_kcontrol *kcontrol,
806 struct snd_ctl_elem_value *ucontrol)
808 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
810 ucontrol->value.integer.value[0] =
811 loopback->setup[kcontrol->id.subdevice]
812 [kcontrol->id.device].notify;
816 static int loopback_notify_put(struct snd_kcontrol *kcontrol,
817 struct snd_ctl_elem_value *ucontrol)
819 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
823 val = ucontrol->value.integer.value[0] ? 1 : 0;
824 if (val != loopback->setup[kcontrol->id.subdevice]
825 [kcontrol->id.device].notify) {
826 loopback->setup[kcontrol->id.subdevice]
827 [kcontrol->id.device].notify = val;
833 static int loopback_active_get(struct snd_kcontrol *kcontrol,
834 struct snd_ctl_elem_value *ucontrol)
836 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
837 struct loopback_cable *cable = loopback->cables
838 [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
839 unsigned int val = 0;
842 val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
844 ucontrol->value.integer.value[0] = val;
848 static int loopback_format_info(struct snd_kcontrol *kcontrol,
849 struct snd_ctl_elem_info *uinfo)
851 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
853 uinfo->value.integer.min = 0;
854 uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
855 uinfo->value.integer.step = 1;
859 static int loopback_format_get(struct snd_kcontrol *kcontrol,
860 struct snd_ctl_elem_value *ucontrol)
862 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
864 ucontrol->value.integer.value[0] =
865 loopback->setup[kcontrol->id.subdevice]
866 [kcontrol->id.device].format;
870 static int loopback_rate_info(struct snd_kcontrol *kcontrol,
871 struct snd_ctl_elem_info *uinfo)
873 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
875 uinfo->value.integer.min = 0;
876 uinfo->value.integer.max = 192000;
877 uinfo->value.integer.step = 1;
881 static int loopback_rate_get(struct snd_kcontrol *kcontrol,
882 struct snd_ctl_elem_value *ucontrol)
884 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
886 ucontrol->value.integer.value[0] =
887 loopback->setup[kcontrol->id.subdevice]
888 [kcontrol->id.device].rate;
892 static int loopback_channels_info(struct snd_kcontrol *kcontrol,
893 struct snd_ctl_elem_info *uinfo)
895 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
897 uinfo->value.integer.min = 1;
898 uinfo->value.integer.max = 1024;
899 uinfo->value.integer.step = 1;
903 static int loopback_channels_get(struct snd_kcontrol *kcontrol,
904 struct snd_ctl_elem_value *ucontrol)
906 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
908 ucontrol->value.integer.value[0] =
909 loopback->setup[kcontrol->id.subdevice]
910 [kcontrol->id.device].channels;
914 static struct snd_kcontrol_new loopback_controls[] __devinitdata = {
916 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
917 .name = "PCM Rate Shift 100000",
918 .info = loopback_rate_shift_info,
919 .get = loopback_rate_shift_get,
920 .put = loopback_rate_shift_put,
923 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
924 .name = "PCM Notify",
925 .info = snd_ctl_boolean_mono_info,
926 .get = loopback_notify_get,
927 .put = loopback_notify_put,
931 .access = SNDRV_CTL_ELEM_ACCESS_READ,
932 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
933 .name = "PCM Slave Active",
934 .info = snd_ctl_boolean_mono_info,
935 .get = loopback_active_get,
939 .access = SNDRV_CTL_ELEM_ACCESS_READ,
940 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
941 .name = "PCM Slave Format",
942 .info = loopback_format_info,
943 .get = loopback_format_get
947 .access = SNDRV_CTL_ELEM_ACCESS_READ,
948 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
949 .name = "PCM Slave Rate",
950 .info = loopback_rate_info,
951 .get = loopback_rate_get
953 #define CHANNELS_IDX 5
955 .access = SNDRV_CTL_ELEM_ACCESS_READ,
956 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
957 .name = "PCM Slave Channels",
958 .info = loopback_channels_info,
959 .get = loopback_channels_get
963 static int __devinit loopback_mixer_new(struct loopback *loopback, int notify)
965 struct snd_card *card = loopback->card;
967 struct snd_kcontrol *kctl;
968 struct loopback_setup *setup;
969 int err, dev, substr, substr_count, idx;
971 strcpy(card->mixername, "Loopback Mixer");
972 for (dev = 0; dev < 2; dev++) {
973 pcm = loopback->pcm[dev];
975 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
976 for (substr = 0; substr < substr_count; substr++) {
977 setup = &loopback->setup[substr][dev];
978 setup->notify = notify;
979 setup->rate_shift = NO_PITCH;
980 setup->format = SNDRV_PCM_FORMAT_S16_LE;
983 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
985 kctl = snd_ctl_new1(&loopback_controls[idx],
989 kctl->id.device = dev;
990 kctl->id.subdevice = substr;
993 setup->active_id = kctl->id;
996 setup->format_id = kctl->id;
999 setup->rate_id = kctl->id;
1002 setup->channels_id = kctl->id;
1007 err = snd_ctl_add(card, kctl);
1016 #ifdef CONFIG_PROC_FS
1018 static void print_dpcm_info(struct snd_info_buffer *buffer,
1019 struct loopback_pcm *dpcm,
1022 snd_iprintf(buffer, " %s\n", id);
1024 snd_iprintf(buffer, " inactive\n");
1027 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1028 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1029 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1030 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1031 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1032 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1033 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1034 snd_iprintf(buffer, " update_pending:\t%u\n",
1035 dpcm->period_update_pending);
1036 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
1037 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
1038 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
1039 dpcm->last_jiffies, jiffies);
1040 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
1043 static void print_substream_info(struct snd_info_buffer *buffer,
1044 struct loopback *loopback,
1048 struct loopback_cable *cable = loopback->cables[sub][num];
1050 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1051 if (cable == NULL) {
1052 snd_iprintf(buffer, " inactive\n");
1055 snd_iprintf(buffer, " valid: %u\n", cable->valid);
1056 snd_iprintf(buffer, " running: %u\n", cable->running);
1057 print_dpcm_info(buffer, cable->streams[0], "Playback");
1058 print_dpcm_info(buffer, cable->streams[1], "Capture");
1061 static void print_cable_info(struct snd_info_entry *entry,
1062 struct snd_info_buffer *buffer)
1064 struct loopback *loopback = entry->private_data;
1067 mutex_lock(&loopback->cable_lock);
1068 num = entry->name[strlen(entry->name)-1];
1069 num = num == '0' ? 0 : 1;
1070 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1071 print_substream_info(buffer, loopback, sub, num);
1072 mutex_unlock(&loopback->cable_lock);
1075 static int __devinit loopback_proc_new(struct loopback *loopback, int cidx)
1078 struct snd_info_entry *entry;
1081 snprintf(name, sizeof(name), "cable#%d", cidx);
1082 err = snd_card_proc_new(loopback->card, name, &entry);
1086 snd_info_set_text_ops(entry, loopback, print_cable_info);
1090 #else /* !CONFIG_PROC_FS */
1092 #define loopback_proc_new(loopback, cidx) do { } while (0)
1096 static int __devinit loopback_probe(struct platform_device *devptr)
1098 struct snd_card *card;
1099 struct loopback *loopback;
1100 int dev = devptr->id;
1103 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
1104 sizeof(struct loopback), &card);
1107 loopback = card->private_data;
1109 if (pcm_substreams[dev] < 1)
1110 pcm_substreams[dev] = 1;
1111 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1112 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1114 loopback->card = card;
1115 mutex_init(&loopback->cable_lock);
1117 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1120 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1123 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1126 loopback_proc_new(loopback, 0);
1127 loopback_proc_new(loopback, 1);
1128 strcpy(card->driver, "Loopback");
1129 strcpy(card->shortname, "Loopback");
1130 sprintf(card->longname, "Loopback %i", dev + 1);
1131 err = snd_card_register(card);
1133 platform_set_drvdata(devptr, card);
1137 snd_card_free(card);
1141 static int __devexit loopback_remove(struct platform_device *devptr)
1143 snd_card_free(platform_get_drvdata(devptr));
1144 platform_set_drvdata(devptr, NULL);
1149 static int loopback_suspend(struct platform_device *pdev,
1152 struct snd_card *card = platform_get_drvdata(pdev);
1153 struct loopback *loopback = card->private_data;
1155 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1157 snd_pcm_suspend_all(loopback->pcm[0]);
1158 snd_pcm_suspend_all(loopback->pcm[1]);
1162 static int loopback_resume(struct platform_device *pdev)
1164 struct snd_card *card = platform_get_drvdata(pdev);
1166 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1171 #define SND_LOOPBACK_DRIVER "snd_aloop"
1173 static struct platform_driver loopback_driver = {
1174 .probe = loopback_probe,
1175 .remove = __devexit_p(loopback_remove),
1177 .suspend = loopback_suspend,
1178 .resume = loopback_resume,
1181 .name = SND_LOOPBACK_DRIVER
1185 static void loopback_unregister_all(void)
1189 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1190 platform_device_unregister(devices[i]);
1191 platform_driver_unregister(&loopback_driver);
1194 static int __init alsa_card_loopback_init(void)
1198 err = platform_driver_register(&loopback_driver);
1204 for (i = 0; i < SNDRV_CARDS; i++) {
1205 struct platform_device *device;
1208 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1212 if (!platform_get_drvdata(device)) {
1213 platform_device_unregister(device);
1216 devices[i] = device;
1221 printk(KERN_ERR "aloop: No loopback enabled\n");
1223 loopback_unregister_all();
1229 static void __exit alsa_card_loopback_exit(void)
1231 loopback_unregister_all();
1234 module_init(alsa_card_loopback_init)
1235 module_exit(alsa_card_loopback_exit)