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 dpcm->timer.expires = jiffies + tick;
185 add_timer(&dpcm->timer);
188 /* call in cable->lock */
189 static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
191 del_timer(&dpcm->timer);
192 dpcm->timer.expires = 0;
195 #define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
196 #define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
197 #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
199 static int loopback_check_format(struct loopback_cable *cable, int stream)
201 struct snd_pcm_runtime *runtime, *cruntime;
202 struct loopback_setup *setup;
203 struct snd_card *card;
206 if (cable->valid != CABLE_VALID_BOTH) {
207 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
211 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
213 cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
215 check = runtime->format != cruntime->format ||
216 runtime->rate != cruntime->rate ||
217 runtime->channels != cruntime->channels;
220 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
223 snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
224 substream, SNDRV_PCM_STATE_DRAINING);
226 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
228 setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
229 card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
230 if (setup->format != runtime->format) {
231 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
233 setup->format = runtime->format;
235 if (setup->rate != runtime->rate) {
236 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
238 setup->rate = runtime->rate;
240 if (setup->channels != runtime->channels) {
241 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
242 &setup->channels_id);
243 setup->channels = runtime->channels;
249 static void loopback_active_notify(struct loopback_pcm *dpcm)
251 snd_ctl_notify(dpcm->loopback->card,
252 SNDRV_CTL_EVENT_MASK_VALUE,
253 &get_setup(dpcm)->active_id);
256 static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
258 struct snd_pcm_runtime *runtime = substream->runtime;
259 struct loopback_pcm *dpcm = runtime->private_data;
260 struct loopback_cable *cable = dpcm->cable;
261 int err, stream = 1 << substream->stream;
264 case SNDRV_PCM_TRIGGER_START:
265 err = loopback_check_format(cable, substream->stream);
268 dpcm->last_jiffies = jiffies;
269 dpcm->pcm_rate_shift = 0;
270 dpcm->last_drift = 0;
271 spin_lock(&cable->lock);
272 cable->running |= stream;
273 cable->pause &= ~stream;
274 loopback_timer_start(dpcm);
275 spin_unlock(&cable->lock);
276 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
277 loopback_active_notify(dpcm);
279 case SNDRV_PCM_TRIGGER_STOP:
280 spin_lock(&cable->lock);
281 cable->running &= ~stream;
282 cable->pause &= ~stream;
283 loopback_timer_stop(dpcm);
284 spin_unlock(&cable->lock);
285 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
286 loopback_active_notify(dpcm);
288 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
289 case SNDRV_PCM_TRIGGER_SUSPEND:
290 spin_lock(&cable->lock);
291 cable->pause |= stream;
292 loopback_timer_stop(dpcm);
293 spin_unlock(&cable->lock);
295 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
296 case SNDRV_PCM_TRIGGER_RESUME:
297 spin_lock(&cable->lock);
298 dpcm->last_jiffies = jiffies;
299 cable->pause &= ~stream;
300 loopback_timer_start(dpcm);
301 spin_unlock(&cable->lock);
309 static void params_change_substream(struct loopback_pcm *dpcm,
310 struct snd_pcm_runtime *runtime)
312 struct snd_pcm_runtime *dst_runtime;
314 if (dpcm == NULL || dpcm->substream == NULL)
316 dst_runtime = dpcm->substream->runtime;
317 if (dst_runtime == NULL)
319 dst_runtime->hw = dpcm->cable->hw;
322 static void params_change(struct snd_pcm_substream *substream)
324 struct snd_pcm_runtime *runtime = substream->runtime;
325 struct loopback_pcm *dpcm = runtime->private_data;
326 struct loopback_cable *cable = dpcm->cable;
328 cable->hw.formats = pcm_format_to_bits(runtime->format);
329 cable->hw.rate_min = runtime->rate;
330 cable->hw.rate_max = runtime->rate;
331 cable->hw.channels_min = runtime->channels;
332 cable->hw.channels_max = runtime->channels;
333 params_change_substream(cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
335 params_change_substream(cable->streams[SNDRV_PCM_STREAM_CAPTURE],
339 static int loopback_prepare(struct snd_pcm_substream *substream)
341 struct snd_pcm_runtime *runtime = substream->runtime;
342 struct loopback_pcm *dpcm = runtime->private_data;
343 struct loopback_cable *cable = dpcm->cable;
346 salign = (snd_pcm_format_width(runtime->format) *
347 runtime->channels) / 8;
348 bps = salign * runtime->rate;
349 if (bps <= 0 || salign <= 0)
353 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
354 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
355 /* clear capture buffer */
356 dpcm->silent_size = dpcm->pcm_buffer_size;
357 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
358 runtime->buffer_size * runtime->channels);
362 dpcm->period_update_pending = 0;
364 dpcm->pcm_salign = salign;
365 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
367 mutex_lock(&dpcm->loopback->cable_lock);
368 if (!(cable->valid & ~(1 << substream->stream)) ||
369 (get_setup(dpcm)->notify &&
370 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
371 params_change(substream);
372 cable->valid |= 1 << substream->stream;
373 mutex_unlock(&dpcm->loopback->cable_lock);
378 static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
380 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
381 char *dst = runtime->dma_area;
382 unsigned int dst_off = dpcm->buf_pos;
384 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
386 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
387 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
390 unsigned int size = bytes;
391 if (dst_off + size > dpcm->pcm_buffer_size)
392 size = dpcm->pcm_buffer_size - dst_off;
393 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
394 bytes_to_frames(runtime, size) *
396 dpcm->silent_size += size;
404 static void copy_play_buf(struct loopback_pcm *play,
405 struct loopback_pcm *capt,
408 struct snd_pcm_runtime *runtime = play->substream->runtime;
409 char *src = runtime->dma_area;
410 char *dst = capt->substream->runtime->dma_area;
411 unsigned int src_off = play->buf_pos;
412 unsigned int dst_off = capt->buf_pos;
413 unsigned int clear_bytes = 0;
415 /* check if playback is draining, trim the capture copy size
416 * when our pointer is at the end of playback ring buffer */
417 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
418 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
419 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
420 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
421 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
422 appl_ptr1 += play->buf_pos / play->pcm_salign;
423 if (appl_ptr < appl_ptr1)
424 appl_ptr1 -= runtime->buffer_size;
425 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
427 clear_bytes = bytes - diff;
433 unsigned int size = bytes;
434 if (src_off + size > play->pcm_buffer_size)
435 size = play->pcm_buffer_size - src_off;
436 if (dst_off + size > capt->pcm_buffer_size)
437 size = capt->pcm_buffer_size - dst_off;
438 memcpy(dst + dst_off, src + src_off, size);
439 capt->silent_size = 0;
443 src_off = (src_off + size) % play->pcm_buffer_size;
444 dst_off = (dst_off + size) % capt->pcm_buffer_size;
447 if (clear_bytes > 0) {
448 clear_capture_buf(capt, clear_bytes);
449 capt->silent_size = 0;
453 static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
454 unsigned int jiffies_delta)
456 unsigned long last_pos;
459 last_pos = byte_pos(dpcm, dpcm->irq_pos);
460 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
461 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
462 if (delta >= dpcm->last_drift)
463 delta -= dpcm->last_drift;
464 dpcm->last_drift = 0;
465 if (dpcm->irq_pos >= dpcm->period_size_frac) {
466 dpcm->irq_pos %= dpcm->period_size_frac;
467 dpcm->period_update_pending = 1;
472 static inline void bytepos_finish(struct loopback_pcm *dpcm,
475 dpcm->buf_pos += delta;
476 dpcm->buf_pos %= dpcm->pcm_buffer_size;
479 /* call in cable->lock */
480 static unsigned int loopback_pos_update(struct loopback_cable *cable)
482 struct loopback_pcm *dpcm_play =
483 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
484 struct loopback_pcm *dpcm_capt =
485 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
486 unsigned long delta_play = 0, delta_capt = 0;
487 unsigned int running, count1, count2;
489 running = cable->running ^ cable->pause;
490 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
491 delta_play = jiffies - dpcm_play->last_jiffies;
492 dpcm_play->last_jiffies += delta_play;
495 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
496 delta_capt = jiffies - dpcm_capt->last_jiffies;
497 dpcm_capt->last_jiffies += delta_capt;
500 if (delta_play == 0 && delta_capt == 0)
503 if (delta_play > delta_capt) {
504 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
505 bytepos_finish(dpcm_play, count1);
506 delta_play = delta_capt;
507 } else if (delta_play < delta_capt) {
508 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
509 clear_capture_buf(dpcm_capt, count1);
510 bytepos_finish(dpcm_capt, count1);
511 delta_capt = delta_play;
514 if (delta_play == 0 && delta_capt == 0)
517 /* note delta_capt == delta_play at this moment */
518 count1 = bytepos_delta(dpcm_play, delta_play);
519 count2 = bytepos_delta(dpcm_capt, delta_capt);
520 if (count1 < count2) {
521 dpcm_capt->last_drift = count2 - count1;
523 } else if (count1 > count2) {
524 dpcm_play->last_drift = count1 - count2;
526 copy_play_buf(dpcm_play, dpcm_capt, count1);
527 bytepos_finish(dpcm_play, count1);
528 bytepos_finish(dpcm_capt, count1);
533 static void loopback_timer_function(unsigned long data)
535 struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
538 spin_lock_irqsave(&dpcm->cable->lock, flags);
539 if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) {
540 loopback_timer_start(dpcm);
541 if (dpcm->period_update_pending) {
542 dpcm->period_update_pending = 0;
543 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
544 /* need to unlock before calling below */
545 snd_pcm_period_elapsed(dpcm->substream);
549 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
552 static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
554 struct snd_pcm_runtime *runtime = substream->runtime;
555 struct loopback_pcm *dpcm = runtime->private_data;
556 snd_pcm_uframes_t pos;
558 spin_lock(&dpcm->cable->lock);
559 loopback_pos_update(dpcm->cable);
561 spin_unlock(&dpcm->cable->lock);
562 return bytes_to_frames(runtime, pos);
565 static struct snd_pcm_hardware loopback_pcm_hardware =
567 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
568 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
569 SNDRV_PCM_INFO_RESUME),
570 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
571 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
572 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
573 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
578 .buffer_bytes_max = 2 * 1024 * 1024,
579 .period_bytes_min = 64,
580 /* note check overflow in frac_pos() using pcm_rate_shift before
581 changing period_bytes_max value */
582 .period_bytes_max = 1024 * 1024,
588 static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
590 struct loopback_pcm *dpcm = runtime->private_data;
594 static int loopback_hw_params(struct snd_pcm_substream *substream,
595 struct snd_pcm_hw_params *params)
597 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
598 params_buffer_bytes(params));
601 static int loopback_hw_free(struct snd_pcm_substream *substream)
603 struct snd_pcm_runtime *runtime = substream->runtime;
604 struct loopback_pcm *dpcm = runtime->private_data;
605 struct loopback_cable *cable = dpcm->cable;
607 mutex_lock(&dpcm->loopback->cable_lock);
608 cable->valid &= ~(1 << substream->stream);
609 mutex_unlock(&dpcm->loopback->cable_lock);
610 return snd_pcm_lib_free_vmalloc_buffer(substream);
613 static unsigned int get_cable_index(struct snd_pcm_substream *substream)
615 if (!substream->pcm->device)
616 return substream->stream;
618 return !substream->stream;
621 static int rule_format(struct snd_pcm_hw_params *params,
622 struct snd_pcm_hw_rule *rule)
625 struct snd_pcm_hardware *hw = rule->private;
626 struct snd_mask *maskp = hw_param_mask(params, rule->var);
628 maskp->bits[0] &= (u_int32_t)hw->formats;
629 maskp->bits[1] &= (u_int32_t)(hw->formats >> 32);
630 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
631 if (! maskp->bits[0] && ! maskp->bits[1])
636 static int rule_rate(struct snd_pcm_hw_params *params,
637 struct snd_pcm_hw_rule *rule)
639 struct snd_pcm_hardware *hw = rule->private;
640 struct snd_interval t;
642 t.min = hw->rate_min;
643 t.max = hw->rate_max;
644 t.openmin = t.openmax = 0;
646 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
649 static int rule_channels(struct snd_pcm_hw_params *params,
650 struct snd_pcm_hw_rule *rule)
652 struct snd_pcm_hardware *hw = rule->private;
653 struct snd_interval t;
655 t.min = hw->channels_min;
656 t.max = hw->channels_max;
657 t.openmin = t.openmax = 0;
659 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
662 static int loopback_open(struct snd_pcm_substream *substream)
664 struct snd_pcm_runtime *runtime = substream->runtime;
665 struct loopback *loopback = substream->private_data;
666 struct loopback_pcm *dpcm;
667 struct loopback_cable *cable;
669 int dev = get_cable_index(substream);
671 mutex_lock(&loopback->cable_lock);
672 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
677 dpcm->loopback = loopback;
678 dpcm->substream = substream;
679 setup_timer(&dpcm->timer, loopback_timer_function,
680 (unsigned long)dpcm);
682 cable = loopback->cables[substream->number][dev];
684 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
690 spin_lock_init(&cable->lock);
691 cable->hw = loopback_pcm_hardware;
692 loopback->cables[substream->number][dev] = cable;
695 cable->streams[substream->stream] = dpcm;
697 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
699 /* use dynamic rules based on actual runtime->hw values */
700 /* note that the default rules created in the PCM midlevel code */
701 /* are cached -> they do not reflect the actual state */
702 err = snd_pcm_hw_rule_add(runtime, 0,
703 SNDRV_PCM_HW_PARAM_FORMAT,
704 rule_format, &runtime->hw,
705 SNDRV_PCM_HW_PARAM_FORMAT, -1);
708 err = snd_pcm_hw_rule_add(runtime, 0,
709 SNDRV_PCM_HW_PARAM_RATE,
710 rule_rate, &runtime->hw,
711 SNDRV_PCM_HW_PARAM_RATE, -1);
714 err = snd_pcm_hw_rule_add(runtime, 0,
715 SNDRV_PCM_HW_PARAM_CHANNELS,
716 rule_channels, &runtime->hw,
717 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
721 runtime->private_data = dpcm;
722 runtime->private_free = loopback_runtime_free;
723 if (get_notify(dpcm))
724 runtime->hw = loopback_pcm_hardware;
726 runtime->hw = cable->hw;
728 mutex_unlock(&loopback->cable_lock);
732 static int loopback_close(struct snd_pcm_substream *substream)
734 struct loopback *loopback = substream->private_data;
735 struct loopback_pcm *dpcm = substream->runtime->private_data;
736 struct loopback_cable *cable;
737 int dev = get_cable_index(substream);
739 loopback_timer_stop(dpcm);
740 mutex_lock(&loopback->cable_lock);
741 cable = loopback->cables[substream->number][dev];
742 if (cable->streams[!substream->stream]) {
743 /* other stream is still alive */
744 cable->streams[substream->stream] = NULL;
747 loopback->cables[substream->number][dev] = NULL;
750 mutex_unlock(&loopback->cable_lock);
754 static struct snd_pcm_ops loopback_playback_ops = {
755 .open = loopback_open,
756 .close = loopback_close,
757 .ioctl = snd_pcm_lib_ioctl,
758 .hw_params = loopback_hw_params,
759 .hw_free = loopback_hw_free,
760 .prepare = loopback_prepare,
761 .trigger = loopback_trigger,
762 .pointer = loopback_pointer,
763 .page = snd_pcm_lib_get_vmalloc_page,
764 .mmap = snd_pcm_lib_mmap_vmalloc,
767 static struct snd_pcm_ops loopback_capture_ops = {
768 .open = loopback_open,
769 .close = loopback_close,
770 .ioctl = snd_pcm_lib_ioctl,
771 .hw_params = loopback_hw_params,
772 .hw_free = loopback_hw_free,
773 .prepare = loopback_prepare,
774 .trigger = loopback_trigger,
775 .pointer = loopback_pointer,
776 .page = snd_pcm_lib_get_vmalloc_page,
777 .mmap = snd_pcm_lib_mmap_vmalloc,
780 static int loopback_pcm_new(struct loopback *loopback,
781 int device, int substreams)
786 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
787 substreams, substreams, &pcm);
790 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
791 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
793 pcm->private_data = loopback;
795 strcpy(pcm->name, "Loopback PCM");
797 loopback->pcm[device] = pcm;
801 static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
802 struct snd_ctl_elem_info *uinfo)
804 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
806 uinfo->value.integer.min = 80000;
807 uinfo->value.integer.max = 120000;
808 uinfo->value.integer.step = 1;
812 static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
813 struct snd_ctl_elem_value *ucontrol)
815 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
817 ucontrol->value.integer.value[0] =
818 loopback->setup[kcontrol->id.subdevice]
819 [kcontrol->id.device].rate_shift;
823 static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
824 struct snd_ctl_elem_value *ucontrol)
826 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
830 val = ucontrol->value.integer.value[0];
835 mutex_lock(&loopback->cable_lock);
836 if (val != loopback->setup[kcontrol->id.subdevice]
837 [kcontrol->id.device].rate_shift) {
838 loopback->setup[kcontrol->id.subdevice]
839 [kcontrol->id.device].rate_shift = val;
842 mutex_unlock(&loopback->cable_lock);
846 static int loopback_notify_get(struct snd_kcontrol *kcontrol,
847 struct snd_ctl_elem_value *ucontrol)
849 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
851 ucontrol->value.integer.value[0] =
852 loopback->setup[kcontrol->id.subdevice]
853 [kcontrol->id.device].notify;
857 static int loopback_notify_put(struct snd_kcontrol *kcontrol,
858 struct snd_ctl_elem_value *ucontrol)
860 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
864 val = ucontrol->value.integer.value[0] ? 1 : 0;
865 if (val != loopback->setup[kcontrol->id.subdevice]
866 [kcontrol->id.device].notify) {
867 loopback->setup[kcontrol->id.subdevice]
868 [kcontrol->id.device].notify = val;
874 static int loopback_active_get(struct snd_kcontrol *kcontrol,
875 struct snd_ctl_elem_value *ucontrol)
877 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
878 struct loopback_cable *cable = loopback->cables
879 [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
880 unsigned int val = 0;
883 val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
885 ucontrol->value.integer.value[0] = val;
889 static int loopback_format_info(struct snd_kcontrol *kcontrol,
890 struct snd_ctl_elem_info *uinfo)
892 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
894 uinfo->value.integer.min = 0;
895 uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
896 uinfo->value.integer.step = 1;
900 static int loopback_format_get(struct snd_kcontrol *kcontrol,
901 struct snd_ctl_elem_value *ucontrol)
903 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
905 ucontrol->value.integer.value[0] =
906 loopback->setup[kcontrol->id.subdevice]
907 [kcontrol->id.device].format;
911 static int loopback_rate_info(struct snd_kcontrol *kcontrol,
912 struct snd_ctl_elem_info *uinfo)
914 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
916 uinfo->value.integer.min = 0;
917 uinfo->value.integer.max = 192000;
918 uinfo->value.integer.step = 1;
922 static int loopback_rate_get(struct snd_kcontrol *kcontrol,
923 struct snd_ctl_elem_value *ucontrol)
925 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
927 ucontrol->value.integer.value[0] =
928 loopback->setup[kcontrol->id.subdevice]
929 [kcontrol->id.device].rate;
933 static int loopback_channels_info(struct snd_kcontrol *kcontrol,
934 struct snd_ctl_elem_info *uinfo)
936 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
938 uinfo->value.integer.min = 1;
939 uinfo->value.integer.max = 1024;
940 uinfo->value.integer.step = 1;
944 static int loopback_channels_get(struct snd_kcontrol *kcontrol,
945 struct snd_ctl_elem_value *ucontrol)
947 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
949 ucontrol->value.integer.value[0] =
950 loopback->setup[kcontrol->id.subdevice]
951 [kcontrol->id.device].channels;
955 static struct snd_kcontrol_new loopback_controls[] = {
957 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
958 .name = "PCM Rate Shift 100000",
959 .info = loopback_rate_shift_info,
960 .get = loopback_rate_shift_get,
961 .put = loopback_rate_shift_put,
964 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
965 .name = "PCM Notify",
966 .info = snd_ctl_boolean_mono_info,
967 .get = loopback_notify_get,
968 .put = loopback_notify_put,
972 .access = SNDRV_CTL_ELEM_ACCESS_READ,
973 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
974 .name = "PCM Slave Active",
975 .info = snd_ctl_boolean_mono_info,
976 .get = loopback_active_get,
980 .access = SNDRV_CTL_ELEM_ACCESS_READ,
981 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
982 .name = "PCM Slave Format",
983 .info = loopback_format_info,
984 .get = loopback_format_get
988 .access = SNDRV_CTL_ELEM_ACCESS_READ,
989 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
990 .name = "PCM Slave Rate",
991 .info = loopback_rate_info,
992 .get = loopback_rate_get
994 #define CHANNELS_IDX 5
996 .access = SNDRV_CTL_ELEM_ACCESS_READ,
997 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
998 .name = "PCM Slave Channels",
999 .info = loopback_channels_info,
1000 .get = loopback_channels_get
1004 static int loopback_mixer_new(struct loopback *loopback, int notify)
1006 struct snd_card *card = loopback->card;
1007 struct snd_pcm *pcm;
1008 struct snd_kcontrol *kctl;
1009 struct loopback_setup *setup;
1010 int err, dev, substr, substr_count, idx;
1012 strcpy(card->mixername, "Loopback Mixer");
1013 for (dev = 0; dev < 2; dev++) {
1014 pcm = loopback->pcm[dev];
1016 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1017 for (substr = 0; substr < substr_count; substr++) {
1018 setup = &loopback->setup[substr][dev];
1019 setup->notify = notify;
1020 setup->rate_shift = NO_PITCH;
1021 setup->format = SNDRV_PCM_FORMAT_S16_LE;
1022 setup->rate = 48000;
1023 setup->channels = 2;
1024 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1026 kctl = snd_ctl_new1(&loopback_controls[idx],
1030 kctl->id.device = dev;
1031 kctl->id.subdevice = substr;
1034 setup->active_id = kctl->id;
1037 setup->format_id = kctl->id;
1040 setup->rate_id = kctl->id;
1043 setup->channels_id = kctl->id;
1048 err = snd_ctl_add(card, kctl);
1057 #ifdef CONFIG_PROC_FS
1059 static void print_dpcm_info(struct snd_info_buffer *buffer,
1060 struct loopback_pcm *dpcm,
1063 snd_iprintf(buffer, " %s\n", id);
1065 snd_iprintf(buffer, " inactive\n");
1068 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1069 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1070 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1071 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1072 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1073 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1074 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1075 snd_iprintf(buffer, " update_pending:\t%u\n",
1076 dpcm->period_update_pending);
1077 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
1078 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
1079 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
1080 dpcm->last_jiffies, jiffies);
1081 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
1084 static void print_substream_info(struct snd_info_buffer *buffer,
1085 struct loopback *loopback,
1089 struct loopback_cable *cable = loopback->cables[sub][num];
1091 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1092 if (cable == NULL) {
1093 snd_iprintf(buffer, " inactive\n");
1096 snd_iprintf(buffer, " valid: %u\n", cable->valid);
1097 snd_iprintf(buffer, " running: %u\n", cable->running);
1098 snd_iprintf(buffer, " pause: %u\n", cable->pause);
1099 print_dpcm_info(buffer, cable->streams[0], "Playback");
1100 print_dpcm_info(buffer, cable->streams[1], "Capture");
1103 static void print_cable_info(struct snd_info_entry *entry,
1104 struct snd_info_buffer *buffer)
1106 struct loopback *loopback = entry->private_data;
1109 mutex_lock(&loopback->cable_lock);
1110 num = entry->name[strlen(entry->name)-1];
1111 num = num == '0' ? 0 : 1;
1112 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1113 print_substream_info(buffer, loopback, sub, num);
1114 mutex_unlock(&loopback->cable_lock);
1117 static int loopback_proc_new(struct loopback *loopback, int cidx)
1120 struct snd_info_entry *entry;
1123 snprintf(name, sizeof(name), "cable#%d", cidx);
1124 err = snd_card_proc_new(loopback->card, name, &entry);
1128 snd_info_set_text_ops(entry, loopback, print_cable_info);
1132 #else /* !CONFIG_PROC_FS */
1134 #define loopback_proc_new(loopback, cidx) do { } while (0)
1138 static int loopback_probe(struct platform_device *devptr)
1140 struct snd_card *card;
1141 struct loopback *loopback;
1142 int dev = devptr->id;
1145 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1146 sizeof(struct loopback), &card);
1149 loopback = card->private_data;
1151 if (pcm_substreams[dev] < 1)
1152 pcm_substreams[dev] = 1;
1153 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1154 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1156 loopback->card = card;
1157 mutex_init(&loopback->cable_lock);
1159 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1162 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1165 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1168 loopback_proc_new(loopback, 0);
1169 loopback_proc_new(loopback, 1);
1170 strcpy(card->driver, "Loopback");
1171 strcpy(card->shortname, "Loopback");
1172 sprintf(card->longname, "Loopback %i", dev + 1);
1173 err = snd_card_register(card);
1175 platform_set_drvdata(devptr, card);
1179 snd_card_free(card);
1183 static int loopback_remove(struct platform_device *devptr)
1185 snd_card_free(platform_get_drvdata(devptr));
1189 #ifdef CONFIG_PM_SLEEP
1190 static int loopback_suspend(struct device *pdev)
1192 struct snd_card *card = dev_get_drvdata(pdev);
1193 struct loopback *loopback = card->private_data;
1195 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1197 snd_pcm_suspend_all(loopback->pcm[0]);
1198 snd_pcm_suspend_all(loopback->pcm[1]);
1202 static int loopback_resume(struct device *pdev)
1204 struct snd_card *card = dev_get_drvdata(pdev);
1206 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1210 static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1211 #define LOOPBACK_PM_OPS &loopback_pm
1213 #define LOOPBACK_PM_OPS NULL
1216 #define SND_LOOPBACK_DRIVER "snd_aloop"
1218 static struct platform_driver loopback_driver = {
1219 .probe = loopback_probe,
1220 .remove = loopback_remove,
1222 .name = SND_LOOPBACK_DRIVER,
1223 .pm = LOOPBACK_PM_OPS,
1227 static void loopback_unregister_all(void)
1231 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1232 platform_device_unregister(devices[i]);
1233 platform_driver_unregister(&loopback_driver);
1236 static int __init alsa_card_loopback_init(void)
1240 err = platform_driver_register(&loopback_driver);
1246 for (i = 0; i < SNDRV_CARDS; i++) {
1247 struct platform_device *device;
1250 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1254 if (!platform_get_drvdata(device)) {
1255 platform_device_unregister(device);
1258 devices[i] = device;
1263 printk(KERN_ERR "aloop: No loopback enabled\n");
1265 loopback_unregister_all();
1271 static void __exit alsa_card_loopback_exit(void)
1273 loopback_unregister_all();
1276 module_init(alsa_card_loopback_init)
1277 module_exit(alsa_card_loopback_exit)