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/module.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 bool 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;
83 struct loopback_setup {
84 unsigned int notify: 1;
85 unsigned int rate_shift;
88 unsigned int channels;
89 struct snd_ctl_elem_id active_id;
90 struct snd_ctl_elem_id format_id;
91 struct snd_ctl_elem_id rate_id;
92 struct snd_ctl_elem_id channels_id;
96 struct snd_card *card;
97 struct mutex cable_lock;
98 struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
99 struct snd_pcm *pcm[2];
100 struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
103 struct loopback_pcm {
104 struct loopback *loopback;
105 struct snd_pcm_substream *substream;
106 struct loopback_cable *cable;
107 unsigned int pcm_buffer_size;
108 unsigned int buf_pos; /* position in buffer */
109 unsigned int silent_size;
111 unsigned int pcm_period_size;
112 unsigned int pcm_bps; /* bytes per second */
113 unsigned int pcm_salign; /* bytes per sample * channels */
114 unsigned int pcm_rate_shift; /* rate shift value */
116 unsigned int period_update_pending :1;
118 unsigned int irq_pos; /* fractional IRQ position */
119 unsigned int period_size_frac;
120 unsigned int last_drift;
121 unsigned long last_jiffies;
122 struct timer_list timer;
125 static struct platform_device *devices[SNDRV_CARDS];
127 static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
129 if (dpcm->pcm_rate_shift == NO_PITCH) {
132 x = div_u64(NO_PITCH * (unsigned long long)x,
133 HZ * (unsigned long long)dpcm->pcm_rate_shift);
135 return x - (x % dpcm->pcm_salign);
138 static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
140 if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
143 x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
149 static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
151 int device = dpcm->substream->pstr->pcm->device;
153 if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
155 return &dpcm->loopback->setup[dpcm->substream->number][device];
158 static inline unsigned int get_notify(struct loopback_pcm *dpcm)
160 return get_setup(dpcm)->notify;
163 static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
165 return get_setup(dpcm)->rate_shift;
168 /* call in cable->lock */
169 static void loopback_timer_start(struct loopback_pcm *dpcm)
172 unsigned int rate_shift = get_rate_shift(dpcm);
174 if (rate_shift != dpcm->pcm_rate_shift) {
175 dpcm->pcm_rate_shift = rate_shift;
176 dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
178 if (dpcm->period_size_frac <= dpcm->irq_pos) {
179 dpcm->irq_pos %= dpcm->period_size_frac;
180 dpcm->period_update_pending = 1;
182 tick = dpcm->period_size_frac - dpcm->irq_pos;
183 tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
184 mod_timer(&dpcm->timer, jiffies + tick);
187 /* call in cable->lock */
188 static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
190 del_timer(&dpcm->timer);
191 dpcm->timer.expires = 0;
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)
198 static int loopback_check_format(struct loopback_cable *cable, int stream)
200 struct snd_pcm_runtime *runtime, *cruntime;
201 struct loopback_setup *setup;
202 struct snd_card *card;
205 if (cable->valid != CABLE_VALID_BOTH) {
206 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
210 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
212 cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
214 check = runtime->format != cruntime->format ||
215 runtime->rate != cruntime->rate ||
216 runtime->channels != cruntime->channels;
219 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
222 snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
223 substream, SNDRV_PCM_STATE_DRAINING);
225 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
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,
232 setup->format = runtime->format;
234 if (setup->rate != runtime->rate) {
235 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
237 setup->rate = runtime->rate;
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;
248 static void loopback_active_notify(struct loopback_pcm *dpcm)
250 snd_ctl_notify(dpcm->loopback->card,
251 SNDRV_CTL_EVENT_MASK_VALUE,
252 &get_setup(dpcm)->active_id);
255 static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
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, stream = 1 << substream->stream;
263 case SNDRV_PCM_TRIGGER_START:
264 err = loopback_check_format(cable, substream->stream);
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 loopback_timer_start(dpcm);
274 spin_unlock(&cable->lock);
275 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
276 loopback_active_notify(dpcm);
278 case SNDRV_PCM_TRIGGER_STOP:
279 spin_lock(&cable->lock);
280 cable->running &= ~stream;
281 cable->pause &= ~stream;
282 loopback_timer_stop(dpcm);
283 spin_unlock(&cable->lock);
284 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
285 loopback_active_notify(dpcm);
287 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
288 case SNDRV_PCM_TRIGGER_SUSPEND:
289 spin_lock(&cable->lock);
290 cable->pause |= stream;
291 loopback_timer_stop(dpcm);
292 spin_unlock(&cable->lock);
294 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
295 case SNDRV_PCM_TRIGGER_RESUME:
296 spin_lock(&cable->lock);
297 dpcm->last_jiffies = jiffies;
298 cable->pause &= ~stream;
299 loopback_timer_start(dpcm);
300 spin_unlock(&cable->lock);
308 static void params_change_substream(struct loopback_pcm *dpcm,
309 struct snd_pcm_runtime *runtime)
311 struct snd_pcm_runtime *dst_runtime;
313 if (dpcm == NULL || dpcm->substream == NULL)
315 dst_runtime = dpcm->substream->runtime;
316 if (dst_runtime == NULL)
318 dst_runtime->hw = dpcm->cable->hw;
321 static void params_change(struct snd_pcm_substream *substream)
323 struct snd_pcm_runtime *runtime = substream->runtime;
324 struct loopback_pcm *dpcm = runtime->private_data;
325 struct loopback_cable *cable = dpcm->cable;
327 cable->hw.formats = pcm_format_to_bits(runtime->format);
328 cable->hw.rate_min = runtime->rate;
329 cable->hw.rate_max = runtime->rate;
330 cable->hw.channels_min = runtime->channels;
331 cable->hw.channels_max = runtime->channels;
332 params_change_substream(cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
334 params_change_substream(cable->streams[SNDRV_PCM_STREAM_CAPTURE],
338 static int loopback_prepare(struct snd_pcm_substream *substream)
340 struct snd_pcm_runtime *runtime = substream->runtime;
341 struct loopback_pcm *dpcm = runtime->private_data;
342 struct loopback_cable *cable = dpcm->cable;
345 salign = (snd_pcm_format_width(runtime->format) *
346 runtime->channels) / 8;
347 bps = salign * runtime->rate;
348 if (bps <= 0 || salign <= 0)
352 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
353 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
354 /* clear capture buffer */
355 dpcm->silent_size = dpcm->pcm_buffer_size;
356 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
357 runtime->buffer_size * runtime->channels);
361 dpcm->period_update_pending = 0;
363 dpcm->pcm_salign = salign;
364 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
366 mutex_lock(&dpcm->loopback->cable_lock);
367 if (!(cable->valid & ~(1 << substream->stream)) ||
368 (get_setup(dpcm)->notify &&
369 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
370 params_change(substream);
371 cable->valid |= 1 << substream->stream;
372 mutex_unlock(&dpcm->loopback->cable_lock);
377 static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
379 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
380 char *dst = runtime->dma_area;
381 unsigned int dst_off = dpcm->buf_pos;
383 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
385 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
386 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
389 unsigned int size = bytes;
390 if (dst_off + size > dpcm->pcm_buffer_size)
391 size = dpcm->pcm_buffer_size - dst_off;
392 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
393 bytes_to_frames(runtime, size) *
395 dpcm->silent_size += size;
403 static void copy_play_buf(struct loopback_pcm *play,
404 struct loopback_pcm *capt,
407 struct snd_pcm_runtime *runtime = play->substream->runtime;
408 char *src = runtime->dma_area;
409 char *dst = capt->substream->runtime->dma_area;
410 unsigned int src_off = play->buf_pos;
411 unsigned int dst_off = capt->buf_pos;
412 unsigned int clear_bytes = 0;
414 /* check if playback is draining, trim the capture copy size
415 * when our pointer is at the end of playback ring buffer */
416 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
417 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
418 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
419 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
420 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
421 appl_ptr1 += play->buf_pos / play->pcm_salign;
422 if (appl_ptr < appl_ptr1)
423 appl_ptr1 -= runtime->buffer_size;
424 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
426 clear_bytes = bytes - diff;
432 unsigned int size = bytes;
433 if (src_off + size > play->pcm_buffer_size)
434 size = play->pcm_buffer_size - src_off;
435 if (dst_off + size > capt->pcm_buffer_size)
436 size = capt->pcm_buffer_size - dst_off;
437 memcpy(dst + dst_off, src + src_off, size);
438 capt->silent_size = 0;
442 src_off = (src_off + size) % play->pcm_buffer_size;
443 dst_off = (dst_off + size) % capt->pcm_buffer_size;
446 if (clear_bytes > 0) {
447 clear_capture_buf(capt, clear_bytes);
448 capt->silent_size = 0;
452 static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
453 unsigned int jiffies_delta)
455 unsigned long last_pos;
458 last_pos = byte_pos(dpcm, dpcm->irq_pos);
459 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
460 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
461 if (delta >= dpcm->last_drift)
462 delta -= dpcm->last_drift;
463 dpcm->last_drift = 0;
464 if (dpcm->irq_pos >= dpcm->period_size_frac) {
465 dpcm->irq_pos %= dpcm->period_size_frac;
466 dpcm->period_update_pending = 1;
471 static inline void bytepos_finish(struct loopback_pcm *dpcm,
474 dpcm->buf_pos += delta;
475 dpcm->buf_pos %= dpcm->pcm_buffer_size;
478 /* call in cable->lock */
479 static unsigned int loopback_pos_update(struct loopback_cable *cable)
481 struct loopback_pcm *dpcm_play =
482 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
483 struct loopback_pcm *dpcm_capt =
484 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
485 unsigned long delta_play = 0, delta_capt = 0;
486 unsigned int running, count1, count2;
488 running = cable->running ^ cable->pause;
489 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
490 delta_play = jiffies - dpcm_play->last_jiffies;
491 dpcm_play->last_jiffies += delta_play;
494 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
495 delta_capt = jiffies - dpcm_capt->last_jiffies;
496 dpcm_capt->last_jiffies += delta_capt;
499 if (delta_play == 0 && delta_capt == 0)
502 if (delta_play > delta_capt) {
503 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
504 bytepos_finish(dpcm_play, count1);
505 delta_play = delta_capt;
506 } else if (delta_play < delta_capt) {
507 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
508 clear_capture_buf(dpcm_capt, count1);
509 bytepos_finish(dpcm_capt, count1);
510 delta_capt = delta_play;
513 if (delta_play == 0 && delta_capt == 0)
516 /* note delta_capt == delta_play at this moment */
517 count1 = bytepos_delta(dpcm_play, delta_play);
518 count2 = bytepos_delta(dpcm_capt, delta_capt);
519 if (count1 < count2) {
520 dpcm_capt->last_drift = count2 - count1;
522 } else if (count1 > count2) {
523 dpcm_play->last_drift = count1 - count2;
525 copy_play_buf(dpcm_play, dpcm_capt, count1);
526 bytepos_finish(dpcm_play, count1);
527 bytepos_finish(dpcm_capt, count1);
532 static void loopback_timer_function(unsigned long data)
534 struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
537 spin_lock_irqsave(&dpcm->cable->lock, flags);
538 if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) {
539 loopback_timer_start(dpcm);
540 if (dpcm->period_update_pending) {
541 dpcm->period_update_pending = 0;
542 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
543 /* need to unlock before calling below */
544 snd_pcm_period_elapsed(dpcm->substream);
548 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
551 static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
553 struct snd_pcm_runtime *runtime = substream->runtime;
554 struct loopback_pcm *dpcm = runtime->private_data;
555 snd_pcm_uframes_t pos;
557 spin_lock(&dpcm->cable->lock);
558 loopback_pos_update(dpcm->cable);
560 spin_unlock(&dpcm->cable->lock);
561 return bytes_to_frames(runtime, pos);
564 static struct snd_pcm_hardware loopback_pcm_hardware =
566 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
567 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
568 SNDRV_PCM_INFO_RESUME),
569 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
570 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
571 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
572 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
577 .buffer_bytes_max = 2 * 1024 * 1024,
578 .period_bytes_min = 64,
579 /* note check overflow in frac_pos() using pcm_rate_shift before
580 changing period_bytes_max value */
581 .period_bytes_max = 1024 * 1024,
587 static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
589 struct loopback_pcm *dpcm = runtime->private_data;
593 static int loopback_hw_params(struct snd_pcm_substream *substream,
594 struct snd_pcm_hw_params *params)
596 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
597 params_buffer_bytes(params));
600 static int loopback_hw_free(struct snd_pcm_substream *substream)
602 struct snd_pcm_runtime *runtime = substream->runtime;
603 struct loopback_pcm *dpcm = runtime->private_data;
604 struct loopback_cable *cable = dpcm->cable;
606 mutex_lock(&dpcm->loopback->cable_lock);
607 cable->valid &= ~(1 << substream->stream);
608 mutex_unlock(&dpcm->loopback->cable_lock);
609 return snd_pcm_lib_free_vmalloc_buffer(substream);
612 static unsigned int get_cable_index(struct snd_pcm_substream *substream)
614 if (!substream->pcm->device)
615 return substream->stream;
617 return !substream->stream;
620 static int rule_format(struct snd_pcm_hw_params *params,
621 struct snd_pcm_hw_rule *rule)
624 struct snd_pcm_hardware *hw = rule->private;
625 struct snd_mask *maskp = hw_param_mask(params, rule->var);
627 maskp->bits[0] &= (u_int32_t)hw->formats;
628 maskp->bits[1] &= (u_int32_t)(hw->formats >> 32);
629 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
630 if (! maskp->bits[0] && ! maskp->bits[1])
635 static int rule_rate(struct snd_pcm_hw_params *params,
636 struct snd_pcm_hw_rule *rule)
638 struct snd_pcm_hardware *hw = rule->private;
639 struct snd_interval t;
641 t.min = hw->rate_min;
642 t.max = hw->rate_max;
643 t.openmin = t.openmax = 0;
645 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
648 static int rule_channels(struct snd_pcm_hw_params *params,
649 struct snd_pcm_hw_rule *rule)
651 struct snd_pcm_hardware *hw = rule->private;
652 struct snd_interval t;
654 t.min = hw->channels_min;
655 t.max = hw->channels_max;
656 t.openmin = t.openmax = 0;
658 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
661 static int loopback_open(struct snd_pcm_substream *substream)
663 struct snd_pcm_runtime *runtime = substream->runtime;
664 struct loopback *loopback = substream->private_data;
665 struct loopback_pcm *dpcm;
666 struct loopback_cable *cable;
668 int dev = get_cable_index(substream);
670 mutex_lock(&loopback->cable_lock);
671 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
676 dpcm->loopback = loopback;
677 dpcm->substream = substream;
678 setup_timer(&dpcm->timer, loopback_timer_function,
679 (unsigned long)dpcm);
681 cable = loopback->cables[substream->number][dev];
683 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
689 spin_lock_init(&cable->lock);
690 cable->hw = loopback_pcm_hardware;
691 loopback->cables[substream->number][dev] = cable;
694 cable->streams[substream->stream] = dpcm;
696 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
698 /* use dynamic rules based on actual runtime->hw values */
699 /* note that the default rules created in the PCM midlevel code */
700 /* are cached -> they do not reflect the actual state */
701 err = snd_pcm_hw_rule_add(runtime, 0,
702 SNDRV_PCM_HW_PARAM_FORMAT,
703 rule_format, &runtime->hw,
704 SNDRV_PCM_HW_PARAM_FORMAT, -1);
707 err = snd_pcm_hw_rule_add(runtime, 0,
708 SNDRV_PCM_HW_PARAM_RATE,
709 rule_rate, &runtime->hw,
710 SNDRV_PCM_HW_PARAM_RATE, -1);
713 err = snd_pcm_hw_rule_add(runtime, 0,
714 SNDRV_PCM_HW_PARAM_CHANNELS,
715 rule_channels, &runtime->hw,
716 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
720 runtime->private_data = dpcm;
721 runtime->private_free = loopback_runtime_free;
722 if (get_notify(dpcm))
723 runtime->hw = loopback_pcm_hardware;
725 runtime->hw = cable->hw;
727 mutex_unlock(&loopback->cable_lock);
731 static int loopback_close(struct snd_pcm_substream *substream)
733 struct loopback *loopback = substream->private_data;
734 struct loopback_pcm *dpcm = substream->runtime->private_data;
735 struct loopback_cable *cable;
736 int dev = get_cable_index(substream);
738 loopback_timer_stop(dpcm);
739 mutex_lock(&loopback->cable_lock);
740 cable = loopback->cables[substream->number][dev];
741 if (cable->streams[!substream->stream]) {
742 /* other stream is still alive */
743 cable->streams[substream->stream] = NULL;
746 loopback->cables[substream->number][dev] = NULL;
749 mutex_unlock(&loopback->cable_lock);
753 static struct snd_pcm_ops loopback_playback_ops = {
754 .open = loopback_open,
755 .close = loopback_close,
756 .ioctl = snd_pcm_lib_ioctl,
757 .hw_params = loopback_hw_params,
758 .hw_free = loopback_hw_free,
759 .prepare = loopback_prepare,
760 .trigger = loopback_trigger,
761 .pointer = loopback_pointer,
762 .page = snd_pcm_lib_get_vmalloc_page,
763 .mmap = snd_pcm_lib_mmap_vmalloc,
766 static struct snd_pcm_ops loopback_capture_ops = {
767 .open = loopback_open,
768 .close = loopback_close,
769 .ioctl = snd_pcm_lib_ioctl,
770 .hw_params = loopback_hw_params,
771 .hw_free = loopback_hw_free,
772 .prepare = loopback_prepare,
773 .trigger = loopback_trigger,
774 .pointer = loopback_pointer,
775 .page = snd_pcm_lib_get_vmalloc_page,
776 .mmap = snd_pcm_lib_mmap_vmalloc,
779 static int loopback_pcm_new(struct loopback *loopback,
780 int device, int substreams)
785 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
786 substreams, substreams, &pcm);
789 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
790 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
792 pcm->private_data = loopback;
794 strcpy(pcm->name, "Loopback PCM");
796 loopback->pcm[device] = pcm;
800 static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
801 struct snd_ctl_elem_info *uinfo)
803 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
805 uinfo->value.integer.min = 80000;
806 uinfo->value.integer.max = 120000;
807 uinfo->value.integer.step = 1;
811 static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
812 struct snd_ctl_elem_value *ucontrol)
814 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
816 ucontrol->value.integer.value[0] =
817 loopback->setup[kcontrol->id.subdevice]
818 [kcontrol->id.device].rate_shift;
822 static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
823 struct snd_ctl_elem_value *ucontrol)
825 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
829 val = ucontrol->value.integer.value[0];
834 mutex_lock(&loopback->cable_lock);
835 if (val != loopback->setup[kcontrol->id.subdevice]
836 [kcontrol->id.device].rate_shift) {
837 loopback->setup[kcontrol->id.subdevice]
838 [kcontrol->id.device].rate_shift = val;
841 mutex_unlock(&loopback->cable_lock);
845 static int loopback_notify_get(struct snd_kcontrol *kcontrol,
846 struct snd_ctl_elem_value *ucontrol)
848 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
850 ucontrol->value.integer.value[0] =
851 loopback->setup[kcontrol->id.subdevice]
852 [kcontrol->id.device].notify;
856 static int loopback_notify_put(struct snd_kcontrol *kcontrol,
857 struct snd_ctl_elem_value *ucontrol)
859 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
863 val = ucontrol->value.integer.value[0] ? 1 : 0;
864 if (val != loopback->setup[kcontrol->id.subdevice]
865 [kcontrol->id.device].notify) {
866 loopback->setup[kcontrol->id.subdevice]
867 [kcontrol->id.device].notify = val;
873 static int loopback_active_get(struct snd_kcontrol *kcontrol,
874 struct snd_ctl_elem_value *ucontrol)
876 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
877 struct loopback_cable *cable = loopback->cables
878 [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
879 unsigned int val = 0;
882 val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
884 ucontrol->value.integer.value[0] = val;
888 static int loopback_format_info(struct snd_kcontrol *kcontrol,
889 struct snd_ctl_elem_info *uinfo)
891 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
893 uinfo->value.integer.min = 0;
894 uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
895 uinfo->value.integer.step = 1;
899 static int loopback_format_get(struct snd_kcontrol *kcontrol,
900 struct snd_ctl_elem_value *ucontrol)
902 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
904 ucontrol->value.integer.value[0] =
905 loopback->setup[kcontrol->id.subdevice]
906 [kcontrol->id.device].format;
910 static int loopback_rate_info(struct snd_kcontrol *kcontrol,
911 struct snd_ctl_elem_info *uinfo)
913 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
915 uinfo->value.integer.min = 0;
916 uinfo->value.integer.max = 192000;
917 uinfo->value.integer.step = 1;
921 static int loopback_rate_get(struct snd_kcontrol *kcontrol,
922 struct snd_ctl_elem_value *ucontrol)
924 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
926 ucontrol->value.integer.value[0] =
927 loopback->setup[kcontrol->id.subdevice]
928 [kcontrol->id.device].rate;
932 static int loopback_channels_info(struct snd_kcontrol *kcontrol,
933 struct snd_ctl_elem_info *uinfo)
935 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
937 uinfo->value.integer.min = 1;
938 uinfo->value.integer.max = 1024;
939 uinfo->value.integer.step = 1;
943 static int loopback_channels_get(struct snd_kcontrol *kcontrol,
944 struct snd_ctl_elem_value *ucontrol)
946 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
948 ucontrol->value.integer.value[0] =
949 loopback->setup[kcontrol->id.subdevice]
950 [kcontrol->id.device].channels;
954 static struct snd_kcontrol_new loopback_controls[] = {
956 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
957 .name = "PCM Rate Shift 100000",
958 .info = loopback_rate_shift_info,
959 .get = loopback_rate_shift_get,
960 .put = loopback_rate_shift_put,
963 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
964 .name = "PCM Notify",
965 .info = snd_ctl_boolean_mono_info,
966 .get = loopback_notify_get,
967 .put = loopback_notify_put,
971 .access = SNDRV_CTL_ELEM_ACCESS_READ,
972 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
973 .name = "PCM Slave Active",
974 .info = snd_ctl_boolean_mono_info,
975 .get = loopback_active_get,
979 .access = SNDRV_CTL_ELEM_ACCESS_READ,
980 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
981 .name = "PCM Slave Format",
982 .info = loopback_format_info,
983 .get = loopback_format_get
987 .access = SNDRV_CTL_ELEM_ACCESS_READ,
988 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
989 .name = "PCM Slave Rate",
990 .info = loopback_rate_info,
991 .get = loopback_rate_get
993 #define CHANNELS_IDX 5
995 .access = SNDRV_CTL_ELEM_ACCESS_READ,
996 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
997 .name = "PCM Slave Channels",
998 .info = loopback_channels_info,
999 .get = loopback_channels_get
1003 static int loopback_mixer_new(struct loopback *loopback, int notify)
1005 struct snd_card *card = loopback->card;
1006 struct snd_pcm *pcm;
1007 struct snd_kcontrol *kctl;
1008 struct loopback_setup *setup;
1009 int err, dev, substr, substr_count, idx;
1011 strcpy(card->mixername, "Loopback Mixer");
1012 for (dev = 0; dev < 2; dev++) {
1013 pcm = loopback->pcm[dev];
1015 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1016 for (substr = 0; substr < substr_count; substr++) {
1017 setup = &loopback->setup[substr][dev];
1018 setup->notify = notify;
1019 setup->rate_shift = NO_PITCH;
1020 setup->format = SNDRV_PCM_FORMAT_S16_LE;
1021 setup->rate = 48000;
1022 setup->channels = 2;
1023 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1025 kctl = snd_ctl_new1(&loopback_controls[idx],
1029 kctl->id.device = dev;
1030 kctl->id.subdevice = substr;
1033 setup->active_id = kctl->id;
1036 setup->format_id = kctl->id;
1039 setup->rate_id = kctl->id;
1042 setup->channels_id = kctl->id;
1047 err = snd_ctl_add(card, kctl);
1056 static void print_dpcm_info(struct snd_info_buffer *buffer,
1057 struct loopback_pcm *dpcm,
1060 snd_iprintf(buffer, " %s\n", id);
1062 snd_iprintf(buffer, " inactive\n");
1065 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1066 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1067 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1068 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1069 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1070 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1071 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1072 snd_iprintf(buffer, " update_pending:\t%u\n",
1073 dpcm->period_update_pending);
1074 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
1075 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
1076 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
1077 dpcm->last_jiffies, jiffies);
1078 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
1081 static void print_substream_info(struct snd_info_buffer *buffer,
1082 struct loopback *loopback,
1086 struct loopback_cable *cable = loopback->cables[sub][num];
1088 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1089 if (cable == NULL) {
1090 snd_iprintf(buffer, " inactive\n");
1093 snd_iprintf(buffer, " valid: %u\n", cable->valid);
1094 snd_iprintf(buffer, " running: %u\n", cable->running);
1095 snd_iprintf(buffer, " pause: %u\n", cable->pause);
1096 print_dpcm_info(buffer, cable->streams[0], "Playback");
1097 print_dpcm_info(buffer, cable->streams[1], "Capture");
1100 static void print_cable_info(struct snd_info_entry *entry,
1101 struct snd_info_buffer *buffer)
1103 struct loopback *loopback = entry->private_data;
1106 mutex_lock(&loopback->cable_lock);
1107 num = entry->name[strlen(entry->name)-1];
1108 num = num == '0' ? 0 : 1;
1109 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1110 print_substream_info(buffer, loopback, sub, num);
1111 mutex_unlock(&loopback->cable_lock);
1114 static int loopback_proc_new(struct loopback *loopback, int cidx)
1117 struct snd_info_entry *entry;
1120 snprintf(name, sizeof(name), "cable#%d", cidx);
1121 err = snd_card_proc_new(loopback->card, name, &entry);
1125 snd_info_set_text_ops(entry, loopback, print_cable_info);
1129 static int loopback_probe(struct platform_device *devptr)
1131 struct snd_card *card;
1132 struct loopback *loopback;
1133 int dev = devptr->id;
1136 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1137 sizeof(struct loopback), &card);
1140 loopback = card->private_data;
1142 if (pcm_substreams[dev] < 1)
1143 pcm_substreams[dev] = 1;
1144 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1145 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1147 loopback->card = card;
1148 mutex_init(&loopback->cable_lock);
1150 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1153 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1156 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1159 loopback_proc_new(loopback, 0);
1160 loopback_proc_new(loopback, 1);
1161 strcpy(card->driver, "Loopback");
1162 strcpy(card->shortname, "Loopback");
1163 sprintf(card->longname, "Loopback %i", dev + 1);
1164 err = snd_card_register(card);
1166 platform_set_drvdata(devptr, card);
1170 snd_card_free(card);
1174 static int loopback_remove(struct platform_device *devptr)
1176 snd_card_free(platform_get_drvdata(devptr));
1180 #ifdef CONFIG_PM_SLEEP
1181 static int loopback_suspend(struct device *pdev)
1183 struct snd_card *card = dev_get_drvdata(pdev);
1184 struct loopback *loopback = card->private_data;
1186 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1188 snd_pcm_suspend_all(loopback->pcm[0]);
1189 snd_pcm_suspend_all(loopback->pcm[1]);
1193 static int loopback_resume(struct device *pdev)
1195 struct snd_card *card = dev_get_drvdata(pdev);
1197 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1201 static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1202 #define LOOPBACK_PM_OPS &loopback_pm
1204 #define LOOPBACK_PM_OPS NULL
1207 #define SND_LOOPBACK_DRIVER "snd_aloop"
1209 static struct platform_driver loopback_driver = {
1210 .probe = loopback_probe,
1211 .remove = loopback_remove,
1213 .name = SND_LOOPBACK_DRIVER,
1214 .pm = LOOPBACK_PM_OPS,
1218 static void loopback_unregister_all(void)
1222 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1223 platform_device_unregister(devices[i]);
1224 platform_driver_unregister(&loopback_driver);
1227 static int __init alsa_card_loopback_init(void)
1231 err = platform_driver_register(&loopback_driver);
1237 for (i = 0; i < SNDRV_CARDS; i++) {
1238 struct platform_device *device;
1241 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1245 if (!platform_get_drvdata(device)) {
1246 platform_device_unregister(device);
1249 devices[i] = device;
1254 printk(KERN_ERR "aloop: No loopback enabled\n");
1256 loopback_unregister_all();
1262 static void __exit alsa_card_loopback_exit(void)
1264 loopback_unregister_all();
1267 module_init(alsa_card_loopback_init)
1268 module_exit(alsa_card_loopback_exit)