1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Generic MIDI synth driver for ALSA sequencer
4 * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
5 * Jaroslav Kysela <perex@perex.cz>
9 Possible options for midisynth module:
10 - automatic opening of midi ports on first received event or subscription
11 (close will be performed when client leaves)
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/errno.h>
18 #include <linux/string.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <sound/core.h>
22 #include <sound/rawmidi.h>
23 #include <sound/seq_kernel.h>
24 #include <sound/seq_device.h>
25 #include <sound/seq_midi_event.h>
26 #include <sound/initval.h>
28 MODULE_AUTHOR("Frank van de Pol <fvdpol@coil.demon.nl>, Jaroslav Kysela <perex@perex.cz>");
29 MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI synth.");
30 MODULE_LICENSE("GPL");
31 static int output_buffer_size = PAGE_SIZE;
32 module_param(output_buffer_size, int, 0644);
33 MODULE_PARM_DESC(output_buffer_size, "Output buffer size in bytes.");
34 static int input_buffer_size = PAGE_SIZE;
35 module_param(input_buffer_size, int, 0644);
36 MODULE_PARM_DESC(input_buffer_size, "Input buffer size in bytes.");
38 /* data for this midi synth driver */
39 struct seq_midisynth {
40 struct snd_card *card;
43 struct snd_rawmidi_file input_rfile;
44 struct snd_rawmidi_file output_rfile;
47 struct snd_midi_event *parser;
50 struct seq_midisynth_client {
53 int ports_per_device[SNDRV_RAWMIDI_DEVICES];
54 struct seq_midisynth *ports[SNDRV_RAWMIDI_DEVICES];
57 static struct seq_midisynth_client *synths[SNDRV_CARDS];
58 static DEFINE_MUTEX(register_mutex);
60 /* handle rawmidi input event (MIDI v1.0 stream) */
61 static void snd_midi_input_event(struct snd_rawmidi_substream *substream)
63 struct snd_rawmidi_runtime *runtime;
64 struct seq_midisynth *msynth;
65 struct snd_seq_event ev;
69 if (substream == NULL)
71 runtime = substream->runtime;
72 msynth = runtime->private_data;
75 memset(&ev, 0, sizeof(ev));
76 while (runtime->avail > 0) {
77 res = snd_rawmidi_kernel_read(substream, buf, sizeof(buf));
80 if (msynth->parser == NULL)
84 if (!snd_midi_event_encode_byte(msynth->parser,
87 ev.source.port = msynth->seq_port;
88 ev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
89 snd_seq_kernel_client_dispatch(msynth->seq_client, &ev, 1, 0);
90 /* clear event and reset header */
91 memset(&ev, 0, sizeof(ev));
96 static int dump_midi(struct snd_rawmidi_substream *substream, const char *buf, int count)
98 struct snd_rawmidi_runtime *runtime;
101 if (snd_BUG_ON(!substream || !buf))
103 runtime = substream->runtime;
104 if ((tmp = runtime->avail) < count) {
105 if (printk_ratelimit())
106 pr_err("ALSA: seq_midi: MIDI output buffer overrun\n");
109 if (snd_rawmidi_kernel_write(substream, buf, count) < count)
114 /* callback for snd_seq_dump_var_event(), bridging to dump_midi() */
115 static int __dump_midi(void *ptr, void *buf, int count)
117 return dump_midi(ptr, buf, count);
120 static int event_process_midi(struct snd_seq_event *ev, int direct,
121 void *private_data, int atomic, int hop)
123 struct seq_midisynth *msynth = private_data;
124 unsigned char msg[10]; /* buffer for constructing midi messages */
125 struct snd_rawmidi_substream *substream;
128 if (snd_BUG_ON(!msynth))
130 substream = msynth->output_rfile.output;
131 if (substream == NULL)
133 if (ev->type == SNDRV_SEQ_EVENT_SYSEX) { /* special case, to save space */
134 if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE) {
136 pr_debug("ALSA: seq_midi: invalid sysex event flags = 0x%x\n", ev->flags);
139 snd_seq_dump_var_event(ev, __dump_midi, substream);
140 snd_midi_event_reset_decode(msynth->parser);
142 if (msynth->parser == NULL)
144 len = snd_midi_event_decode(msynth->parser, msg, sizeof(msg), ev);
147 if (dump_midi(substream, msg, len) < 0)
148 snd_midi_event_reset_decode(msynth->parser);
154 static int snd_seq_midisynth_new(struct seq_midisynth *msynth,
155 struct snd_card *card,
159 if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &msynth->parser) < 0)
162 msynth->device = device;
163 msynth->subdevice = subdevice;
167 /* open associated midi device for input */
168 static int midisynth_subscribe(void *private_data, struct snd_seq_port_subscribe *info)
171 struct seq_midisynth *msynth = private_data;
172 struct snd_rawmidi_runtime *runtime;
173 struct snd_rawmidi_params params;
176 if ((err = snd_rawmidi_kernel_open(msynth->card, msynth->device,
178 SNDRV_RAWMIDI_LFLG_INPUT,
179 &msynth->input_rfile)) < 0) {
180 pr_debug("ALSA: seq_midi: midi input open failed!!!\n");
183 runtime = msynth->input_rfile.input->runtime;
184 memset(¶ms, 0, sizeof(params));
185 params.avail_min = 1;
186 params.buffer_size = input_buffer_size;
187 if ((err = snd_rawmidi_input_params(msynth->input_rfile.input, ¶ms)) < 0) {
188 snd_rawmidi_kernel_release(&msynth->input_rfile);
191 snd_midi_event_reset_encode(msynth->parser);
192 runtime->event = snd_midi_input_event;
193 runtime->private_data = msynth;
194 snd_rawmidi_kernel_read(msynth->input_rfile.input, NULL, 0);
198 /* close associated midi device for input */
199 static int midisynth_unsubscribe(void *private_data, struct snd_seq_port_subscribe *info)
202 struct seq_midisynth *msynth = private_data;
204 if (snd_BUG_ON(!msynth->input_rfile.input))
206 err = snd_rawmidi_kernel_release(&msynth->input_rfile);
210 /* open associated midi device for output */
211 static int midisynth_use(void *private_data, struct snd_seq_port_subscribe *info)
214 struct seq_midisynth *msynth = private_data;
215 struct snd_rawmidi_params params;
218 if ((err = snd_rawmidi_kernel_open(msynth->card, msynth->device,
220 SNDRV_RAWMIDI_LFLG_OUTPUT,
221 &msynth->output_rfile)) < 0) {
222 pr_debug("ALSA: seq_midi: midi output open failed!!!\n");
225 memset(¶ms, 0, sizeof(params));
226 params.avail_min = 1;
227 params.buffer_size = output_buffer_size;
228 params.no_active_sensing = 1;
229 if ((err = snd_rawmidi_output_params(msynth->output_rfile.output, ¶ms)) < 0) {
230 snd_rawmidi_kernel_release(&msynth->output_rfile);
233 snd_midi_event_reset_decode(msynth->parser);
237 /* close associated midi device for output */
238 static int midisynth_unuse(void *private_data, struct snd_seq_port_subscribe *info)
240 struct seq_midisynth *msynth = private_data;
242 if (snd_BUG_ON(!msynth->output_rfile.output))
244 snd_rawmidi_drain_output(msynth->output_rfile.output);
245 return snd_rawmidi_kernel_release(&msynth->output_rfile);
248 /* delete given midi synth port */
249 static void snd_seq_midisynth_delete(struct seq_midisynth *msynth)
254 if (msynth->seq_client > 0) {
256 snd_seq_event_port_detach(msynth->seq_client, msynth->seq_port);
259 snd_midi_event_free(msynth->parser);
262 /* register new midi synth port */
264 snd_seq_midisynth_probe(struct device *_dev)
266 struct snd_seq_device *dev = to_seq_dev(_dev);
267 struct seq_midisynth_client *client;
268 struct seq_midisynth *msynth, *ms;
269 struct snd_seq_port_info *port;
270 struct snd_rawmidi_info *info;
271 struct snd_rawmidi *rmidi = dev->private_data;
273 unsigned int p, ports;
274 struct snd_seq_port_callback pcallbacks;
275 struct snd_card *card = dev->card;
276 int device = dev->device;
277 unsigned int input_count = 0, output_count = 0;
279 if (snd_BUG_ON(!card || device < 0 || device >= SNDRV_RAWMIDI_DEVICES))
281 info = kmalloc(sizeof(*info), GFP_KERNEL);
284 info->device = device;
285 info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
287 if (snd_rawmidi_info_select(card, info) >= 0)
288 output_count = info->subdevices_count;
289 info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
290 if (snd_rawmidi_info_select(card, info) >= 0) {
291 input_count = info->subdevices_count;
293 ports = output_count;
294 if (ports < input_count)
300 if (ports > (256 / SNDRV_RAWMIDI_DEVICES))
301 ports = 256 / SNDRV_RAWMIDI_DEVICES;
303 mutex_lock(®ister_mutex);
304 client = synths[card->number];
305 if (client == NULL) {
307 client = kzalloc(sizeof(*client), GFP_KERNEL);
308 if (client == NULL) {
309 mutex_unlock(®ister_mutex);
314 snd_seq_create_kernel_client(
315 card, 0, "%s", card->shortname[0] ?
316 (const char *)card->shortname : "External MIDI");
317 if (client->seq_client < 0) {
319 mutex_unlock(®ister_mutex);
325 msynth = kcalloc(ports, sizeof(struct seq_midisynth), GFP_KERNEL);
326 port = kmalloc(sizeof(*port), GFP_KERNEL);
327 if (msynth == NULL || port == NULL)
330 for (p = 0; p < ports; p++) {
333 if (snd_seq_midisynth_new(ms, card, device, p) < 0)
337 memset(port, 0, sizeof(*port));
338 port->addr.client = client->seq_client;
339 port->addr.port = device * (256 / SNDRV_RAWMIDI_DEVICES) + p;
340 port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
341 memset(info, 0, sizeof(*info));
342 info->device = device;
343 if (p < output_count)
344 info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
346 info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
348 if (snd_rawmidi_info_select(card, info) >= 0)
349 strcpy(port->name, info->subname);
350 if (! port->name[0]) {
353 snprintf(port->name, sizeof(port->name), "%s-%u", info->name, p);
355 snprintf(port->name, sizeof(port->name), "%s", info->name);
359 sprintf(port->name, "MIDI %d-%d-%u", card->number, device, p);
361 sprintf(port->name, "MIDI %d-%d", card->number, device);
364 if ((info->flags & SNDRV_RAWMIDI_INFO_OUTPUT) && p < output_count)
365 port->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
366 if ((info->flags & SNDRV_RAWMIDI_INFO_INPUT) && p < input_count)
367 port->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
368 if ((port->capability & (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ)) == (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ) &&
369 info->flags & SNDRV_RAWMIDI_INFO_DUPLEX)
370 port->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
371 port->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
372 | SNDRV_SEQ_PORT_TYPE_HARDWARE
373 | SNDRV_SEQ_PORT_TYPE_PORT;
374 port->midi_channels = 16;
375 memset(&pcallbacks, 0, sizeof(pcallbacks));
376 pcallbacks.owner = THIS_MODULE;
377 pcallbacks.private_data = ms;
378 pcallbacks.subscribe = midisynth_subscribe;
379 pcallbacks.unsubscribe = midisynth_unsubscribe;
380 pcallbacks.use = midisynth_use;
381 pcallbacks.unuse = midisynth_unuse;
382 pcallbacks.event_input = event_process_midi;
383 port->kernel = &pcallbacks;
384 if (rmidi->ops && rmidi->ops->get_port_info)
385 rmidi->ops->get_port_info(rmidi, p, port);
386 if (snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_CREATE_PORT, port)<0)
388 ms->seq_client = client->seq_client;
389 ms->seq_port = port->addr.port;
391 client->ports_per_device[device] = ports;
392 client->ports[device] = msynth;
395 synths[card->number] = client;
396 mutex_unlock(®ister_mutex);
399 return 0; /* success */
402 if (msynth != NULL) {
403 for (p = 0; p < ports; p++)
404 snd_seq_midisynth_delete(&msynth[p]);
408 snd_seq_delete_kernel_client(client->seq_client);
413 mutex_unlock(®ister_mutex);
417 /* release midi synth port */
419 snd_seq_midisynth_remove(struct device *_dev)
421 struct snd_seq_device *dev = to_seq_dev(_dev);
422 struct seq_midisynth_client *client;
423 struct seq_midisynth *msynth;
424 struct snd_card *card = dev->card;
425 int device = dev->device, p, ports;
427 mutex_lock(®ister_mutex);
428 client = synths[card->number];
429 if (client == NULL || client->ports[device] == NULL) {
430 mutex_unlock(®ister_mutex);
433 ports = client->ports_per_device[device];
434 client->ports_per_device[device] = 0;
435 msynth = client->ports[device];
436 client->ports[device] = NULL;
437 for (p = 0; p < ports; p++)
438 snd_seq_midisynth_delete(&msynth[p]);
441 if (client->num_ports <= 0) {
442 snd_seq_delete_kernel_client(client->seq_client);
443 synths[card->number] = NULL;
446 mutex_unlock(®ister_mutex);
450 static struct snd_seq_driver seq_midisynth_driver = {
452 .name = KBUILD_MODNAME,
453 .probe = snd_seq_midisynth_probe,
454 .remove = snd_seq_midisynth_remove,
456 .id = SNDRV_SEQ_DEV_ID_MIDISYNTH,
460 module_snd_seq_driver(seq_midisynth_driver);