2019-05-27 06:55:05 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Virtual Raw MIDI client on Sequencer
|
|
|
|
*
|
|
|
|
* Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>,
|
|
|
|
* Jaroslav Kysela <perex@perex.cz>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Virtual Raw MIDI client
|
|
|
|
*
|
|
|
|
* The virtual rawmidi client is a sequencer client which associate
|
|
|
|
* a rawmidi device file. The created rawmidi device file can be
|
|
|
|
* accessed as a normal raw midi, but its MIDI source and destination
|
|
|
|
* are arbitrary. For example, a user-client software synth connected
|
|
|
|
* to this port can be used as a normal midi device as well.
|
|
|
|
*
|
|
|
|
* The virtual rawmidi device accepts also multiple opens. Each file
|
|
|
|
* has its own input buffer, so that no conflict would occur. The drain
|
|
|
|
* of input/output buffer acts only to the local buffer.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/wait.h>
|
2011-07-15 16:38:28 +00:00
|
|
|
#include <linux/module.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <sound/core.h>
|
|
|
|
#include <sound/rawmidi.h>
|
|
|
|
#include <sound/info.h>
|
|
|
|
#include <sound/control.h>
|
|
|
|
#include <sound/minors.h>
|
|
|
|
#include <sound/seq_kernel.h>
|
|
|
|
#include <sound/seq_midi_event.h>
|
|
|
|
#include <sound/seq_virmidi.h>
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
|
|
|
|
MODULE_DESCRIPTION("Virtual Raw MIDI client on Sequencer");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize an event record
|
|
|
|
*/
|
2005-11-17 13:04:02 +00:00
|
|
|
static void snd_virmidi_init_event(struct snd_virmidi *vmidi,
|
|
|
|
struct snd_seq_event *ev)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
memset(ev, 0, sizeof(*ev));
|
|
|
|
ev->source.port = vmidi->port;
|
|
|
|
switch (vmidi->seq_mode) {
|
|
|
|
case SNDRV_VIRMIDI_SEQ_DISPATCH:
|
|
|
|
ev->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
|
|
|
|
break;
|
|
|
|
case SNDRV_VIRMIDI_SEQ_ATTACH:
|
|
|
|
/* FIXME: source and destination are same - not good.. */
|
|
|
|
ev->dest.client = vmidi->client;
|
|
|
|
ev->dest.port = vmidi->port;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ev->type = SNDRV_SEQ_EVENT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* decode input event and put to read buffer of each opened file
|
|
|
|
*/
|
ALSA: seq: fix function cast warnings
clang-16 points out a control flow integrity (kcfi) issue when event
callbacks get converted to incompatible types:
sound/core/seq/seq_midi.c:135:30: error: cast from 'int (*)(struct snd_rawmidi_substream *, const char *, int)' to 'snd_seq_dump_func_t' (aka 'int (*)(void *, void *, int)') converts to incompatible function type [-Werror,-Wcast-function-type-strict]
135 | snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)dump_midi, substream);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sound/core/seq/seq_virmidi.c:83:31: error: cast from 'int (*)(struct snd_rawmidi_substream *, const unsigned char *, int)' to 'snd_seq_dump_func_t' (aka 'int (*)(void *, void *, int)') converts to incompatible function type [-Werror,-Wcast-function-type-strict]
83 | snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)snd_rawmidi_receive, vmidi->substream);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For addressing those errors, introduce wrapper functions that are used
for callbacks and bridge to the actual function call with pointer
cast.
The code was originally added with the initial ALSA merge in linux-2.5.4.
[ the patch description shamelessly copied from Arnd's original patch
-- tiwai ]
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-by: Arnd Bergmann <arnd@arndb.de>
Link: https://lore.kernel.org/r/20240213101020.459183-1-arnd@kernel.org
Link: https://lore.kernel.org/r/20240213135343.16411-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2024-02-13 13:53:43 +00:00
|
|
|
|
|
|
|
/* callback for snd_seq_dump_var_event(), bridging to snd_rawmidi_receive() */
|
|
|
|
static int dump_to_rawmidi(void *ptr, void *buf, int count)
|
|
|
|
{
|
|
|
|
return snd_rawmidi_receive(ptr, buf, count);
|
|
|
|
}
|
|
|
|
|
2005-11-17 13:04:02 +00:00
|
|
|
static int snd_virmidi_dev_receive_event(struct snd_virmidi_dev *rdev,
|
2017-10-09 08:02:56 +00:00
|
|
|
struct snd_seq_event *ev,
|
|
|
|
bool atomic)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-11-17 13:04:02 +00:00
|
|
|
struct snd_virmidi *vmidi;
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned char msg[4];
|
|
|
|
int len;
|
|
|
|
|
2017-10-09 08:02:56 +00:00
|
|
|
if (atomic)
|
|
|
|
read_lock(&rdev->filelist_lock);
|
|
|
|
else
|
|
|
|
down_read(&rdev->filelist_sem);
|
2006-10-05 14:02:22 +00:00
|
|
|
list_for_each_entry(vmidi, &rdev->filelist, list) {
|
2018-07-30 12:48:29 +00:00
|
|
|
if (!READ_ONCE(vmidi->trigger))
|
2005-04-16 22:20:36 +00:00
|
|
|
continue;
|
|
|
|
if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
|
|
|
|
if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
|
|
|
|
continue;
|
ALSA: seq: fix function cast warnings
clang-16 points out a control flow integrity (kcfi) issue when event
callbacks get converted to incompatible types:
sound/core/seq/seq_midi.c:135:30: error: cast from 'int (*)(struct snd_rawmidi_substream *, const char *, int)' to 'snd_seq_dump_func_t' (aka 'int (*)(void *, void *, int)') converts to incompatible function type [-Werror,-Wcast-function-type-strict]
135 | snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)dump_midi, substream);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sound/core/seq/seq_virmidi.c:83:31: error: cast from 'int (*)(struct snd_rawmidi_substream *, const unsigned char *, int)' to 'snd_seq_dump_func_t' (aka 'int (*)(void *, void *, int)') converts to incompatible function type [-Werror,-Wcast-function-type-strict]
83 | snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)snd_rawmidi_receive, vmidi->substream);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For addressing those errors, introduce wrapper functions that are used
for callbacks and bridge to the actual function call with pointer
cast.
The code was originally added with the initial ALSA merge in linux-2.5.4.
[ the patch description shamelessly copied from Arnd's original patch
-- tiwai ]
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-by: Arnd Bergmann <arnd@arndb.de>
Link: https://lore.kernel.org/r/20240213101020.459183-1-arnd@kernel.org
Link: https://lore.kernel.org/r/20240213135343.16411-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2024-02-13 13:53:43 +00:00
|
|
|
snd_seq_dump_var_event(ev, dump_to_rawmidi, vmidi->substream);
|
2020-03-16 09:05:05 +00:00
|
|
|
snd_midi_event_reset_decode(vmidi->parser);
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
|
|
|
len = snd_midi_event_decode(vmidi->parser, msg, sizeof(msg), ev);
|
|
|
|
if (len > 0)
|
|
|
|
snd_rawmidi_receive(vmidi->substream, msg, len);
|
|
|
|
}
|
|
|
|
}
|
2017-10-09 08:02:56 +00:00
|
|
|
if (atomic)
|
|
|
|
read_unlock(&rdev->filelist_lock);
|
|
|
|
else
|
|
|
|
up_read(&rdev->filelist_sem);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* event handler of virmidi port
|
|
|
|
*/
|
2005-11-17 13:04:02 +00:00
|
|
|
static int snd_virmidi_event_input(struct snd_seq_event *ev, int direct,
|
2005-04-16 22:20:36 +00:00
|
|
|
void *private_data, int atomic, int hop)
|
|
|
|
{
|
2005-11-17 13:04:02 +00:00
|
|
|
struct snd_virmidi_dev *rdev;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
rdev = private_data;
|
|
|
|
if (!(rdev->flags & SNDRV_VIRMIDI_USE))
|
|
|
|
return 0; /* ignored */
|
2017-10-09 08:02:56 +00:00
|
|
|
return snd_virmidi_dev_receive_event(rdev, ev, atomic);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* trigger rawmidi stream for input
|
|
|
|
*/
|
2005-11-17 13:04:02 +00:00
|
|
|
static void snd_virmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-11-17 13:04:02 +00:00
|
|
|
struct snd_virmidi *vmidi = substream->runtime->private_data;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2018-07-30 12:48:29 +00:00
|
|
|
WRITE_ONCE(vmidi->trigger, !!up);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 21:03:05 +00:00
|
|
|
/* process rawmidi bytes and send events;
|
|
|
|
* we need no lock here for vmidi->event since it's handled only in this work
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2018-07-29 21:03:05 +00:00
|
|
|
static void snd_vmidi_output_work(struct work_struct *work)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2018-07-29 21:03:05 +00:00
|
|
|
struct snd_virmidi *vmidi;
|
|
|
|
struct snd_rawmidi_substream *substream;
|
|
|
|
unsigned char input;
|
|
|
|
int ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2018-07-29 21:03:05 +00:00
|
|
|
vmidi = container_of(work, struct snd_virmidi, output_work);
|
|
|
|
substream = vmidi->substream;
|
|
|
|
|
|
|
|
/* discard the outputs in dispatch mode unless subscribed */
|
|
|
|
if (vmidi->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH &&
|
|
|
|
!(vmidi->rdev->flags & SNDRV_VIRMIDI_SUBSCRIBE)) {
|
2018-09-13 06:20:43 +00:00
|
|
|
snd_rawmidi_proceed(substream);
|
2018-07-29 21:03:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-30 12:48:29 +00:00
|
|
|
while (READ_ONCE(vmidi->trigger)) {
|
2018-07-29 21:03:05 +00:00
|
|
|
if (snd_rawmidi_transmit(substream, &input, 1) != 1)
|
|
|
|
break;
|
2018-08-01 12:38:18 +00:00
|
|
|
if (!snd_midi_event_encode_byte(vmidi->parser, input,
|
|
|
|
&vmidi->event))
|
2018-07-29 21:03:05 +00:00
|
|
|
continue;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) {
|
2018-07-29 21:03:05 +00:00
|
|
|
ret = snd_seq_kernel_client_dispatch(vmidi->client,
|
|
|
|
&vmidi->event,
|
|
|
|
false, 0);
|
2005-04-16 22:20:36 +00:00
|
|
|
vmidi->event.type = SNDRV_SEQ_EVENT_NONE;
|
2018-07-29 21:03:05 +00:00
|
|
|
if (ret < 0)
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-07-29 21:03:05 +00:00
|
|
|
/* rawmidi input might be huge, allow to have a break */
|
|
|
|
cond_resched();
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 21:03:05 +00:00
|
|
|
/*
|
|
|
|
* trigger rawmidi stream for output
|
|
|
|
*/
|
|
|
|
static void snd_virmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
|
|
|
|
{
|
|
|
|
struct snd_virmidi *vmidi = substream->runtime->private_data;
|
|
|
|
|
2018-07-30 12:48:29 +00:00
|
|
|
WRITE_ONCE(vmidi->trigger, !!up);
|
2018-07-29 21:03:05 +00:00
|
|
|
if (up)
|
|
|
|
queue_work(system_highpri_wq, &vmidi->output_work);
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* open rawmidi handle for input
|
|
|
|
*/
|
2005-11-17 13:04:02 +00:00
|
|
|
static int snd_virmidi_input_open(struct snd_rawmidi_substream *substream)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-11-17 13:04:02 +00:00
|
|
|
struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
|
|
|
|
struct snd_rawmidi_runtime *runtime = substream->runtime;
|
|
|
|
struct snd_virmidi *vmidi;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-09-09 12:20:49 +00:00
|
|
|
vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (vmidi == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
vmidi->substream = substream;
|
|
|
|
if (snd_midi_event_new(0, &vmidi->parser) < 0) {
|
|
|
|
kfree(vmidi);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
vmidi->seq_mode = rdev->seq_mode;
|
|
|
|
vmidi->client = rdev->client;
|
|
|
|
vmidi->port = rdev->port;
|
|
|
|
runtime->private_data = vmidi;
|
2024-02-27 08:53:01 +00:00
|
|
|
scoped_guard(rwsem_write, &rdev->filelist_sem) {
|
|
|
|
guard(write_lock_irq)(&rdev->filelist_lock);
|
|
|
|
list_add_tail(&vmidi->list, &rdev->filelist);
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
vmidi->rdev = rdev;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* open rawmidi handle for output
|
|
|
|
*/
|
2005-11-17 13:04:02 +00:00
|
|
|
static int snd_virmidi_output_open(struct snd_rawmidi_substream *substream)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-11-17 13:04:02 +00:00
|
|
|
struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
|
|
|
|
struct snd_rawmidi_runtime *runtime = substream->runtime;
|
|
|
|
struct snd_virmidi *vmidi;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-09-09 12:20:49 +00:00
|
|
|
vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (vmidi == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
vmidi->substream = substream;
|
|
|
|
if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &vmidi->parser) < 0) {
|
|
|
|
kfree(vmidi);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
vmidi->seq_mode = rdev->seq_mode;
|
|
|
|
vmidi->client = rdev->client;
|
|
|
|
vmidi->port = rdev->port;
|
|
|
|
snd_virmidi_init_event(vmidi, &vmidi->event);
|
|
|
|
vmidi->rdev = rdev;
|
2018-07-29 21:03:05 +00:00
|
|
|
INIT_WORK(&vmidi->output_work, snd_vmidi_output_work);
|
2005-04-16 22:20:36 +00:00
|
|
|
runtime->private_data = vmidi;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* close rawmidi handle for input
|
|
|
|
*/
|
2005-11-17 13:04:02 +00:00
|
|
|
static int snd_virmidi_input_close(struct snd_rawmidi_substream *substream)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2016-02-01 11:06:42 +00:00
|
|
|
struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
|
2005-11-17 13:04:02 +00:00
|
|
|
struct snd_virmidi *vmidi = substream->runtime->private_data;
|
2016-02-01 11:06:42 +00:00
|
|
|
|
2024-02-27 08:53:01 +00:00
|
|
|
scoped_guard(rwsem_write, &rdev->filelist_sem) {
|
|
|
|
guard(write_lock_irq)(&rdev->filelist_lock);
|
|
|
|
list_del(&vmidi->list);
|
|
|
|
}
|
2016-02-01 11:06:42 +00:00
|
|
|
snd_midi_event_free(vmidi->parser);
|
2005-04-16 22:20:36 +00:00
|
|
|
substream->runtime->private_data = NULL;
|
|
|
|
kfree(vmidi);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* close rawmidi handle for output
|
|
|
|
*/
|
2005-11-17 13:04:02 +00:00
|
|
|
static int snd_virmidi_output_close(struct snd_rawmidi_substream *substream)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-11-17 13:04:02 +00:00
|
|
|
struct snd_virmidi *vmidi = substream->runtime->private_data;
|
2018-07-29 21:03:05 +00:00
|
|
|
|
2018-07-30 12:48:29 +00:00
|
|
|
WRITE_ONCE(vmidi->trigger, false); /* to be sure */
|
2018-07-29 21:03:05 +00:00
|
|
|
cancel_work_sync(&vmidi->output_work);
|
2005-04-16 22:20:36 +00:00
|
|
|
snd_midi_event_free(vmidi->parser);
|
|
|
|
substream->runtime->private_data = NULL;
|
|
|
|
kfree(vmidi);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-06 12:41:45 +00:00
|
|
|
/*
|
|
|
|
* drain output work queue
|
|
|
|
*/
|
|
|
|
static void snd_virmidi_output_drain(struct snd_rawmidi_substream *substream)
|
|
|
|
{
|
|
|
|
struct snd_virmidi *vmidi = substream->runtime->private_data;
|
|
|
|
|
|
|
|
flush_work(&vmidi->output_work);
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* subscribe callback - allow output to rawmidi device
|
|
|
|
*/
|
2005-11-17 13:04:02 +00:00
|
|
|
static int snd_virmidi_subscribe(void *private_data,
|
|
|
|
struct snd_seq_port_subscribe *info)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-11-17 13:04:02 +00:00
|
|
|
struct snd_virmidi_dev *rdev;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
rdev = private_data;
|
|
|
|
if (!try_module_get(rdev->card->module))
|
|
|
|
return -EFAULT;
|
|
|
|
rdev->flags |= SNDRV_VIRMIDI_SUBSCRIBE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* unsubscribe callback - disallow output to rawmidi device
|
|
|
|
*/
|
2005-11-17 13:04:02 +00:00
|
|
|
static int snd_virmidi_unsubscribe(void *private_data,
|
|
|
|
struct snd_seq_port_subscribe *info)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-11-17 13:04:02 +00:00
|
|
|
struct snd_virmidi_dev *rdev;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
rdev = private_data;
|
|
|
|
rdev->flags &= ~SNDRV_VIRMIDI_SUBSCRIBE;
|
|
|
|
module_put(rdev->card->module);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* use callback - allow input to rawmidi device
|
|
|
|
*/
|
2005-11-17 13:04:02 +00:00
|
|
|
static int snd_virmidi_use(void *private_data,
|
|
|
|
struct snd_seq_port_subscribe *info)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-11-17 13:04:02 +00:00
|
|
|
struct snd_virmidi_dev *rdev;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
rdev = private_data;
|
|
|
|
if (!try_module_get(rdev->card->module))
|
|
|
|
return -EFAULT;
|
|
|
|
rdev->flags |= SNDRV_VIRMIDI_USE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* unuse callback - disallow input to rawmidi device
|
|
|
|
*/
|
2005-11-17 13:04:02 +00:00
|
|
|
static int snd_virmidi_unuse(void *private_data,
|
|
|
|
struct snd_seq_port_subscribe *info)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-11-17 13:04:02 +00:00
|
|
|
struct snd_virmidi_dev *rdev;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
rdev = private_data;
|
|
|
|
rdev->flags &= ~SNDRV_VIRMIDI_USE;
|
|
|
|
module_put(rdev->card->module);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Register functions
|
|
|
|
*/
|
|
|
|
|
2017-01-05 16:27:17 +00:00
|
|
|
static const struct snd_rawmidi_ops snd_virmidi_input_ops = {
|
2005-04-16 22:20:36 +00:00
|
|
|
.open = snd_virmidi_input_open,
|
|
|
|
.close = snd_virmidi_input_close,
|
|
|
|
.trigger = snd_virmidi_input_trigger,
|
|
|
|
};
|
|
|
|
|
2017-01-05 16:27:17 +00:00
|
|
|
static const struct snd_rawmidi_ops snd_virmidi_output_ops = {
|
2005-04-16 22:20:36 +00:00
|
|
|
.open = snd_virmidi_output_open,
|
|
|
|
.close = snd_virmidi_output_close,
|
|
|
|
.trigger = snd_virmidi_output_trigger,
|
2022-01-06 12:41:45 +00:00
|
|
|
.drain = snd_virmidi_output_drain,
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create a sequencer client and a port
|
|
|
|
*/
|
2005-11-17 13:04:02 +00:00
|
|
|
static int snd_virmidi_dev_attach_seq(struct snd_virmidi_dev *rdev)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int client;
|
2005-11-17 13:04:02 +00:00
|
|
|
struct snd_seq_port_callback pcallbacks;
|
2024-02-22 11:15:07 +00:00
|
|
|
struct snd_seq_port_info *pinfo __free(kfree) = NULL;
|
2005-04-16 22:20:36 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (rdev->client >= 0)
|
|
|
|
return 0;
|
|
|
|
|
some kmalloc/memset ->kzalloc (tree wide)
Transform some calls to kmalloc/memset to a single kzalloc (or kcalloc).
Here is a short excerpt of the semantic patch performing
this transformation:
@@
type T2;
expression x;
identifier f,fld;
expression E;
expression E1,E2;
expression e1,e2,e3,y;
statement S;
@@
x =
- kmalloc
+ kzalloc
(E1,E2)
... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
- memset((T2)x,0,E1);
@@
expression E1,E2,E3;
@@
- kzalloc(E1 * E2,E3)
+ kcalloc(E1,E2,E3)
[akpm@linux-foundation.org: get kcalloc args the right way around]
Signed-off-by: Yoann Padioleau <padator@wanadoo.fr>
Cc: Richard Henderson <rth@twiddle.net>
Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
Acked-by: Russell King <rmk@arm.linux.org.uk>
Cc: Bryan Wu <bryan.wu@analog.com>
Acked-by: Jiri Slaby <jirislaby@gmail.com>
Cc: Dave Airlie <airlied@linux.ie>
Acked-by: Roland Dreier <rolandd@cisco.com>
Cc: Jiri Kosina <jkosina@suse.cz>
Acked-by: Dmitry Torokhov <dtor@mail.ru>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Acked-by: Mauro Carvalho Chehab <mchehab@infradead.org>
Acked-by: Pierre Ossman <drzeus-list@drzeus.cx>
Cc: Jeff Garzik <jeff@garzik.org>
Cc: "David S. Miller" <davem@davemloft.net>
Acked-by: Greg KH <greg@kroah.com>
Cc: James Bottomley <James.Bottomley@steeleye.com>
Cc: "Antonino A. Daplas" <adaplas@pol.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-07-19 08:49:03 +00:00
|
|
|
pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
|
2024-02-22 11:15:07 +00:00
|
|
|
if (!pinfo)
|
|
|
|
return -ENOMEM;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-12-12 08:33:37 +00:00
|
|
|
client = snd_seq_create_kernel_client(rdev->card, rdev->device,
|
|
|
|
"%s %d-%d", rdev->rmidi->name,
|
|
|
|
rdev->card->number,
|
|
|
|
rdev->device);
|
2024-02-22 11:15:07 +00:00
|
|
|
if (client < 0)
|
|
|
|
return client;
|
2005-04-16 22:20:36 +00:00
|
|
|
rdev->client = client;
|
|
|
|
|
|
|
|
/* create a port */
|
|
|
|
pinfo->addr.client = client;
|
|
|
|
sprintf(pinfo->name, "VirMIDI %d-%d", rdev->card->number, rdev->device);
|
|
|
|
/* set all capabilities */
|
|
|
|
pinfo->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
|
|
|
|
pinfo->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
|
|
|
|
pinfo->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
|
2023-05-23 07:53:49 +00:00
|
|
|
pinfo->direction = SNDRV_SEQ_PORT_DIR_BIDIRECTION;
|
2006-05-02 14:08:41 +00:00
|
|
|
pinfo->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
|
|
|
|
| SNDRV_SEQ_PORT_TYPE_SOFTWARE
|
|
|
|
| SNDRV_SEQ_PORT_TYPE_PORT;
|
2005-04-16 22:20:36 +00:00
|
|
|
pinfo->midi_channels = 16;
|
|
|
|
memset(&pcallbacks, 0, sizeof(pcallbacks));
|
|
|
|
pcallbacks.owner = THIS_MODULE;
|
|
|
|
pcallbacks.private_data = rdev;
|
|
|
|
pcallbacks.subscribe = snd_virmidi_subscribe;
|
|
|
|
pcallbacks.unsubscribe = snd_virmidi_unsubscribe;
|
|
|
|
pcallbacks.use = snd_virmidi_use;
|
|
|
|
pcallbacks.unuse = snd_virmidi_unuse;
|
|
|
|
pcallbacks.event_input = snd_virmidi_event_input;
|
|
|
|
pinfo->kernel = &pcallbacks;
|
2005-04-06 07:43:59 +00:00
|
|
|
err = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, pinfo);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (err < 0) {
|
|
|
|
snd_seq_delete_kernel_client(client);
|
|
|
|
rdev->client = -1;
|
2024-02-22 11:15:07 +00:00
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rdev->port = pinfo->addr.port;
|
2024-02-22 11:15:07 +00:00
|
|
|
return 0; /* success */
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* release the sequencer client
|
|
|
|
*/
|
2005-11-17 13:04:02 +00:00
|
|
|
static void snd_virmidi_dev_detach_seq(struct snd_virmidi_dev *rdev)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
if (rdev->client >= 0) {
|
|
|
|
snd_seq_delete_kernel_client(rdev->client);
|
|
|
|
rdev->client = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* register the device
|
|
|
|
*/
|
2005-11-17 13:04:02 +00:00
|
|
|
static int snd_virmidi_dev_register(struct snd_rawmidi *rmidi)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-11-17 13:04:02 +00:00
|
|
|
struct snd_virmidi_dev *rdev = rmidi->private_data;
|
2005-04-16 22:20:36 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
switch (rdev->seq_mode) {
|
|
|
|
case SNDRV_VIRMIDI_SEQ_DISPATCH:
|
|
|
|
err = snd_virmidi_dev_attach_seq(rdev);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
break;
|
|
|
|
case SNDRV_VIRMIDI_SEQ_ATTACH:
|
|
|
|
if (rdev->client == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
/* should check presence of port more strictly.. */
|
|
|
|
break;
|
|
|
|
default:
|
2014-02-04 17:24:34 +00:00
|
|
|
pr_err("ALSA: seq_virmidi: seq_mode is not set: %d\n", rdev->seq_mode);
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* unregister the device
|
|
|
|
*/
|
2005-11-17 13:04:02 +00:00
|
|
|
static int snd_virmidi_dev_unregister(struct snd_rawmidi *rmidi)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-11-17 13:04:02 +00:00
|
|
|
struct snd_virmidi_dev *rdev = rmidi->private_data;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (rdev->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH)
|
|
|
|
snd_virmidi_dev_detach_seq(rdev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
*/
|
2015-11-22 07:55:07 +00:00
|
|
|
static const struct snd_rawmidi_global_ops snd_virmidi_global_ops = {
|
2005-04-16 22:20:36 +00:00
|
|
|
.dev_register = snd_virmidi_dev_register,
|
|
|
|
.dev_unregister = snd_virmidi_dev_unregister,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* free device
|
|
|
|
*/
|
2005-11-17 13:04:02 +00:00
|
|
|
static void snd_virmidi_free(struct snd_rawmidi *rmidi)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-11-17 13:04:02 +00:00
|
|
|
struct snd_virmidi_dev *rdev = rmidi->private_data;
|
2005-04-16 22:20:36 +00:00
|
|
|
kfree(rdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create a new device
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/* exported */
|
2005-11-17 13:04:02 +00:00
|
|
|
int snd_virmidi_new(struct snd_card *card, int device, struct snd_rawmidi **rrmidi)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-11-17 13:04:02 +00:00
|
|
|
struct snd_rawmidi *rmidi;
|
|
|
|
struct snd_virmidi_dev *rdev;
|
2005-04-16 22:20:36 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
*rrmidi = NULL;
|
2021-06-08 14:05:30 +00:00
|
|
|
err = snd_rawmidi_new(card, "VirMidi", device,
|
|
|
|
16, /* may be configurable */
|
|
|
|
16, /* may be configurable */
|
|
|
|
&rmidi);
|
|
|
|
if (err < 0)
|
2005-04-16 22:20:36 +00:00
|
|
|
return err;
|
|
|
|
strcpy(rmidi->name, rmidi->id);
|
2005-09-09 12:20:49 +00:00
|
|
|
rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (rdev == NULL) {
|
|
|
|
snd_device_free(card, rmidi);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
rdev->card = card;
|
|
|
|
rdev->rmidi = rmidi;
|
|
|
|
rdev->device = device;
|
|
|
|
rdev->client = -1;
|
2017-10-09 08:02:56 +00:00
|
|
|
init_rwsem(&rdev->filelist_sem);
|
2005-04-16 22:20:36 +00:00
|
|
|
rwlock_init(&rdev->filelist_lock);
|
|
|
|
INIT_LIST_HEAD(&rdev->filelist);
|
|
|
|
rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
|
|
|
|
rmidi->private_data = rdev;
|
|
|
|
rmidi->private_free = snd_virmidi_free;
|
|
|
|
rmidi->ops = &snd_virmidi_global_ops;
|
|
|
|
snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_virmidi_input_ops);
|
|
|
|
snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_virmidi_output_ops);
|
|
|
|
rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT |
|
|
|
|
SNDRV_RAWMIDI_INFO_OUTPUT |
|
|
|
|
SNDRV_RAWMIDI_INFO_DUPLEX;
|
|
|
|
*rrmidi = rmidi;
|
|
|
|
return 0;
|
|
|
|
}
|
2017-06-16 14:15:24 +00:00
|
|
|
EXPORT_SYMBOL(snd_virmidi_new);
|