1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Virtual Raw MIDI client on Sequencer
5 * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>,
6 * Jaroslav Kysela <perex@perex.cz>
10 * Virtual Raw MIDI client
12 * The virtual rawmidi client is a sequencer client which associate
13 * a rawmidi device file. The created rawmidi device file can be
14 * accessed as a normal raw midi, but its MIDI source and destination
15 * are arbitrary. For example, a user-client software synth connected
16 * to this port can be used as a normal midi device as well.
18 * The virtual rawmidi device accepts also multiple opens. Each file
19 * has its own input buffer, so that no conflict would occur. The drain
20 * of input/output buffer acts only to the local buffer.
24 #include <linux/init.h>
25 #include <linux/wait.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <sound/core.h>
29 #include <sound/rawmidi.h>
30 #include <sound/info.h>
31 #include <sound/control.h>
32 #include <sound/minors.h>
33 #include <sound/seq_kernel.h>
34 #include <sound/seq_midi_event.h>
35 #include <sound/seq_virmidi.h>
37 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
38 MODULE_DESCRIPTION("Virtual Raw MIDI client on Sequencer");
39 MODULE_LICENSE("GPL");
42 * initialize an event record
44 static void snd_virmidi_init_event(struct snd_virmidi *vmidi,
45 struct snd_seq_event *ev)
47 memset(ev, 0, sizeof(*ev));
48 ev->source.port = vmidi->port;
49 switch (vmidi->seq_mode) {
50 case SNDRV_VIRMIDI_SEQ_DISPATCH:
51 ev->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
53 case SNDRV_VIRMIDI_SEQ_ATTACH:
54 /* FIXME: source and destination are same - not good.. */
55 ev->dest.client = vmidi->client;
56 ev->dest.port = vmidi->port;
59 ev->type = SNDRV_SEQ_EVENT_NONE;
63 * decode input event and put to read buffer of each opened file
66 /* callback for snd_seq_dump_var_event(), bridging to snd_rawmidi_receive() */
67 static int dump_to_rawmidi(void *ptr, void *buf, int count)
69 return snd_rawmidi_receive(ptr, buf, count);
72 static int snd_virmidi_dev_receive_event(struct snd_virmidi_dev *rdev,
73 struct snd_seq_event *ev,
76 struct snd_virmidi *vmidi;
81 read_lock(&rdev->filelist_lock);
83 down_read(&rdev->filelist_sem);
84 list_for_each_entry(vmidi, &rdev->filelist, list) {
85 if (!READ_ONCE(vmidi->trigger))
87 if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
88 if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
90 snd_seq_dump_var_event(ev, dump_to_rawmidi, vmidi->substream);
91 snd_midi_event_reset_decode(vmidi->parser);
93 len = snd_midi_event_decode(vmidi->parser, msg, sizeof(msg), ev);
95 snd_rawmidi_receive(vmidi->substream, msg, len);
99 read_unlock(&rdev->filelist_lock);
101 up_read(&rdev->filelist_sem);
107 * event handler of virmidi port
109 static int snd_virmidi_event_input(struct snd_seq_event *ev, int direct,
110 void *private_data, int atomic, int hop)
112 struct snd_virmidi_dev *rdev;
115 if (!(rdev->flags & SNDRV_VIRMIDI_USE))
116 return 0; /* ignored */
117 return snd_virmidi_dev_receive_event(rdev, ev, atomic);
121 * trigger rawmidi stream for input
123 static void snd_virmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
125 struct snd_virmidi *vmidi = substream->runtime->private_data;
127 WRITE_ONCE(vmidi->trigger, !!up);
130 /* process rawmidi bytes and send events;
131 * we need no lock here for vmidi->event since it's handled only in this work
133 static void snd_vmidi_output_work(struct work_struct *work)
135 struct snd_virmidi *vmidi;
136 struct snd_rawmidi_substream *substream;
140 vmidi = container_of(work, struct snd_virmidi, output_work);
141 substream = vmidi->substream;
143 /* discard the outputs in dispatch mode unless subscribed */
144 if (vmidi->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH &&
145 !(vmidi->rdev->flags & SNDRV_VIRMIDI_SUBSCRIBE)) {
146 snd_rawmidi_proceed(substream);
150 while (READ_ONCE(vmidi->trigger)) {
151 if (snd_rawmidi_transmit(substream, &input, 1) != 1)
153 if (!snd_midi_event_encode_byte(vmidi->parser, input,
156 if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) {
157 ret = snd_seq_kernel_client_dispatch(vmidi->client,
160 vmidi->event.type = SNDRV_SEQ_EVENT_NONE;
164 /* rawmidi input might be huge, allow to have a break */
170 * trigger rawmidi stream for output
172 static void snd_virmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
174 struct snd_virmidi *vmidi = substream->runtime->private_data;
176 WRITE_ONCE(vmidi->trigger, !!up);
178 queue_work(system_highpri_wq, &vmidi->output_work);
182 * open rawmidi handle for input
184 static int snd_virmidi_input_open(struct snd_rawmidi_substream *substream)
186 struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
187 struct snd_rawmidi_runtime *runtime = substream->runtime;
188 struct snd_virmidi *vmidi;
190 vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
193 vmidi->substream = substream;
194 if (snd_midi_event_new(0, &vmidi->parser) < 0) {
198 vmidi->seq_mode = rdev->seq_mode;
199 vmidi->client = rdev->client;
200 vmidi->port = rdev->port;
201 runtime->private_data = vmidi;
202 down_write(&rdev->filelist_sem);
203 write_lock_irq(&rdev->filelist_lock);
204 list_add_tail(&vmidi->list, &rdev->filelist);
205 write_unlock_irq(&rdev->filelist_lock);
206 up_write(&rdev->filelist_sem);
212 * open rawmidi handle for output
214 static int snd_virmidi_output_open(struct snd_rawmidi_substream *substream)
216 struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
217 struct snd_rawmidi_runtime *runtime = substream->runtime;
218 struct snd_virmidi *vmidi;
220 vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
223 vmidi->substream = substream;
224 if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &vmidi->parser) < 0) {
228 vmidi->seq_mode = rdev->seq_mode;
229 vmidi->client = rdev->client;
230 vmidi->port = rdev->port;
231 snd_virmidi_init_event(vmidi, &vmidi->event);
233 INIT_WORK(&vmidi->output_work, snd_vmidi_output_work);
234 runtime->private_data = vmidi;
239 * close rawmidi handle for input
241 static int snd_virmidi_input_close(struct snd_rawmidi_substream *substream)
243 struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
244 struct snd_virmidi *vmidi = substream->runtime->private_data;
246 down_write(&rdev->filelist_sem);
247 write_lock_irq(&rdev->filelist_lock);
248 list_del(&vmidi->list);
249 write_unlock_irq(&rdev->filelist_lock);
250 up_write(&rdev->filelist_sem);
251 snd_midi_event_free(vmidi->parser);
252 substream->runtime->private_data = NULL;
258 * close rawmidi handle for output
260 static int snd_virmidi_output_close(struct snd_rawmidi_substream *substream)
262 struct snd_virmidi *vmidi = substream->runtime->private_data;
264 WRITE_ONCE(vmidi->trigger, false); /* to be sure */
265 cancel_work_sync(&vmidi->output_work);
266 snd_midi_event_free(vmidi->parser);
267 substream->runtime->private_data = NULL;
273 * subscribe callback - allow output to rawmidi device
275 static int snd_virmidi_subscribe(void *private_data,
276 struct snd_seq_port_subscribe *info)
278 struct snd_virmidi_dev *rdev;
281 if (!try_module_get(rdev->card->module))
283 rdev->flags |= SNDRV_VIRMIDI_SUBSCRIBE;
288 * unsubscribe callback - disallow output to rawmidi device
290 static int snd_virmidi_unsubscribe(void *private_data,
291 struct snd_seq_port_subscribe *info)
293 struct snd_virmidi_dev *rdev;
296 rdev->flags &= ~SNDRV_VIRMIDI_SUBSCRIBE;
297 module_put(rdev->card->module);
303 * use callback - allow input to rawmidi device
305 static int snd_virmidi_use(void *private_data,
306 struct snd_seq_port_subscribe *info)
308 struct snd_virmidi_dev *rdev;
311 if (!try_module_get(rdev->card->module))
313 rdev->flags |= SNDRV_VIRMIDI_USE;
318 * unuse callback - disallow input to rawmidi device
320 static int snd_virmidi_unuse(void *private_data,
321 struct snd_seq_port_subscribe *info)
323 struct snd_virmidi_dev *rdev;
326 rdev->flags &= ~SNDRV_VIRMIDI_USE;
327 module_put(rdev->card->module);
336 static const struct snd_rawmidi_ops snd_virmidi_input_ops = {
337 .open = snd_virmidi_input_open,
338 .close = snd_virmidi_input_close,
339 .trigger = snd_virmidi_input_trigger,
342 static const struct snd_rawmidi_ops snd_virmidi_output_ops = {
343 .open = snd_virmidi_output_open,
344 .close = snd_virmidi_output_close,
345 .trigger = snd_virmidi_output_trigger,
349 * create a sequencer client and a port
351 static int snd_virmidi_dev_attach_seq(struct snd_virmidi_dev *rdev)
354 struct snd_seq_port_callback pcallbacks;
355 struct snd_seq_port_info *pinfo;
358 if (rdev->client >= 0)
361 pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
367 client = snd_seq_create_kernel_client(rdev->card, rdev->device,
368 "%s %d-%d", rdev->rmidi->name,
375 rdev->client = client;
378 pinfo->addr.client = client;
379 sprintf(pinfo->name, "VirMIDI %d-%d", rdev->card->number, rdev->device);
380 /* set all capabilities */
381 pinfo->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
382 pinfo->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
383 pinfo->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
384 pinfo->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
385 | SNDRV_SEQ_PORT_TYPE_SOFTWARE
386 | SNDRV_SEQ_PORT_TYPE_PORT;
387 pinfo->midi_channels = 16;
388 memset(&pcallbacks, 0, sizeof(pcallbacks));
389 pcallbacks.owner = THIS_MODULE;
390 pcallbacks.private_data = rdev;
391 pcallbacks.subscribe = snd_virmidi_subscribe;
392 pcallbacks.unsubscribe = snd_virmidi_unsubscribe;
393 pcallbacks.use = snd_virmidi_use;
394 pcallbacks.unuse = snd_virmidi_unuse;
395 pcallbacks.event_input = snd_virmidi_event_input;
396 pinfo->kernel = &pcallbacks;
397 err = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, pinfo);
399 snd_seq_delete_kernel_client(client);
404 rdev->port = pinfo->addr.port;
405 err = 0; /* success */
414 * release the sequencer client
416 static void snd_virmidi_dev_detach_seq(struct snd_virmidi_dev *rdev)
418 if (rdev->client >= 0) {
419 snd_seq_delete_kernel_client(rdev->client);
425 * register the device
427 static int snd_virmidi_dev_register(struct snd_rawmidi *rmidi)
429 struct snd_virmidi_dev *rdev = rmidi->private_data;
432 switch (rdev->seq_mode) {
433 case SNDRV_VIRMIDI_SEQ_DISPATCH:
434 err = snd_virmidi_dev_attach_seq(rdev);
438 case SNDRV_VIRMIDI_SEQ_ATTACH:
439 if (rdev->client == 0)
441 /* should check presence of port more strictly.. */
444 pr_err("ALSA: seq_virmidi: seq_mode is not set: %d\n", rdev->seq_mode);
452 * unregister the device
454 static int snd_virmidi_dev_unregister(struct snd_rawmidi *rmidi)
456 struct snd_virmidi_dev *rdev = rmidi->private_data;
458 if (rdev->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH)
459 snd_virmidi_dev_detach_seq(rdev);
466 static const struct snd_rawmidi_global_ops snd_virmidi_global_ops = {
467 .dev_register = snd_virmidi_dev_register,
468 .dev_unregister = snd_virmidi_dev_unregister,
474 static void snd_virmidi_free(struct snd_rawmidi *rmidi)
476 struct snd_virmidi_dev *rdev = rmidi->private_data;
481 * create a new device
485 int snd_virmidi_new(struct snd_card *card, int device, struct snd_rawmidi **rrmidi)
487 struct snd_rawmidi *rmidi;
488 struct snd_virmidi_dev *rdev;
492 if ((err = snd_rawmidi_new(card, "VirMidi", device,
493 16, /* may be configurable */
494 16, /* may be configurable */
497 strcpy(rmidi->name, rmidi->id);
498 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
500 snd_device_free(card, rmidi);
505 rdev->device = device;
507 init_rwsem(&rdev->filelist_sem);
508 rwlock_init(&rdev->filelist_lock);
509 INIT_LIST_HEAD(&rdev->filelist);
510 rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
511 rmidi->private_data = rdev;
512 rmidi->private_free = snd_virmidi_free;
513 rmidi->ops = &snd_virmidi_global_ops;
514 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_virmidi_input_ops);
515 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_virmidi_output_ops);
516 rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT |
517 SNDRV_RAWMIDI_INFO_OUTPUT |
518 SNDRV_RAWMIDI_INFO_DUPLEX;
522 EXPORT_SYMBOL(snd_virmidi_new);