mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-06 03:20:14 +08:00
Microphone: kwsnd gains a capture substream fed by the host (SDK option soundIn, the page's Microphone setting and ?mic=0, the CLI's --sound-in-wav and --sound-in). Camera: kwcam is a V4L2 capture device (/dev/video0) fed by the host: I420, NV12 and YUYV at 320x240 to 1280x720, and H.264 encoded by the host's WebCodecs encoder where it has one, with bitrate, GOP and key frame controls (SDK option camera, the page's Camera setting and ?cam=0, the CLI's --camera). The base's rc writes the udev database entry of /dev/video*, so programs that list cameras through libudev find it. kwva (protocol 5): an encode whose input surface is a camera buffer's dma-buf names the frame to the host instead of sending its pixels, and pixels an app writes into an exported surface reach the host (put).
668 lines
24 KiB
Diff
668 lines
24 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Pooya Parsa <pooya@pi0.io>
|
|
Date: Sun, 15 Nov 2026 10:16:10 +0000
|
|
Subject: [PATCH] wasm: kwsnd, ALSA playback and capture with the JavaScript
|
|
host
|
|
|
|
An ALSA PCM for programs that play and record sound, registered only
|
|
when the host has audio (wasm_driver_sound_init() < 0 means none, so a
|
|
normal boot only pays for the code). With audio it is card 0 with one
|
|
PCM (hw:0,0: /dev/snd/pcmC0D0p and controlC0), no mixer: U8 and S16_LE,
|
|
mono or stereo, 8000-48000 Hz (continuous), up to 64 KiB of buffer,
|
|
periods of 2 ms or more in a buffer of 10 ms or more. The PCM has a
|
|
capture substream too (/dev/snd/pcmC0D0c) with the same formats when
|
|
the init import's value has KWSND_INIT_CAPTURE (bit 30) set: a host
|
|
that does not know of capture never sets it, and the capture imports
|
|
are then never called. alsa-lib's default device for an unknown card
|
|
is plug:hw, so no PCM timer (dmix) is needed. The init import also
|
|
returns the host's preferred rate, which the card's long name shows.
|
|
|
|
Nothing runs and nothing beyond the card is allocated until a program
|
|
opens a substream: the per-open state is allocated at open, and the
|
|
ring is ALSA's managed buffer (SNDRV_DMA_TYPE_CONTINUOUS, allocated at
|
|
hw_params), which is direct-mapped, so the host reads and writes it at
|
|
a plain linear address and mmap works through remap_pfn_range.
|
|
|
|
An hrtimer on CLOCK_MONOTONIC (the host's monotonic clock) paces each
|
|
stream every 2 to 10 ms (the period time, clamped). For playback each
|
|
tick hands the frames the elapsed time is worth to
|
|
wasm_driver_sound_write(), which copies them out during the call, and
|
|
advances the hardware pointer by as much; the host's return value
|
|
(frames it still has queued) becomes runtime->delay. Frames past
|
|
appl_ptr are never sent unless the stream fills silence: the pointer
|
|
still advances, so the core sees an underrun and stops the stream, and
|
|
the host plays silence. A late tick (a long stay in the kernel)
|
|
advances by three ticks' worth at most, or 15 ms' worth with ticks
|
|
shorter than 5 ms, and skips the rest, so the queued frames play on at
|
|
the normal pace rather than being rushed out, and the pointer never
|
|
moves by a whole buffer.
|
|
|
|
For capture each tick asks wasm_driver_sound_read() for the frames the
|
|
elapsed time is worth, written into the ring during the call, fills
|
|
what the host did not have with silence, and advances the pointer by
|
|
the whole amount, so a capture never waits for the host or spins (a
|
|
host without data yet, such as a microphone not granted, gives
|
|
silence); a program that does not read gets an overrun from the core.
|
|
|
|
A blocking read or write has no timeout (substream->wait_time): ALSA's
|
|
default, 1.1 buffers and 100 ms at least, is for a DMA engine that
|
|
stopped, while this pointer moves whenever the kernel runs. A wait that
|
|
long means a host that did not run the kernel (a busy thread, a frozen
|
|
tab), and it would end in EIO although the next tick wakes it.
|
|
|
|
wasm_driver_sound_event() (playback) and
|
|
wasm_driver_sound_capture_event() (capture) report open, prepare (rate,
|
|
channels, format), start, stop (and whether it was the end of a drain)
|
|
and close. No import suspends.
|
|
|
|
On the lowered kernel this adds 142 KiB (49 KiB with gzip -9), of which
|
|
OSS PCM emulation (/dev/dsp, which static SDL 1.2 builds use) is 29 KiB
|
|
(9 KiB).
|
|
|
|
On SMP the timers are pinned to CPU 0, where the host's imports answer.
|
|
Open, prepare and close send their event through kw_host_call(). The
|
|
trigger runs in atomic context on any CPU: on another CPU a start or
|
|
stop leaves its event in the stream and queues a work item on CPU 0,
|
|
which sends it and starts the timer. Whatever sends an event first
|
|
sends the ones left (between two prepares a stream starts and stops at
|
|
most once), so the host sees them in order, and the tick sends a start
|
|
before its frames.
|
|
---
|
|
sound/drivers/Kconfig | 9 +
|
|
sound/drivers/Makefile | 2 +
|
|
sound/drivers/kwsnd.c | 548 +++++++++++++++++++++++++++++++++++++++++
|
|
3 files changed, 559 insertions(+)
|
|
create mode 100644 sound/drivers/kwsnd.c
|
|
|
|
diff --git a/sound/drivers/Kconfig b/sound/drivers/Kconfig
|
|
--- a/sound/drivers/Kconfig
|
|
+++ b/sound/drivers/Kconfig
|
|
@@ -76,6 +76,15 @@ config SND_PCSP
|
|
Say M if you don't.
|
|
Say Y only if you really know what you do.
|
|
|
|
+config SND_KWSND
|
|
+ tristate "unkernel host audio"
|
|
+ depends on WASM
|
|
+ select SND_PCM
|
|
+ help
|
|
+ A playback-only PCM whose samples go to the JavaScript host of a
|
|
+ unkernel instance (js/core/sound.ts). The card is registered only
|
|
+ when the host has audio.
|
|
+
|
|
config SND_DUMMY
|
|
tristate "Dummy (/dev/null) soundcard"
|
|
select SND_PCM
|
|
diff --git a/sound/drivers/Makefile b/sound/drivers/Makefile
|
|
--- a/sound/drivers/Makefile
|
|
+++ b/sound/drivers/Makefile
|
|
@@ -5,6 +5,7 @@
|
|
#
|
|
|
|
snd-dummy-y := dummy.o
|
|
+snd-kwsnd-y := kwsnd.o
|
|
snd-aloop-y := aloop.o
|
|
snd-mtpav-y := mtpav.o
|
|
snd-mts64-y := mts64.o
|
|
@@ -16,6 +17,7 @@ snd-virmidi-y := virmidi.o
|
|
|
|
# Toplevel Module Dependency
|
|
obj-$(CONFIG_SND_DUMMY) += snd-dummy.o
|
|
+obj-$(CONFIG_SND_KWSND) += snd-kwsnd.o
|
|
obj-$(CONFIG_SND_ALOOP) += snd-aloop.o
|
|
obj-$(CONFIG_SND_VIRMIDI) += snd-virmidi.o
|
|
obj-$(CONFIG_SND_PCMTEST) += snd-pcmtest.o
|
|
diff --git a/sound/drivers/kwsnd.c b/sound/drivers/kwsnd.c
|
|
new file mode 100644
|
|
--- /dev/null
|
|
+++ b/sound/drivers/kwsnd.c
|
|
@@ -0,0 +1,548 @@
|
|
+// SPDX-License-Identifier: GPL-2.0-only
|
|
+/*
|
|
+ * kwsnd: ALSA playback to, and capture from, the JavaScript host (unkernel).
|
|
+ *
|
|
+ * One card with one PCM (hw:0,0; /dev/snd/pcmC0D0p and controlC0),
|
|
+ * registered only when the host has audio (wasm_driver_sound_init() < 0
|
|
+ * means none). The PCM has a playback substream, and a capture substream
|
|
+ * (/dev/snd/pcmC0D0c) when the host says it has a source
|
|
+ * (KWSND_INIT_CAPTURE in what init returns; a host that does not know of
|
|
+ * capture never sets it, and then the capture imports are never called).
|
|
+ * No mixer.
|
|
+ *
|
|
+ * Nothing runs and nothing beyond the card is allocated until a program
|
|
+ * opens the PCM: the ring buffer is ALSA's managed buffer (one physically
|
|
+ * contiguous allocation at hw_params, freed at hw_free), so the host gets a
|
|
+ * plain linear address.
|
|
+ *
|
|
+ * Playback is paced by an hrtimer on CLOCK_MONOTONIC, which is the host's
|
|
+ * monotonic clock (WASI clock_time_get), every 2 to 10 ms (the period time,
|
|
+ * clamped). Each tick works out how many frames the elapsed time is worth,
|
|
+ * hands that many frames of the ring to the host with
|
|
+ * wasm_driver_sound_write() (the host copies them out during the call) and
|
|
+ * advances the hardware pointer by the same amount. So the pointer always
|
|
+ * means "the host has these frames"; the host's own queue (its return
|
|
+ * value, reported as runtime->delay) adds the rest of the latency. The
|
|
+ * host plays on its own clock and absorbs the drift between the two.
|
|
+ *
|
|
+ * Data the program has not written yet is never sent: when the elapsed
|
|
+ * time is worth more than the queued frames, the pointer still advances
|
|
+ * (so the core sees the underrun and stops the stream, or, with silence
|
|
+ * filling as dmix uses, keeps running) but only the written part goes to
|
|
+ * the host, which plays silence for the rest. A tick that comes late (a
|
|
+ * long stay in the kernel, a host that did not run) advances by three
|
|
+ * ticks' worth at most (15 ms' worth with ticks shorter than 5 ms, so a
|
|
+ * short period tolerates the same stall) and skips the rest of the missed
|
|
+ * time: the queued frames then play on at the normal pace instead of being
|
|
+ * rushed out, the program sees no underrun from the stall alone, and the
|
|
+ * pointer never moves by a whole buffer, which the core cannot see.
|
|
+ *
|
|
+ * A tick reports to the core (snd_pcm_period_elapsed()) only when the
|
|
+ * pointer crossed a period boundary or reached appl_ptr (the end of a
|
|
+ * drain, an underrun). The core takes each report for a whole period, and
|
|
+ * one that falls short of the next boundary more than half a buffer after
|
|
+ * the pointer last moved (the first tick after a stall, when a period is
|
|
+ * longer than three ticks) for a missed buffer: it would add a buffer to
|
|
+ * the pointer and stop the stream. Between reports the core reads the
|
|
+ * pointer only when the program calls it, so a tick works from its own
|
|
+ * position, not the core's hw_ptr.
|
|
+ *
|
|
+ * Capture runs on the same timer: each tick works out the frames the
|
|
+ * elapsed time is worth, asks the host for them with
|
|
+ * wasm_driver_sound_read() into the ring (the host writes them during the
|
|
+ * call and returns how many bytes it had), fills the rest with silence and
|
|
+ * advances the pointer by the whole amount. So a capture never waits for
|
|
+ * the host and never spins: a host that has nothing yet (a microphone not
|
|
+ * granted, a source that ran dry) gives silence, at the guest's pace. The
|
|
+ * host absorbs the drift between its source's clock and this one. A
|
|
+ * program that does not read gets an overrun from the core as with any
|
|
+ * device.
|
|
+ *
|
|
+ * The host is told about every state change with wasm_driver_sound_event()
|
|
+ * (playback) or wasm_driver_sound_capture_event() (capture): open, prepare
|
|
+ * (rate, channels, format), start, stop (drain or not) and close. No import
|
|
+ * suspends.
|
|
+ *
|
|
+ * SMP: the host's imports answer on CPU 0 alone. The timer runs there
|
|
+ * (pinned), open, prepare and close send their event through
|
|
+ * kw_host_call(), and a start or stop on another CPU (the trigger, in
|
|
+ * atomic context) leaves its event in the stream for a work item on CPU
|
|
+ * 0, which sends it and starts the timer. Everything that sends one first
|
|
+ * sends what is left (kwsnd_flush()), so the host sees them in order; a
|
|
+ * stream starts at most once and stops at most once between two prepares.
|
|
+ */
|
|
+
|
|
+#include <linux/hrtimer.h>
|
|
+#include <linux/init.h>
|
|
+#include <linux/math64.h>
|
|
+#include <linux/module.h>
|
|
+#include <linux/slab.h>
|
|
+#include <linux/workqueue.h>
|
|
+#include <sound/core.h>
|
|
+#include <sound/pcm.h>
|
|
+#include <asm/wasm.h>
|
|
+
|
|
+/* Events for wasm_driver_sound_event(). */
|
|
+#define KWSND_EV_CLOSE 0
|
|
+#define KWSND_EV_OPEN 1
|
|
+#define KWSND_EV_PREPARE 2 /* rate, channels, format (SNDRV_PCM_FORMAT_*) */
|
|
+#define KWSND_EV_START 3
|
|
+#define KWSND_EV_STOP 4 /* rate = 1 when the stream drained */
|
|
+
|
|
+#define KWSND_TICK_MIN_NS (2 * NSEC_PER_MSEC)
|
|
+#define KWSND_TICK_MAX_NS (10 * NSEC_PER_MSEC)
|
|
+/* The shortest buffer: a short period is no reason for less slack. */
|
|
+#define KWSND_BUFFER_MIN_NS (10 * NSEC_PER_MSEC)
|
|
+/* A late tick hands over this much at least, however short the tick. */
|
|
+#define KWSND_BURST_MIN_NS (15 * NSEC_PER_MSEC)
|
|
+/* In what wasm_driver_sound_init() returns: the host has a capture source. */
|
|
+#define KWSND_INIT_CAPTURE (1L << 30)
|
|
+
|
|
+/* Host imports, module "unkernel". None of them suspends. */
|
|
+/*
|
|
+ * < 0: no audio. Else the host's preferred rate in Hz (0: no preference),
|
|
+ * with KWSND_INIT_CAPTURE set when it also has a capture source.
|
|
+ */
|
|
+long wasm_driver_sound_init(void)
|
|
+ __unkernel_import(wasm_driver_sound_init);
|
|
+void wasm_driver_sound_event(u32 event, u32 rate, u32 channels, u32 format)
|
|
+ __unkernel_import(wasm_driver_sound_event);
|
|
+/* Copy @bytes of PCM at @buf; returns the frames the host now has queued. */
|
|
+long wasm_driver_sound_write(const void *buf, long bytes)
|
|
+ __unkernel_import(wasm_driver_sound_write);
|
|
+/* The capture stream's events, as wasm_driver_sound_event()'s. */
|
|
+void wasm_driver_sound_capture_event(u32 event, u32 rate, u32 channels, u32 format)
|
|
+ __unkernel_import(wasm_driver_sound_capture_event);
|
|
+/* Write up to @bytes of captured PCM at @buf; returns the bytes written. */
|
|
+long wasm_driver_sound_read(void *buf, long bytes)
|
|
+ __unkernel_import(wasm_driver_sound_read);
|
|
+
|
|
+struct kwsnd_stream {
|
|
+ struct snd_pcm_substream *substream;
|
|
+ struct hrtimer timer;
|
|
+ ktime_t base; /* when frame 0 was due */
|
|
+ u64 played; /* frames handed to the host since start */
|
|
+ u32 pos0; /* the hardware position at start */
|
|
+ u64 tick_ns;
|
|
+ u32 burst; /* most frames one tick hands over */
|
|
+ bool running;
|
|
+ bool capture; /* the capture substream: frames come from the host */
|
|
+#ifdef CONFIG_SMP
|
|
+ /* Left by the trigger on another CPU for CPU 0 (stream lock). */
|
|
+ bool ev_start, ev_stop, ev_drained;
|
|
+ struct work_struct kick; /* sends them on CPU 0, starts the timer */
|
|
+#endif
|
|
+};
|
|
+
|
|
+/* Tell the host about a state change of the playback or capture stream. */
|
|
+static void kwsnd_send(bool capture, u32 event, u32 rate, u32 channels, u32 format)
|
|
+{
|
|
+ if (capture)
|
|
+ wasm_driver_sound_capture_event(event, rate, channels, format);
|
|
+ else
|
|
+ wasm_driver_sound_event(event, rate, channels, format);
|
|
+}
|
|
+
|
|
+#ifdef CONFIG_SMP
|
|
+/* The timer stays on CPU 0, where the host's imports answer. */
|
|
+#define KWSND_TIMER_MODE HRTIMER_MODE_REL_PINNED
|
|
+
|
|
+/* CPU 0, stream lock: send the events the trigger left, in order. */
|
|
+static void kwsnd_flush(struct kwsnd_stream *s)
|
|
+{
|
|
+ if (s->ev_start) {
|
|
+ s->ev_start = false;
|
|
+ kwsnd_send(s->capture, KWSND_EV_START, 0, 0, 0);
|
|
+ }
|
|
+ if (s->ev_stop) {
|
|
+ s->ev_stop = false;
|
|
+ kwsnd_send(s->capture, KWSND_EV_STOP, s->ev_drained, 0, 0);
|
|
+ }
|
|
+}
|
|
+
|
|
+static void kwsnd_kick(struct work_struct *work)
|
|
+{
|
|
+ struct kwsnd_stream *s = container_of(work, struct kwsnd_stream, kick);
|
|
+ unsigned long flags;
|
|
+
|
|
+ snd_pcm_stream_lock_irqsave(s->substream, flags);
|
|
+ kwsnd_flush(s);
|
|
+ if (s->running && !hrtimer_active(&s->timer))
|
|
+ hrtimer_start(&s->timer, ns_to_ktime(s->tick_ns), KWSND_TIMER_MODE);
|
|
+ snd_pcm_stream_unlock_irqrestore(s->substream, flags);
|
|
+}
|
|
+
|
|
+struct kwsnd_call {
|
|
+ struct kwsnd_stream *s;
|
|
+ bool capture;
|
|
+ u32 event, rate, channels, format;
|
|
+};
|
|
+
|
|
+static long kwsnd_event_fn(void *arg)
|
|
+{
|
|
+ struct kwsnd_call *c = arg;
|
|
+ unsigned long flags;
|
|
+
|
|
+ if (c->s) {
|
|
+ snd_pcm_stream_lock_irqsave(c->s->substream, flags);
|
|
+ kwsnd_flush(c->s);
|
|
+ snd_pcm_stream_unlock_irqrestore(c->s->substream, flags);
|
|
+ }
|
|
+ kwsnd_send(c->capture, c->event, c->rate, c->channels, c->format);
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+/* An event from process context (open, prepare, close), sent on CPU 0. */
|
|
+static void kwsnd_event(struct kwsnd_stream *s, bool capture, u32 event,
|
|
+ u32 rate, u32 channels, u32 format)
|
|
+{
|
|
+ struct kwsnd_call c = { s, capture, event, rate, channels, format };
|
|
+
|
|
+ kw_host_call(kwsnd_event_fn, &c);
|
|
+}
|
|
+#else
|
|
+#define KWSND_TIMER_MODE HRTIMER_MODE_REL
|
|
+#define kwsnd_event(s, capture, event, rate, channels, format) \
|
|
+ kwsnd_send(capture, event, rate, channels, format)
|
|
+#endif
|
|
+
|
|
+static const struct snd_pcm_hardware kwsnd_hw = {
|
|
+ .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
|
|
+ SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER,
|
|
+ .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8,
|
|
+ .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
|
|
+ .rate_min = 8000,
|
|
+ .rate_max = 48000,
|
|
+ .channels_min = 1,
|
|
+ .channels_max = 2,
|
|
+ .buffer_bytes_max = 64 * 1024,
|
|
+ .period_bytes_min = 64,
|
|
+ .period_bytes_max = 32 * 1024,
|
|
+ .periods_min = 2,
|
|
+ .periods_max = 1024,
|
|
+};
|
|
+
|
|
+/* Hand @frames frames from ring position @pos on to the host. */
|
|
+static void kwsnd_push(struct snd_pcm_runtime *runtime, snd_pcm_uframes_t pos,
|
|
+ snd_pcm_uframes_t frames)
|
|
+{
|
|
+ long queued = -1;
|
|
+
|
|
+ while (frames) {
|
|
+ snd_pcm_uframes_t n = min(frames, runtime->buffer_size - pos);
|
|
+
|
|
+ queued = wasm_driver_sound_write(runtime->dma_area +
|
|
+ frames_to_bytes(runtime, pos),
|
|
+ frames_to_bytes(runtime, n));
|
|
+ frames -= n;
|
|
+ pos = 0;
|
|
+ }
|
|
+ if (queued >= 0)
|
|
+ runtime->delay = queued;
|
|
+}
|
|
+
|
|
+/*
|
|
+ * Fill @frames frames from ring position @pos with what the host captured,
|
|
+ * and with silence where it had nothing.
|
|
+ */
|
|
+static void kwsnd_pull(struct snd_pcm_runtime *runtime, snd_pcm_uframes_t pos,
|
|
+ snd_pcm_uframes_t frames)
|
|
+{
|
|
+ u32 frame = frames_to_bytes(runtime, 1);
|
|
+
|
|
+ while (frames) {
|
|
+ snd_pcm_uframes_t n = min(frames, runtime->buffer_size - pos);
|
|
+ u8 *buf = runtime->dma_area + frames_to_bytes(runtime, pos);
|
|
+ long bytes = frames_to_bytes(runtime, n);
|
|
+ long got = wasm_driver_sound_read(buf, bytes);
|
|
+
|
|
+ got = clamp_t(long, got, 0, bytes);
|
|
+ got -= got % frame;
|
|
+ if (got < bytes)
|
|
+ snd_pcm_format_set_silence(runtime->format, buf + got,
|
|
+ (bytes - got) / frame * runtime->channels);
|
|
+ frames -= n;
|
|
+ pos = 0;
|
|
+ }
|
|
+}
|
|
+
|
|
+static enum hrtimer_restart kwsnd_tick(struct hrtimer *timer)
|
|
+{
|
|
+ struct kwsnd_stream *s = container_of(timer, struct kwsnd_stream, timer);
|
|
+ struct snd_pcm_runtime *runtime = s->substream->runtime;
|
|
+ ktime_t now = ktime_get();
|
|
+ u64 due, want;
|
|
+ unsigned long flags;
|
|
+
|
|
+ snd_pcm_stream_lock_irqsave(s->substream, flags);
|
|
+#ifdef CONFIG_SMP
|
|
+ kwsnd_flush(s); /* a start before its frames */
|
|
+#endif
|
|
+ if (!s->running)
|
|
+ goto stopped;
|
|
+
|
|
+ due = div_u64(ktime_to_ns(ktime_sub(now, s->base)) * runtime->rate,
|
|
+ NSEC_PER_SEC);
|
|
+ want = due > s->played ? due - s->played : 0;
|
|
+ if (want > s->burst) {
|
|
+ /*
|
|
+ * Late by more than the burst (a long stay in the kernel, a host
|
|
+ * that did not run): skip the missed time instead of catching up,
|
|
+ * so the queued frames still play at the normal pace.
|
|
+ */
|
|
+ want = s->burst;
|
|
+ s->base = ktime_sub_ns(now, div_u64((s->played + want) *
|
|
+ NSEC_PER_SEC, runtime->rate));
|
|
+ }
|
|
+ if (want && s->capture) {
|
|
+ u64 pos = s->pos0 + s->played;
|
|
+ u32 ring;
|
|
+
|
|
+ div_u64_rem(pos, runtime->buffer_size, &ring);
|
|
+ kwsnd_pull(runtime, ring, want);
|
|
+ s->played += want;
|
|
+ /* A period filled: may stop the stream (an overrun). */
|
|
+ if (div_u64(pos, runtime->period_size) !=
|
|
+ div_u64(pos + want, runtime->period_size))
|
|
+ snd_pcm_period_elapsed_under_stream_lock(s->substream);
|
|
+ } else if (want) {
|
|
+ u64 pos = s->pos0 + s->played; /* period-aligned like hw_ptr */
|
|
+ u32 ring, ahead;
|
|
+ snd_pcm_sframes_t queued;
|
|
+ u64 send = want;
|
|
+ bool report;
|
|
+
|
|
+ /*
|
|
+ * The core's hw_ptr is where the pointer was at the last report
|
|
+ * or call from the program, less than a period ago: the frames
|
|
+ * the program has queued start @ahead frames after it.
|
|
+ */
|
|
+ div_u64_rem(pos, runtime->buffer_size, &ring);
|
|
+ ahead = (ring + runtime->buffer_size -
|
|
+ runtime->status->hw_ptr % runtime->buffer_size) %
|
|
+ runtime->buffer_size;
|
|
+ queued = snd_pcm_playback_hw_avail(runtime) - ahead;
|
|
+ /* Without silence filling, frames past appl_ptr are stale. */
|
|
+ if (!runtime->silence_size && send > (u64)max_t(snd_pcm_sframes_t, queued, 0))
|
|
+ send = max_t(snd_pcm_sframes_t, queued, 0);
|
|
+ kwsnd_push(runtime, ring, send);
|
|
+ s->played += want;
|
|
+ /*
|
|
+ * A period ended, or the pointer reached appl_ptr: the end of a
|
|
+ * drain, or an underrun, which the core must see now, before a
|
|
+ * write in progress (it copies without the lock) moves appl_ptr
|
|
+ * past frames the pointer has gone by without sending them.
|
|
+ */
|
|
+ report = div_u64(pos, runtime->period_size) !=
|
|
+ div_u64(pos + want, runtime->period_size) ||
|
|
+ (snd_pcm_sframes_t)want >= queued;
|
|
+ /* May stop the stream (underrun, drain done): see kwsnd_trigger. */
|
|
+ if (report)
|
|
+ snd_pcm_period_elapsed_under_stream_lock(s->substream);
|
|
+ }
|
|
+ if (!s->running)
|
|
+ goto stopped;
|
|
+ snd_pcm_stream_unlock_irqrestore(s->substream, flags);
|
|
+ hrtimer_forward_now(timer, ns_to_ktime(s->tick_ns));
|
|
+ return HRTIMER_RESTART;
|
|
+stopped:
|
|
+ snd_pcm_stream_unlock_irqrestore(s->substream, flags);
|
|
+ return HRTIMER_NORESTART;
|
|
+}
|
|
+
|
|
+static int kwsnd_open(struct snd_pcm_substream *substream)
|
|
+{
|
|
+ struct snd_pcm_runtime *runtime = substream->runtime;
|
|
+ struct kwsnd_stream *s;
|
|
+ int err;
|
|
+
|
|
+ s = kzalloc_obj(*s);
|
|
+ if (!s)
|
|
+ return -ENOMEM;
|
|
+ s->substream = substream;
|
|
+ s->capture = substream->stream == SNDRV_PCM_STREAM_CAPTURE;
|
|
+ hrtimer_setup(&s->timer, kwsnd_tick, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
|
|
+ runtime->hw = kwsnd_hw;
|
|
+ /* These return 1 when they narrowed the range. */
|
|
+ err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
|
|
+ if (err >= 0)
|
|
+ /* No period shorter than the shortest tick. */
|
|
+ err = snd_pcm_hw_constraint_minmax(runtime,
|
|
+ SNDRV_PCM_HW_PARAM_PERIOD_TIME,
|
|
+ KWSND_TICK_MIN_NS / NSEC_PER_USEC,
|
|
+ UINT_MAX);
|
|
+ if (err >= 0)
|
|
+ err = snd_pcm_hw_constraint_minmax(runtime,
|
|
+ SNDRV_PCM_HW_PARAM_BUFFER_TIME,
|
|
+ KWSND_BUFFER_MIN_NS / NSEC_PER_USEC,
|
|
+ UINT_MAX);
|
|
+ if (err < 0) {
|
|
+ kfree(s);
|
|
+ return err;
|
|
+ }
|
|
+ runtime->private_data = s;
|
|
+ /*
|
|
+ * No timeout for a blocking read or write. ALSA's (1.1 buffers, 100
|
|
+ * ms at least) is for a DMA engine that stopped; this pointer moves
|
|
+ * whenever the kernel runs, so a wait that long means a host that did
|
|
+ * not run the kernel (a busy thread, a frozen tab), after which the
|
|
+ * wait would end in EIO even though the next tick wakes it.
|
|
+ */
|
|
+ substream->wait_time = INT_MAX;
|
|
+#ifdef CONFIG_SMP
|
|
+ INIT_WORK(&s->kick, kwsnd_kick);
|
|
+#endif
|
|
+ kwsnd_event(NULL, s->capture, KWSND_EV_OPEN, 0, 0, 0);
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+static int kwsnd_close(struct snd_pcm_substream *substream)
|
|
+{
|
|
+ struct kwsnd_stream *s = substream->runtime->private_data;
|
|
+ bool capture = s->capture;
|
|
+
|
|
+#ifdef CONFIG_SMP
|
|
+ cancel_work_sync(&s->kick); /* its events go with the close */
|
|
+ hrtimer_cancel(&s->timer);
|
|
+ kwsnd_event(s, capture, KWSND_EV_CLOSE, 0, 0, 0);
|
|
+ kfree(s);
|
|
+#else
|
|
+ hrtimer_cancel(&s->timer);
|
|
+ kfree(s);
|
|
+ kwsnd_send(capture, KWSND_EV_CLOSE, 0, 0, 0);
|
|
+#endif
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+static int kwsnd_prepare(struct snd_pcm_substream *substream)
|
|
+{
|
|
+ struct snd_pcm_runtime *runtime = substream->runtime;
|
|
+ struct kwsnd_stream *s = runtime->private_data;
|
|
+ u64 period_ns = div_u64((u64)runtime->period_size * NSEC_PER_SEC,
|
|
+ runtime->rate);
|
|
+
|
|
+#ifdef CONFIG_SMP
|
|
+ cancel_work_sync(&s->kick); /* its events go before the prepare */
|
|
+#endif
|
|
+ hrtimer_cancel(&s->timer);
|
|
+ s->tick_ns = clamp_t(u64, period_ns, KWSND_TICK_MIN_NS, KWSND_TICK_MAX_NS);
|
|
+ s->burst = min_t(u64, runtime->buffer_size - 1,
|
|
+ div_u64(max_t(u64, 3 * s->tick_ns, KWSND_BURST_MIN_NS) *
|
|
+ runtime->rate, NSEC_PER_SEC));
|
|
+ runtime->delay = 0;
|
|
+ kwsnd_event(s, s->capture, KWSND_EV_PREPARE, runtime->rate,
|
|
+ runtime->channels, runtime->format);
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+/*
|
|
+ * Called with the stream lock held, possibly from kwsnd_tick itself (an
|
|
+ * underrun or the end of a drain), whose callback then returns
|
|
+ * HRTIMER_NORESTART.
|
|
+ */
|
|
+static int kwsnd_trigger(struct snd_pcm_substream *substream, int cmd)
|
|
+{
|
|
+ struct snd_pcm_runtime *runtime = substream->runtime;
|
|
+ struct kwsnd_stream *s = runtime->private_data;
|
|
+
|
|
+ switch (cmd) {
|
|
+ case SNDRV_PCM_TRIGGER_START:
|
|
+ s->base = ktime_get();
|
|
+ s->played = 0;
|
|
+ s->pos0 = runtime->status->hw_ptr % runtime->buffer_size;
|
|
+ s->running = true;
|
|
+#ifdef CONFIG_SMP
|
|
+ if (smp_processor_id()) {
|
|
+ s->ev_start = true;
|
|
+ queue_work_on(0, system_highpri_wq, &s->kick);
|
|
+ return 0;
|
|
+ }
|
|
+ kwsnd_flush(s);
|
|
+#endif
|
|
+ hrtimer_start(&s->timer, ns_to_ktime(s->tick_ns), KWSND_TIMER_MODE);
|
|
+ kwsnd_send(s->capture, KWSND_EV_START, 0, 0, 0);
|
|
+ return 0;
|
|
+ case SNDRV_PCM_TRIGGER_STOP:
|
|
+ s->running = false;
|
|
+ hrtimer_try_to_cancel(&s->timer);
|
|
+#ifdef CONFIG_SMP
|
|
+ if (smp_processor_id()) {
|
|
+ s->ev_stop = true;
|
|
+ s->ev_drained = runtime->state == SNDRV_PCM_STATE_DRAINING;
|
|
+ queue_work_on(0, system_highpri_wq, &s->kick);
|
|
+ return 0;
|
|
+ }
|
|
+ kwsnd_flush(s);
|
|
+#endif
|
|
+ kwsnd_send(s->capture, KWSND_EV_STOP,
|
|
+ runtime->state == SNDRV_PCM_STATE_DRAINING, 0, 0);
|
|
+ return 0;
|
|
+ }
|
|
+ return -EINVAL;
|
|
+}
|
|
+
|
|
+static snd_pcm_uframes_t kwsnd_pointer(struct snd_pcm_substream *substream)
|
|
+{
|
|
+ struct kwsnd_stream *s = substream->runtime->private_data;
|
|
+ u32 pos;
|
|
+
|
|
+ div_u64_rem(s->pos0 + s->played, substream->runtime->buffer_size, &pos);
|
|
+ return pos;
|
|
+}
|
|
+
|
|
+static const struct snd_pcm_ops kwsnd_ops = {
|
|
+ .open = kwsnd_open,
|
|
+ .close = kwsnd_close,
|
|
+ .prepare = kwsnd_prepare,
|
|
+ .trigger = kwsnd_trigger,
|
|
+ .pointer = kwsnd_pointer,
|
|
+};
|
|
+
|
|
+static int __init kwsnd_init(void)
|
|
+{
|
|
+ struct snd_card *card;
|
|
+ struct snd_pcm *pcm;
|
|
+ long pref = wasm_driver_sound_init();
|
|
+ bool capture;
|
|
+ int err;
|
|
+
|
|
+ if (pref < 0) {
|
|
+ pr_info("kwsnd: no host audio\n");
|
|
+ return 0;
|
|
+ }
|
|
+ capture = pref & KWSND_INIT_CAPTURE;
|
|
+ pref &= ~KWSND_INIT_CAPTURE;
|
|
+ err = snd_card_new(NULL, -1, "kwsnd", THIS_MODULE, 0, &card);
|
|
+ if (err)
|
|
+ return err;
|
|
+ strscpy(card->driver, "kwsnd");
|
|
+ strscpy(card->shortname, "unkernel audio");
|
|
+ /* Programs may pick any rate; this is the one the host plays natively. */
|
|
+ if (pref)
|
|
+ snprintf(card->longname, sizeof(card->longname),
|
|
+ "unkernel host audio, %ld Hz preferred", pref);
|
|
+ else
|
|
+ strscpy(card->longname, "unkernel host audio");
|
|
+
|
|
+ err = snd_pcm_new(card, "kwsnd", 0, 1, capture, &pcm);
|
|
+ if (err)
|
|
+ goto err_free;
|
|
+ strscpy(pcm->name, "unkernel host audio");
|
|
+ snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &kwsnd_ops);
|
|
+ if (capture)
|
|
+ snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &kwsnd_ops);
|
|
+ /* No preallocation: allocated at hw_params, up to the maximum. */
|
|
+ snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, NULL,
|
|
+ 0, kwsnd_hw.buffer_bytes_max);
|
|
+
|
|
+ err = snd_card_register(card);
|
|
+ if (err)
|
|
+ goto err_free;
|
|
+ pr_info("kwsnd: %s%s\n", card->longname, capture ? ", with capture" : "");
|
|
+ return 0;
|
|
+
|
|
+err_free:
|
|
+ snd_card_free(card);
|
|
+ return err;
|
|
+}
|
|
+device_initcall(kwsnd_init);
|