1 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
8 * More accurate positioning and full-duplex support:
9 * Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de>
11 * Major (almost complete) rewrite:
12 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
14 * A next major update in 2010 (separate timers for playback and capture):
15 * Copyright (c) Jaroslav Kysela <perex@perex.cz>
18 #include <linux/init.h>
19 #include <linux/jiffies.h>
20 #include <linux/slab.h>
21 #include <linux/time.h>
22 #include <linux/wait.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <sound/core.h>
26 #include <sound/control.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/info.h>
30 #include <sound/initval.h>
31 #include <sound/timer.h>
33 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
34 MODULE_DESCRIPTION("A loopback soundcard");
35 MODULE_LICENSE("GPL");
37 #define MAX_PCM_SUBSTREAMS 8
39 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
40 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
41 static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
42 static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
43 static int pcm_notify[SNDRV_CARDS];
44 static char *timer_source[SNDRV_CARDS];
46 module_param_array(index, int, NULL, 0444);
47 MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
48 module_param_array(id, charp, NULL, 0444);
49 MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
50 module_param_array(enable, bool, NULL, 0444);
51 MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
52 module_param_array(pcm_substreams, int, NULL, 0444);
53 MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
54 module_param_array(pcm_notify, int, NULL, 0444);
55 MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
56 module_param_array(timer_source, charp, NULL, 0444);
57 MODULE_PARM_DESC(timer_source, "Sound card name or number and device/subdevice number of timer to be used. Empty string for jiffies timer [default].");
59 #define NO_PITCH 100000
61 #define CABLE_VALID_PLAYBACK BIT(SNDRV_PCM_STREAM_PLAYBACK)
62 #define CABLE_VALID_CAPTURE BIT(SNDRV_PCM_STREAM_CAPTURE)
63 #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK | CABLE_VALID_CAPTURE)
65 struct loopback_cable;
70 * call in loopback->cable_lock
72 int (*open)(struct loopback_pcm *dpcm);
76 int (*start)(struct loopback_pcm *dpcm);
80 int (*stop)(struct loopback_pcm *dpcm);
82 int (*stop_sync)(struct loopback_pcm *dpcm);
84 int (*close_substream)(struct loopback_pcm *dpcm);
86 * call in loopback->cable_lock
88 int (*close_cable)(struct loopback_pcm *dpcm);
92 unsigned int (*pos_update)(struct loopback_cable *cable);
94 void (*dpcm_info)(struct loopback_pcm *dpcm,
95 struct snd_info_buffer *buffer);
98 struct loopback_cable {
100 struct loopback_pcm *streams[2];
101 struct snd_pcm_hardware hw;
104 unsigned int running;
107 const struct loopback_ops *ops;
108 /* If sound timer is used */
111 struct snd_timer_id id;
112 struct work_struct event_work;
113 struct snd_timer_instance *instance;
117 struct loopback_setup {
118 unsigned int notify: 1;
119 unsigned int rate_shift;
120 snd_pcm_format_t format;
122 snd_pcm_access_t access;
123 unsigned int channels;
124 struct snd_ctl_elem_id active_id;
125 struct snd_ctl_elem_id format_id;
126 struct snd_ctl_elem_id rate_id;
127 struct snd_ctl_elem_id channels_id;
128 struct snd_ctl_elem_id access_id;
132 struct snd_card *card;
133 struct mutex cable_lock;
134 struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
135 struct snd_pcm *pcm[2];
136 struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
137 const char *timer_source;
140 struct loopback_pcm {
141 struct loopback *loopback;
142 struct snd_pcm_substream *substream;
143 struct loopback_cable *cable;
144 unsigned int pcm_buffer_size;
145 unsigned int buf_pos; /* position in buffer */
146 unsigned int silent_size;
148 unsigned int pcm_period_size;
149 unsigned int pcm_bps; /* bytes per second */
150 unsigned int pcm_salign; /* bytes per sample * channels */
151 unsigned int pcm_rate_shift; /* rate shift value */
153 unsigned int period_update_pending :1;
155 unsigned int irq_pos; /* fractional IRQ position in jiffies
158 unsigned int period_size_frac; /* period size in jiffies ticks */
159 unsigned int last_drift;
160 unsigned long last_jiffies;
161 /* If jiffies timer is used */
162 struct timer_list timer;
164 /* size of per channel buffer in case of non-interleaved access */
165 unsigned int channel_buf_n;
168 static struct platform_device *devices[SNDRV_CARDS];
170 static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
172 if (dpcm->pcm_rate_shift == NO_PITCH) {
175 x = div_u64(NO_PITCH * (unsigned long long)x,
176 HZ * (unsigned long long)dpcm->pcm_rate_shift);
178 return x - (x % dpcm->pcm_salign);
181 static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
183 if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
186 x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
192 static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
194 int device = dpcm->substream->pstr->pcm->device;
196 if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
198 return &dpcm->loopback->setup[dpcm->substream->number][device];
201 static inline unsigned int get_notify(struct loopback_pcm *dpcm)
203 return get_setup(dpcm)->notify;
206 static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
208 return get_setup(dpcm)->rate_shift;
211 /* call in cable->lock */
212 static int loopback_jiffies_timer_start(struct loopback_pcm *dpcm)
215 unsigned int rate_shift = get_rate_shift(dpcm);
217 if (rate_shift != dpcm->pcm_rate_shift) {
218 dpcm->pcm_rate_shift = rate_shift;
219 dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
221 if (dpcm->period_size_frac <= dpcm->irq_pos) {
222 dpcm->irq_pos %= dpcm->period_size_frac;
223 dpcm->period_update_pending = 1;
225 tick = dpcm->period_size_frac - dpcm->irq_pos;
226 tick = DIV_ROUND_UP(tick, dpcm->pcm_bps);
227 mod_timer(&dpcm->timer, jiffies + tick);
232 /* call in cable->lock */
233 static int loopback_snd_timer_start(struct loopback_pcm *dpcm)
235 struct loopback_cable *cable = dpcm->cable;
238 /* Loopback device has to use same period as timer card. Therefore
239 * wake up for each snd_pcm_period_elapsed() call of timer card.
241 err = snd_timer_start(cable->snd_timer.instance, 1);
243 /* do not report error if trying to start but already
244 * running. For example called by opposite substream
250 pcm_err(dpcm->substream->pcm,
251 "snd_timer_start(%d,%d,%d) failed with %d",
252 cable->snd_timer.id.card,
253 cable->snd_timer.id.device,
254 cable->snd_timer.id.subdevice,
261 /* call in cable->lock */
262 static inline int loopback_jiffies_timer_stop(struct loopback_pcm *dpcm)
264 del_timer(&dpcm->timer);
265 dpcm->timer.expires = 0;
270 /* call in cable->lock */
271 static int loopback_snd_timer_stop(struct loopback_pcm *dpcm)
273 struct loopback_cable *cable = dpcm->cable;
276 /* only stop if both devices (playback and capture) are not running */
277 if (cable->running ^ cable->pause)
280 err = snd_timer_stop(cable->snd_timer.instance);
282 pcm_err(dpcm->substream->pcm,
283 "snd_timer_stop(%d,%d,%d) failed with %d",
284 cable->snd_timer.id.card,
285 cable->snd_timer.id.device,
286 cable->snd_timer.id.subdevice,
293 static inline int loopback_jiffies_timer_stop_sync(struct loopback_pcm *dpcm)
295 del_timer_sync(&dpcm->timer);
300 /* call in loopback->cable_lock */
301 static int loopback_snd_timer_close_cable(struct loopback_pcm *dpcm)
303 struct loopback_cable *cable = dpcm->cable;
305 /* snd_timer was not opened */
306 if (!cable->snd_timer.instance)
309 /* will only be called from free_cable() when other stream was
310 * already closed. Other stream cannot be reopened as long as
311 * loopback->cable_lock is locked. Therefore no need to lock
314 snd_timer_close(cable->snd_timer.instance);
316 /* wait till drain work has finished if requested */
317 cancel_work_sync(&cable->snd_timer.event_work);
319 snd_timer_instance_free(cable->snd_timer.instance);
320 memset(&cable->snd_timer, 0, sizeof(cable->snd_timer));
325 static int loopback_check_format(struct loopback_cable *cable, int stream)
327 struct snd_pcm_runtime *runtime, *cruntime;
328 struct loopback_setup *setup;
329 struct snd_card *card;
332 if (cable->valid != CABLE_VALID_BOTH) {
333 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
337 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
339 cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
341 check = runtime->format != cruntime->format ||
342 runtime->rate != cruntime->rate ||
343 runtime->channels != cruntime->channels ||
344 runtime->access != cruntime->access;
347 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
350 snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
351 substream, SNDRV_PCM_STATE_DRAINING);
353 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
355 setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
356 card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
357 if (setup->format != runtime->format) {
358 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
360 setup->format = runtime->format;
362 if (setup->rate != runtime->rate) {
363 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
365 setup->rate = runtime->rate;
367 if (setup->channels != runtime->channels) {
368 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
369 &setup->channels_id);
370 setup->channels = runtime->channels;
372 if (setup->access != runtime->access) {
373 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
375 setup->access = runtime->access;
381 static void loopback_active_notify(struct loopback_pcm *dpcm)
383 snd_ctl_notify(dpcm->loopback->card,
384 SNDRV_CTL_EVENT_MASK_VALUE,
385 &get_setup(dpcm)->active_id);
388 static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
390 struct snd_pcm_runtime *runtime = substream->runtime;
391 struct loopback_pcm *dpcm = runtime->private_data;
392 struct loopback_cable *cable = dpcm->cable;
393 int err = 0, stream = 1 << substream->stream;
396 case SNDRV_PCM_TRIGGER_START:
397 err = loopback_check_format(cable, substream->stream);
400 dpcm->last_jiffies = jiffies;
401 dpcm->pcm_rate_shift = 0;
402 dpcm->last_drift = 0;
403 spin_lock(&cable->lock);
404 cable->running |= stream;
405 cable->pause &= ~stream;
406 err = cable->ops->start(dpcm);
407 spin_unlock(&cable->lock);
408 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
409 loopback_active_notify(dpcm);
411 case SNDRV_PCM_TRIGGER_STOP:
412 spin_lock(&cable->lock);
413 cable->running &= ~stream;
414 cable->pause &= ~stream;
415 err = cable->ops->stop(dpcm);
416 spin_unlock(&cable->lock);
417 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
418 loopback_active_notify(dpcm);
420 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
421 case SNDRV_PCM_TRIGGER_SUSPEND:
422 spin_lock(&cable->lock);
423 cable->pause |= stream;
424 err = cable->ops->stop(dpcm);
425 spin_unlock(&cable->lock);
426 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
427 loopback_active_notify(dpcm);
429 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
430 case SNDRV_PCM_TRIGGER_RESUME:
431 spin_lock(&cable->lock);
432 dpcm->last_jiffies = jiffies;
433 cable->pause &= ~stream;
434 err = cable->ops->start(dpcm);
435 spin_unlock(&cable->lock);
436 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
437 loopback_active_notify(dpcm);
445 static void params_change(struct snd_pcm_substream *substream)
447 struct snd_pcm_runtime *runtime = substream->runtime;
448 struct loopback_pcm *dpcm = runtime->private_data;
449 struct loopback_cable *cable = dpcm->cable;
451 cable->hw.formats = pcm_format_to_bits(runtime->format);
452 cable->hw.rate_min = runtime->rate;
453 cable->hw.rate_max = runtime->rate;
454 cable->hw.channels_min = runtime->channels;
455 cable->hw.channels_max = runtime->channels;
457 if (cable->snd_timer.instance) {
458 cable->hw.period_bytes_min =
459 frames_to_bytes(runtime, runtime->period_size);
460 cable->hw.period_bytes_max = cable->hw.period_bytes_min;
465 static int loopback_prepare(struct snd_pcm_substream *substream)
467 struct snd_pcm_runtime *runtime = substream->runtime;
468 struct loopback_pcm *dpcm = runtime->private_data;
469 struct loopback_cable *cable = dpcm->cable;
470 int err, bps, salign;
472 if (cable->ops->stop_sync) {
473 err = cable->ops->stop_sync(dpcm);
478 salign = (snd_pcm_format_physical_width(runtime->format) *
479 runtime->channels) / 8;
480 bps = salign * runtime->rate;
481 if (bps <= 0 || salign <= 0)
485 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
486 dpcm->channel_buf_n = dpcm->pcm_buffer_size / runtime->channels;
487 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
488 /* clear capture buffer */
489 dpcm->silent_size = dpcm->pcm_buffer_size;
490 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
491 runtime->buffer_size * runtime->channels);
495 dpcm->period_update_pending = 0;
497 dpcm->pcm_salign = salign;
498 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
500 mutex_lock(&dpcm->loopback->cable_lock);
501 if (!(cable->valid & ~(1 << substream->stream)) ||
502 (get_setup(dpcm)->notify &&
503 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
504 params_change(substream);
505 cable->valid |= 1 << substream->stream;
506 mutex_unlock(&dpcm->loopback->cable_lock);
511 static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
513 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
514 char *dst = runtime->dma_area;
515 unsigned int dst_off = dpcm->buf_pos;
517 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
519 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
520 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
523 unsigned int size = bytes;
524 if (dst_off + size > dpcm->pcm_buffer_size)
525 size = dpcm->pcm_buffer_size - dst_off;
526 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
527 bytes_to_frames(runtime, size) *
529 dpcm->silent_size += size;
537 static void copy_play_buf_part_n(struct loopback_pcm *play, struct loopback_pcm *capt,
538 unsigned int size, unsigned int src_off, unsigned int dst_off)
540 unsigned int channels = capt->substream->runtime->channels;
541 unsigned int size_p_ch = size / channels;
542 unsigned int src_off_ch = src_off / channels;
543 unsigned int dst_off_ch = dst_off / channels;
546 for (i = 0; i < channels; i++) {
547 memcpy(capt->substream->runtime->dma_area + capt->channel_buf_n * i + dst_off_ch,
548 play->substream->runtime->dma_area + play->channel_buf_n * i + src_off_ch,
553 static void copy_play_buf(struct loopback_pcm *play,
554 struct loopback_pcm *capt,
557 struct snd_pcm_runtime *runtime = play->substream->runtime;
558 char *src = runtime->dma_area;
559 char *dst = capt->substream->runtime->dma_area;
560 unsigned int src_off = play->buf_pos;
561 unsigned int dst_off = capt->buf_pos;
562 unsigned int clear_bytes = 0;
564 /* check if playback is draining, trim the capture copy size
565 * when our pointer is at the end of playback ring buffer */
566 if (runtime->state == SNDRV_PCM_STATE_DRAINING &&
567 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
568 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
569 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
570 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
571 appl_ptr1 += play->buf_pos / play->pcm_salign;
572 if (appl_ptr < appl_ptr1)
573 appl_ptr1 -= runtime->buffer_size;
574 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
576 clear_bytes = bytes - diff;
582 unsigned int size = bytes;
583 if (src_off + size > play->pcm_buffer_size)
584 size = play->pcm_buffer_size - src_off;
585 if (dst_off + size > capt->pcm_buffer_size)
586 size = capt->pcm_buffer_size - dst_off;
587 if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED ||
588 runtime->access == SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED)
589 copy_play_buf_part_n(play, capt, size, src_off, dst_off);
591 memcpy(dst + dst_off, src + src_off, size);
592 capt->silent_size = 0;
596 src_off = (src_off + size) % play->pcm_buffer_size;
597 dst_off = (dst_off + size) % capt->pcm_buffer_size;
600 if (clear_bytes > 0) {
601 clear_capture_buf(capt, clear_bytes);
602 capt->silent_size = 0;
606 static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
607 unsigned int jiffies_delta)
609 unsigned long last_pos;
612 last_pos = byte_pos(dpcm, dpcm->irq_pos);
613 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
614 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
615 if (delta >= dpcm->last_drift)
616 delta -= dpcm->last_drift;
617 dpcm->last_drift = 0;
618 if (dpcm->irq_pos >= dpcm->period_size_frac) {
619 dpcm->irq_pos %= dpcm->period_size_frac;
620 dpcm->period_update_pending = 1;
625 static inline void bytepos_finish(struct loopback_pcm *dpcm,
628 dpcm->buf_pos += delta;
629 dpcm->buf_pos %= dpcm->pcm_buffer_size;
632 /* call in cable->lock */
633 static unsigned int loopback_jiffies_timer_pos_update
634 (struct loopback_cable *cable)
636 struct loopback_pcm *dpcm_play =
637 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
638 struct loopback_pcm *dpcm_capt =
639 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
640 unsigned long delta_play = 0, delta_capt = 0, cur_jiffies;
641 unsigned int running, count1, count2;
643 cur_jiffies = jiffies;
644 running = cable->running ^ cable->pause;
645 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
646 delta_play = cur_jiffies - dpcm_play->last_jiffies;
647 dpcm_play->last_jiffies += delta_play;
650 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
651 delta_capt = cur_jiffies - dpcm_capt->last_jiffies;
652 dpcm_capt->last_jiffies += delta_capt;
655 if (delta_play == 0 && delta_capt == 0)
658 if (delta_play > delta_capt) {
659 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
660 bytepos_finish(dpcm_play, count1);
661 delta_play = delta_capt;
662 } else if (delta_play < delta_capt) {
663 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
664 clear_capture_buf(dpcm_capt, count1);
665 bytepos_finish(dpcm_capt, count1);
666 delta_capt = delta_play;
669 if (delta_play == 0 && delta_capt == 0)
672 /* note delta_capt == delta_play at this moment */
673 count1 = bytepos_delta(dpcm_play, delta_play);
674 count2 = bytepos_delta(dpcm_capt, delta_capt);
675 if (count1 < count2) {
676 dpcm_capt->last_drift = count2 - count1;
678 } else if (count1 > count2) {
679 dpcm_play->last_drift = count1 - count2;
681 copy_play_buf(dpcm_play, dpcm_capt, count1);
682 bytepos_finish(dpcm_play, count1);
683 bytepos_finish(dpcm_capt, count1);
688 static void loopback_jiffies_timer_function(struct timer_list *t)
690 struct loopback_pcm *dpcm = from_timer(dpcm, t, timer);
693 spin_lock_irqsave(&dpcm->cable->lock, flags);
694 if (loopback_jiffies_timer_pos_update(dpcm->cable) &
695 (1 << dpcm->substream->stream)) {
696 loopback_jiffies_timer_start(dpcm);
697 if (dpcm->period_update_pending) {
698 dpcm->period_update_pending = 0;
699 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
700 /* need to unlock before calling below */
701 snd_pcm_period_elapsed(dpcm->substream);
705 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
708 /* call in cable->lock */
709 static int loopback_snd_timer_check_resolution(struct snd_pcm_runtime *runtime,
710 unsigned long resolution)
712 if (resolution != runtime->timer_resolution) {
713 struct loopback_pcm *dpcm = runtime->private_data;
714 struct loopback_cable *cable = dpcm->cable;
715 /* Worst case estimation of possible values for resolution
716 * resolution <= (512 * 1024) frames / 8kHz in nsec
717 * resolution <= 65.536.000.000 nsec
719 * period_size <= 65.536.000.000 nsec / 1000nsec/usec * 192kHz +
721 * period_size <= 12.582.912.000.000 <64bit
722 * / 1.000.000 usec/sec
724 snd_pcm_uframes_t period_size_usec =
725 resolution / 1000 * runtime->rate;
726 /* round to nearest sample rate */
727 snd_pcm_uframes_t period_size =
728 (period_size_usec + 500 * 1000) / (1000 * 1000);
730 pcm_err(dpcm->substream->pcm,
731 "Period size (%lu frames) of loopback device is not corresponding to timer resolution (%lu nsec = %lu frames) of card timer %d,%d,%d. Use period size of %lu frames for loopback device.",
732 runtime->period_size, resolution, period_size,
733 cable->snd_timer.id.card,
734 cable->snd_timer.id.device,
735 cable->snd_timer.id.subdevice,
742 static void loopback_snd_timer_period_elapsed(struct loopback_cable *cable,
744 unsigned long resolution)
746 struct loopback_pcm *dpcm_play, *dpcm_capt;
747 struct snd_pcm_substream *substream_play, *substream_capt;
748 struct snd_pcm_runtime *valid_runtime;
749 unsigned int running, elapsed_bytes;
752 spin_lock_irqsave(&cable->lock, flags);
753 running = cable->running ^ cable->pause;
754 /* no need to do anything if no stream is running */
756 spin_unlock_irqrestore(&cable->lock, flags);
760 dpcm_play = cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
761 dpcm_capt = cable->streams[SNDRV_PCM_STREAM_CAPTURE];
763 if (event == SNDRV_TIMER_EVENT_MSTOP) {
765 dpcm_play->substream->runtime->state !=
766 SNDRV_PCM_STATE_DRAINING) {
767 spin_unlock_irqrestore(&cable->lock, flags);
772 substream_play = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
773 dpcm_play->substream : NULL;
774 substream_capt = (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) ?
775 dpcm_capt->substream : NULL;
776 valid_runtime = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
777 dpcm_play->substream->runtime :
778 dpcm_capt->substream->runtime;
780 /* resolution is only valid for SNDRV_TIMER_EVENT_TICK events */
781 if (event == SNDRV_TIMER_EVENT_TICK) {
782 /* The hardware rules guarantee that playback and capture period
783 * are the same. Therefore only one device has to be checked
786 if (loopback_snd_timer_check_resolution(valid_runtime,
788 spin_unlock_irqrestore(&cable->lock, flags);
790 snd_pcm_stop_xrun(substream_play);
792 snd_pcm_stop_xrun(substream_capt);
797 elapsed_bytes = frames_to_bytes(valid_runtime,
798 valid_runtime->period_size);
799 /* The same timer interrupt is used for playback and capture device */
800 if ((running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
801 (running & (1 << SNDRV_PCM_STREAM_CAPTURE))) {
802 copy_play_buf(dpcm_play, dpcm_capt, elapsed_bytes);
803 bytepos_finish(dpcm_play, elapsed_bytes);
804 bytepos_finish(dpcm_capt, elapsed_bytes);
805 } else if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
806 bytepos_finish(dpcm_play, elapsed_bytes);
807 } else if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
808 clear_capture_buf(dpcm_capt, elapsed_bytes);
809 bytepos_finish(dpcm_capt, elapsed_bytes);
811 spin_unlock_irqrestore(&cable->lock, flags);
814 snd_pcm_period_elapsed(substream_play);
816 snd_pcm_period_elapsed(substream_capt);
819 static void loopback_snd_timer_function(struct snd_timer_instance *timeri,
820 unsigned long resolution,
823 struct loopback_cable *cable = timeri->callback_data;
825 loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_TICK,
829 static void loopback_snd_timer_work(struct work_struct *work)
831 struct loopback_cable *cable;
833 cable = container_of(work, struct loopback_cable, snd_timer.event_work);
834 loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_MSTOP, 0);
837 static void loopback_snd_timer_event(struct snd_timer_instance *timeri,
839 struct timespec64 *tstamp,
840 unsigned long resolution)
842 /* Do not lock cable->lock here because timer->lock is already hold.
843 * There are other functions which first lock cable->lock and than
846 * spin_lock(&cable->lock)
847 * loopback_snd_timer_start()
849 * spin_lock(&timer->lock)
850 * Therefore when using the oposit order of locks here it could result
854 if (event == SNDRV_TIMER_EVENT_MSTOP) {
855 struct loopback_cable *cable = timeri->callback_data;
857 /* sound card of the timer was stopped. Therefore there will not
858 * be any further timer callbacks. Due to this forward audio
859 * data from here if in draining state. When still in running
860 * state the streaming will be aborted by the usual timeout. It
861 * should not be aborted here because may be the timer sound
862 * card does only a recovery and the timer is back soon.
863 * This work triggers loopback_snd_timer_work()
865 schedule_work(&cable->snd_timer.event_work);
869 static void loopback_jiffies_timer_dpcm_info(struct loopback_pcm *dpcm,
870 struct snd_info_buffer *buffer)
872 snd_iprintf(buffer, " update_pending:\t%u\n",
873 dpcm->period_update_pending);
874 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
875 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
876 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
877 dpcm->last_jiffies, jiffies);
878 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
881 static void loopback_snd_timer_dpcm_info(struct loopback_pcm *dpcm,
882 struct snd_info_buffer *buffer)
884 struct loopback_cable *cable = dpcm->cable;
886 snd_iprintf(buffer, " sound timer:\thw:%d,%d,%d\n",
887 cable->snd_timer.id.card,
888 cable->snd_timer.id.device,
889 cable->snd_timer.id.subdevice);
890 snd_iprintf(buffer, " timer open:\t\t%s\n",
891 (cable->snd_timer.stream == SNDRV_PCM_STREAM_CAPTURE) ?
892 "capture" : "playback");
895 static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
897 struct snd_pcm_runtime *runtime = substream->runtime;
898 struct loopback_pcm *dpcm = runtime->private_data;
899 snd_pcm_uframes_t pos;
901 spin_lock(&dpcm->cable->lock);
902 if (dpcm->cable->ops->pos_update)
903 dpcm->cable->ops->pos_update(dpcm->cable);
905 spin_unlock(&dpcm->cable->lock);
906 return bytes_to_frames(runtime, pos);
909 static const struct snd_pcm_hardware loopback_pcm_hardware =
911 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
912 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
913 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_NONINTERLEAVED),
914 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
915 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |
916 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
917 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
918 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
919 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
924 .buffer_bytes_max = 2 * 1024 * 1024,
925 .period_bytes_min = 64,
926 /* note check overflow in frac_pos() using pcm_rate_shift before
927 changing period_bytes_max value */
928 .period_bytes_max = 1024 * 1024,
934 static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
936 struct loopback_pcm *dpcm = runtime->private_data;
940 static int loopback_hw_free(struct snd_pcm_substream *substream)
942 struct snd_pcm_runtime *runtime = substream->runtime;
943 struct loopback_pcm *dpcm = runtime->private_data;
944 struct loopback_cable *cable = dpcm->cable;
946 mutex_lock(&dpcm->loopback->cable_lock);
947 cable->valid &= ~(1 << substream->stream);
948 mutex_unlock(&dpcm->loopback->cable_lock);
952 static unsigned int get_cable_index(struct snd_pcm_substream *substream)
954 if (!substream->pcm->device)
955 return substream->stream;
957 return !substream->stream;
960 static int rule_format(struct snd_pcm_hw_params *params,
961 struct snd_pcm_hw_rule *rule)
963 struct loopback_pcm *dpcm = rule->private;
964 struct loopback_cable *cable = dpcm->cable;
968 mutex_lock(&dpcm->loopback->cable_lock);
969 m.bits[0] = (u_int32_t)cable->hw.formats;
970 m.bits[1] = (u_int32_t)(cable->hw.formats >> 32);
971 mutex_unlock(&dpcm->loopback->cable_lock);
972 return snd_mask_refine(hw_param_mask(params, rule->var), &m);
975 static int rule_rate(struct snd_pcm_hw_params *params,
976 struct snd_pcm_hw_rule *rule)
978 struct loopback_pcm *dpcm = rule->private;
979 struct loopback_cable *cable = dpcm->cable;
980 struct snd_interval t;
982 mutex_lock(&dpcm->loopback->cable_lock);
983 t.min = cable->hw.rate_min;
984 t.max = cable->hw.rate_max;
985 mutex_unlock(&dpcm->loopback->cable_lock);
986 t.openmin = t.openmax = 0;
988 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
991 static int rule_channels(struct snd_pcm_hw_params *params,
992 struct snd_pcm_hw_rule *rule)
994 struct loopback_pcm *dpcm = rule->private;
995 struct loopback_cable *cable = dpcm->cable;
996 struct snd_interval t;
998 mutex_lock(&dpcm->loopback->cable_lock);
999 t.min = cable->hw.channels_min;
1000 t.max = cable->hw.channels_max;
1001 mutex_unlock(&dpcm->loopback->cable_lock);
1002 t.openmin = t.openmax = 0;
1004 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1007 static int rule_period_bytes(struct snd_pcm_hw_params *params,
1008 struct snd_pcm_hw_rule *rule)
1010 struct loopback_pcm *dpcm = rule->private;
1011 struct loopback_cable *cable = dpcm->cable;
1012 struct snd_interval t;
1014 mutex_lock(&dpcm->loopback->cable_lock);
1015 t.min = cable->hw.period_bytes_min;
1016 t.max = cable->hw.period_bytes_max;
1017 mutex_unlock(&dpcm->loopback->cable_lock);
1021 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1024 static void free_cable(struct snd_pcm_substream *substream)
1026 struct loopback *loopback = substream->private_data;
1027 int dev = get_cable_index(substream);
1028 struct loopback_cable *cable;
1030 cable = loopback->cables[substream->number][dev];
1033 if (cable->streams[!substream->stream]) {
1034 /* other stream is still alive */
1035 spin_lock_irq(&cable->lock);
1036 cable->streams[substream->stream] = NULL;
1037 spin_unlock_irq(&cable->lock);
1039 struct loopback_pcm *dpcm = substream->runtime->private_data;
1041 if (cable->ops && cable->ops->close_cable && dpcm)
1042 cable->ops->close_cable(dpcm);
1043 /* free the cable */
1044 loopback->cables[substream->number][dev] = NULL;
1049 static int loopback_jiffies_timer_open(struct loopback_pcm *dpcm)
1051 timer_setup(&dpcm->timer, loopback_jiffies_timer_function, 0);
1056 static const struct loopback_ops loopback_jiffies_timer_ops = {
1057 .open = loopback_jiffies_timer_open,
1058 .start = loopback_jiffies_timer_start,
1059 .stop = loopback_jiffies_timer_stop,
1060 .stop_sync = loopback_jiffies_timer_stop_sync,
1061 .close_substream = loopback_jiffies_timer_stop_sync,
1062 .pos_update = loopback_jiffies_timer_pos_update,
1063 .dpcm_info = loopback_jiffies_timer_dpcm_info,
1066 static int loopback_parse_timer_id(const char *str,
1067 struct snd_timer_id *tid)
1069 /* [<pref>:](<card name>|<card idx>)[{.,}<dev idx>[{.,}<subdev idx>]] */
1070 const char * const sep_dev = ".,";
1071 const char * const sep_pref = ":";
1072 const char *name = str;
1073 char *sep, save = '\0';
1074 int card_idx = 0, dev = 0, subdev = 0;
1077 sep = strpbrk(str, sep_pref);
1080 sep = strpbrk(name, sep_dev);
1085 err = kstrtoint(name, 0, &card_idx);
1086 if (err == -EINVAL) {
1087 /* Must be the name, not number */
1088 for (card_idx = 0; card_idx < snd_ecards_limit; card_idx++) {
1089 struct snd_card *card = snd_card_ref(card_idx);
1092 if (!strcmp(card->id, name))
1094 snd_card_unref(card);
1103 char *sep2, save2 = '\0';
1105 sep2 = strpbrk(sep + 1, sep_dev);
1110 err = kstrtoint(sep + 1, 0, &dev);
1114 err = kstrtoint(sep2 + 1, 0, &subdev);
1119 tid->card = card_idx;
1121 tid->subdevice = subdev;
1126 /* call in loopback->cable_lock */
1127 static int loopback_snd_timer_open(struct loopback_pcm *dpcm)
1130 struct snd_timer_id tid = {
1131 .dev_class = SNDRV_TIMER_CLASS_PCM,
1132 .dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION,
1134 struct snd_timer_instance *timeri;
1135 struct loopback_cable *cable = dpcm->cable;
1137 /* check if timer was already opened. It is only opened once
1138 * per playback and capture subdevice (aka cable).
1140 if (cable->snd_timer.instance)
1143 err = loopback_parse_timer_id(dpcm->loopback->timer_source, &tid);
1145 pcm_err(dpcm->substream->pcm,
1146 "Parsing timer source \'%s\' failed with %d",
1147 dpcm->loopback->timer_source, err);
1151 cable->snd_timer.stream = dpcm->substream->stream;
1152 cable->snd_timer.id = tid;
1154 timeri = snd_timer_instance_new(dpcm->loopback->card->id);
1159 /* The callback has to be called from another work. If
1160 * SNDRV_TIMER_IFLG_FAST is specified it will be called from the
1161 * snd_pcm_period_elapsed() call of the selected sound card.
1162 * snd_pcm_period_elapsed() helds snd_pcm_stream_lock_irqsave().
1163 * Due to our callback loopback_snd_timer_function() also calls
1164 * snd_pcm_period_elapsed() which calls snd_pcm_stream_lock_irqsave().
1165 * This would end up in a dead lock.
1167 timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1168 timeri->callback = loopback_snd_timer_function;
1169 timeri->callback_data = (void *)cable;
1170 timeri->ccallback = loopback_snd_timer_event;
1172 /* initialise a work used for draining */
1173 INIT_WORK(&cable->snd_timer.event_work, loopback_snd_timer_work);
1175 /* The mutex loopback->cable_lock is kept locked.
1176 * Therefore snd_timer_open() cannot be called a second time
1177 * by the other device of the same cable.
1178 * Therefore the following issue cannot happen:
1179 * [proc1] Call loopback_timer_open() ->
1180 * Unlock cable->lock for snd_timer_close/open() call
1181 * [proc2] Call loopback_timer_open() -> snd_timer_open(),
1183 * [proc1] Call snd_timer_open() and overwrite running timer
1186 err = snd_timer_open(timeri, &cable->snd_timer.id, current->pid);
1188 pcm_err(dpcm->substream->pcm,
1189 "snd_timer_open (%d,%d,%d) failed with %d",
1190 cable->snd_timer.id.card,
1191 cable->snd_timer.id.device,
1192 cable->snd_timer.id.subdevice,
1194 snd_timer_instance_free(timeri);
1198 cable->snd_timer.instance = timeri;
1204 /* stop_sync() is not required for sound timer because it does not need to be
1205 * restarted in loopback_prepare() on Xrun recovery
1207 static const struct loopback_ops loopback_snd_timer_ops = {
1208 .open = loopback_snd_timer_open,
1209 .start = loopback_snd_timer_start,
1210 .stop = loopback_snd_timer_stop,
1211 .close_cable = loopback_snd_timer_close_cable,
1212 .dpcm_info = loopback_snd_timer_dpcm_info,
1215 static int loopback_open(struct snd_pcm_substream *substream)
1217 struct snd_pcm_runtime *runtime = substream->runtime;
1218 struct loopback *loopback = substream->private_data;
1219 struct loopback_pcm *dpcm;
1220 struct loopback_cable *cable = NULL;
1222 int dev = get_cable_index(substream);
1224 mutex_lock(&loopback->cable_lock);
1225 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1230 dpcm->loopback = loopback;
1231 dpcm->substream = substream;
1233 cable = loopback->cables[substream->number][dev];
1235 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
1240 spin_lock_init(&cable->lock);
1241 cable->hw = loopback_pcm_hardware;
1242 if (loopback->timer_source)
1243 cable->ops = &loopback_snd_timer_ops;
1245 cable->ops = &loopback_jiffies_timer_ops;
1246 loopback->cables[substream->number][dev] = cable;
1248 dpcm->cable = cable;
1249 runtime->private_data = dpcm;
1251 if (cable->ops->open) {
1252 err = cable->ops->open(dpcm);
1257 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
1259 /* use dynamic rules based on actual runtime->hw values */
1260 /* note that the default rules created in the PCM midlevel code */
1261 /* are cached -> they do not reflect the actual state */
1262 err = snd_pcm_hw_rule_add(runtime, 0,
1263 SNDRV_PCM_HW_PARAM_FORMAT,
1265 SNDRV_PCM_HW_PARAM_FORMAT, -1);
1268 err = snd_pcm_hw_rule_add(runtime, 0,
1269 SNDRV_PCM_HW_PARAM_RATE,
1271 SNDRV_PCM_HW_PARAM_RATE, -1);
1274 err = snd_pcm_hw_rule_add(runtime, 0,
1275 SNDRV_PCM_HW_PARAM_CHANNELS,
1276 rule_channels, dpcm,
1277 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1281 /* In case of sound timer the period time of both devices of the same
1282 * loop has to be the same.
1283 * This rule only takes effect if a sound timer was chosen
1285 if (cable->snd_timer.instance) {
1286 err = snd_pcm_hw_rule_add(runtime, 0,
1287 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1288 rule_period_bytes, dpcm,
1289 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, -1);
1294 /* loopback_runtime_free() has not to be called if kfree(dpcm) was
1295 * already called here. Otherwise it will end up with a double free.
1297 runtime->private_free = loopback_runtime_free;
1298 if (get_notify(dpcm))
1299 runtime->hw = loopback_pcm_hardware;
1301 runtime->hw = cable->hw;
1303 spin_lock_irq(&cable->lock);
1304 cable->streams[substream->stream] = dpcm;
1305 spin_unlock_irq(&cable->lock);
1309 free_cable(substream);
1312 mutex_unlock(&loopback->cable_lock);
1316 static int loopback_close(struct snd_pcm_substream *substream)
1318 struct loopback *loopback = substream->private_data;
1319 struct loopback_pcm *dpcm = substream->runtime->private_data;
1322 if (dpcm->cable->ops->close_substream)
1323 err = dpcm->cable->ops->close_substream(dpcm);
1324 mutex_lock(&loopback->cable_lock);
1325 free_cable(substream);
1326 mutex_unlock(&loopback->cable_lock);
1330 static const struct snd_pcm_ops loopback_pcm_ops = {
1331 .open = loopback_open,
1332 .close = loopback_close,
1333 .hw_free = loopback_hw_free,
1334 .prepare = loopback_prepare,
1335 .trigger = loopback_trigger,
1336 .pointer = loopback_pointer,
1339 static int loopback_pcm_new(struct loopback *loopback,
1340 int device, int substreams)
1342 struct snd_pcm *pcm;
1345 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
1346 substreams, substreams, &pcm);
1349 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_pcm_ops);
1350 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_pcm_ops);
1351 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
1353 pcm->private_data = loopback;
1354 pcm->info_flags = 0;
1355 strcpy(pcm->name, "Loopback PCM");
1357 loopback->pcm[device] = pcm;
1361 static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
1362 struct snd_ctl_elem_info *uinfo)
1364 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1366 uinfo->value.integer.min = 80000;
1367 uinfo->value.integer.max = 120000;
1368 uinfo->value.integer.step = 1;
1372 static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
1373 struct snd_ctl_elem_value *ucontrol)
1375 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1377 mutex_lock(&loopback->cable_lock);
1378 ucontrol->value.integer.value[0] =
1379 loopback->setup[kcontrol->id.subdevice]
1380 [kcontrol->id.device].rate_shift;
1381 mutex_unlock(&loopback->cable_lock);
1385 static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
1386 struct snd_ctl_elem_value *ucontrol)
1388 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1392 val = ucontrol->value.integer.value[0];
1397 mutex_lock(&loopback->cable_lock);
1398 if (val != loopback->setup[kcontrol->id.subdevice]
1399 [kcontrol->id.device].rate_shift) {
1400 loopback->setup[kcontrol->id.subdevice]
1401 [kcontrol->id.device].rate_shift = val;
1404 mutex_unlock(&loopback->cable_lock);
1408 static int loopback_notify_get(struct snd_kcontrol *kcontrol,
1409 struct snd_ctl_elem_value *ucontrol)
1411 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1413 mutex_lock(&loopback->cable_lock);
1414 ucontrol->value.integer.value[0] =
1415 loopback->setup[kcontrol->id.subdevice]
1416 [kcontrol->id.device].notify;
1417 mutex_unlock(&loopback->cable_lock);
1421 static int loopback_notify_put(struct snd_kcontrol *kcontrol,
1422 struct snd_ctl_elem_value *ucontrol)
1424 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1428 val = ucontrol->value.integer.value[0] ? 1 : 0;
1429 mutex_lock(&loopback->cable_lock);
1430 if (val != loopback->setup[kcontrol->id.subdevice]
1431 [kcontrol->id.device].notify) {
1432 loopback->setup[kcontrol->id.subdevice]
1433 [kcontrol->id.device].notify = val;
1436 mutex_unlock(&loopback->cable_lock);
1440 static int loopback_active_get(struct snd_kcontrol *kcontrol,
1441 struct snd_ctl_elem_value *ucontrol)
1443 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1444 struct loopback_cable *cable;
1446 unsigned int val = 0;
1448 mutex_lock(&loopback->cable_lock);
1449 cable = loopback->cables[kcontrol->id.subdevice][kcontrol->id.device ^ 1];
1450 if (cable != NULL) {
1451 unsigned int running = cable->running ^ cable->pause;
1453 val = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 1 : 0;
1455 mutex_unlock(&loopback->cable_lock);
1456 ucontrol->value.integer.value[0] = val;
1460 static int loopback_format_info(struct snd_kcontrol *kcontrol,
1461 struct snd_ctl_elem_info *uinfo)
1463 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1465 uinfo->value.integer.min = 0;
1466 uinfo->value.integer.max = (__force int)SNDRV_PCM_FORMAT_LAST;
1467 uinfo->value.integer.step = 1;
1471 static int loopback_format_get(struct snd_kcontrol *kcontrol,
1472 struct snd_ctl_elem_value *ucontrol)
1474 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1476 ucontrol->value.integer.value[0] =
1477 (__force int)loopback->setup[kcontrol->id.subdevice]
1478 [kcontrol->id.device].format;
1482 static int loopback_rate_info(struct snd_kcontrol *kcontrol,
1483 struct snd_ctl_elem_info *uinfo)
1485 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1487 uinfo->value.integer.min = 0;
1488 uinfo->value.integer.max = 192000;
1489 uinfo->value.integer.step = 1;
1493 static int loopback_rate_get(struct snd_kcontrol *kcontrol,
1494 struct snd_ctl_elem_value *ucontrol)
1496 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1498 mutex_lock(&loopback->cable_lock);
1499 ucontrol->value.integer.value[0] =
1500 loopback->setup[kcontrol->id.subdevice]
1501 [kcontrol->id.device].rate;
1502 mutex_unlock(&loopback->cable_lock);
1506 static int loopback_channels_info(struct snd_kcontrol *kcontrol,
1507 struct snd_ctl_elem_info *uinfo)
1509 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1511 uinfo->value.integer.min = 1;
1512 uinfo->value.integer.max = 1024;
1513 uinfo->value.integer.step = 1;
1517 static int loopback_channels_get(struct snd_kcontrol *kcontrol,
1518 struct snd_ctl_elem_value *ucontrol)
1520 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1522 mutex_lock(&loopback->cable_lock);
1523 ucontrol->value.integer.value[0] =
1524 loopback->setup[kcontrol->id.subdevice]
1525 [kcontrol->id.device].channels;
1526 mutex_unlock(&loopback->cable_lock);
1530 static int loopback_access_info(struct snd_kcontrol *kcontrol,
1531 struct snd_ctl_elem_info *uinfo)
1533 const char * const texts[] = {"Interleaved", "Non-interleaved"};
1535 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1538 static int loopback_access_get(struct snd_kcontrol *kcontrol,
1539 struct snd_ctl_elem_value *ucontrol)
1541 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1542 snd_pcm_access_t access;
1544 mutex_lock(&loopback->cable_lock);
1545 access = loopback->setup[kcontrol->id.subdevice][kcontrol->id.device].access;
1547 ucontrol->value.enumerated.item[0] = access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED ||
1548 access == SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1550 mutex_unlock(&loopback->cable_lock);
1554 static const struct snd_kcontrol_new loopback_controls[] = {
1556 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1557 .name = "PCM Rate Shift 100000",
1558 .info = loopback_rate_shift_info,
1559 .get = loopback_rate_shift_get,
1560 .put = loopback_rate_shift_put,
1563 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1564 .name = "PCM Notify",
1565 .info = snd_ctl_boolean_mono_info,
1566 .get = loopback_notify_get,
1567 .put = loopback_notify_put,
1569 #define ACTIVE_IDX 2
1571 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1572 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1573 .name = "PCM Slave Active",
1574 .info = snd_ctl_boolean_mono_info,
1575 .get = loopback_active_get,
1577 #define FORMAT_IDX 3
1579 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1580 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1581 .name = "PCM Slave Format",
1582 .info = loopback_format_info,
1583 .get = loopback_format_get
1587 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1588 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1589 .name = "PCM Slave Rate",
1590 .info = loopback_rate_info,
1591 .get = loopback_rate_get
1593 #define CHANNELS_IDX 5
1595 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1596 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1597 .name = "PCM Slave Channels",
1598 .info = loopback_channels_info,
1599 .get = loopback_channels_get
1601 #define ACCESS_IDX 6
1603 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1604 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1605 .name = "PCM Slave Access Mode",
1606 .info = loopback_access_info,
1607 .get = loopback_access_get,
1611 static int loopback_mixer_new(struct loopback *loopback, int notify)
1613 struct snd_card *card = loopback->card;
1614 struct snd_pcm *pcm;
1615 struct snd_kcontrol *kctl;
1616 struct loopback_setup *setup;
1617 int err, dev, substr, substr_count, idx;
1619 strcpy(card->mixername, "Loopback Mixer");
1620 for (dev = 0; dev < 2; dev++) {
1621 pcm = loopback->pcm[dev];
1623 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1624 for (substr = 0; substr < substr_count; substr++) {
1625 setup = &loopback->setup[substr][dev];
1626 setup->notify = notify;
1627 setup->rate_shift = NO_PITCH;
1628 setup->format = SNDRV_PCM_FORMAT_S16_LE;
1629 setup->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1630 setup->rate = 48000;
1631 setup->channels = 2;
1632 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1634 kctl = snd_ctl_new1(&loopback_controls[idx],
1638 kctl->id.device = dev;
1639 kctl->id.subdevice = substr;
1641 /* Add the control before copying the id so that
1642 * the numid field of the id is set in the copy.
1644 err = snd_ctl_add(card, kctl);
1650 setup->active_id = kctl->id;
1653 setup->format_id = kctl->id;
1656 setup->rate_id = kctl->id;
1659 setup->channels_id = kctl->id;
1662 setup->access_id = kctl->id;
1673 static void print_dpcm_info(struct snd_info_buffer *buffer,
1674 struct loopback_pcm *dpcm,
1677 snd_iprintf(buffer, " %s\n", id);
1679 snd_iprintf(buffer, " inactive\n");
1682 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1683 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1684 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1685 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1686 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1687 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1688 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1689 if (dpcm->cable->ops->dpcm_info)
1690 dpcm->cable->ops->dpcm_info(dpcm, buffer);
1693 static void print_substream_info(struct snd_info_buffer *buffer,
1694 struct loopback *loopback,
1698 struct loopback_cable *cable = loopback->cables[sub][num];
1700 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1701 if (cable == NULL) {
1702 snd_iprintf(buffer, " inactive\n");
1705 snd_iprintf(buffer, " valid: %u\n", cable->valid);
1706 snd_iprintf(buffer, " running: %u\n", cable->running);
1707 snd_iprintf(buffer, " pause: %u\n", cable->pause);
1708 print_dpcm_info(buffer, cable->streams[0], "Playback");
1709 print_dpcm_info(buffer, cable->streams[1], "Capture");
1712 static void print_cable_info(struct snd_info_entry *entry,
1713 struct snd_info_buffer *buffer)
1715 struct loopback *loopback = entry->private_data;
1718 mutex_lock(&loopback->cable_lock);
1719 num = entry->name[strlen(entry->name)-1];
1720 num = num == '0' ? 0 : 1;
1721 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1722 print_substream_info(buffer, loopback, sub, num);
1723 mutex_unlock(&loopback->cable_lock);
1726 static int loopback_cable_proc_new(struct loopback *loopback, int cidx)
1730 snprintf(name, sizeof(name), "cable#%d", cidx);
1731 return snd_card_ro_proc_new(loopback->card, name, loopback,
1735 static void loopback_set_timer_source(struct loopback *loopback,
1738 if (loopback->timer_source) {
1739 devm_kfree(loopback->card->dev, loopback->timer_source);
1740 loopback->timer_source = NULL;
1742 if (value && *value)
1743 loopback->timer_source = devm_kstrdup(loopback->card->dev,
1747 static void print_timer_source_info(struct snd_info_entry *entry,
1748 struct snd_info_buffer *buffer)
1750 struct loopback *loopback = entry->private_data;
1752 mutex_lock(&loopback->cable_lock);
1753 snd_iprintf(buffer, "%s\n",
1754 loopback->timer_source ? loopback->timer_source : "");
1755 mutex_unlock(&loopback->cable_lock);
1758 static void change_timer_source_info(struct snd_info_entry *entry,
1759 struct snd_info_buffer *buffer)
1761 struct loopback *loopback = entry->private_data;
1764 mutex_lock(&loopback->cable_lock);
1765 if (!snd_info_get_line(buffer, line, sizeof(line)))
1766 loopback_set_timer_source(loopback, strim(line));
1767 mutex_unlock(&loopback->cable_lock);
1770 static int loopback_timer_source_proc_new(struct loopback *loopback)
1772 return snd_card_rw_proc_new(loopback->card, "timer_source", loopback,
1773 print_timer_source_info,
1774 change_timer_source_info);
1777 static int loopback_probe(struct platform_device *devptr)
1779 struct snd_card *card;
1780 struct loopback *loopback;
1781 int dev = devptr->id;
1784 err = snd_devm_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1785 sizeof(struct loopback), &card);
1788 loopback = card->private_data;
1790 if (pcm_substreams[dev] < 1)
1791 pcm_substreams[dev] = 1;
1792 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1793 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1795 loopback->card = card;
1796 loopback_set_timer_source(loopback, timer_source[dev]);
1798 mutex_init(&loopback->cable_lock);
1800 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1803 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1806 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1809 loopback_cable_proc_new(loopback, 0);
1810 loopback_cable_proc_new(loopback, 1);
1811 loopback_timer_source_proc_new(loopback);
1812 strcpy(card->driver, "Loopback");
1813 strcpy(card->shortname, "Loopback");
1814 sprintf(card->longname, "Loopback %i", dev + 1);
1815 err = snd_card_register(card);
1818 platform_set_drvdata(devptr, card);
1822 #ifdef CONFIG_PM_SLEEP
1823 static int loopback_suspend(struct device *pdev)
1825 struct snd_card *card = dev_get_drvdata(pdev);
1827 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1831 static int loopback_resume(struct device *pdev)
1833 struct snd_card *card = dev_get_drvdata(pdev);
1835 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1839 static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1840 #define LOOPBACK_PM_OPS &loopback_pm
1842 #define LOOPBACK_PM_OPS NULL
1845 #define SND_LOOPBACK_DRIVER "snd_aloop"
1847 static struct platform_driver loopback_driver = {
1848 .probe = loopback_probe,
1850 .name = SND_LOOPBACK_DRIVER,
1851 .pm = LOOPBACK_PM_OPS,
1855 static void loopback_unregister_all(void)
1859 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1860 platform_device_unregister(devices[i]);
1861 platform_driver_unregister(&loopback_driver);
1864 static int __init alsa_card_loopback_init(void)
1868 err = platform_driver_register(&loopback_driver);
1874 for (i = 0; i < SNDRV_CARDS; i++) {
1875 struct platform_device *device;
1878 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1882 if (!platform_get_drvdata(device)) {
1883 platform_device_unregister(device);
1886 devices[i] = device;
1891 printk(KERN_ERR "aloop: No loopback enabled\n");
1893 loopback_unregister_all();
1899 static void __exit alsa_card_loopback_exit(void)
1901 loopback_unregister_all();
1904 module_init(alsa_card_loopback_init)
1905 module_exit(alsa_card_loopback_exit)