1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Generic MIDI synth driver for ALSA sequencer
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>
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 tmp = runtime->avail;
106 if (printk_ratelimit())
107 pr_err("ALSA: seq_midi: MIDI output buffer overrun\n");
110 if (snd_rawmidi_kernel_write(substream, buf, count) < count)
115 static int event_process_midi(struct snd_seq_event *ev, int direct,
116 void *private_data, int atomic, int hop)
118 struct seq_midisynth *msynth = private_data;
119 unsigned char msg[10]; /* buffer for constructing midi messages */
120 struct snd_rawmidi_substream *substream;
123 if (snd_BUG_ON(!msynth))
125 substream = msynth->output_rfile.output;
126 if (substream == NULL)
128 if (ev->type == SNDRV_SEQ_EVENT_SYSEX) { /* special case, to save space */
129 if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE) {
131 pr_debug("ALSA: seq_midi: invalid sysex event flags = 0x%x\n", ev->flags);
134 snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)dump_midi, substream);
135 snd_midi_event_reset_decode(msynth->parser);
137 if (msynth->parser == NULL)
139 len = snd_midi_event_decode(msynth->parser, msg, sizeof(msg), ev);
142 if (dump_midi(substream, msg, len) < 0)
143 snd_midi_event_reset_decode(msynth->parser);
149 static int snd_seq_midisynth_new(struct seq_midisynth *msynth,
150 struct snd_card *card,
154 if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &msynth->parser) < 0)
157 msynth->device = device;
158 msynth->subdevice = subdevice;
162 /* open associated midi device for input */
163 static int midisynth_subscribe(void *private_data, struct snd_seq_port_subscribe *info)
166 struct seq_midisynth *msynth = private_data;
167 struct snd_rawmidi_runtime *runtime;
168 struct snd_rawmidi_params params;
171 err = snd_rawmidi_kernel_open(msynth->card, msynth->device,
173 SNDRV_RAWMIDI_LFLG_INPUT,
174 &msynth->input_rfile);
176 pr_debug("ALSA: seq_midi: midi input open failed!!!\n");
179 runtime = msynth->input_rfile.input->runtime;
180 memset(¶ms, 0, sizeof(params));
181 params.avail_min = 1;
182 params.buffer_size = input_buffer_size;
183 err = snd_rawmidi_input_params(msynth->input_rfile.input, ¶ms);
185 snd_rawmidi_kernel_release(&msynth->input_rfile);
188 snd_midi_event_reset_encode(msynth->parser);
189 runtime->event = snd_midi_input_event;
190 runtime->private_data = msynth;
191 snd_rawmidi_kernel_read(msynth->input_rfile.input, NULL, 0);
195 /* close associated midi device for input */
196 static int midisynth_unsubscribe(void *private_data, struct snd_seq_port_subscribe *info)
199 struct seq_midisynth *msynth = private_data;
201 if (snd_BUG_ON(!msynth->input_rfile.input))
203 err = snd_rawmidi_kernel_release(&msynth->input_rfile);
207 /* open associated midi device for output */
208 static int midisynth_use(void *private_data, struct snd_seq_port_subscribe *info)
211 struct seq_midisynth *msynth = private_data;
212 struct snd_rawmidi_params params;
215 err = snd_rawmidi_kernel_open(msynth->card, msynth->device,
217 SNDRV_RAWMIDI_LFLG_OUTPUT,
218 &msynth->output_rfile);
220 pr_debug("ALSA: seq_midi: midi output open failed!!!\n");
223 memset(¶ms, 0, sizeof(params));
224 params.avail_min = 1;
225 params.buffer_size = output_buffer_size;
226 params.no_active_sensing = 1;
227 err = snd_rawmidi_output_params(msynth->output_rfile.output, ¶ms);
229 snd_rawmidi_kernel_release(&msynth->output_rfile);
232 snd_midi_event_reset_decode(msynth->parser);
236 /* close associated midi device for output */
237 static int midisynth_unuse(void *private_data, struct snd_seq_port_subscribe *info)
239 struct seq_midisynth *msynth = private_data;
241 if (snd_BUG_ON(!msynth->output_rfile.output))
243 snd_rawmidi_drain_output(msynth->output_rfile.output);
244 return snd_rawmidi_kernel_release(&msynth->output_rfile);
247 /* delete given midi synth port */
248 static void snd_seq_midisynth_delete(struct seq_midisynth *msynth)
253 if (msynth->seq_client > 0) {
255 snd_seq_event_port_detach(msynth->seq_client, msynth->seq_port);
258 snd_midi_event_free(msynth->parser);
261 /* register new midi synth port */
263 snd_seq_midisynth_probe(struct device *_dev)
265 struct snd_seq_device *dev = to_seq_dev(_dev);
266 struct seq_midisynth_client *client;
267 struct seq_midisynth *msynth, *ms;
268 struct snd_seq_port_info *port;
269 struct snd_rawmidi_info *info;
270 struct snd_rawmidi *rmidi = dev->private_data;
272 unsigned int p, ports;
273 struct snd_seq_port_callback pcallbacks;
274 struct snd_card *card = dev->card;
275 int device = dev->device;
276 unsigned int input_count = 0, output_count = 0;
278 if (snd_BUG_ON(!card || device < 0 || device >= SNDRV_RAWMIDI_DEVICES))
280 info = kmalloc(sizeof(*info), GFP_KERNEL);
283 info->device = device;
284 info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
286 if (snd_rawmidi_info_select(card, info) >= 0)
287 output_count = info->subdevices_count;
288 info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
289 if (snd_rawmidi_info_select(card, info) >= 0) {
290 input_count = info->subdevices_count;
292 ports = output_count;
293 if (ports < input_count)
299 if (ports > (256 / SNDRV_RAWMIDI_DEVICES))
300 ports = 256 / SNDRV_RAWMIDI_DEVICES;
302 mutex_lock(®ister_mutex);
303 client = synths[card->number];
304 if (client == NULL) {
306 client = kzalloc(sizeof(*client), GFP_KERNEL);
307 if (client == NULL) {
308 mutex_unlock(®ister_mutex);
313 snd_seq_create_kernel_client(
314 card, 0, "%s", card->shortname[0] ?
315 (const char *)card->shortname : "External MIDI");
316 if (client->seq_client < 0) {
318 mutex_unlock(®ister_mutex);
324 msynth = kcalloc(ports, sizeof(struct seq_midisynth), GFP_KERNEL);
325 port = kmalloc(sizeof(*port), GFP_KERNEL);
326 if (msynth == NULL || port == NULL)
329 for (p = 0; p < ports; p++) {
332 if (snd_seq_midisynth_new(ms, card, device, p) < 0)
336 memset(port, 0, sizeof(*port));
337 port->addr.client = client->seq_client;
338 port->addr.port = device * (256 / SNDRV_RAWMIDI_DEVICES) + p;
339 port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
340 memset(info, 0, sizeof(*info));
341 info->device = device;
342 if (p < output_count)
343 info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
345 info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
347 if (snd_rawmidi_info_select(card, info) >= 0)
348 strcpy(port->name, info->subname);
349 if (! port->name[0]) {
352 snprintf(port->name, sizeof(port->name), "%s-%u", info->name, p);
354 snprintf(port->name, sizeof(port->name), "%s", info->name);
358 sprintf(port->name, "MIDI %d-%d-%u", card->number, device, p);
360 sprintf(port->name, "MIDI %d-%d", card->number, device);
363 if ((info->flags & SNDRV_RAWMIDI_INFO_OUTPUT) && p < output_count)
364 port->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
365 if ((info->flags & SNDRV_RAWMIDI_INFO_INPUT) && p < input_count)
366 port->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
367 if ((port->capability & (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ)) == (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ) &&
368 info->flags & SNDRV_RAWMIDI_INFO_DUPLEX)
369 port->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
370 port->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
371 | SNDRV_SEQ_PORT_TYPE_HARDWARE
372 | SNDRV_SEQ_PORT_TYPE_PORT;
373 port->midi_channels = 16;
374 memset(&pcallbacks, 0, sizeof(pcallbacks));
375 pcallbacks.owner = THIS_MODULE;
376 pcallbacks.private_data = ms;
377 pcallbacks.subscribe = midisynth_subscribe;
378 pcallbacks.unsubscribe = midisynth_unsubscribe;
379 pcallbacks.use = midisynth_use;
380 pcallbacks.unuse = midisynth_unuse;
381 pcallbacks.event_input = event_process_midi;
382 port->kernel = &pcallbacks;
383 if (rmidi->ops && rmidi->ops->get_port_info)
384 rmidi->ops->get_port_info(rmidi, p, port);
385 if (snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_CREATE_PORT, port)<0)
387 ms->seq_client = client->seq_client;
388 ms->seq_port = port->addr.port;
390 client->ports_per_device[device] = ports;
391 client->ports[device] = msynth;
394 synths[card->number] = client;
395 mutex_unlock(®ister_mutex);
398 return 0; /* success */
401 if (msynth != NULL) {
402 for (p = 0; p < ports; p++)
403 snd_seq_midisynth_delete(&msynth[p]);
407 snd_seq_delete_kernel_client(client->seq_client);
412 mutex_unlock(®ister_mutex);
416 /* release midi synth port */
418 snd_seq_midisynth_remove(struct device *_dev)
420 struct snd_seq_device *dev = to_seq_dev(_dev);
421 struct seq_midisynth_client *client;
422 struct seq_midisynth *msynth;
423 struct snd_card *card = dev->card;
424 int device = dev->device, p, ports;
426 mutex_lock(®ister_mutex);
427 client = synths[card->number];
428 if (client == NULL || client->ports[device] == NULL) {
429 mutex_unlock(®ister_mutex);
432 ports = client->ports_per_device[device];
433 client->ports_per_device[device] = 0;
434 msynth = client->ports[device];
435 client->ports[device] = NULL;
436 for (p = 0; p < ports; p++)
437 snd_seq_midisynth_delete(&msynth[p]);
440 if (client->num_ports <= 0) {
441 snd_seq_delete_kernel_client(client->seq_client);
442 synths[card->number] = NULL;
445 mutex_unlock(®ister_mutex);
449 static struct snd_seq_driver seq_midisynth_driver = {
451 .name = KBUILD_MODNAME,
452 .probe = snd_seq_midisynth_probe,
453 .remove = snd_seq_midisynth_remove,
455 .id = SNDRV_SEQ_DEV_ID_MIDISYNTH,
459 module_snd_seq_driver(seq_midisynth_driver);