xref: /openbmc/linux/sound/core/seq/seq_midi.c (revision 49c23519)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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>
6  */
7 
8 /*
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)
12 */
13 
14 
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>
27 
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.");
37 
38 /* data for this midi synth driver */
39 struct seq_midisynth {
40 	struct snd_card *card;
41 	struct snd_rawmidi *rmidi;
42 	int device;
43 	int subdevice;
44 	struct snd_rawmidi_file input_rfile;
45 	struct snd_rawmidi_file output_rfile;
46 	int seq_client;
47 	int seq_port;
48 	struct snd_midi_event *parser;
49 };
50 
51 struct seq_midisynth_client {
52 	int seq_client;
53 	int num_ports;
54 	int ports_per_device[SNDRV_RAWMIDI_DEVICES];
55  	struct seq_midisynth *ports[SNDRV_RAWMIDI_DEVICES];
56 };
57 
58 static struct seq_midisynth_client *synths[SNDRV_CARDS];
59 static DEFINE_MUTEX(register_mutex);
60 
61 /* handle rawmidi input event (MIDI v1.0 stream) */
62 static void snd_midi_input_event(struct snd_rawmidi_substream *substream)
63 {
64 	struct snd_rawmidi_runtime *runtime;
65 	struct seq_midisynth *msynth;
66 	struct snd_seq_event ev;
67 	char buf[16], *pbuf;
68 	long res;
69 
70 	if (substream == NULL)
71 		return;
72 	runtime = substream->runtime;
73 	msynth = runtime->private_data;
74 	if (msynth == NULL)
75 		return;
76 	memset(&ev, 0, sizeof(ev));
77 	while (runtime->avail > 0) {
78 		res = snd_rawmidi_kernel_read(substream, buf, sizeof(buf));
79 		if (res <= 0)
80 			continue;
81 		if (msynth->parser == NULL)
82 			continue;
83 		pbuf = buf;
84 		while (res-- > 0) {
85 			if (!snd_midi_event_encode_byte(msynth->parser,
86 							*pbuf++, &ev))
87 				continue;
88 			ev.source.port = msynth->seq_port;
89 			ev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
90 			snd_seq_kernel_client_dispatch(msynth->seq_client, &ev, 1, 0);
91 			/* clear event and reset header */
92 			memset(&ev, 0, sizeof(ev));
93 		}
94 	}
95 }
96 
97 static int dump_midi(struct snd_rawmidi_substream *substream, const char *buf, int count)
98 {
99 	struct snd_rawmidi_runtime *runtime;
100 	int tmp;
101 
102 	if (snd_BUG_ON(!substream || !buf))
103 		return -EINVAL;
104 	runtime = substream->runtime;
105 	tmp = runtime->avail;
106 	if (tmp < count) {
107 		if (printk_ratelimit())
108 			pr_err("ALSA: seq_midi: MIDI output buffer overrun\n");
109 		return -ENOMEM;
110 	}
111 	if (snd_rawmidi_kernel_write(substream, buf, count) < count)
112 		return -EINVAL;
113 	return 0;
114 }
115 
116 /* callback for snd_seq_dump_var_event(), bridging to dump_midi() */
117 static int __dump_midi(void *ptr, void *buf, int count)
118 {
119 	return dump_midi(ptr, buf, count);
120 }
121 
122 static int event_process_midi(struct snd_seq_event *ev, int direct,
123 			      void *private_data, int atomic, int hop)
124 {
125 	struct seq_midisynth *msynth = private_data;
126 	unsigned char msg[10];	/* buffer for constructing midi messages */
127 	struct snd_rawmidi_substream *substream;
128 	int len;
129 
130 	if (snd_BUG_ON(!msynth))
131 		return -EINVAL;
132 	substream = msynth->output_rfile.output;
133 	if (substream == NULL)
134 		return -ENODEV;
135 	if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {	/* special case, to save space */
136 		if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE) {
137 			/* invalid event */
138 			pr_debug("ALSA: seq_midi: invalid sysex event flags = 0x%x\n", ev->flags);
139 			return 0;
140 		}
141 		snd_seq_dump_var_event(ev, __dump_midi, substream);
142 		snd_midi_event_reset_decode(msynth->parser);
143 	} else {
144 		if (msynth->parser == NULL)
145 			return -EIO;
146 		len = snd_midi_event_decode(msynth->parser, msg, sizeof(msg), ev);
147 		if (len < 0)
148 			return 0;
149 		if (dump_midi(substream, msg, len) < 0)
150 			snd_midi_event_reset_decode(msynth->parser);
151 	}
152 	return 0;
153 }
154 
155 
156 static int snd_seq_midisynth_new(struct seq_midisynth *msynth,
157 				 struct snd_card *card,
158 				 int device,
159 				 int subdevice)
160 {
161 	if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &msynth->parser) < 0)
162 		return -ENOMEM;
163 	msynth->card = card;
164 	msynth->device = device;
165 	msynth->subdevice = subdevice;
166 	return 0;
167 }
168 
169 /* open associated midi device for input */
170 static int midisynth_subscribe(void *private_data, struct snd_seq_port_subscribe *info)
171 {
172 	int err;
173 	struct seq_midisynth *msynth = private_data;
174 	struct snd_rawmidi_runtime *runtime;
175 	struct snd_rawmidi_params params;
176 
177 	/* open midi port */
178 	err = snd_rawmidi_kernel_open(msynth->rmidi, msynth->subdevice,
179 				      SNDRV_RAWMIDI_LFLG_INPUT,
180 				      &msynth->input_rfile);
181 	if (err < 0) {
182 		pr_debug("ALSA: seq_midi: midi input open failed!!!\n");
183 		return err;
184 	}
185 	runtime = msynth->input_rfile.input->runtime;
186 	memset(&params, 0, sizeof(params));
187 	params.avail_min = 1;
188 	params.buffer_size = input_buffer_size;
189 	err = snd_rawmidi_input_params(msynth->input_rfile.input, &params);
190 	if (err < 0) {
191 		snd_rawmidi_kernel_release(&msynth->input_rfile);
192 		return err;
193 	}
194 	snd_midi_event_reset_encode(msynth->parser);
195 	runtime->event = snd_midi_input_event;
196 	runtime->private_data = msynth;
197 	snd_rawmidi_kernel_read(msynth->input_rfile.input, NULL, 0);
198 	return 0;
199 }
200 
201 /* close associated midi device for input */
202 static int midisynth_unsubscribe(void *private_data, struct snd_seq_port_subscribe *info)
203 {
204 	int err;
205 	struct seq_midisynth *msynth = private_data;
206 
207 	if (snd_BUG_ON(!msynth->input_rfile.input))
208 		return -EINVAL;
209 	err = snd_rawmidi_kernel_release(&msynth->input_rfile);
210 	return err;
211 }
212 
213 /* open associated midi device for output */
214 static int midisynth_use(void *private_data, struct snd_seq_port_subscribe *info)
215 {
216 	int err;
217 	struct seq_midisynth *msynth = private_data;
218 	struct snd_rawmidi_params params;
219 
220 	/* open midi port */
221 	err = snd_rawmidi_kernel_open(msynth->rmidi, msynth->subdevice,
222 				      SNDRV_RAWMIDI_LFLG_OUTPUT,
223 				      &msynth->output_rfile);
224 	if (err < 0) {
225 		pr_debug("ALSA: seq_midi: midi output open failed!!!\n");
226 		return err;
227 	}
228 	memset(&params, 0, sizeof(params));
229 	params.avail_min = 1;
230 	params.buffer_size = output_buffer_size;
231 	params.no_active_sensing = 1;
232 	err = snd_rawmidi_output_params(msynth->output_rfile.output, &params);
233 	if (err < 0) {
234 		snd_rawmidi_kernel_release(&msynth->output_rfile);
235 		return err;
236 	}
237 	snd_midi_event_reset_decode(msynth->parser);
238 	return 0;
239 }
240 
241 /* close associated midi device for output */
242 static int midisynth_unuse(void *private_data, struct snd_seq_port_subscribe *info)
243 {
244 	struct seq_midisynth *msynth = private_data;
245 
246 	if (snd_BUG_ON(!msynth->output_rfile.output))
247 		return -EINVAL;
248 	snd_rawmidi_drain_output(msynth->output_rfile.output);
249 	return snd_rawmidi_kernel_release(&msynth->output_rfile);
250 }
251 
252 /* delete given midi synth port */
253 static void snd_seq_midisynth_delete(struct seq_midisynth *msynth)
254 {
255 	if (msynth == NULL)
256 		return;
257 
258 	if (msynth->seq_client > 0) {
259 		/* delete port */
260 		snd_seq_event_port_detach(msynth->seq_client, msynth->seq_port);
261 	}
262 
263 	snd_midi_event_free(msynth->parser);
264 }
265 
266 /* register new midi synth port */
267 static int
268 snd_seq_midisynth_probe(struct device *_dev)
269 {
270 	struct snd_seq_device *dev = to_seq_dev(_dev);
271 	struct seq_midisynth_client *client;
272 	struct seq_midisynth *msynth, *ms;
273 	struct snd_seq_port_info *port;
274 	struct snd_rawmidi_info *info;
275 	struct snd_rawmidi *rmidi = dev->private_data;
276 	int newclient = 0;
277 	unsigned int p, ports;
278 	struct snd_seq_port_callback pcallbacks;
279 	struct snd_card *card = dev->card;
280 	int device = dev->device;
281 	unsigned int input_count = 0, output_count = 0;
282 
283 	if (snd_BUG_ON(!card || device < 0 || device >= SNDRV_RAWMIDI_DEVICES))
284 		return -EINVAL;
285 	info = kmalloc(sizeof(*info), GFP_KERNEL);
286 	if (! info)
287 		return -ENOMEM;
288 	info->device = device;
289 	info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
290 	info->subdevice = 0;
291 	if (snd_rawmidi_info_select(card, info) >= 0)
292 		output_count = info->subdevices_count;
293 	info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
294 	if (snd_rawmidi_info_select(card, info) >= 0) {
295 		input_count = info->subdevices_count;
296 	}
297 	ports = output_count;
298 	if (ports < input_count)
299 		ports = input_count;
300 	if (ports == 0) {
301 		kfree(info);
302 		return -ENODEV;
303 	}
304 	if (ports > (256 / SNDRV_RAWMIDI_DEVICES))
305 		ports = 256 / SNDRV_RAWMIDI_DEVICES;
306 
307 	mutex_lock(&register_mutex);
308 	client = synths[card->number];
309 	if (client == NULL) {
310 		newclient = 1;
311 		client = kzalloc(sizeof(*client), GFP_KERNEL);
312 		if (client == NULL) {
313 			mutex_unlock(&register_mutex);
314 			kfree(info);
315 			return -ENOMEM;
316 		}
317 		client->seq_client =
318 			snd_seq_create_kernel_client(
319 				card, 0, "%s", card->shortname[0] ?
320 				(const char *)card->shortname : "External MIDI");
321 		if (client->seq_client < 0) {
322 			kfree(client);
323 			mutex_unlock(&register_mutex);
324 			kfree(info);
325 			return -ENOMEM;
326 		}
327 	}
328 
329 	msynth = kcalloc(ports, sizeof(struct seq_midisynth), GFP_KERNEL);
330 	port = kmalloc(sizeof(*port), GFP_KERNEL);
331 	if (msynth == NULL || port == NULL)
332 		goto __nomem;
333 
334 	for (p = 0; p < ports; p++) {
335 		ms = &msynth[p];
336 		ms->rmidi = rmidi;
337 
338 		if (snd_seq_midisynth_new(ms, card, device, p) < 0)
339 			goto __nomem;
340 
341 		/* declare port */
342 		memset(port, 0, sizeof(*port));
343 		port->addr.client = client->seq_client;
344 		port->addr.port = device * (256 / SNDRV_RAWMIDI_DEVICES) + p;
345 		port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
346 		memset(info, 0, sizeof(*info));
347 		info->device = device;
348 		if (p < output_count)
349 			info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
350 		else
351 			info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
352 		info->subdevice = p;
353 		if (snd_rawmidi_info_select(card, info) >= 0)
354 			strcpy(port->name, info->subname);
355 		if (! port->name[0]) {
356 			if (info->name[0]) {
357 				if (ports > 1)
358 					scnprintf(port->name, sizeof(port->name), "%s-%u", info->name, p);
359 				else
360 					scnprintf(port->name, sizeof(port->name), "%s", info->name);
361 			} else {
362 				/* last resort */
363 				if (ports > 1)
364 					sprintf(port->name, "MIDI %d-%d-%u", card->number, device, p);
365 				else
366 					sprintf(port->name, "MIDI %d-%d", card->number, device);
367 			}
368 		}
369 		if ((info->flags & SNDRV_RAWMIDI_INFO_OUTPUT) && p < output_count)
370 			port->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
371 		if ((info->flags & SNDRV_RAWMIDI_INFO_INPUT) && p < input_count)
372 			port->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
373 		if ((port->capability & (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ)) == (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ) &&
374 		    info->flags & SNDRV_RAWMIDI_INFO_DUPLEX)
375 			port->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
376 		if (port->capability & SNDRV_SEQ_PORT_CAP_READ)
377 			port->direction |= SNDRV_SEQ_PORT_DIR_INPUT;
378 		if (port->capability & SNDRV_SEQ_PORT_CAP_WRITE)
379 			port->direction |= SNDRV_SEQ_PORT_DIR_OUTPUT;
380 		port->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
381 			| SNDRV_SEQ_PORT_TYPE_HARDWARE
382 			| SNDRV_SEQ_PORT_TYPE_PORT;
383 		port->midi_channels = 16;
384 		memset(&pcallbacks, 0, sizeof(pcallbacks));
385 		pcallbacks.owner = THIS_MODULE;
386 		pcallbacks.private_data = ms;
387 		pcallbacks.subscribe = midisynth_subscribe;
388 		pcallbacks.unsubscribe = midisynth_unsubscribe;
389 		pcallbacks.use = midisynth_use;
390 		pcallbacks.unuse = midisynth_unuse;
391 		pcallbacks.event_input = event_process_midi;
392 		port->kernel = &pcallbacks;
393 		if (rmidi->ops && rmidi->ops->get_port_info)
394 			rmidi->ops->get_port_info(rmidi, p, port);
395 		if (snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_CREATE_PORT, port)<0)
396 			goto __nomem;
397 		ms->seq_client = client->seq_client;
398 		ms->seq_port = port->addr.port;
399 	}
400 	client->ports_per_device[device] = ports;
401 	client->ports[device] = msynth;
402 	client->num_ports++;
403 	if (newclient)
404 		synths[card->number] = client;
405 	mutex_unlock(&register_mutex);
406 	kfree(info);
407 	kfree(port);
408 	return 0;	/* success */
409 
410       __nomem:
411 	if (msynth != NULL) {
412 	      	for (p = 0; p < ports; p++)
413 	      		snd_seq_midisynth_delete(&msynth[p]);
414 		kfree(msynth);
415 	}
416 	if (newclient) {
417 		snd_seq_delete_kernel_client(client->seq_client);
418 		kfree(client);
419 	}
420 	kfree(info);
421 	kfree(port);
422 	mutex_unlock(&register_mutex);
423 	return -ENOMEM;
424 }
425 
426 /* release midi synth port */
427 static int
428 snd_seq_midisynth_remove(struct device *_dev)
429 {
430 	struct snd_seq_device *dev = to_seq_dev(_dev);
431 	struct seq_midisynth_client *client;
432 	struct seq_midisynth *msynth;
433 	struct snd_card *card = dev->card;
434 	int device = dev->device, p, ports;
435 
436 	mutex_lock(&register_mutex);
437 	client = synths[card->number];
438 	if (client == NULL || client->ports[device] == NULL) {
439 		mutex_unlock(&register_mutex);
440 		return -ENODEV;
441 	}
442 	ports = client->ports_per_device[device];
443 	client->ports_per_device[device] = 0;
444 	msynth = client->ports[device];
445 	client->ports[device] = NULL;
446 	for (p = 0; p < ports; p++)
447 		snd_seq_midisynth_delete(&msynth[p]);
448 	kfree(msynth);
449 	client->num_ports--;
450 	if (client->num_ports <= 0) {
451 		snd_seq_delete_kernel_client(client->seq_client);
452 		synths[card->number] = NULL;
453 		kfree(client);
454 	}
455 	mutex_unlock(&register_mutex);
456 	return 0;
457 }
458 
459 static struct snd_seq_driver seq_midisynth_driver = {
460 	.driver = {
461 		.name = KBUILD_MODNAME,
462 		.probe = snd_seq_midisynth_probe,
463 		.remove = snd_seq_midisynth_remove,
464 	},
465 	.id = SNDRV_SEQ_DEV_ID_MIDISYNTH,
466 	.argsize = 0,
467 };
468 
469 module_snd_seq_driver(seq_midisynth_driver);
470