34 #include <linux/slab.h>
35 #include <linux/time.h>
36 #include <linux/wait.h>
37 #include <linux/module.h>
50 #define MAX_PCM_SUBSTREAMS 8
65 MODULE_PARM_DESC(pcm_substreams,
"PCM substreams # (1-8) for loopback driver.");
67 MODULE_PARM_DESC(pcm_notify,
"Break capture when PCM format/rate/channels changes.");
69 #define NO_PITCH 100000
128 static inline unsigned int byte_pos(
struct loopback_pcm *dpcm,
unsigned int x)
133 x = div_u64(
NO_PITCH * (
unsigned long long)x,
139 static inline unsigned int frac_pos(
struct loopback_pcm *dpcm,
unsigned int x)
159 static inline unsigned int get_notify(
struct loopback_pcm *dpcm)
161 return get_setup(dpcm)->notify;
164 static inline unsigned int get_rate_shift(
struct loopback_pcm *dpcm)
166 return get_setup(dpcm)->rate_shift;
169 static void loopback_timer_start(
struct loopback_pcm *dpcm)
172 unsigned int rate_shift = get_rate_shift(dpcm);
185 dpcm->
timer.expires = jiffies + tick;
190 static inline void loopback_timer_stop(
struct loopback_pcm *dpcm)
194 dpcm->
timer.expires = 0;
198 #define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
199 #define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
200 #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
202 static int loopback_check_format(
struct loopback_cable *cable,
int stream)
252 static void loopback_active_notify(
struct loopback_pcm *dpcm)
256 &get_setup(dpcm)->active_id);
268 err = loopback_check_format(cable, substream->
stream);
274 spin_lock(&cable->
lock);
276 cable->
pause &= ~stream;
277 spin_unlock(&cable->
lock);
278 loopback_timer_start(dpcm);
280 loopback_active_notify(dpcm);
283 spin_lock(&cable->
lock);
285 cable->
pause &= ~stream;
286 spin_unlock(&cable->
lock);
287 loopback_timer_stop(dpcm);
289 loopback_active_notify(dpcm);
292 spin_lock(&cable->
lock);
293 cable->
pause |= stream;
294 spin_unlock(&cable->
lock);
295 loopback_timer_stop(dpcm);
298 spin_lock(&cable->
lock);
300 cable->
pause &= ~stream;
301 spin_unlock(&cable->
lock);
302 loopback_timer_start(dpcm);
310 static void params_change_substream(
struct loopback_pcm *dpcm,
318 if (dst_runtime ==
NULL)
320 dst_runtime->
hw = dpcm->
cable->hw;
329 cable->
hw.formats = (1ULL << runtime->
format);
330 cable->
hw.rate_min = runtime->
rate;
331 cable->
hw.rate_max = runtime->
rate;
349 bps = salign * runtime->
rate;
350 if (bps <= 0 || salign <= 0)
370 (get_setup(dpcm)->
notify &&
372 params_change(substream);
383 unsigned int dst_off = dpcm->
buf_pos;
395 bytes_to_frames(runtime, size) *
411 char *dst = capt->
substream->runtime->dma_area;
412 unsigned int src_off = play->
buf_pos;
413 unsigned int dst_off = capt->
buf_pos;
414 unsigned int clear_bytes = 0;
419 snd_pcm_playback_hw_avail(runtime) < runtime->
buffer_size) {
421 appl_ptr = appl_ptr1 = runtime->
control->appl_ptr;
424 if (appl_ptr < appl_ptr1)
426 diff = (appl_ptr - appl_ptr1) * play->
pcm_salign;
428 clear_bytes = bytes - diff;
434 unsigned int size =
bytes;
439 memcpy(dst + dst_off, src + src_off, size);
448 if (clear_bytes > 0) {
449 clear_capture_buf(capt, clear_bytes);
454 static inline unsigned int bytepos_delta(
struct loopback_pcm *dpcm,
455 unsigned int jiffies_delta)
457 unsigned long last_pos;
460 last_pos = byte_pos(dpcm, dpcm->
irq_pos);
462 delta = byte_pos(dpcm, dpcm->
irq_pos) - last_pos;
473 static inline void bytepos_finish(
struct loopback_pcm *dpcm,
480 static unsigned int loopback_pos_update(
struct loopback_cable *cable)
486 unsigned long delta_play = 0, delta_capt = 0;
502 if (delta_play == 0 && delta_capt == 0)
505 if (delta_play > delta_capt) {
506 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
507 bytepos_finish(dpcm_play, count1);
508 delta_play = delta_capt;
509 }
else if (delta_play < delta_capt) {
510 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
511 clear_capture_buf(dpcm_capt, count1);
512 bytepos_finish(dpcm_capt, count1);
513 delta_capt = delta_play;
516 if (delta_play == 0 && delta_capt == 0)
520 count1 = bytepos_delta(dpcm_play, delta_play);
521 count2 = bytepos_delta(dpcm_capt, delta_capt);
522 if (count1 < count2) {
525 }
else if (count1 > count2) {
528 copy_play_buf(dpcm_play, dpcm_capt, count1);
529 bytepos_finish(dpcm_play, count1);
530 bytepos_finish(dpcm_capt, count1);
532 spin_unlock_irqrestore(&cable->
lock, flags);
536 static void loopback_timer_function(
unsigned long data)
539 unsigned int running;
541 running = loopback_pos_update(dpcm->
cable);
542 if (running & (1 << dpcm->
substream->stream)) {
543 loopback_timer_start(dpcm);
556 loopback_pos_update(dpcm->
cable);
557 return bytes_to_frames(runtime, dpcm->
buf_pos);
572 .buffer_bytes_max = 2 * 1024 * 1024,
573 .period_bytes_min = 64,
576 .period_bytes_max = 1024 * 1024,
609 if (!substream->
pcm->device)
612 return !substream->
stream;
620 struct snd_mask *maskp = hw_param_mask(params, rule->
var);
625 if (! maskp->
bits[0] && ! maskp->
bits[1])
638 t.openmin =
t.openmax = 0;
651 t.openmin =
t.openmax = 0;
663 int dev = get_cable_index(substream);
674 (
unsigned long)dpcm);
686 cable->
hw = loopback_pcm_hardware;
699 rule_format, &runtime->
hw,
705 rule_rate, &runtime->
hw,
711 rule_channels, &runtime->
hw,
718 if (get_notify(dpcm))
719 runtime->
hw = loopback_pcm_hardware;
721 runtime->
hw = cable->
hw;
732 int dev = get_cable_index(substream);
734 loopback_timer_stop(dpcm);
749 static struct snd_pcm_ops loopback_playback_ops = {
750 .open = loopback_open,
751 .close = loopback_close,
753 .hw_params = loopback_hw_params,
754 .hw_free = loopback_hw_free,
755 .prepare = loopback_prepare,
756 .trigger = loopback_trigger,
757 .pointer = loopback_pointer,
763 .open = loopback_open,
764 .close = loopback_close,
766 .hw_params = loopback_hw_params,
767 .hw_free = loopback_hw_free,
768 .prepare = loopback_prepare,
769 .trigger = loopback_trigger,
770 .pointer = loopback_pointer,
775 static int __devinit loopback_pcm_new(
struct loopback *loopback,
776 int device,
int substreams)
782 substreams, substreams, &pcm);
796 static int loopback_rate_shift_info(
struct snd_kcontrol *kcontrol,
807 static int loopback_rate_shift_get(
struct snd_kcontrol *kcontrol,
812 ucontrol->
value.integer.value[0] =
813 loopback->
setup[kcontrol->
id.subdevice]
814 [kcontrol->
id.device].rate_shift;
818 static int loopback_rate_shift_put(
struct snd_kcontrol *kcontrol,
825 val = ucontrol->
value.integer.value[0];
831 if (val != loopback->
setup[kcontrol->
id.subdevice]
832 [kcontrol->
id.device].rate_shift) {
833 loopback->
setup[kcontrol->
id.subdevice]
834 [kcontrol->
id.device].rate_shift =
val;
841 static int loopback_notify_get(
struct snd_kcontrol *kcontrol,
846 ucontrol->
value.integer.value[0] =
847 loopback->
setup[kcontrol->
id.subdevice]
848 [kcontrol->
id.device].notify;
852 static int loopback_notify_put(
struct snd_kcontrol *kcontrol,
859 val = ucontrol->
value.integer.value[0] ? 1 : 0;
860 if (val != loopback->
setup[kcontrol->
id.subdevice]
861 [kcontrol->
id.device].notify) {
862 loopback->
setup[kcontrol->
id.subdevice]
863 [kcontrol->
id.device].notify =
val;
869 static int loopback_active_get(
struct snd_kcontrol *kcontrol,
874 [kcontrol->
id.subdevice][kcontrol->
id.device ^ 1];
875 unsigned int val = 0;
880 ucontrol->
value.integer.value[0] =
val;
884 static int loopback_format_info(
struct snd_kcontrol *kcontrol,
895 static int loopback_format_get(
struct snd_kcontrol *kcontrol,
900 ucontrol->
value.integer.value[0] =
901 loopback->
setup[kcontrol->
id.subdevice]
902 [kcontrol->
id.device].format;
906 static int loopback_rate_info(
struct snd_kcontrol *kcontrol,
917 static int loopback_rate_get(
struct snd_kcontrol *kcontrol,
922 ucontrol->
value.integer.value[0] =
923 loopback->
setup[kcontrol->
id.subdevice]
924 [kcontrol->
id.device].rate;
928 static int loopback_channels_info(
struct snd_kcontrol *kcontrol,
939 static int loopback_channels_get(
struct snd_kcontrol *kcontrol,
944 ucontrol->
value.integer.value[0] =
945 loopback->
setup[kcontrol->
id.subdevice]
946 [kcontrol->
id.device].channels;
953 .name =
"PCM Rate Shift 100000",
954 .info = loopback_rate_shift_info,
955 .get = loopback_rate_shift_get,
956 .put = loopback_rate_shift_put,
960 .name =
"PCM Notify",
962 .get = loopback_notify_get,
963 .put = loopback_notify_put,
969 .name =
"PCM Slave Active",
971 .get = loopback_active_get,
977 .name =
"PCM Slave Format",
978 .info = loopback_format_info,
979 .get = loopback_format_get
985 .name =
"PCM Slave Rate",
986 .info = loopback_rate_info,
987 .get = loopback_rate_get
989 #define CHANNELS_IDX 5
993 .name =
"PCM Slave Channels",
994 .info = loopback_channels_info,
995 .get = loopback_channels_get
999 static int __devinit loopback_mixer_new(
struct loopback *loopback,
int notify)
1005 int err,
dev, substr, substr_count,
idx;
1008 for (dev = 0; dev < 2; dev++) {
1009 pcm = loopback->
pcm[
dev];
1012 for (substr = 0; substr < substr_count; substr++) {
1013 setup = &loopback->
setup[substr][
dev];
1017 setup->
rate = 48000;
1019 for (idx = 0; idx <
ARRAY_SIZE(loopback_controls);
1025 kctl->
id.device =
dev;
1026 kctl->
id.subdevice = substr;
1052 #ifdef CONFIG_PROC_FS
1058 snd_iprintf(buffer,
" %s\n",
id);
1060 snd_iprintf(buffer,
" inactive\n");
1064 snd_iprintf(buffer,
" buffer_pos:\t\t%u\n", dpcm->
buf_pos);
1065 snd_iprintf(buffer,
" silent_size:\t%u\n", dpcm->
silent_size);
1067 snd_iprintf(buffer,
" bytes_per_sec:\t%u\n", dpcm->
pcm_bps);
1068 snd_iprintf(buffer,
" sample_align:\t%u\n", dpcm->
pcm_salign);
1069 snd_iprintf(buffer,
" rate_shift:\t\t%u\n", dpcm->
pcm_rate_shift);
1070 snd_iprintf(buffer,
" update_pending:\t%u\n",
1072 snd_iprintf(buffer,
" irq_pos:\t\t%u\n", dpcm->
irq_pos);
1074 snd_iprintf(buffer,
" last_jiffies:\t%lu (%lu)\n",
1076 snd_iprintf(buffer,
" timer_expires:\t%lu\n", dpcm->
timer.expires);
1080 struct loopback *loopback,
1086 snd_iprintf(buffer,
"Cable %i substream %i:\n", num, sub);
1087 if (cable ==
NULL) {
1088 snd_iprintf(buffer,
" inactive\n");
1091 snd_iprintf(buffer,
" valid: %u\n", cable->
valid);
1092 snd_iprintf(buffer,
" running: %u\n", cable->
running);
1093 snd_iprintf(buffer,
" pause: %u\n", cable->
pause);
1094 print_dpcm_info(buffer, cable->
streams[0],
"Playback");
1095 print_dpcm_info(buffer, cable->
streams[1],
"Capture");
1106 num = num ==
'0' ? 0 : 1;
1108 print_substream_info(buffer, loopback, sub, num);
1118 snprintf(name,
sizeof(name),
"cable#%d", cidx);
1119 err = snd_card_proc_new(loopback->
card, name, &entry);
1123 snd_info_set_text_ops(entry, loopback, print_cable_info);
1129 #define loopback_proc_new(loopback, cidx) do { } while (0)
1137 int dev = devptr->
id;
1141 sizeof(
struct loopback), &card);
1146 if (pcm_substreams[dev] < 1)
1147 pcm_substreams[
dev] = 1;
1148 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1154 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1157 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1160 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1170 platform_set_drvdata(devptr, card);
1181 platform_set_drvdata(devptr,
NULL);
1185 #ifdef CONFIG_PM_SLEEP
1186 static int loopback_suspend(
struct device *pdev)
1193 snd_pcm_suspend_all(loopback->
pcm[0]);
1194 snd_pcm_suspend_all(loopback->
pcm[1]);
1198 static int loopback_resume(
struct device *pdev)
1207 #define LOOPBACK_PM_OPS &loopback_pm
1209 #define LOOPBACK_PM_OPS NULL
1212 #define SND_LOOPBACK_DRIVER "snd_aloop"
1215 .probe = loopback_probe,
1224 static void loopback_unregister_all(
void)
1233 static int __init alsa_card_loopback_init(
void)
1251 if (!platform_get_drvdata(device)) {
1255 devices[
i] = device;
1262 loopback_unregister_all();
1268 static void __exit alsa_card_loopback_exit(
void)
1270 loopback_unregister_all();