From 81693e7f5e73c8fde658a2a050f85dd8481a1057 Mon Sep 17 00:00:00 2001 From: Mark Farrugia Date: Mon, 1 Oct 2018 17:47:44 +1000 Subject: Add configfs test script Add initial bash script for configuring AVIRT via the configfs Linux filesystem Signed-off-by: Mark Farrugia --- scripts/test_configfs.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100755 scripts/test_configfs.sh (limited to 'scripts') diff --git a/scripts/test_configfs.sh b/scripts/test_configfs.sh new file mode 100755 index 0000000..1ae0fe5 --- /dev/null +++ b/scripts/test_configfs.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +mkdir -p /config && mount -t configfs none /config + +mkdir /config/avirt/streams/playback_media +echo "2">/config/avirt/streams/playback_media/channels + +mkdir /config/avirt/streams/playback_navigation +echo "1">/config/avirt/streams/playback_navigation/channels + +mkdir /config/avirt/streams/playback_emergency +echo "1">/config/avirt/streams/playback_emergency/channels + +mkdir /config/avirt/streams/capture_voice +echo "1">/config/avirt/streams/capture_voice/channels + +echo "1">/config/avirt/streams/sealed -- cgit 1.2.3-korg From 7e4d6e2f61c97da680591b77fb89a69f69a3af7f Mon Sep 17 00:00:00 2001 From: Mark Farrugia Date: Wed, 3 Oct 2018 08:44:49 +1000 Subject: Fix scripts, move all to scripts folder Signed-off-by: Mark Farrugia --- make-agl.sh | 12 ------------ scripts/load.sh | 10 ++++++++++ scripts/make-agl.sh | 12 ++++++++++++ scripts/unload.sh | 10 ++++++++++ unload.sh | 10 ---------- 5 files changed, 32 insertions(+), 22 deletions(-) delete mode 100755 make-agl.sh create mode 100755 scripts/load.sh create mode 100755 scripts/make-agl.sh create mode 100755 scripts/unload.sh delete mode 100755 unload.sh (limited to 'scripts') diff --git a/make-agl.sh b/make-agl.sh deleted file mode 100755 index 4421af8..0000000 --- a/make-agl.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -# Get SDK path -sdk_id=$1 # first arg must be XDS_SDK_ID -shift 1 -long_sdkpath=$(xds-cli sdks get $sdk_id | grep Path) -sdkpath=${long_sdkpath:4} - -# Build -/opt/AGL/bin/xds-cli exec --config xds-project.conf -- \ - LDFLAGS= CONFIG_AVIRT=m CONFIG_AVIRT_BUILDLOCAL=y CONFIG_AVIRT_DUMMYAP=m \ - make -C $sdkpath/sysroots/aarch64-agl-linux/usr/src/kernel M=$(pwd) $@ diff --git a/scripts/load.sh b/scripts/load.sh new file mode 100755 index 0000000..2cd472d --- /dev/null +++ b/scripts/load.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +# Load the core +insmod avirt_core.ko + +# Load the additional Audio Paths +#insmod dummy/avirt_dummyap.ko +insmod loopback/avirt_loopbackap.ko + +echo "Drivers Loaded!" diff --git a/scripts/make-agl.sh b/scripts/make-agl.sh new file mode 100755 index 0000000..4421af8 --- /dev/null +++ b/scripts/make-agl.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# Get SDK path +sdk_id=$1 # first arg must be XDS_SDK_ID +shift 1 +long_sdkpath=$(xds-cli sdks get $sdk_id | grep Path) +sdkpath=${long_sdkpath:4} + +# Build +/opt/AGL/bin/xds-cli exec --config xds-project.conf -- \ + LDFLAGS= CONFIG_AVIRT=m CONFIG_AVIRT_BUILDLOCAL=y CONFIG_AVIRT_DUMMYAP=m \ + make -C $sdkpath/sysroots/aarch64-agl-linux/usr/src/kernel M=$(pwd) $@ diff --git a/scripts/unload.sh b/scripts/unload.sh new file mode 100755 index 0000000..a011a2b --- /dev/null +++ b/scripts/unload.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +rm_module() { + lsmod |grep "^$1\>" && rmmod $1 || true +} + +rm_module avirt_loopbackap +rm_module avirt_dummyap +rm_module avirt_core +echo "Drivers Removed!" diff --git a/unload.sh b/unload.sh deleted file mode 100755 index a011a2b..0000000 --- a/unload.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh - -rm_module() { - lsmod |grep "^$1\>" && rmmod $1 || true -} - -rm_module avirt_loopbackap -rm_module avirt_dummyap -rm_module avirt_core -echo "Drivers Removed!" -- cgit 1.2.3-korg From 39af5aad096d103c171096f05cc0487e66de0571 Mon Sep 17 00:00:00 2001 From: Mark Farrugia Date: Mon, 8 Oct 2018 17:02:51 +1100 Subject: Add working loopback implementation, helper scripts Reworked the original aloop implementation to have the PCM devices loop on themselves, and subscribe the the AVIRT system. Signed-off-by: Mark Farrugia --- loopback/Makefile | 1 + loopback/loopback.c | 1185 +++++++++++++++++++++++++++++++++++++++------- scripts/load.sh | 5 +- scripts/test_configfs.sh | 4 + scripts/unload.sh | 3 + 5 files changed, 1013 insertions(+), 185 deletions(-) (limited to 'scripts') diff --git a/loopback/Makefile b/loopback/Makefile index 55da206..0464aba 100644 --- a/loopback/Makefile +++ b/loopback/Makefile @@ -1,3 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_AVIRT_LOOPBACKAP) += avirt_loopbackap.o $(info $(src)) diff --git a/loopback/loopback.c b/loopback/loopback.c index e9c4bcb..2e02711 100644 --- a/loopback/loopback.c +++ b/loopback/loopback.c @@ -1,308 +1,1125 @@ -// SPDX-License-Identifier: GPL-2.0 /* - * ALSA Virtual Soundcard + * Loopback soundcard * - * loopback_audiopath.c - AVIRT sample Audio Path definition + * Original code: + * Copyright (c) by Jaroslav Kysela + * + * More accurate positioning and full-duplex support: + * Copyright (c) Ahmet İnan + * + * Major (almost complete) rewrite: + * Copyright (c) by Takashi Iwai + * + * A next major update in 2010 (separate timers for playback and capture): + * Copyright (c) Jaroslav Kysela + * + * Adapt to use AVIRT, looping is now conducted on the same device + * Copyright (c) by Mark Farrugia + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Copyright (C) 2010-2018 Fiberdyne Systems Pty Ltd */ -#include +#include +#include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include -MODULE_AUTHOR("JOSHANNE "); -MODULE_AUTHOR("MFARRUGI "); -MODULE_DESCRIPTION("Sample Audio Path Module Interface"); -MODULE_LICENSE("GPL v2"); +MODULE_AUTHOR("Jaroslav Kysela "); +MODULE_DESCRIPTION("A loopback soundcard"); +MODULE_LICENSE("GPL"); +MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}"); + +#define AP_UID "ap_loopback" -#define AP_LOGNAME "LOOPAP" +#define AP_INFOK(fmt, args...) DINFO(AP_UID, fmt, ##args) +#define AP_PRINTK(fmt, args...) DDEBUG(AP_UID, fmt, ##args) +#define AP_ERRORK(fmt, args...) DERROR(AP_UID, fmt, ##args) -#define AP_INFOK(fmt, args...) DINFO(AP_LOGNAME, fmt, ##args) -#define AP_PRINTK(fmt, args...) DPRINT(AP_LOGNAME, fmt, ##args) -#define AP_ERRORK(fmt, args...) DERROR(AP_LOGNAME, fmt, ##args) +#define NO_PITCH 100000 static struct avirt_coreinfo *coreinfo; +static struct loopback *loopback; + +struct loopback_pcm; + +struct loopback_cable { + spinlock_t lock; + struct loopback_pcm *streams[2]; + struct snd_pcm_hardware hw; + /* flags */ + unsigned int valid; + unsigned int running; + unsigned int pause; +}; + +struct loopback_setup { + unsigned int notify : 1; + unsigned int rate_shift; + unsigned int format; + unsigned int rate; + unsigned int channels; + struct snd_ctl_elem_id active_id; + struct snd_ctl_elem_id format_id; + struct snd_ctl_elem_id rate_id; + struct snd_ctl_elem_id channels_id; +}; + +struct loopback { + struct snd_card *card; + struct mutex cable_lock; + struct loopback_cable *cables[MAX_STREAMS]; + struct snd_pcm *pcm[MAX_STREAMS]; + struct loopback_setup setup[MAX_STREAMS]; +}; struct loopback_pcm { + struct loopback *loopback; struct snd_pcm_substream *substream; - spinlock_t lock; + struct loopback_cable *cable; + unsigned int pcm_buffer_size; + unsigned int buf_pos; /* position in buffer */ + unsigned int silent_size; + /* PCM parameters */ + unsigned int pcm_period_size; + unsigned int pcm_bps; /* bytes per second */ + unsigned int pcm_salign; /* bytes per sample * channels */ + unsigned int pcm_rate_shift; /* rate shift value */ + /* flags */ + unsigned int period_update_pending : 1; + /* timer stuff */ + unsigned int irq_pos; /* fractional IRQ position */ + unsigned int period_size_frac; + unsigned int last_drift; + unsigned long last_jiffies; struct timer_list timer; - unsigned long base_time; - unsigned int frac_pos; /* fractional sample position (based HZ) */ - unsigned int frac_period_rest; - unsigned int frac_buffer_size; /* buffer_size * HZ */ - unsigned int frac_period_size; /* period_size * HZ */ - unsigned int rate; - int elapsed; }; -int systimer_create(struct snd_pcm_substream *substream); -void systimer_free(struct snd_pcm_substream *substream); -int systimer_start(struct snd_pcm_substream *substream); -int systimer_stop(struct snd_pcm_substream *substream); -snd_pcm_uframes_t systimer_pointer(struct snd_pcm_substream *substream); -int systimer_prepare(struct snd_pcm_substream *substream); -void systimer_rearm(struct loopback_pcm *dpcm); -void systimer_update(struct loopback_pcm *dpcm); +static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x) +{ + if (dpcm->pcm_rate_shift == NO_PITCH) { + x /= HZ; + } else { + x = div_u64(NO_PITCH * (unsigned long long)x, + HZ * (unsigned long long)dpcm->pcm_rate_shift); + } + return x - (x % dpcm->pcm_salign); +} -void loopback_callback(struct timer_list *tlist); +static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x) +{ + if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */ + return x * HZ; + } else { + x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ, + NO_PITCH); + } + return x; +} -/******************************** - * Loopback Timer Functions - ********************************/ +static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm) +{ + return &dpcm->loopback->setup[dpcm->substream->pcm->device]; +} -int systimer_create(struct snd_pcm_substream *substream) +static inline unsigned int get_notify(struct loopback_pcm *dpcm) { - struct snd_pcm_runtime *runtime = substream->runtime; + return get_setup(dpcm)->notify; +} - struct loopback_pcm *dpcm; - dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); - if (!dpcm) - return -ENOMEM; +static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm) +{ + return get_setup(dpcm)->rate_shift; +} - timer_setup(&dpcm->timer, loopback_callback, 0); - spin_lock_init(&dpcm->lock); - dpcm->substream = substream; +/* call in cable->lock */ +static void loopback_timer_start(struct loopback_pcm *dpcm) +{ + unsigned long tick; + unsigned int rate_shift = get_rate_shift(dpcm); - runtime->private_data = dpcm; - return 0; + if (rate_shift != dpcm->pcm_rate_shift) { + dpcm->pcm_rate_shift = rate_shift; + dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size); + } + if (dpcm->period_size_frac <= dpcm->irq_pos) { + dpcm->irq_pos %= dpcm->period_size_frac; + dpcm->period_update_pending = 1; + } + tick = dpcm->period_size_frac - dpcm->irq_pos; + tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps; + mod_timer(&dpcm->timer, jiffies + tick); } -void systimer_free(struct snd_pcm_substream *substream) +/* call in cable->lock */ +static inline void loopback_timer_stop(struct loopback_pcm *dpcm) { - struct snd_pcm_runtime *runtime = substream->runtime; - kfree(runtime->private_data); + del_timer(&dpcm->timer); + dpcm->timer.expires = 0; } -int systimer_start(struct snd_pcm_substream *substream) +static inline void loopback_timer_stop_sync(struct loopback_pcm *dpcm) { - struct snd_pcm_runtime *runtime = substream->runtime; - struct loopback_pcm *dpcm = runtime->private_data; - spin_lock(&dpcm->lock); - dpcm->base_time = jiffies; - systimer_rearm(dpcm); - spin_unlock(&dpcm->lock); + del_timer_sync(&dpcm->timer); +} + +#define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK) +#define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE) +#define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK | CABLE_VALID_CAPTURE) + +static int loopback_check_format(struct loopback_cable *cable, int stream) +{ + struct snd_pcm_runtime *runtime, *cruntime; + struct loopback_setup *setup; + struct snd_card *card; + int check; + + if (cable->valid != CABLE_VALID_BOTH) { + if (stream == SNDRV_PCM_STREAM_PLAYBACK) + goto __notify; + return 0; + } + runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->substream->runtime; + cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->substream->runtime; + check = runtime->format != cruntime->format || + runtime->rate != cruntime->rate || + runtime->channels != cruntime->channels; + if (!check) + return 0; + if (stream == SNDRV_PCM_STREAM_CAPTURE) { + return -EIO; + } else { + snd_pcm_stop( + cable->streams[SNDRV_PCM_STREAM_CAPTURE]->substream, + SNDRV_PCM_STATE_DRAINING); + __notify: + runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK] + ->substream->runtime; + setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]); + card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card; + if (setup->format != runtime->format) { + snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, + &setup->format_id); + setup->format = runtime->format; + } + if (setup->rate != runtime->rate) { + snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, + &setup->rate_id); + setup->rate = runtime->rate; + } + if (setup->channels != runtime->channels) { + snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, + &setup->channels_id); + setup->channels = runtime->channels; + } + } return 0; } -int systimer_stop(struct snd_pcm_substream *substream) +static void loopback_active_notify(struct loopback_pcm *dpcm) +{ + snd_ctl_notify(dpcm->loopback->card, SNDRV_CTL_EVENT_MASK_VALUE, + &get_setup(dpcm)->active_id); +} + +static int loopback_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_pcm_runtime *runtime = substream->runtime; struct loopback_pcm *dpcm = runtime->private_data; - spin_lock(&dpcm->lock); - del_timer(&dpcm->timer); - spin_unlock(&dpcm->lock); + struct loopback_cable *cable = dpcm->cable; + int err, stream = 1 << substream->stream; + + AP_INFOK(); + + switch (cmd) { + case SNDRV_PCM_TRIGGER_START: + err = loopback_check_format(cable, substream->stream); + if (err < 0) + return err; + dpcm->last_jiffies = jiffies; + dpcm->pcm_rate_shift = 0; + dpcm->last_drift = 0; + spin_lock(&cable->lock); + cable->running |= stream; + cable->pause &= ~stream; + loopback_timer_start(dpcm); + spin_unlock(&cable->lock); + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) + loopback_active_notify(dpcm); + break; + case SNDRV_PCM_TRIGGER_STOP: + spin_lock(&cable->lock); + cable->running &= ~stream; + cable->pause &= ~stream; + loopback_timer_stop(dpcm); + spin_unlock(&cable->lock); + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) + loopback_active_notify(dpcm); + break; + case SNDRV_PCM_TRIGGER_PAUSE_PUSH: + case SNDRV_PCM_TRIGGER_SUSPEND: + spin_lock(&cable->lock); + cable->pause |= stream; + loopback_timer_stop(dpcm); + spin_unlock(&cable->lock); + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) + loopback_active_notify(dpcm); + break; + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: + case SNDRV_PCM_TRIGGER_RESUME: + spin_lock(&cable->lock); + dpcm->last_jiffies = jiffies; + cable->pause &= ~stream; + loopback_timer_start(dpcm); + spin_unlock(&cable->lock); + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) + loopback_active_notify(dpcm); + break; + default: + return -EINVAL; + } return 0; } -snd_pcm_uframes_t systimer_pointer(struct snd_pcm_substream *substream) +static void params_change(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; struct loopback_pcm *dpcm = runtime->private_data; + struct loopback_cable *cable = dpcm->cable; - snd_pcm_uframes_t pos; - - spin_lock(&dpcm->lock); - systimer_update(dpcm); - pos = dpcm->frac_pos / HZ; - spin_unlock(&dpcm->lock); - return pos; + cable->hw.formats = pcm_format_to_bits(runtime->format); + cable->hw.rate_min = runtime->rate; + cable->hw.rate_max = runtime->rate; + cable->hw.channels_min = runtime->channels; + cable->hw.channels_max = runtime->channels; } -int systimer_prepare(struct snd_pcm_substream *substream) +static int loopback_prepare(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; struct loopback_pcm *dpcm = runtime->private_data; + struct loopback_cable *cable = dpcm->cable; + int bps, salign; - dpcm->frac_pos = 0; - dpcm->rate = runtime->rate; - dpcm->frac_buffer_size = runtime->buffer_size * HZ; - dpcm->frac_period_size = runtime->period_size * HZ; - dpcm->frac_period_rest = dpcm->frac_period_size; - dpcm->elapsed = 0; + loopback_timer_stop_sync(dpcm); + + salign = + (snd_pcm_format_width(runtime->format) * runtime->channels) / 8; + bps = salign * runtime->rate; + if (bps <= 0 || salign <= 0) + return -EINVAL; + + dpcm->buf_pos = 0; + dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size); + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { + /* clear capture buffer */ + dpcm->silent_size = dpcm->pcm_buffer_size; + snd_pcm_format_set_silence(runtime->format, runtime->dma_area, + runtime->buffer_size * + runtime->channels); + } + + dpcm->irq_pos = 0; + dpcm->period_update_pending = 0; + dpcm->pcm_bps = bps; + dpcm->pcm_salign = salign; + dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size); + + mutex_lock(&dpcm->loopback->cable_lock); + if (!(cable->valid & ~(1 << substream->stream)) || + (get_setup(dpcm)->notify && + substream->stream == SNDRV_PCM_STREAM_PLAYBACK)) + params_change(substream); + cable->valid |= 1 << substream->stream; + mutex_unlock(&dpcm->loopback->cable_lock); return 0; } -void systimer_rearm(struct loopback_pcm *dpcm) +static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes) { - mod_timer(&dpcm->timer, jiffies + (dpcm->frac_period_rest + dpcm->rate - - 1) / dpcm->rate); + struct snd_pcm_runtime *runtime = dpcm->substream->runtime; + char *dst = runtime->dma_area; + unsigned int dst_off = dpcm->buf_pos; + + if (dpcm->silent_size >= dpcm->pcm_buffer_size) + return; + if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size) + bytes = dpcm->pcm_buffer_size - dpcm->silent_size; + + for (;;) { + unsigned int size = bytes; + if (dst_off + size > dpcm->pcm_buffer_size) + size = dpcm->pcm_buffer_size - dst_off; + snd_pcm_format_set_silence(runtime->format, dst + dst_off, + bytes_to_frames(runtime, size) * + runtime->channels); + dpcm->silent_size += size; + bytes -= size; + if (!bytes) + break; + dst_off = 0; + } } -void systimer_update(struct loopback_pcm *dpcm) +static void copy_play_buf(struct loopback_pcm *play, struct loopback_pcm *capt, + unsigned int bytes) { - unsigned long delta; + struct snd_pcm_runtime *runtime = play->substream->runtime; + char *src = runtime->dma_area; + char *dst = capt->substream->runtime->dma_area; + unsigned int src_off = play->buf_pos; + unsigned int dst_off = capt->buf_pos; + unsigned int clear_bytes = 0; - delta = jiffies - dpcm->base_time; - if (!delta) - return; - dpcm->base_time += delta; - delta *= dpcm->rate; - dpcm->frac_pos += delta; - while (dpcm->frac_pos >= dpcm->frac_buffer_size) - dpcm->frac_pos -= dpcm->frac_buffer_size; - while (dpcm->frac_period_rest <= delta) { - dpcm->elapsed++; - dpcm->frac_period_rest += dpcm->frac_period_size; + /* check if playback is draining, trim the capture copy size + * when our pointer is at the end of playback ring buffer */ + if (runtime->status->state == SNDRV_PCM_STATE_DRAINING && + snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) { + snd_pcm_uframes_t appl_ptr, appl_ptr1, diff; + appl_ptr = appl_ptr1 = runtime->control->appl_ptr; + appl_ptr1 -= appl_ptr1 % runtime->buffer_size; + appl_ptr1 += play->buf_pos / play->pcm_salign; + if (appl_ptr < appl_ptr1) + appl_ptr1 -= runtime->buffer_size; + diff = (appl_ptr - appl_ptr1) * play->pcm_salign; + if (diff < bytes) { + clear_bytes = bytes - diff; + bytes = diff; + } + } + + for (;;) { + unsigned int size = bytes; + if (src_off + size > play->pcm_buffer_size) + size = play->pcm_buffer_size - src_off; + if (dst_off + size > capt->pcm_buffer_size) + size = capt->pcm_buffer_size - dst_off; + memcpy(dst + dst_off, src + src_off, size); + capt->silent_size = 0; + bytes -= size; + if (!bytes) + break; + src_off = (src_off + size) % play->pcm_buffer_size; + dst_off = (dst_off + size) % capt->pcm_buffer_size; + } + + if (clear_bytes > 0) { + clear_capture_buf(capt, clear_bytes); + capt->silent_size = 0; } - dpcm->frac_period_rest -= delta; } -/******* - * Loopback Timer Callback - *******/ +static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm, + unsigned int jiffies_delta) +{ + unsigned long last_pos; + unsigned int delta; -// -void loopback_callback(struct timer_list *tlist) + last_pos = byte_pos(dpcm, dpcm->irq_pos); + dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps; + delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos; + if (delta >= dpcm->last_drift) + delta -= dpcm->last_drift; + dpcm->last_drift = 0; + if (dpcm->irq_pos >= dpcm->period_size_frac) { + dpcm->irq_pos %= dpcm->period_size_frac; + dpcm->period_update_pending = 1; + } + return delta; +} + +static inline void bytepos_finish(struct loopback_pcm *dpcm, unsigned int delta) { - struct loopback_pcm *dpcm = from_timer(dpcm, tlist, timer); + dpcm->buf_pos += delta; + dpcm->buf_pos %= dpcm->pcm_buffer_size; +} - unsigned long flags; - int elapsed = 0; +/* call in cable->lock */ +static unsigned int loopback_pos_update(struct loopback_cable *cable) +{ + struct loopback_pcm *dpcm_play = + cable->streams[SNDRV_PCM_STREAM_PLAYBACK]; + struct loopback_pcm *dpcm_capt = + cable->streams[SNDRV_PCM_STREAM_CAPTURE]; + unsigned long delta_play = 0, delta_capt = 0; + unsigned int running, count1, count2; + + running = cable->running ^ cable->pause; + if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) { + delta_play = jiffies - dpcm_play->last_jiffies; + dpcm_play->last_jiffies += delta_play; + } + + if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) { + delta_capt = jiffies - dpcm_capt->last_jiffies; + dpcm_capt->last_jiffies += delta_capt; + } - AP_INFOK(""); - spin_lock_irqsave(&dpcm->lock, flags); + if (delta_play == 0 && delta_capt == 0) + goto unlock; - // Perform copy from playback to capture - systimer_update(dpcm); - systimer_rearm(dpcm); - elapsed = dpcm->elapsed; - dpcm->elapsed = 0; + if (delta_play > delta_capt) { + count1 = bytepos_delta(dpcm_play, delta_play - delta_capt); + bytepos_finish(dpcm_play, count1); + delta_play = delta_capt; + } else if (delta_play < delta_capt) { + count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play); + clear_capture_buf(dpcm_capt, count1); + bytepos_finish(dpcm_capt, count1); + delta_capt = delta_play; + } + + if (delta_play == 0 && delta_capt == 0) + goto unlock; - spin_unlock_irqrestore(&dpcm->lock, flags); - if (elapsed) - coreinfo->pcm_buff_complete(dpcm->substream); + /* note delta_capt == delta_play at this moment */ + count1 = bytepos_delta(dpcm_play, delta_play); + count2 = bytepos_delta(dpcm_capt, delta_capt); + if (count1 < count2) { + dpcm_capt->last_drift = count2 - count1; + count1 = count2; + } else if (count1 > count2) { + dpcm_play->last_drift = count1 - count2; + } + copy_play_buf(dpcm_play, dpcm_capt, count1); + bytepos_finish(dpcm_play, count1); + bytepos_finish(dpcm_capt, count1); +unlock: + return running; } -/******************************************************************************* - * Audio Path ALSA PCM Callbacks - ******************************************************************************/ -static int loopback_pcm_open(struct snd_pcm_substream *substream) +static void loopback_timer_function(struct timer_list *t) { - int err; + struct loopback_pcm *dpcm = from_timer(dpcm, t, timer); + unsigned long flags; + + spin_lock_irqsave(&dpcm->cable->lock, flags); + if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) { + loopback_timer_start(dpcm); + if (dpcm->period_update_pending) { + dpcm->period_update_pending = 0; + spin_unlock_irqrestore(&dpcm->cable->lock, flags); + /* need to unlock before calling below */ + avirt_pcm_period_elapsed(dpcm->substream); + return; + } + } + spin_unlock_irqrestore(&dpcm->cable->lock, flags); +} +static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream) +{ struct snd_pcm_runtime *runtime = substream->runtime; struct loopback_pcm *dpcm = runtime->private_data; + snd_pcm_uframes_t pos; - err = systimer_create(substream); - if (err < 0) - return err; + spin_lock(&dpcm->cable->lock); + loopback_pos_update(dpcm->cable); + pos = dpcm->buf_pos; + spin_unlock(&dpcm->cable->lock); + return bytes_to_frames(runtime, pos); +} - return 0; +static const struct snd_pcm_hardware loopback_pcm_hardware = { + .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP | + SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE | + SNDRV_PCM_INFO_RESUME), + .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | + SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE | + SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE), + .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000, + .rate_min = 8000, + .rate_max = 192000, + .channels_min = 1, + .channels_max = 32, + .buffer_bytes_max = 2 * 1024 * 1024, + .period_bytes_min = 64, + /* note check overflow in frac_pos() using pcm_rate_shift before + changing period_bytes_max value */ + .period_bytes_max = 1024 * 1024, + .periods_min = 1, + .periods_max = 1024, + .fifo_size = 0, +}; + +static void loopback_runtime_free(struct snd_pcm_runtime *runtime) +{ + struct loopback_pcm *dpcm = runtime->private_data; + kfree(dpcm); } -static int loopback_pcm_close(struct snd_pcm_substream *substream) +static int loopback_hw_free(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; struct loopback_pcm *dpcm = runtime->private_data; + struct loopback_cable *cable = dpcm->cable; - AP_INFOK("Close"); + mutex_lock(&dpcm->loopback->cable_lock); + cable->valid &= ~(1 << substream->stream); + mutex_unlock(&dpcm->loopback->cable_lock); - systimer_free(substream); return 0; } -static snd_pcm_uframes_t - loopback_pcm_pointer(struct snd_pcm_substream *substream) +static int rule_format(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule) { - AP_INFOK("Pointer"); + struct loopback_pcm *dpcm = rule->private; + struct loopback_cable *cable = dpcm->cable; + struct snd_mask m; - return systimer_pointer(substream); + snd_mask_none(&m); + mutex_lock(&dpcm->loopback->cable_lock); + m.bits[0] = (u_int32_t)cable->hw.formats; + m.bits[1] = (u_int32_t)(cable->hw.formats >> 32); + mutex_unlock(&dpcm->loopback->cable_lock); + return snd_mask_refine(hw_param_mask(params, rule->var), &m); } -static int loopback_pcm_trigger(struct snd_pcm_substream *substream, int cmd) +static int rule_rate(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule) { - AP_INFOK("Trigger"); + struct loopback_pcm *dpcm = rule->private; + struct loopback_cable *cable = dpcm->cable; + struct snd_interval t; - switch (cmd) { - case SNDRV_PCM_TRIGGER_START: - case SNDRV_PCM_TRIGGER_RESUME: - return systimer_start(substream); - case SNDRV_PCM_TRIGGER_STOP: - case SNDRV_PCM_TRIGGER_SUSPEND: - return systimer_stop(substream); + mutex_lock(&dpcm->loopback->cable_lock); + t.min = cable->hw.rate_min; + t.max = cable->hw.rate_max; + mutex_unlock(&dpcm->loopback->cable_lock); + t.openmin = t.openmax = 0; + t.integer = 0; + return snd_interval_refine(hw_param_interval(params, rule->var), &t); +} + +static int rule_channels(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule) +{ + struct loopback_pcm *dpcm = rule->private; + struct loopback_cable *cable = dpcm->cable; + struct snd_interval t; + + mutex_lock(&dpcm->loopback->cable_lock); + t.min = cable->hw.channels_min; + t.max = cable->hw.channels_max; + mutex_unlock(&dpcm->loopback->cable_lock); + t.openmin = t.openmax = 0; + t.integer = 0; + return snd_interval_refine(hw_param_interval(params, rule->var), &t); +} + +static void free_cable(struct snd_pcm_substream *substream) +{ + struct loopback_cable *cable; + + cable = loopback->cables[substream->pcm->device]; + if (!cable) + return; + if (cable->streams[!substream->stream]) { + /* other stream is still alive */ + spin_lock_irq(&cable->lock); + cable->streams[substream->stream] = NULL; + spin_unlock_irq(&cable->lock); + } else { + /* free the cable */ + loopback->cables[substream->pcm->device] = NULL; + kfree(cable); } - return -EINVAL; } -static int loopback_pcm_prepare(struct snd_pcm_substream *substream) +static int loopback_open(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; - struct loopback_pcm *dpcm = runtime->private_data; + struct loopback_pcm *dpcm; + struct loopback_cable *cable = NULL; + int err = 0; + + mutex_lock(&loopback->cable_lock); + dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); + if (!dpcm) { + err = -ENOMEM; + goto unlock; + } + dpcm->loopback = loopback; + dpcm->substream = substream; + timer_setup(&dpcm->timer, loopback_timer_function, 0); + + cable = loopback->cables[substream->pcm->device]; + if (!cable) { + cable = kzalloc(sizeof(*cable), GFP_KERNEL); + if (!cable) { + err = -ENOMEM; + goto unlock; + } + spin_lock_init(&cable->lock); + cable->hw = loopback_pcm_hardware; + loopback->cables[substream->pcm->device] = cable; + } + dpcm->cable = cable; + + snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); + + /* use dynamic rules based on actual runtime->hw values */ + /* note that the default rules created in the PCM midlevel code */ + /* are cached -> they do not reflect the actual state */ + err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, + rule_format, dpcm, SNDRV_PCM_HW_PARAM_FORMAT, + -1); + if (err < 0) + goto unlock; + err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, + rule_rate, dpcm, SNDRV_PCM_HW_PARAM_RATE, -1); + if (err < 0) + goto unlock; + err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, + rule_channels, dpcm, + SNDRV_PCM_HW_PARAM_CHANNELS, -1); + if (err < 0) + goto unlock; + + runtime->private_data = dpcm; + runtime->private_free = loopback_runtime_free; + if (get_notify(dpcm)) + runtime->hw = loopback_pcm_hardware; + else + runtime->hw = cable->hw; + + spin_lock_irq(&cable->lock); + cable->streams[substream->stream] = dpcm; + spin_unlock_irq(&cable->lock); + +unlock: + if (err < 0) { + free_cable(substream); + kfree(dpcm); + } + mutex_unlock(&loopback->cable_lock); + return err; +} + +static int loopback_close(struct snd_pcm_substream *substream) +{ + struct loopback_pcm *dpcm = substream->runtime->private_data; + + loopback_timer_stop_sync(dpcm); + mutex_lock(&loopback->cable_lock); + free_cable(substream); + mutex_unlock(&loopback->cable_lock); + return 0; +} + +static const struct snd_pcm_ops loopbackap_pcm_ops = { + .open = loopback_open, + .close = loopback_close, + .hw_free = loopback_hw_free, + .prepare = loopback_prepare, + .trigger = loopback_trigger, + .pointer = loopback_pointer, +}; + +static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_info *uinfo) +{ + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; + uinfo->count = 1; + uinfo->value.integer.min = 80000; + uinfo->value.integer.max = 120000; + uinfo->value.integer.step = 1; + return 0; +} + +static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct loopback *loopback = snd_kcontrol_chip(kcontrol); + + mutex_lock(&loopback->cable_lock); + ucontrol->value.integer.value[0] = + loopback->setup[kcontrol->id.device].rate_shift; + mutex_unlock(&loopback->cable_lock); + return 0; +} + +static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct loopback *loopback = snd_kcontrol_chip(kcontrol); + unsigned int val; + int change = 0; + + val = ucontrol->value.integer.value[0]; + if (val < 80000) + val = 80000; + if (val > 120000) + val = 120000; + mutex_lock(&loopback->cable_lock); + if (val != loopback->setup[kcontrol->id.device].rate_shift) { + loopback->setup[kcontrol->id.device].rate_shift = val; + change = 1; + } + mutex_unlock(&loopback->cable_lock); + return change; +} + +static int loopback_notify_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct loopback *loopback = snd_kcontrol_chip(kcontrol); + + mutex_lock(&loopback->cable_lock); + ucontrol->value.integer.value[0] = + loopback->setup[kcontrol->id.device].notify; + mutex_unlock(&loopback->cable_lock); + return 0; +} + +static int loopback_notify_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct loopback *loopback = snd_kcontrol_chip(kcontrol); + unsigned int val; + int change = 0; + val = ucontrol->value.integer.value[0] ? 1 : 0; + mutex_lock(&loopback->cable_lock); + if (val != loopback->setup[kcontrol->id.device].notify) { + loopback->setup[kcontrol->id.device].notify = val; + change = 1; + } + mutex_unlock(&loopback->cable_lock); + return change; +} + +static int loopback_active_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct loopback *loopback = snd_kcontrol_chip(kcontrol); + struct loopback_cable *cable; + + unsigned int val = 0; + + mutex_lock(&loopback->cable_lock); + cable = loopback->cables[kcontrol->id.device]; + if (cable != NULL) { + unsigned int running = cable->running ^ cable->pause; + + val = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 1 : 0; + } + mutex_unlock(&loopback->cable_lock); + ucontrol->value.integer.value[0] = val; + return 0; +} + +static int loopback_format_info(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_info *uinfo) +{ + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; + uinfo->count = 1; + uinfo->value.integer.min = 0; + uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST; + uinfo->value.integer.step = 1; + return 0; +} + +static int loopback_format_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct loopback *loopback = snd_kcontrol_chip(kcontrol); + + ucontrol->value.integer.value[0] = + loopback->setup[kcontrol->id.device].format; + return 0; +} + +static int loopback_rate_info(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_info *uinfo) +{ + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; + uinfo->count = 1; + uinfo->value.integer.min = 0; + uinfo->value.integer.max = 192000; + uinfo->value.integer.step = 1; + return 0; +} - AP_INFOK("%s\n%s\n%d\n%d", substream->pcm->name, - substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? "PLAYBACK" : - "CAPTURE", - runtime->rate, runtime->channels); +static int loopback_rate_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct loopback *loopback = snd_kcontrol_chip(kcontrol); + mutex_lock(&loopback->cable_lock); + ucontrol->value.integer.value[0] = + loopback->setup[kcontrol->id.device].rate; + mutex_unlock(&loopback->cable_lock); + return 0; +} - return systimer_prepare(substream); +static int loopback_channels_info(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_info *uinfo) +{ + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; + uinfo->count = 1; + uinfo->value.integer.min = 1; + uinfo->value.integer.max = 1024; + uinfo->value.integer.step = 1; + return 0; } -static struct snd_pcm_ops loopbackap_pcm_ops = { - .open = loopback_pcm_open, - .close = loopback_pcm_close, - .prepare = loopback_pcm_prepare, - .pointer = loopback_pcm_pointer, - .trigger = loopback_pcm_trigger, +static int loopback_channels_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct loopback *loopback = snd_kcontrol_chip(kcontrol); + + mutex_lock(&loopback->cable_lock); + ucontrol->value.integer.value[0] = + loopback->setup[kcontrol->id.device].channels; + mutex_unlock(&loopback->cable_lock); + return 0; +} + +static struct snd_kcontrol_new loopback_controls[] = { + { + .iface = SNDRV_CTL_ELEM_IFACE_PCM, + .name = "PCM Rate Shift 100000", + .info = loopback_rate_shift_info, + .get = loopback_rate_shift_get, + .put = loopback_rate_shift_put, + }, + { + .iface = SNDRV_CTL_ELEM_IFACE_PCM, + .name = "PCM Notify", + .info = snd_ctl_boolean_mono_info, + .get = loopback_notify_get, + .put = loopback_notify_put, + }, +#define ACTIVE_IDX 2 + { + .access = SNDRV_CTL_ELEM_ACCESS_READ, + .iface = SNDRV_CTL_ELEM_IFACE_PCM, + .name = "PCM Slave Active", + .info = snd_ctl_boolean_mono_info, + .get = loopback_active_get, + }, +#define FORMAT_IDX 3 + { .access = SNDRV_CTL_ELEM_ACCESS_READ, + .iface = SNDRV_CTL_ELEM_IFACE_PCM, + .name = "PCM Slave Format", + .info = loopback_format_info, + .get = loopback_format_get }, +#define RATE_IDX 4 + { .access = SNDRV_CTL_ELEM_ACCESS_READ, + .iface = SNDRV_CTL_ELEM_IFACE_PCM, + .name = "PCM Slave Rate", + .info = loopback_rate_info, + .get = loopback_rate_get }, +#define CHANNELS_IDX 5 + { .access = SNDRV_CTL_ELEM_ACCESS_READ, + .iface = SNDRV_CTL_ELEM_IFACE_PCM, + .name = "PCM Slave Channels", + .info = loopback_channels_info, + .get = loopback_channels_get } }; +static int loopback_mixer_new(struct loopback *loopback, int notify) +{ + struct snd_card *card = loopback->card; + struct snd_pcm *pcm; + struct snd_kcontrol *kctl; + struct loopback_setup *setup; + int dev, dev_count, idx, err; + + strcpy(card->mixername, "Loopback Mixer"); + dev_count = avirt_stream_count(SNDRV_PCM_STREAM_PLAYBACK); + for (dev = 0; dev < dev_count; dev++) { + pcm = loopback->pcm[dev]; + setup = &loopback->setup[dev]; + setup->notify = notify; + setup->rate_shift = NO_PITCH; + setup->format = SNDRV_PCM_FORMAT_S16_LE; + setup->rate = 48000; + setup->channels = 2; + for (idx = 0; idx < ARRAY_SIZE(loopback_controls); idx++) { + kctl = snd_ctl_new1(&loopback_controls[idx], loopback); + if (!kctl) + return -ENOMEM; + kctl->id.device = dev; + kctl->id.subdevice = 0; + switch (idx) { + case ACTIVE_IDX: + setup->active_id = kctl->id; + break; + case FORMAT_IDX: + setup->format_id = kctl->id; + break; + case RATE_IDX: + setup->rate_id = kctl->id; + break; + case CHANNELS_IDX: + setup->channels_id = kctl->id; + break; + default: + break; + } + err = snd_ctl_add(card, kctl); + if (err < 0) + return err; + } + } + return 0; +} + +static void print_dpcm_info(struct snd_info_buffer *buffer, + struct loopback_pcm *dpcm, const char *id) +{ + snd_iprintf(buffer, " %s\n", id); + if (dpcm == NULL) { + snd_iprintf(buffer, " inactive\n"); + return; + } + snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size); + snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos); + snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size); + snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size); + snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps); + snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign); + snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift); + snd_iprintf(buffer, " update_pending:\t%u\n", + dpcm->period_update_pending); + snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos); + snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac); + snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n", + dpcm->last_jiffies, jiffies); + snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires); +} + +static void print_substream_info(struct snd_info_buffer *buffer, + struct loopback *loopback, int device) +{ + struct loopback_cable *cable = loopback->cables[device]; + + snd_iprintf(buffer, "Cable device %i:\n", device); + if (cable == NULL) { + snd_iprintf(buffer, " inactive\n"); + return; + } + snd_iprintf(buffer, " valid: %u\n", cable->valid); + snd_iprintf(buffer, " running: %u\n", cable->running); + snd_iprintf(buffer, " pause: %u\n", cable->pause); + print_dpcm_info(buffer, cable->streams[0], "Playback"); + print_dpcm_info(buffer, cable->streams[1], "Capture"); +} + +static void print_cable_info(struct snd_info_entry *entry, + struct snd_info_buffer *buffer) +{ + struct loopback *loopback = entry->private_data; + int device; + + mutex_lock(&loopback->cable_lock); + for (device = 0; device < MAX_STREAMS; device++) + print_substream_info(buffer, loopback, device); + mutex_unlock(&loopback->cable_lock); +} + +static int loopback_proc_new(struct loopback *loopback, int cidx) +{ + char name[32]; + struct snd_info_entry *entry; + int err; + + snprintf(name, sizeof(name), "cable#%d", cidx); + err = snd_card_proc_new(loopback->card, name, &entry); + if (err < 0) + return err; + + snd_info_set_text_ops(entry, loopback, print_cable_info); + return 0; +} + +int loopbackap_configure(struct snd_card *card, + struct config_group *avirt_stream_group, + unsigned int stream_count) +{ + int err; + struct list_head *entry; + + loopback = kzalloc(sizeof(struct loopback), GFP_KERNEL); + if (!loopback) + return -ENOMEM; + loopback->card = card; + mutex_init(&loopback->cable_lock); + + list_for_each (entry, &avirt_stream_group->cg_children) { + struct config_item *item = + container_of(entry, struct config_item, ci_entry); + struct avirt_stream *stream = + avirt_stream_from_config_item(item); + loopback->pcm[stream->device] = stream->pcm; + + AP_INFOK("stream name:%s device:%d channels:%d", stream->name, + stream->device, stream->channels); + } + + err = loopback_mixer_new(loopback, 1); + if (err < 0) + return err; + + loopback_proc_new(loopback, 0); + loopback_proc_new(loopback, 1); + + return 0; +} + /******************************************************************************* * Loopback Audio Path AVIRT registration ******************************************************************************/ -static struct snd_pcm_hardware loopbackap_hw = { - .formats = SNDRV_PCM_FMTBIT_S16_LE, - .info = (SNDRV_PCM_INFO_INTERLEAVED // Channel interleaved audio - | SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP | - SNDRV_PCM_INFO_MMAP_VALID), - /* - .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | - SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | - SNDRV_PCM_RATE_48000, - .rate_min = 8000, - .rate_max = 48000, - */ - .rates = SNDRV_PCM_RATE_48000, - .rate_min = 48000, - .rate_max = 48000, - .channels_min = 1, - .channels_max = 8, - //.buffer_bytes_max = 32768, - .periods_min = 1, - .periods_max = 8, -}; - static struct avirt_audiopath loopbackap_module = { + .uid = AP_UID, .name = "Loopback Audio Path", .version = { 0, 0, 1 }, - .value = 10, - .hw = &loopbackap_hw, + .hw = &loopback_pcm_hardware, .pcm_ops = &loopbackap_pcm_ops, - .configure = loopback_configure, + .configure = loopbackap_configure, }; -static int __init loopback_init(void) +static int __init alsa_card_loopback_init(void) { int err = 0; - AP_INFOK("init()"); - - err = avirt_register_audiopath(&loopbackap_module, &coreinfo); + err = avirt_audiopath_register(&loopbackap_module, &coreinfo); if ((err < 0) || (!coreinfo)) { - AP_ERRORK("%s: coreinfo is NULL!\n", __func__); + AP_ERRORK("coreinfo is NULL!"); return err; } - return err; + return 0; } -static void __exit loopback_exit(void) +static void __exit alsa_card_loopback_exit(void) { - AP_INFOK("loopback: exit()"); - - avirt_deregister_audiopath(&loopbackap_module); + avirt_audiopath_deregister(&loopbackap_module); } -module_init(loopback_init); -module_exit(loopback_exit); +module_init(alsa_card_loopback_init); +module_exit(alsa_card_loopback_exit) diff --git a/scripts/load.sh b/scripts/load.sh index 2cd472d..82f613a 100755 --- a/scripts/load.sh +++ b/scripts/load.sh @@ -4,7 +4,10 @@ insmod avirt_core.ko # Load the additional Audio Paths -#insmod dummy/avirt_dummyap.ko +insmod dummy/avirt_dummyap.ko insmod loopback/avirt_loopbackap.ko +# Run the test script +./scripts/test_configfs.sh + echo "Drivers Loaded!" diff --git a/scripts/test_configfs.sh b/scripts/test_configfs.sh index 1ae0fe5..baa5e58 100755 --- a/scripts/test_configfs.sh +++ b/scripts/test_configfs.sh @@ -4,14 +4,18 @@ mkdir -p /config && mount -t configfs none /config mkdir /config/avirt/streams/playback_media echo "2">/config/avirt/streams/playback_media/channels +echo "ap_loopback">/config/avirt/streams/playback_media/map mkdir /config/avirt/streams/playback_navigation echo "1">/config/avirt/streams/playback_navigation/channels +echo "ap_loopback">/config/avirt/streams/playback_navigation/map mkdir /config/avirt/streams/playback_emergency echo "1">/config/avirt/streams/playback_emergency/channels +echo "ap_loopback">/config/avirt/streams/playback_emergency/map mkdir /config/avirt/streams/capture_voice echo "1">/config/avirt/streams/capture_voice/channels +echo "ap_loopback">/config/avirt/streams/capture_voice/map echo "1">/config/avirt/streams/sealed diff --git a/scripts/unload.sh b/scripts/unload.sh index a011a2b..2c158d5 100755 --- a/scripts/unload.sh +++ b/scripts/unload.sh @@ -1,5 +1,8 @@ #!/bin/sh +rmdir /config/avirt/streams/playback_* +rmdir /config/avirt/streams/capture_* + rm_module() { lsmod |grep "^$1\>" && rmmod $1 || true } -- cgit 1.2.3-korg From d66dbf0820e9e7e14a6f723c1ce92b7ac4e0280d Mon Sep 17 00:00:00 2001 From: Mark Farrugia Date: Thu, 25 Oct 2018 17:37:07 +1100 Subject: Refactor all avirt_ symbols to have have snd prefix, as per Linux sound This will aid the eventual migration the the upstream mainline Linux kernel Fix some checkpatch warnings, and other compiler warnings Signed-off-by: Mark Farrugia --- Makefile | 16 ++-- alsa-pcm.c | 70 ++++++++--------- configfs.c | 116 +++++++++++++++------------- core.c | 197 ++++++++++++++++++++++++----------------------- core.h | 33 ++++---- docs/2.Building.md | 2 +- docs/3.Usage.md | 40 +++++----- dummy/Makefile | 4 +- dummy/dummy.c | 22 +++--- loopback/Makefile | 4 +- loopback/loopback.c | 32 ++++---- scripts/load.sh | 6 +- scripts/make-agl.sh | 6 +- scripts/test_configfs.sh | 26 +++---- scripts/unload.sh | 10 +-- sound/avirt.h | 54 ++++++------- 16 files changed, 325 insertions(+), 313 deletions(-) (limited to 'scripts') diff --git a/Makefile b/Makefile index 791a60a..84b8b4d 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 -obj-$(CONFIG_AVIRT) += avirt_core.o -avirt_core-y := core.o -avirt_core-y += alsa-pcm.o -avirt_core-y += configfs.o +obj-$(CONFIG_AVIRT) += snd-avirt-core.o +snd-avirt-core-y := core.o +snd-avirt-core-y += alsa-pcm.o +snd-avirt-core-y += configfs.o ifeq ($(CONFIG_AVIRT_BUILDLOCAL),) CCFLAGS_AVIRT := "drivers/staging/" @@ -13,8 +13,8 @@ endif ccflags-y += -I${CCFLAGS_AVIRT} $(info $(KERNELRELEASE)) -obj-$(CONFIG_AVIRT_DUMMYAP) += dummy/ -obj-$(CONFIG_AVIRT_LOOPBACKAP) += loopback/ +obj-$(CONFIG_AVIRT_AP_DUMMY) += dummy/ +obj-$(CONFIG_AVIRT_AP_LOOPBACK) += loopback/ ### # For out-of-tree building @@ -29,8 +29,8 @@ endif all: CONFIG_AVIRT=m CONFIG_AVIRT_BUILDLOCAL=y \ - CONFIG_AVIRT_DUMMYAP=m \ - CONFIG_AVIRT_LOOPBACKAP=m \ + CONFIG_AVIRT_AP_DUMMY=m \ + CONFIG_AVIRT_AP_LOOPBACK=m \ make -C $(KERNEL_SRC) M=$(PWD) clean: diff --git a/alsa-pcm.c b/alsa-pcm.c index 4d7b549..603c096 100644 --- a/alsa-pcm.c +++ b/alsa-pcm.c @@ -3,7 +3,7 @@ * AVIRT - ALSA Virtual Soundcard * * Copyright (c) 2010-2018 Fiberdyne Systems Pty Ltd - * + * * alsa-pcm.c - AVIRT PCM interface */ @@ -15,24 +15,24 @@ #define D_PRINTK(fmt, args...) DDEBUG(D_LOGNAME, fmt, ##args) #define D_ERRORK(fmt, args...) DERROR(D_LOGNAME, fmt, ##args) -#define DO_AUDIOPATH_CB(ap, callback, substream, ...) \ - (((ap)->pcm_ops->callback) ? \ - (ap)->pcm_ops->callback((substream), ##__VA_ARGS__) : \ +#define DO_AUDIOPATH_CB(ap, callback, substream, ...) \ + (((ap)->pcm_ops->callback) ? \ + (ap)->pcm_ops->callback((substream), ##__VA_ARGS__) : \ 0) /** - * avirt_pcm_period_elapsed - PCM buffer complete callback + * snd_avirt_pcm_period_elapsed - PCM buffer complete callback * @substreamid: pointer to ALSA PCM substream * - * This should be called from a child Audio Path once it has finished processing - * the pcm buffer + * This should be called from a child Audio Path once it has finished + * processing the pcm buffer */ -void avirt_pcm_period_elapsed(struct snd_pcm_substream *substream) +void snd_avirt_pcm_period_elapsed(struct snd_pcm_substream *substream) { // Notify ALSA middle layer of the elapsed period boundary snd_pcm_period_elapsed(substream); } -EXPORT_SYMBOL_GPL(avirt_pcm_period_elapsed); +EXPORT_SYMBOL_GPL(snd_avirt_pcm_period_elapsed); /******************************************************************************* * ALSA PCM Callbacks @@ -41,20 +41,20 @@ EXPORT_SYMBOL_GPL(avirt_pcm_period_elapsed); * pcm_open - Implements 'open' callback for PCM middle layer * @substream: pointer to ALSA PCM substream * - * This is called when an ALSA PCM substream is opened. The substream device is - * configured here. + * This is called when an ALSA PCM substream is opened. The substream device + * is configured here. * * Returns 0 on success or error code otherwise. */ static int pcm_open(struct snd_pcm_substream *substream) { - struct avirt_audiopath *audiopath; - struct avirt_stream *stream; + struct snd_avirt_audiopath *audiopath; + struct snd_avirt_stream *stream; struct snd_pcm_hardware *hw; unsigned int chans = 0; - stream = __avirt_stream_find_by_device(substream->pcm->device); - audiopath = avirt_audiopath_get(stream->map); + stream = snd_avirt_stream_find_by_device(substream->pcm->device); + audiopath = snd_avirt_audiopath_get(stream->map); CHK_NULL_V(audiopath, "Cannot find Audio Path uid: '%s'!", stream->map); substream->private_data = audiopath; @@ -62,7 +62,7 @@ static int pcm_open(struct snd_pcm_substream *substream) hw = &substream->runtime->hw; memcpy(hw, audiopath->hw, sizeof(struct snd_pcm_hardware)); - stream = __avirt_stream_find_by_device(substream->pcm->device); + stream = snd_avirt_stream_find_by_device(substream->pcm->device); if (IS_ERR_VALUE(stream) || !stream) return PTR_ERR(stream); @@ -87,7 +87,7 @@ static int pcm_close(struct snd_pcm_substream *substream) { // Do additional Audio Path 'close' callback return DO_AUDIOPATH_CB( - ((struct avirt_audiopath *)substream->private_data), close, + ((struct snd_avirt_audiopath *)substream->private_data), close, substream); } @@ -106,10 +106,10 @@ static int pcm_hw_params(struct snd_pcm_substream *substream, { int retval; size_t bufsz; - struct avirt_audiopath *audiopath; - struct avirt_stream *stream; + struct snd_avirt_audiopath *audiopath; + struct snd_avirt_stream *stream; - stream = __avirt_stream_find_by_device(substream->pcm->device); + stream = snd_avirt_stream_find_by_device(substream->pcm->device); if (IS_ERR_VALUE(stream) || !stream) return PTR_ERR(stream); @@ -120,7 +120,7 @@ static int pcm_hw_params(struct snd_pcm_substream *substream, return -EINVAL; } - audiopath = ((struct avirt_audiopath *)substream->private_data); + audiopath = ((struct snd_avirt_audiopath *)substream->private_data); bufsz = params_buffer_bytes(hw_params) * audiopath->hw->periods_max; retval = snd_pcm_lib_alloc_vmalloc_buffer(substream, bufsz); @@ -145,8 +145,8 @@ static int pcm_hw_free(struct snd_pcm_substream *substream) // Do additional Audio Path 'hw_free' callback err = DO_AUDIOPATH_CB( - ((struct avirt_audiopath *)substream->private_data), hw_free, - substream); + ((struct snd_avirt_audiopath *)substream->private_data), + hw_free, substream); return snd_pcm_lib_free_vmalloc_buffer(substream); } @@ -165,8 +165,8 @@ static int pcm_prepare(struct snd_pcm_substream *substream) { // Do additional Audio Path 'prepare' callback return DO_AUDIOPATH_CB( - ((struct avirt_audiopath *)substream->private_data), prepare, - substream); + ((struct snd_avirt_audiopath *)substream->private_data), + prepare, substream); } /** @@ -194,8 +194,8 @@ static int pcm_trigger(struct snd_pcm_substream *substream, int cmd) // Do additional Audio Path 'trigger' callback return DO_AUDIOPATH_CB( - ((struct avirt_audiopath *)substream->private_data), trigger, - substream, cmd); + ((struct snd_avirt_audiopath *)substream->private_data), + trigger, substream, cmd); } /** @@ -212,8 +212,8 @@ static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream) { // Do additional Audio Path 'pointer' callback return DO_AUDIOPATH_CB( - ((struct avirt_audiopath *)substream->private_data), pointer, - substream); + ((struct snd_avirt_audiopath *)substream->private_data), + pointer, substream); } /** @@ -235,7 +235,7 @@ static int pcm_get_time_info( struct snd_pcm_audio_tstamp_report *audio_tstamp_report) { return DO_AUDIOPATH_CB( - ((struct avirt_audiopath *)substream->private_data), + ((struct snd_avirt_audiopath *)substream->private_data), get_time_info, substream, system_ts, audio_ts, audio_tstamp_config, audio_tstamp_report); } @@ -265,8 +265,8 @@ static int pcm_copy_user(struct snd_pcm_substream *substream, int channel, // Do additional Audio Path 'copy_user' callback return DO_AUDIOPATH_CB( - ((struct avirt_audiopath *)substream->private_data), copy_user, - substream, channel, pos, src, count); + ((struct snd_avirt_audiopath *)substream->private_data), + copy_user, substream, channel, pos, src, count); } /** @@ -286,7 +286,7 @@ static int pcm_copy_kernel(struct snd_pcm_substream *substream, int channel, unsigned long pos, void *buf, unsigned long count) { return DO_AUDIOPATH_CB( - ((struct avirt_audiopath *)substream->private_data), + ((struct snd_avirt_audiopath *)substream->private_data), copy_kernel, substream, channel, pos, buf, count); } @@ -302,7 +302,7 @@ static int pcm_copy_kernel(struct snd_pcm_substream *substream, int channel, static int pcm_ack(struct snd_pcm_substream *substream) { return DO_AUDIOPATH_CB( - ((struct avirt_audiopath *)substream->private_data), ack, + ((struct snd_avirt_audiopath *)substream->private_data), ack, substream); } @@ -310,7 +310,7 @@ static int pcm_silence(struct snd_pcm_substream *substream, int channel, snd_pcm_uframes_t pos, snd_pcm_uframes_t count) { return DO_AUDIOPATH_CB( - ((struct avirt_audiopath *)substream->private_data), + ((struct snd_avirt_audiopath *)substream->private_data), fill_silence, substream, channel, pos, count); } diff --git a/configfs.c b/configfs.c index 1a7e7c5..12e95a5 100644 --- a/configfs.c +++ b/configfs.c @@ -1,9 +1,9 @@ // SPDX-License-Identifier: GPL-2.0 -/* +/* * AVIRT - ALSA Virtual Soundcard * * Copyright (c) 2010-2018 Fiberdyne Systems Pty Ltd - * + * * configfs.c - AVIRT configfs support */ @@ -17,54 +17,61 @@ #define D_PRINTK(fmt, args...) DDEBUG(D_LOGNAME, fmt, ##args) #define D_ERRORK(fmt, args...) DERROR(D_LOGNAME, fmt, ##args) -static ssize_t cfg_avirt_stream_direction_show(struct config_item *item, - char *page) +static ssize_t cfg_snd_avirt_stream_direction_show(struct config_item *item, + char *page) { ssize_t count; - struct avirt_stream *stream = avirt_stream_from_config_item(item); + struct snd_avirt_stream *stream = + snd_avirt_stream_from_config_item(item); count = sprintf(page, "%d\n", stream->direction); return count; } -CONFIGFS_ATTR_RO(cfg_avirt_stream_, direction); +CONFIGFS_ATTR_RO(cfg_snd_avirt_stream_, direction); -static ssize_t cfg_avirt_stream_map_show(struct config_item *item, char *page) +static ssize_t cfg_snd_avirt_stream_map_show(struct config_item *item, + char *page) { - struct avirt_stream *stream = avirt_stream_from_config_item(item); + struct snd_avirt_stream *stream = + snd_avirt_stream_from_config_item(item); return sprintf(page, "%s\n", stream->map); } -static ssize_t cfg_avirt_stream_map_store(struct config_item *item, - const char *page, size_t count) +static ssize_t cfg_snd_avirt_stream_map_store(struct config_item *item, + const char *page, size_t count) { char *split; - struct avirt_stream *stream = avirt_stream_from_config_item(item); + struct snd_avirt_stream *stream = + snd_avirt_stream_from_config_item(item); split = strsep((char **)&page, "\n"); memcpy(stream->map, (char *)split, count); return count; } -CONFIGFS_ATTR(cfg_avirt_stream_, map); +CONFIGFS_ATTR(cfg_snd_avirt_stream_, map); -static ssize_t cfg_avirt_stream_channels_show(struct config_item *item, - char *page) +static ssize_t cfg_snd_avirt_stream_channels_show(struct config_item *item, + char *page) { ssize_t count; - struct avirt_stream *stream = avirt_stream_from_config_item(item); + struct snd_avirt_stream *stream = + snd_avirt_stream_from_config_item(item); count = sprintf(page, "%d\n", stream->channels); return count; } -static ssize_t cfg_avirt_stream_channels_store(struct config_item *item, - const char *page, size_t count) +static ssize_t cfg_snd_avirt_stream_channels_store(struct config_item *item, + const char *page, + size_t count) { int err; - struct avirt_stream *stream = avirt_stream_from_config_item(item); + struct snd_avirt_stream *stream = + snd_avirt_stream_from_config_item(item); unsigned long tmp; char *p = (char *)page; @@ -79,37 +86,37 @@ static ssize_t cfg_avirt_stream_channels_store(struct config_item *item, return count; } -CONFIGFS_ATTR(cfg_avirt_stream_, channels); +CONFIGFS_ATTR(cfg_snd_avirt_stream_, channels); -static struct configfs_attribute *cfg_avirt_stream_attrs[] = { - &cfg_avirt_stream_attr_channels, - &cfg_avirt_stream_attr_map, - &cfg_avirt_stream_attr_direction, +static struct configfs_attribute *cfg_snd_avirt_stream_attrs[] = { + &cfg_snd_avirt_stream_attr_channels, + &cfg_snd_avirt_stream_attr_map, + &cfg_snd_avirt_stream_attr_direction, NULL, }; -static void cfg_avirt_stream_release(struct config_item *item) +static void cfg_snd_avirt_stream_release(struct config_item *item) { D_INFOK("item->name:%s", item->ci_namebuf); - kfree(avirt_stream_from_config_item(item)); + kfree(snd_avirt_stream_from_config_item(item)); } -static struct configfs_item_operations cfg_avirt_stream_ops = { - .release = cfg_avirt_stream_release, +static struct configfs_item_operations cfg_snd_avirt_stream_ops = { + .release = cfg_snd_avirt_stream_release, }; -static struct config_item_type cfg_avirt_stream_type = { - .ct_item_ops = &cfg_avirt_stream_ops, - .ct_attrs = cfg_avirt_stream_attrs, +static struct config_item_type cfg_snd_avirt_stream_type = { + .ct_item_ops = &cfg_snd_avirt_stream_ops, + .ct_attrs = cfg_snd_avirt_stream_attrs, .ct_owner = THIS_MODULE, }; static struct config_item * - cfg_avirt_stream_make_item(struct config_group *group, const char *name) +cfg_snd_avirt_stream_make_item(struct config_group *group, const char *name) { char *split; int direction; - struct avirt_stream *stream; + struct snd_avirt_stream *stream; // Get prefix (playback_ or capture_) split = strsep((char **)&name, "_"); @@ -130,24 +137,25 @@ static struct config_item * // Get stream name, and create PCM for stream split = strsep((char **)&name, "\n"); - stream = __avirt_stream_create(split, direction); + stream = snd_avirt_stream_create(split, direction); if (IS_ERR(stream)) return ERR_PTR(PTR_ERR(stream)); - config_item_init_type_name(&stream->item, name, &cfg_avirt_stream_type); + config_item_init_type_name(&stream->item, name, + &cfg_snd_avirt_stream_type); return &stream->item; } -static ssize_t cfg_avirt_stream_group_sealed_show(struct config_item *item, - char *page) +static ssize_t cfg_snd_avirt_stream_group_sealed_show(struct config_item *item, + char *page) { - return snprintf(page, PAGE_SIZE, "%d\n", __avirt_streams_sealed()); + return snprintf(page, PAGE_SIZE, "%d\n", snd_avirt_streams_sealed()); } -static ssize_t cfg_avirt_stream_group_sealed_store(struct config_item *item, - const char *page, - size_t count) +static ssize_t cfg_snd_avirt_stream_group_sealed_store(struct config_item *item, + const char *page, + size_t count) { unsigned long tmp; char *p = (char *)page; @@ -159,24 +167,24 @@ static ssize_t cfg_avirt_stream_group_sealed_store(struct config_item *item, return -ERANGE; } - __avirt_streams_seal(); + snd_avirt_streams_seal(); return count; } -CONFIGFS_ATTR(cfg_avirt_stream_group_, sealed); +CONFIGFS_ATTR(cfg_snd_avirt_stream_group_, sealed); -static struct configfs_attribute *cfg_avirt_stream_group_attrs[] = { - &cfg_avirt_stream_group_attr_sealed, +static struct configfs_attribute *cfg_snd_avirt_stream_group_attrs[] = { + &cfg_snd_avirt_stream_group_attr_sealed, NULL, }; -static struct configfs_group_operations cfg_avirt_stream_group_ops = { - .make_item = cfg_avirt_stream_make_item +static struct configfs_group_operations cfg_snd_avirt_stream_group_ops = { + .make_item = cfg_snd_avirt_stream_make_item }; static struct config_item_type cfg_stream_group_type = { - .ct_group_ops = &cfg_avirt_stream_group_ops, - .ct_attrs = cfg_avirt_stream_group_attrs, + .ct_group_ops = &cfg_snd_avirt_stream_group_ops, + .ct_attrs = cfg_snd_avirt_stream_group_attrs, .ct_owner = THIS_MODULE }; @@ -185,17 +193,15 @@ static struct config_item_type cfg_avirt_group_type = { }; static struct configfs_subsystem cfg_subsys = { - .su_group = - { - .cg_item = - { - .ci_namebuf = "avirt", + .su_group = { + .cg_item = { + .ci_namebuf = "snd-avirt", .ci_type = &cfg_avirt_group_type, }, }, }; -int __init __avirt_configfs_init(struct avirt_core *core) +int __init snd_avirt_configfs_init(struct snd_avirt_core *core) { int err; @@ -222,7 +228,7 @@ exit_configfs: return err; } -void __exit __avirt_configfs_exit(struct avirt_core *core) +void __exit snd_avirt_configfs_exit(struct snd_avirt_core *core) { configfs_unregister_default_group(core->stream_group); configfs_unregister_subsystem(&cfg_subsys); diff --git a/core.c b/core.c index 08381e5..aacc0a1 100644 --- a/core.c +++ b/core.c @@ -3,7 +3,7 @@ * AVIRT - ALSA Virtual Soundcard * * Copyright (c) 2010-2018 Fiberdyne Systems Pty Ltd - * + * * core.c - AVIRT core internals */ @@ -27,44 +27,43 @@ MODULE_LICENSE("GPL v2"); #define SND_AVIRTUAL_DRIVER "snd_avirt" -extern struct snd_pcm_ops pcm_ops; - -static struct avirt_core core = { +static struct snd_avirt_core core = { .stream_count = 0, .streams_sealed = false, }; -struct avirt_coreinfo coreinfo = { - .version = {0, 0, 1}, +struct snd_avirt_coreinfo coreinfo = { + .version = { 0, 0, 1 }, }; static LIST_HEAD(audiopath_list); -struct avirt_audiopath_obj { +struct snd_avirt_audiopath_obj { struct kobject kobj; struct list_head list; - struct avirt_audiopath *path; + struct snd_avirt_audiopath *path; }; -static struct kset *avirt_audiopath_kset; +static struct kset *snd_avirt_audiopath_kset; static struct kobject *kobj; -#define to_audiopath_obj(d) container_of(d, struct avirt_audiopath_obj, kobj) -#define to_audiopath_attr(a) \ - container_of(a, struct avirt_audiopath_attribute, attr) +#define to_audiopath_obj(d) \ + container_of(d, struct snd_avirt_audiopath_obj, kobj) +#define to_audiopath_attr(a) \ + container_of(a, struct snd_avirt_audiopath_attribute, attr) /** - * struct avirt_audiopath_attribute - access the attributes of Audio Path + * struct snd_avirt_audiopath_attribute - access the attributes of Audio Path * @attr: attributes of an Audio Path * @show: pointer to the show function * @store: pointer to the store function */ -struct avirt_audiopath_attribute { +struct snd_avirt_audiopath_attribute { struct attribute attr; - ssize_t (*show)(struct avirt_audiopath_obj *d, - struct avirt_audiopath_attribute *attr, char *buf); - ssize_t (*store)(struct avirt_audiopath_obj *d, - struct avirt_audiopath_attribute *attr, + ssize_t (*show)(struct snd_avirt_audiopath_obj *d, + struct snd_avirt_audiopath_attribute *attr, char *buf); + ssize_t (*store)(struct snd_avirt_audiopath_obj *d, + struct snd_avirt_audiopath_attribute *attr, const char *buf, size_t count); }; @@ -77,8 +76,8 @@ struct avirt_audiopath_attribute { static ssize_t audiopath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf) { - struct avirt_audiopath_attribute *audiopath_attr; - struct avirt_audiopath_obj *audiopath_obj; + struct snd_avirt_audiopath_attribute *audiopath_attr; + struct snd_avirt_audiopath_obj *audiopath_obj; audiopath_attr = to_audiopath_attr(attr); audiopath_obj = to_audiopath_obj(kobj); @@ -100,8 +99,8 @@ static ssize_t audiopath_attr_store(struct kobject *kobj, struct attribute *attr, const char *buf, size_t len) { - struct avirt_audiopath_attribute *audiopath_attr; - struct avirt_audiopath_obj *audiopath_obj; + struct snd_avirt_audiopath_attribute *audiopath_attr; + struct snd_avirt_audiopath_obj *audiopath_obj; audiopath_attr = to_audiopath_attr(attr); audiopath_obj = to_audiopath_obj(kobj); @@ -111,84 +110,88 @@ static ssize_t audiopath_attr_store(struct kobject *kobj, return audiopath_attr->store(audiopath_obj, audiopath_attr, buf, len); } -static const struct sysfs_ops avirt_audiopath_sysfs_ops = { +static const struct sysfs_ops snd_avirt_audiopath_sysfs_ops = { .show = audiopath_attr_show, .store = audiopath_attr_store, }; /** - * avirt_audiopath_release - Audio Path release function + * snd_avirt_audiopath_release - Audio Path release function * @kobj: pointer to Audio Paths's kobject */ -static void avirt_audiopath_release(struct kobject *kobj) +static void snd_avirt_audiopath_release(struct kobject *kobj) { - struct avirt_audiopath_obj *audiopath_obj = to_audiopath_obj(kobj); + struct snd_avirt_audiopath_obj *audiopath_obj = to_audiopath_obj(kobj); kfree(audiopath_obj); } -static ssize_t audiopath_name_show(struct avirt_audiopath_obj *audiopath_obj, - struct avirt_audiopath_attribute *attr, - char *buf) +static ssize_t + audiopath_name_show(struct snd_avirt_audiopath_obj *audiopath_obj, + struct snd_avirt_audiopath_attribute *attr, + char *buf) { return sprintf(buf, "%s\n", audiopath_obj->path->name); } -static ssize_t audiopath_version_show(struct avirt_audiopath_obj *audiopath_obj, - struct avirt_audiopath_attribute *attr, - char *buf) +static ssize_t + audiopath_version_show(struct snd_avirt_audiopath_obj *audiopath_obj, + struct snd_avirt_audiopath_attribute *attr, + char *buf) { - struct avirt_audiopath *audiopath = audiopath_obj->path; + struct snd_avirt_audiopath *audiopath = audiopath_obj->path; return sprintf(buf, "%d.%d.%d\n", audiopath->version[0], audiopath->version[1], audiopath->version[2]); } -static struct avirt_audiopath_attribute avirt_audiopath_attrs[] = { +static struct snd_avirt_audiopath_attribute snd_avirt_audiopath_attrs[] = { __ATTR_RO(audiopath_name), __ATTR_RO(audiopath_version), }; -static struct attribute *avirt_audiopath_def_attrs[] = { - &avirt_audiopath_attrs[0].attr, - &avirt_audiopath_attrs[1].attr, +static struct attribute *snd_avirt_audiopath_def_attrs[] = { + &snd_avirt_audiopath_attrs[0].attr, + &snd_avirt_audiopath_attrs[1].attr, NULL, }; -static struct kobj_type avirt_audiopath_ktype = { - .sysfs_ops = &avirt_audiopath_sysfs_ops, - .release = avirt_audiopath_release, - .default_attrs = avirt_audiopath_def_attrs, +static struct kobj_type snd_avirt_audiopath_ktype = { + .sysfs_ops = &snd_avirt_audiopath_sysfs_ops, + .release = snd_avirt_audiopath_release, + .default_attrs = snd_avirt_audiopath_def_attrs, }; /** - * create_avirt_audiopath_obj - creates an Audio Path object + * create_snd_avirt_audiopath_obj - creates an Audio Path object * @uid: Unique ID of the Audio Path * * This creates an Audio Path object and assigns the kset and registers * it with sysfs. * @return: Pointer to the Audio Path object or NULL if it failed. */ -static struct avirt_audiopath_obj *create_avirt_audiopath_obj(const char *uid) +static struct snd_avirt_audiopath_obj * + create_snd_avirt_audiopath_obj(const char *uid) { - struct avirt_audiopath_obj *avirt_audiopath; + struct snd_avirt_audiopath_obj *snd_avirt_audiopath; int retval; - avirt_audiopath = kzalloc(sizeof(*avirt_audiopath), GFP_KERNEL); - if (!avirt_audiopath) + snd_avirt_audiopath = kzalloc(sizeof(*snd_avirt_audiopath), GFP_KERNEL); + if (!snd_avirt_audiopath) return NULL; - avirt_audiopath->kobj.kset = avirt_audiopath_kset; - retval = kobject_init_and_add(&avirt_audiopath->kobj, - &avirt_audiopath_ktype, kobj, "%s", uid); + snd_avirt_audiopath->kobj.kset = snd_avirt_audiopath_kset; + retval = kobject_init_and_add(&snd_avirt_audiopath->kobj, + &snd_avirt_audiopath_ktype, kobj, "%s", + uid); if (retval) { - kobject_put(&avirt_audiopath->kobj); + kobject_put(&snd_avirt_audiopath->kobj); return NULL; } - kobject_uevent(&avirt_audiopath->kobj, KOBJ_ADD); - return avirt_audiopath; + kobject_uevent(&snd_avirt_audiopath->kobj, KOBJ_ADD); + return snd_avirt_audiopath; } -static struct snd_pcm *pcm_create(struct avirt_stream *stream) +static struct snd_pcm *pcm_create(struct snd_avirt_stream *stream) { bool playback = false, capture = false; struct snd_pcm *pcm; @@ -224,25 +227,24 @@ static struct snd_pcm *pcm_create(struct avirt_stream *stream) } /** - * destroy_avirt_audiopath_obj - destroys an Audio Path object + * destroy_snd_avirt_audiopath_obj - destroys an Audio Path object * @name: the Audio Path object */ -static void destroy_avirt_audiopath_obj(struct avirt_audiopath_obj *p) +static void destroy_snd_avirt_audiopath_obj(struct snd_avirt_audiopath_obj *p) { kobject_put(&p->kobj); } /** - * avirt_audiopath_get - retrieves the Audio Path by its UID + * snd_avirt_audiopath_get - retrieves the Audio Path by its UID * @uid: Unique ID for the Audio Path * @return: Corresponding Audio Path */ -struct avirt_audiopath *avirt_audiopath_get(const char *uid) +struct snd_avirt_audiopath *snd_avirt_audiopath_get(const char *uid) { - struct avirt_audiopath_obj *ap_obj; + struct snd_avirt_audiopath_obj *ap_obj; - list_for_each_entry(ap_obj, &audiopath_list, list) - { + list_for_each_entry(ap_obj, &audiopath_list, list) { // pr_info("get_ap %s\n", ap_obj->path->uid); if (!strcmp(ap_obj->path->uid, uid)) return ap_obj->path; @@ -252,22 +254,22 @@ struct avirt_audiopath *avirt_audiopath_get(const char *uid) } /** - * avirt_audiopath_register - register Audio Path with ALSA virtual driver + * snd_avirt_audiopath_register - register Audio Path with AVIRT * @audiopath: Audio Path to be registered * @core: ALSA virtual driver core info * @return: 0 on success or error code otherwise */ -int avirt_audiopath_register(struct avirt_audiopath *audiopath, - struct avirt_coreinfo **info) +int snd_avirt_audiopath_register(struct snd_avirt_audiopath *audiopath, + struct snd_avirt_coreinfo **info) { - struct avirt_audiopath_obj *audiopath_obj; + struct snd_avirt_audiopath_obj *audiopath_obj; if (!audiopath) { D_ERRORK("Audio Path is NULL!"); return -EINVAL; } - audiopath_obj = create_avirt_audiopath_obj(audiopath->uid); + audiopath_obj = create_snd_avirt_audiopath_obj(audiopath->uid); if (!audiopath_obj) { D_INFOK("Failed to alloc driver object"); return -ENOMEM; @@ -288,16 +290,16 @@ int avirt_audiopath_register(struct avirt_audiopath *audiopath, return 0; } -EXPORT_SYMBOL_GPL(avirt_audiopath_register); +EXPORT_SYMBOL_GPL(snd_avirt_audiopath_register); /** - * avirt_audiopath_deregister - deregister Audio Path with ALSA virtual driver + * snd_avirt_audiopath_deregister - deregister Audio Path with AVIRT * @audiopath: Audio Path to be deregistered * @return: 0 on success or error code otherwise */ -int avirt_audiopath_deregister(struct avirt_audiopath *audiopath) +int snd_avirt_audiopath_deregister(struct snd_avirt_audiopath *audiopath) { - struct avirt_audiopath_obj *audiopath_obj; + struct snd_avirt_audiopath_obj *audiopath_obj; // Check if audio path is registered if (!audiopath) { @@ -312,32 +314,31 @@ int avirt_audiopath_deregister(struct avirt_audiopath *audiopath) } list_del(&audiopath_obj->list); - destroy_avirt_audiopath_obj(audiopath_obj); + destroy_snd_avirt_audiopath_obj(audiopath_obj); D_INFOK("Deregistered Audio Path %s", audiopath->uid); return 0; } -EXPORT_SYMBOL_GPL(avirt_audiopath_deregister); +EXPORT_SYMBOL_GPL(snd_avirt_audiopath_deregister); /** - * avirt_stream_count - get the stream count for the given direction + * snd_avirt_stream_count - get the stream count for the given direction * @direction: The direction to get the stream count for * @return: The stream count */ -int avirt_stream_count(unsigned int direction) +int snd_avirt_stream_count(unsigned int direction) { struct list_head *entry; struct config_item *item; - struct avirt_stream *stream; + struct snd_avirt_stream *stream; unsigned int count = 0; if (direction > 1) return -ERANGE; - list_for_each(entry, &core.stream_group->cg_children) - { + list_for_each(entry, &core.stream_group->cg_children) { item = container_of(entry, struct config_item, ci_entry); - stream = avirt_stream_from_config_item(item); + stream = snd_avirt_stream_from_config_item(item); if (!stream) return -EFAULT; if (stream->direction == direction) @@ -346,18 +347,19 @@ int avirt_stream_count(unsigned int direction) return count; } -EXPORT_SYMBOL_GPL(avirt_stream_count); +EXPORT_SYMBOL_GPL(snd_avirt_stream_count); /** - * __avirt_stream_create - Create audio stream, including it's ALSA PCM device + * snd_avirt_stream_create - Create audio stream, including it's ALSA PCM device * @name: The name designated to the audio stream * @direction: The PCM direction (SNDRV_PCM_STREAM_PLAYBACK or * SNDRV_PCM_STREAM_CAPTURE) * @return: The newly created audio stream if successful, or an error pointer */ -struct avirt_stream *__avirt_stream_create(const char *name, int direction) +struct snd_avirt_stream *snd_avirt_stream_create(const char *name, + int direction) { - struct avirt_stream *stream; + struct snd_avirt_stream *stream; stream = kzalloc(sizeof(*stream), GFP_KERNEL); if (!stream) @@ -374,11 +376,11 @@ struct avirt_stream *__avirt_stream_create(const char *name, int direction) return stream; } -int __avirt_streams_seal(void) +int snd_avirt_streams_seal(void) { int err = 0; - struct avirt_audiopath_obj *ap_obj; - struct avirt_stream *stream; + struct snd_avirt_audiopath_obj *ap_obj; + struct snd_avirt_stream *stream; struct config_item *item; struct list_head *entry; @@ -387,9 +389,9 @@ int __avirt_streams_seal(void) return -1; } - list_for_each (entry, &core.stream_group->cg_children) { + list_for_each(entry, &core.stream_group->cg_children) { item = container_of(entry, struct config_item, ci_entry); - stream = avirt_stream_from_config_item(item); + stream = snd_avirt_stream_from_config_item(item); if (!stream) return -EFAULT; stream->pcm = pcm_create(stream); @@ -397,7 +399,7 @@ int __avirt_streams_seal(void) return (PTR_ERR(stream->pcm)); } - list_for_each_entry (ap_obj, &audiopath_list, list) { + list_for_each_entry(ap_obj, &audiopath_list, list) { D_INFOK("configure() AP uid: %s", ap_obj->path->uid); ap_obj->path->configure(core.card, core.stream_group, core.stream_count); @@ -414,14 +416,14 @@ int __avirt_streams_seal(void) return err; } -bool __avirt_streams_sealed(void) +bool snd_avirt_streams_sealed(void) { return core.streams_sealed; } -struct avirt_stream *__avirt_stream_find_by_device(unsigned int device) +struct snd_avirt_stream *snd_avirt_stream_find_by_device(unsigned int device) { - struct avirt_stream *stream; + struct snd_avirt_stream *stream; struct config_item *item; struct list_head *entry; @@ -430,10 +432,9 @@ struct avirt_stream *__avirt_stream_find_by_device(unsigned int device) return ERR_PTR(-EINVAL); } - list_for_each(entry, &core.stream_group->cg_children) - { + list_for_each(entry, &core.stream_group->cg_children) { item = container_of(entry, struct config_item, ci_entry); - stream = avirt_stream_from_config_item(item); + stream = snd_avirt_stream_from_config_item(item); if (!stream) return ERR_PTR(-EFAULT); if (stream->device == device) @@ -478,14 +479,14 @@ static int __init core_init(void) strncpy(core.card->longname, "A virtual sound card driver for ALSA", 80); - avirt_audiopath_kset = + snd_avirt_audiopath_kset = kset_create_and_add("audiopaths", NULL, &core.dev->kobj); - if (!avirt_audiopath_kset) { + if (!snd_avirt_audiopath_kset) { err = -ENOMEM; goto exit_snd_card; } - err = __avirt_configfs_init(&core); + err = snd_avirt_configfs_init(&core); if (err < 0) goto exit_snd_card; @@ -506,9 +507,9 @@ exit_class: */ static void __exit core_exit(void) { - __avirt_configfs_exit(&core); + snd_avirt_configfs_exit(&core); - kset_unregister(avirt_audiopath_kset); + kset_unregister(snd_avirt_audiopath_kset); snd_card_free(core.card); device_destroy(core.avirt_class, 0); class_destroy(core.avirt_class); diff --git a/core.h b/core.h index ca47067..735df65 100644 --- a/core.h +++ b/core.h @@ -14,7 +14,9 @@ #include "utils.h" -struct avirt_core { +extern struct snd_pcm_ops pcm_ops; + +struct snd_avirt_core { struct snd_card *card; struct device *dev; struct class *avirt_class; @@ -24,44 +26,45 @@ struct avirt_core { }; /** - * __avirt_configfs_init - Initialise the configfs system - * @core: The avirt_core pointer + * snd_avirt_configfs_init - Initialise the configfs system + * @core: The snd_avirt_core pointer * @return: 0 on success, negative ERRNO on failure */ -int __init __avirt_configfs_init(struct avirt_core *core); +int __init snd_avirt_configfs_init(struct snd_avirt_core *core); /** - * __avirt_configfs_exit - Clean up and exit the configfs system - * @core: The avirt_core pointer + * snd_avirt_configfs_exit - Clean up and exit the configfs system + * @core: The snd_avirt_core pointer */ -void __exit __avirt_configfs_exit(struct avirt_core *core); +void __exit snd_avirt_configfs_exit(struct snd_avirt_core *core); /** - * __avirt_streams_seal - Register the sound card to user space + * snd_avirt_streams_seal - Register the sound card to user space * @return: 0 on success, negative ERRNO on failure */ -int __avirt_streams_seal(void); +int snd_avirt_streams_seal(void); /** - * __avirt_streams_sealed - Check whether the streams have been sealed or not + * snd_avirt_streams_sealed - Check if the streams have been sealed or not * @return: true if sealed, false otherwise */ -bool __avirt_streams_sealed(void); +bool snd_avirt_streams_sealed(void); /** - * __avirt_stream_find_by_device - Get audio stream from device number + * snd_avirt_stream_find_by_device - Get audio stream from device number * @device: The PCM device number corresponding to the desired stream * @return: The audio stream if found, or an error pointer otherwise */ -struct avirt_stream *__avirt_stream_find_by_device(unsigned int device); +struct snd_avirt_stream *snd_avirt_stream_find_by_device(unsigned int device); /** - * __avirt_stream_create - Create audio stream, including it's ALSA PCM device + * snd_avirt_stream_create - Create audio stream, including it's ALSA PCM device * @name: The name designated to the audio stream * @direction: The PCM direction (SNDRV_PCM_STREAM_PLAYBACK or * SNDRV_PCM_STREAM_CAPTURE) * @return: The newly created audio stream if successful, or an error pointer */ -struct avirt_stream *__avirt_stream_create(const char *name, int direction); +struct snd_avirt_stream *snd_avirt_stream_create(const char *name, + int direction); #endif /* __SOUND_AVIRT_CORE_H */ diff --git a/docs/2.Building.md b/docs/2.Building.md index 3e42746..5fd9d23 100644 --- a/docs/2.Building.md +++ b/docs/2.Building.md @@ -28,7 +28,7 @@ $ make or ```sh -$ CONFIG_AVIRT=m CONFIG_AVIRT_BUILDLOCAL=y CONFIG_AVIRT_DUMMYAP=m make -C /lib/modules/$(uname -r)/build/ M=$(pwd) +$ CONFIG_AVIRT=m CONFIG_AVIRT_BUILDLOCAL=y CONFIG_AVIRT_AP_DUMMY=m CONFIG_AVIRT_AP_LOOPBACK=m make -C /lib/modules/$(uname -r)/build/ M=$(pwd) ``` The latter is executed internally with the make file. diff --git a/docs/3.Usage.md b/docs/3.Usage.md index d65d50d..f535a07 100644 --- a/docs/3.Usage.md +++ b/docs/3.Usage.md @@ -18,9 +18,9 @@ As root, load the required `avirt_core.ko` and subsequent audio path. As an example, the Dummy Audiopath is being loaded here. ```sh -insmod avirt_core.ko -insmod dummy/avirt_dummyap.ko -insmod loopback/avirt_loopbackap.ko +insmod snd-avirt-core.ko +insmod dummy/snd-avirt-ap-dummy.ko +insmod loopback/snd-avirt-ap-loopback.ko ``` ### Note: @@ -47,16 +47,16 @@ For example, in AGL: ```sh mkdir -p /lib/modules/$(uname -r)/extra -cp avirt_core.ko avirt_dummyap.ko avirt_loopbackap.ko /lib/modules/$(uname -r)/extra +cp snd-avirt-core.ko snd-avirt-ap-dummy.ko snd-avirt-ap-loopback.ko /lib/modules/$(uname -r)/extra depmod ``` Once the modules are in place, we can load the modules using: ```sh -modprobe avirt_core -modprobe avirt_dummyap -modprobe avirt_loopbackap +modprobe snd-avirt-core +modprobe snd-avirt-ap-dummy +modprobe snd-avirt-ap-loopback ``` @@ -93,24 +93,24 @@ Finally, we can configure AVIRT, for example: ```sh # Set up each of the cards channels -mkdir /config/avirt/streams/playback_media -echo "2">/config/avirt/streams/playback_media/channels -echo "ap_dummy">/config/avirt/streams/playback_media/map +mkdir /config/snd-avirt/streams/playback_media +echo "2">/config/snd-avirt/streams/playback_media/channels +echo "ap_dummy">/config/snd-avirt/streams/playback_media/map -mkdir /config/avirt/streams/playback_navigation -echo "1">/config/avirt/streams/playback_navigation/channels -echo "ap_dummy">/config/avirt/streams/playback_navigation/map +mkdir /config/snd-avirt/streams/playback_navigation +echo "1">/config/snd-avirt/streams/playback_navigation/channels +echo "ap_dummy">/config/snd-avirt/streams/playback_navigation/map -mkdir /config/avirt/streams/playback_emergency -echo "1">/config/avirt/streams/playback_emergency/channels -echo "ap_dummy">/config/avirt/streams/playback_emergency/map +mkdir /config/snd-avirt/streams/playback_emergency +echo "1">/config/snd-avirt/streams/playback_emergency/channels +echo "ap_dummy">/config/snd-avirt/streams/playback_emergency/map -mkdir /config/avirt/streams/capture_voice -echo "1">/config/avirt/streams/capture_voice/channels -echo "ap_dummy">/config/avirt/streams/capture_voice/map +mkdir /config/snd-avirt/streams/capture_voice +echo "1">/config/snd-avirt/streams/capture_voice/channels +echo "ap_dummy">/config/snd-avirt/streams/capture_voice/map # Finally, seal the card, and initiate configuration -echo "1">/config/avirt/streams/sealed +echo "1">/config/snd-avirt/streams/sealed ``` Alternatively, the test script at `scripts/test_configfs.sh` can be used. diff --git a/dummy/Makefile b/dummy/Makefile index 78579d7..11ceceb 100644 --- a/dummy/Makefile +++ b/dummy/Makefile @@ -1,6 +1,6 @@ -obj-$(CONFIG_AVIRT_DUMMYAP) += avirt_dummyap.o +obj-$(CONFIG_AVIRT_AP_DUMMY) += snd-avirt-ap-dummy.o $(info $(src)) -avirt_dummyap-objs := dummy.o +snd-avirt-ap-dummy-objs := dummy.o ccflags-y += -Idrivers/staging/ ccflags-y += -I$(src)/../ diff --git a/dummy/dummy.c b/dummy/dummy.c index 100972a..5f19564 100644 --- a/dummy/dummy.c +++ b/dummy/dummy.c @@ -34,7 +34,7 @@ MODULE_LICENSE("GPL v2"); #define get_dummy_ops(substream) \ (*(const struct dummy_timer_ops **)(substream)->runtime->private_data) -static struct avirt_coreinfo *coreinfo; +static struct snd_avirt_coreinfo *coreinfo; /******************************************************************************* * System Timer Interface @@ -140,7 +140,7 @@ static void dummy_systimer_callback(struct timer_list *t) dpcm->elapsed = 0; spin_unlock_irqrestore(&dpcm->lock, flags); if (elapsed) - avirt_pcm_period_elapsed(dpcm->substream); + snd_avirt_pcm_period_elapsed(dpcm->substream); } static snd_pcm_uframes_t @@ -241,18 +241,18 @@ static struct snd_pcm_ops dummyap_pcm_ops = { /******************************************************************************* * Dummy Audio Path AVIRT registration ******************************************************************************/ -int dummy_configure(struct snd_card *card, - struct config_group *avirt_stream_group, - unsigned int stream_count) +static int dummy_configure(struct snd_card *card, + struct config_group *snd_avirt_stream_group, + unsigned int stream_count) { // Do something with streams struct list_head *entry; - list_for_each (entry, &avirt_stream_group->cg_children) { + list_for_each (entry, &snd_avirt_stream_group->cg_children) { struct config_item *item = container_of(entry, struct config_item, ci_entry); - struct avirt_stream *stream = - avirt_stream_from_config_item(item); + struct snd_avirt_stream *stream = + snd_avirt_stream_from_config_item(item); AP_INFOK("stream name:%s device:%d channels:%d", stream->name, stream->device, stream->channels); } @@ -272,7 +272,7 @@ static struct snd_pcm_hardware dummyap_hw = { .periods_max = DUMMY_PERIODS_MAX, }; -static struct avirt_audiopath dummyap_module = { +static struct snd_avirt_audiopath dummyap_module = { .uid = "ap_dummy", .name = "Dummy Audio Path", .version = { 0, 0, 1 }, @@ -287,7 +287,7 @@ static int __init dummy_init(void) pr_info("init()\n"); - err = avirt_audiopath_register(&dummyap_module, &coreinfo); + err = snd_avirt_audiopath_register(&dummyap_module, &coreinfo); if ((err < 0) || (!coreinfo)) { pr_err("%s: coreinfo is NULL!\n", __func__); return err; @@ -300,7 +300,7 @@ static void __exit dummy_exit(void) { pr_info("exit()\n"); - avirt_audiopath_deregister(&dummyap_module); + snd_avirt_audiopath_deregister(&dummyap_module); } module_init(dummy_init); diff --git a/loopback/Makefile b/loopback/Makefile index 9d3c606..0546168 100644 --- a/loopback/Makefile +++ b/loopback/Makefile @@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 -obj-$(CONFIG_AVIRT_LOOPBACKAP) += avirt_loopbackap.o +obj-$(CONFIG_AVIRT_AP_LOOPBACK) += snd-avirt-ap-loopback.o $(info $(src)) -avirt_loopbackap-objs := loopback.o +snd-avirt-ap-loopback-objs := loopback.o ccflags-y += -Idrivers/staging/ ccflags-y += -I$(src)/../ diff --git a/loopback/loopback.c b/loopback/loopback.c index ba9225e..b4a82e5 100644 --- a/loopback/loopback.c +++ b/loopback/loopback.c @@ -46,7 +46,7 @@ MODULE_LICENSE("GPL"); #define NO_PITCH 100000 -static struct avirt_coreinfo *coreinfo; +static struct snd_avirt_coreinfo *coreinfo; static struct loopback *loopback; struct loopback_pcm; @@ -509,7 +509,7 @@ static void loopback_timer_function(struct timer_list *t) dpcm->period_update_pending = 0; spin_unlock_irqrestore(&dpcm->cable->lock, flags); /* need to unlock before calling below */ - avirt_pcm_period_elapsed(dpcm->substream); + snd_avirt_pcm_period_elapsed(dpcm->substream); return; } } @@ -529,7 +529,7 @@ static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream) return bytes_to_frames(runtime, pos); } -static const struct snd_pcm_hardware loopback_pcm_hardware = { +static const struct snd_pcm_hardware loopbackap_pcm_hardware = { .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME), @@ -661,7 +661,7 @@ static int loopback_open(struct snd_pcm_substream *substream) goto unlock; } spin_lock_init(&cable->lock); - cable->hw = loopback_pcm_hardware; + cable->hw = loopbackap_pcm_hardware; loopback->cables[substream->pcm->device] = cable; } dpcm->cable = cable; @@ -689,7 +689,7 @@ static int loopback_open(struct snd_pcm_substream *substream) runtime->private_data = dpcm; runtime->private_free = loopback_runtime_free; if (get_notify(dpcm)) - runtime->hw = loopback_pcm_hardware; + runtime->hw = loopbackap_pcm_hardware; else runtime->hw = cable->hw; @@ -936,7 +936,7 @@ static int loopback_mixer_new(struct loopback *loopback, int notify) int dev, dev_count, idx, err; strcpy(card->mixername, "Loopback Mixer"); - dev_count = avirt_stream_count(SNDRV_PCM_STREAM_PLAYBACK); + dev_count = snd_avirt_stream_count(SNDRV_PCM_STREAM_PLAYBACK); for (dev = 0; dev < dev_count; dev++) { pcm = loopback->pcm[dev]; setup = &loopback->setup[dev]; @@ -1043,9 +1043,9 @@ static int loopback_proc_new(struct loopback *loopback, int cidx) return 0; } -int loopbackap_configure(struct snd_card *card, - struct config_group *avirt_stream_group, - unsigned int stream_count) +static int loopbackap_configure(struct snd_card *card, + struct config_group *snd_avirt_stream_group, + unsigned int stream_count) { int err; struct list_head *entry; @@ -1056,11 +1056,11 @@ int loopbackap_configure(struct snd_card *card, loopback->card = card; mutex_init(&loopback->cable_lock); - list_for_each (entry, &avirt_stream_group->cg_children) { + list_for_each (entry, &snd_avirt_stream_group->cg_children) { struct config_item *item = container_of(entry, struct config_item, ci_entry); - struct avirt_stream *stream = - avirt_stream_from_config_item(item); + struct snd_avirt_stream *stream = + snd_avirt_stream_from_config_item(item); loopback->pcm[stream->device] = stream->pcm; AP_INFOK("stream name:%s device:%d channels:%d", stream->name, @@ -1080,11 +1080,11 @@ int loopbackap_configure(struct snd_card *card, /******************************************************************************* * Loopback Audio Path AVIRT registration ******************************************************************************/ -static struct avirt_audiopath loopbackap_module = { +static struct snd_avirt_audiopath loopbackap_module = { .uid = AP_UID, .name = "Loopback Audio Path", .version = { 0, 0, 1 }, - .hw = &loopback_pcm_hardware, + .hw = &loopbackap_pcm_hardware, .pcm_ops = &loopbackap_pcm_ops, .configure = loopbackap_configure, }; @@ -1093,7 +1093,7 @@ static int __init alsa_card_loopback_init(void) { int err = 0; - err = avirt_audiopath_register(&loopbackap_module, &coreinfo); + err = snd_avirt_audiopath_register(&loopbackap_module, &coreinfo); if ((err < 0) || (!coreinfo)) { AP_ERRORK("coreinfo is NULL!"); return err; @@ -1104,7 +1104,7 @@ static int __init alsa_card_loopback_init(void) static void __exit alsa_card_loopback_exit(void) { - avirt_audiopath_deregister(&loopbackap_module); + snd_avirt_audiopath_deregister(&loopbackap_module); } module_init(alsa_card_loopback_init); diff --git a/scripts/load.sh b/scripts/load.sh index 82f613a..103a63b 100755 --- a/scripts/load.sh +++ b/scripts/load.sh @@ -1,11 +1,11 @@ #!/bin/sh # Load the core -insmod avirt_core.ko +insmod snd-avirt-core.ko # Load the additional Audio Paths -insmod dummy/avirt_dummyap.ko -insmod loopback/avirt_loopbackap.ko +insmod dummy/snd-avirt-ap-dummy.ko +insmod loopback/snd-avirt-ap-loopback.ko # Run the test script ./scripts/test_configfs.sh diff --git a/scripts/make-agl.sh b/scripts/make-agl.sh index 4421af8..6c94fbb 100755 --- a/scripts/make-agl.sh +++ b/scripts/make-agl.sh @@ -7,6 +7,8 @@ long_sdkpath=$(xds-cli sdks get $sdk_id | grep Path) sdkpath=${long_sdkpath:4} # Build -/opt/AGL/bin/xds-cli exec --config xds-project.conf -- \ - LDFLAGS= CONFIG_AVIRT=m CONFIG_AVIRT_BUILDLOCAL=y CONFIG_AVIRT_DUMMYAP=m \ +/opt/AGL/bin/xds-cli exec --config xds-project.conf -- \ + LDFLAGS= CONFIG_AVIRT=m CONFIG_AVIRT_BUILDLOCAL=y \ + CONFIG_AVIRT_AP_DUMMY=m \ + CONFIG_AVIRT_AP_LOOPBACK=m \ make -C $sdkpath/sysroots/aarch64-agl-linux/usr/src/kernel M=$(pwd) $@ diff --git a/scripts/test_configfs.sh b/scripts/test_configfs.sh index baa5e58..b6a56f6 100755 --- a/scripts/test_configfs.sh +++ b/scripts/test_configfs.sh @@ -2,20 +2,20 @@ mkdir -p /config && mount -t configfs none /config -mkdir /config/avirt/streams/playback_media -echo "2">/config/avirt/streams/playback_media/channels -echo "ap_loopback">/config/avirt/streams/playback_media/map +mkdir /config/snd-avirt/streams/playback_media +echo "2">/config/snd-avirt/streams/playback_media/channels +echo "ap_loopback">/config/snd-avirt/streams/playback_media/map -mkdir /config/avirt/streams/playback_navigation -echo "1">/config/avirt/streams/playback_navigation/channels -echo "ap_loopback">/config/avirt/streams/playback_navigation/map +mkdir /config/snd-avirt/streams/playback_navigation +echo "1">/config/snd-avirt/streams/playback_navigation/channels +echo "ap_loopback">/config/snd-avirt/streams/playback_navigation/map -mkdir /config/avirt/streams/playback_emergency -echo "1">/config/avirt/streams/playback_emergency/channels -echo "ap_loopback">/config/avirt/streams/playback_emergency/map +mkdir /config/snd-avirt/streams/playback_emergency +echo "1">/config/snd-avirt/streams/playback_emergency/channels +echo "ap_loopback">/config/snd-avirt/streams/playback_emergency/map -mkdir /config/avirt/streams/capture_voice -echo "1">/config/avirt/streams/capture_voice/channels -echo "ap_loopback">/config/avirt/streams/capture_voice/map +mkdir /config/snd-avirt/streams/capture_voice +echo "1">/config/snd-avirt/streams/capture_voice/channels +echo "ap_loopback">/config/snd-avirt/streams/capture_voice/map -echo "1">/config/avirt/streams/sealed +echo "1">/config/snd-avirt/streams/sealed diff --git a/scripts/unload.sh b/scripts/unload.sh index 2c158d5..a18e1f5 100755 --- a/scripts/unload.sh +++ b/scripts/unload.sh @@ -1,13 +1,13 @@ #!/bin/sh -rmdir /config/avirt/streams/playback_* -rmdir /config/avirt/streams/capture_* +rmdir /config/snd-avirt/streams/playback_* +rmdir /config/snd-avirt/streams/capture_* rm_module() { lsmod |grep "^$1\>" && rmmod $1 || true } -rm_module avirt_loopbackap -rm_module avirt_dummyap -rm_module avirt_core +rm_module snd_avirt_ap_loopback +rm_module snd_avirt_ap_dummy +rm_module snd_avirt_core echo "Drivers Removed!" diff --git a/sound/avirt.h b/sound/avirt.h index 5df84fe..1a5c546 100644 --- a/sound/avirt.h +++ b/sound/avirt.h @@ -17,34 +17,34 @@ #define MAX_STREAMS 16 #define MAX_NAME_LEN 80 -#define DINFO(logname, fmt, args...) \ +#define DINFO(logname, fmt, args...) \ snd_printk(KERN_INFO "AVIRT: %s: " fmt "\n", logname, ##args) -#define DERROR(logname, fmt, args...) \ +#define DERROR(logname, fmt, args...) \ snd_printk(KERN_ERR "AVIRT: %s: " fmt "\n", logname, ##args) -#define DDEBUG(logname, fmt, args...) \ +#define DDEBUG(logname, fmt, args...) \ snd_printk(KERN_DEBUG "AVIRT: %s: " fmt "\n", logname, ##args) /** * AVIRT Audio Path configure function type - * Each Audio Path registers this at avirt_audiopath_register time. + * Each Audio Path registers this at snd_avirt_audiopath_register time. * It is then called by the core once AVIRT has been configured */ -typedef int (*avirt_audiopath_configure)(struct snd_card *card, - struct config_group *stream_group, - unsigned int stream_count); +typedef int (*snd_avirt_audiopath_configure)(struct snd_card *card, + struct config_group *stream_group, + unsigned int stream_count); /** * AVIRT Audio Path info */ -struct avirt_audiopath { +struct snd_avirt_audiopath { const char *uid; /* Unique identifier */ const char *name; /* Pretty name */ unsigned int version[3]; /* Version - Major.Minor.Ext */ const struct snd_pcm_hardware *hw; /* ALSA PCM HW conf */ const struct snd_pcm_ops *pcm_ops; /* ALSA PCM op table */ - avirt_audiopath_configure configure; /* Configure callback function */ + snd_avirt_audiopath_configure configure; /* Config callback function */ void *context; }; @@ -52,7 +52,7 @@ struct avirt_audiopath { /* * Audio stream configuration */ -struct avirt_stream { +struct snd_avirt_stream { char name[MAX_NAME_LEN]; /* Stream name */ char map[MAX_NAME_LEN]; /* Stream Audio Path mapping */ unsigned int channels; /* Stream channel count */ @@ -65,58 +65,58 @@ struct avirt_stream { /** * AVIRT core info */ -struct avirt_coreinfo { +struct snd_avirt_coreinfo { unsigned int version[3]; }; /** - * avirt_audiopath_register - register Audio Path with ALSA virtual driver + * snd_avirt_audiopath_register - register Audio Path with AVIRT * @audiopath: Audio Path to be registered * @core: ALSA virtual driver core info * @return: 0 on success or error code otherwise */ -int avirt_audiopath_register(struct avirt_audiopath *audiopath, - struct avirt_coreinfo **coreinfo); +int snd_avirt_audiopath_register(struct snd_avirt_audiopath *audiopath, + struct snd_avirt_coreinfo **coreinfo); /** - * avirt_audiopath_deregister - deregister Audio Path with ALSA virtual driver + * snd_avirt_audiopath_deregister - deregister Audio Path with AVIRT * @audiopath: Audio Path to be deregistered * @return: 0 on success or error code otherwise */ -int avirt_audiopath_deregister(struct avirt_audiopath *audiopath); +int snd_avirt_audiopath_deregister(struct snd_avirt_audiopath *audiopath); /** - * avirt_audiopath_get - retrieves the Audio Path by it's UID + * snd_avirt_audiopath_get - retrieves the Audio Path by it's UID * @uid: Unique ID for the Audio Path * @return: Corresponding Audio Path */ -struct avirt_audiopath *avirt_audiopath_get(const char *uid); +struct snd_avirt_audiopath *snd_avirt_audiopath_get(const char *uid); /** - * avirt_stream_count - get the stream count for the given direction + * snd_avirt_stream_count - get the stream count for the given direction * @direction: The direction to get the stream count for * @return: The stream count */ -int avirt_stream_count(unsigned int direction); +int snd_avirt_stream_count(unsigned int direction); /** - * avirt_stream_from_config_item - Convert a config_item to an avirt_stream + * snd_avirt_stream_from_config_item - Convert config_item to a snd_avirt_stream * @item: The config_item to convert from - * @return: The item's avirt_stream if successful, NULL otherwise + * @return: The item's snd_avirt_stream if successful, NULL otherwise */ -static inline struct avirt_stream * -avirt_stream_from_config_item(struct config_item *item) +static inline struct snd_avirt_stream * + snd_avirt_stream_from_config_item(struct config_item *item) { - return item ? container_of(item, struct avirt_stream, item) : NULL; + return item ? container_of(item, struct snd_avirt_stream, item) : NULL; } /** - * avirt_pcm_period_elapsed - PCM buffer complete callback + * snd_avirt_pcm_period_elapsed - PCM buffer complete callback * @substream: pointer to ALSA PCM substream * * This should be called from a child Audio Path once it has finished processing * the PCM buffer */ -void avirt_pcm_period_elapsed(struct snd_pcm_substream *substream); +void snd_avirt_pcm_period_elapsed(struct snd_pcm_substream *substream); #endif // __SOUND_AVIRT_H -- cgit 1.2.3-korg