xref: /openbmc/linux/sound/core/seq/seq_midi.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
1 /*
2  *   Generic MIDI synth driver for ALSA sequencer
3  *   Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
4  *                         Jaroslav Kysela <perex@suse.cz>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21 
22 /*
23 Possible options for midisynth module:
24 	- automatic opening of midi ports on first received event or subscription
25 	  (close will be performed when client leaves)
26 */
27 
28 
29 #include <sound/driver.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/errno.h>
33 #include <linux/string.h>
34 #include <linux/moduleparam.h>
35 #include <asm/semaphore.h>
36 #include <sound/core.h>
37 #include <sound/rawmidi.h>
38 #include <sound/seq_kernel.h>
39 #include <sound/seq_device.h>
40 #include <sound/seq_midi_event.h>
41 #include <sound/initval.h>
42 
43 MODULE_AUTHOR("Frank van de Pol <fvdpol@coil.demon.nl>, Jaroslav Kysela <perex@suse.cz>");
44 MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI synth.");
45 MODULE_LICENSE("GPL");
46 static int output_buffer_size = PAGE_SIZE;
47 module_param(output_buffer_size, int, 0644);
48 MODULE_PARM_DESC(output_buffer_size, "Output buffer size in bytes.");
49 static int input_buffer_size = PAGE_SIZE;
50 module_param(input_buffer_size, int, 0644);
51 MODULE_PARM_DESC(input_buffer_size, "Input buffer size in bytes.");
52 
53 /* data for this midi synth driver */
54 typedef struct {
55 	snd_card_t *card;
56 	int device;
57 	int subdevice;
58 	snd_rawmidi_file_t input_rfile;
59 	snd_rawmidi_file_t output_rfile;
60 	int seq_client;
61 	int seq_port;
62 	snd_midi_event_t *parser;
63 } seq_midisynth_t;
64 
65 typedef struct {
66 	int seq_client;
67 	int num_ports;
68 	int ports_per_device[SNDRV_RAWMIDI_DEVICES];
69  	seq_midisynth_t *ports[SNDRV_RAWMIDI_DEVICES];
70 } seq_midisynth_client_t;
71 
72 static seq_midisynth_client_t *synths[SNDRV_CARDS];
73 static DECLARE_MUTEX(register_mutex);
74 
75 /* handle rawmidi input event (MIDI v1.0 stream) */
76 static void snd_midi_input_event(snd_rawmidi_substream_t * substream)
77 {
78 	snd_rawmidi_runtime_t *runtime;
79 	seq_midisynth_t *msynth;
80 	snd_seq_event_t ev;
81 	char buf[16], *pbuf;
82 	long res, count;
83 
84 	if (substream == NULL)
85 		return;
86 	runtime = substream->runtime;
87 	msynth = (seq_midisynth_t *) runtime->private_data;
88 	if (msynth == NULL)
89 		return;
90 	memset(&ev, 0, sizeof(ev));
91 	while (runtime->avail > 0) {
92 		res = snd_rawmidi_kernel_read(substream, buf, sizeof(buf));
93 		if (res <= 0)
94 			continue;
95 		if (msynth->parser == NULL)
96 			continue;
97 		pbuf = buf;
98 		while (res > 0) {
99 			count = snd_midi_event_encode(msynth->parser, pbuf, res, &ev);
100 			if (count < 0)
101 				break;
102 			pbuf += count;
103 			res -= count;
104 			if (ev.type != SNDRV_SEQ_EVENT_NONE) {
105 				ev.source.port = msynth->seq_port;
106 				ev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
107 				snd_seq_kernel_client_dispatch(msynth->seq_client, &ev, 1, 0);
108 				/* clear event and reset header */
109 				memset(&ev, 0, sizeof(ev));
110 			}
111 		}
112 	}
113 }
114 
115 static int dump_midi(snd_rawmidi_substream_t *substream, const char *buf, int count)
116 {
117 	snd_rawmidi_runtime_t *runtime;
118 	int tmp;
119 
120 	snd_assert(substream != NULL || buf != NULL, return -EINVAL);
121 	runtime = substream->runtime;
122 	if ((tmp = runtime->avail) < count) {
123 		snd_printd("warning, output event was lost (count = %i, available = %i)\n", count, tmp);
124 		return -ENOMEM;
125 	}
126 	if (snd_rawmidi_kernel_write(substream, buf, count) < count)
127 		return -EINVAL;
128 	return 0;
129 }
130 
131 static int event_process_midi(snd_seq_event_t * ev, int direct,
132 			      void *private_data, int atomic, int hop)
133 {
134 	seq_midisynth_t *msynth = (seq_midisynth_t *) private_data;
135 	unsigned char msg[10];	/* buffer for constructing midi messages */
136 	snd_rawmidi_substream_t *substream;
137 	int res;
138 
139 	snd_assert(msynth != NULL, return -EINVAL);
140 	substream = msynth->output_rfile.output;
141 	if (substream == NULL)
142 		return -ENODEV;
143 	if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {	/* special case, to save space */
144 		if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE) {
145 			/* invalid event */
146 			snd_printd("seq_midi: invalid sysex event flags = 0x%x\n", ev->flags);
147 			return 0;
148 		}
149 		res = snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)dump_midi, substream);
150 		snd_midi_event_reset_decode(msynth->parser);
151 		if (res < 0)
152 			return res;
153 	} else {
154 		if (msynth->parser == NULL)
155 			return -EIO;
156 		res = snd_midi_event_decode(msynth->parser, msg, sizeof(msg), ev);
157 		if (res < 0)
158 			return res;
159 		if ((res = dump_midi(substream, msg, res)) < 0) {
160 			snd_midi_event_reset_decode(msynth->parser);
161 			return res;
162 		}
163 	}
164 	return 0;
165 }
166 
167 
168 static int snd_seq_midisynth_new(seq_midisynth_t *msynth,
169 				 snd_card_t *card,
170 				 int device,
171 				 int subdevice)
172 {
173 	if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &msynth->parser) < 0)
174 		return -ENOMEM;
175 	msynth->card = card;
176 	msynth->device = device;
177 	msynth->subdevice = subdevice;
178 	return 0;
179 }
180 
181 /* open associated midi device for input */
182 static int midisynth_subscribe(void *private_data, snd_seq_port_subscribe_t *info)
183 {
184 	int err;
185 	seq_midisynth_t *msynth = (seq_midisynth_t *)private_data;
186 	snd_rawmidi_runtime_t *runtime;
187 	snd_rawmidi_params_t params;
188 
189 	/* open midi port */
190 	if ((err = snd_rawmidi_kernel_open(msynth->card->number, msynth->device, msynth->subdevice, SNDRV_RAWMIDI_LFLG_INPUT, &msynth->input_rfile)) < 0) {
191 		snd_printd("midi input open failed!!!\n");
192 		return err;
193 	}
194 	runtime = msynth->input_rfile.input->runtime;
195 	memset(&params, 0, sizeof(params));
196 	params.avail_min = 1;
197 	params.buffer_size = input_buffer_size;
198 	if ((err = snd_rawmidi_input_params(msynth->input_rfile.input, &params)) < 0) {
199 		snd_rawmidi_kernel_release(&msynth->input_rfile);
200 		return err;
201 	}
202 	snd_midi_event_reset_encode(msynth->parser);
203 	runtime->event = snd_midi_input_event;
204 	runtime->private_data = msynth;
205 	snd_rawmidi_kernel_read(msynth->input_rfile.input, NULL, 0);
206 	return 0;
207 }
208 
209 /* close associated midi device for input */
210 static int midisynth_unsubscribe(void *private_data, snd_seq_port_subscribe_t *info)
211 {
212 	int err;
213 	seq_midisynth_t *msynth = (seq_midisynth_t *)private_data;
214 
215 	snd_assert(msynth->input_rfile.input != NULL, return -EINVAL);
216 	err = snd_rawmidi_kernel_release(&msynth->input_rfile);
217 	return err;
218 }
219 
220 /* open associated midi device for output */
221 static int midisynth_use(void *private_data, snd_seq_port_subscribe_t *info)
222 {
223 	int err;
224 	seq_midisynth_t *msynth = (seq_midisynth_t *)private_data;
225 	snd_rawmidi_params_t params;
226 
227 	/* open midi port */
228 	if ((err = snd_rawmidi_kernel_open(msynth->card->number, msynth->device, msynth->subdevice, SNDRV_RAWMIDI_LFLG_OUTPUT, &msynth->output_rfile)) < 0) {
229 		snd_printd("midi output open failed!!!\n");
230 		return err;
231 	}
232 	memset(&params, 0, sizeof(params));
233 	params.avail_min = 1;
234 	params.buffer_size = output_buffer_size;
235 	if ((err = snd_rawmidi_output_params(msynth->output_rfile.output, &params)) < 0) {
236 		snd_rawmidi_kernel_release(&msynth->output_rfile);
237 		return err;
238 	}
239 	snd_midi_event_reset_decode(msynth->parser);
240 	return 0;
241 }
242 
243 /* close associated midi device for output */
244 static int midisynth_unuse(void *private_data, snd_seq_port_subscribe_t *info)
245 {
246 	seq_midisynth_t *msynth = (seq_midisynth_t *)private_data;
247 	unsigned char buf = 0xff; /* MIDI reset */
248 
249 	snd_assert(msynth->output_rfile.output != NULL, return -EINVAL);
250 	/* sending single MIDI reset message to shut the device up */
251 	snd_rawmidi_kernel_write(msynth->output_rfile.output, &buf, 1);
252 	snd_rawmidi_drain_output(msynth->output_rfile.output);
253 	return snd_rawmidi_kernel_release(&msynth->output_rfile);
254 }
255 
256 /* delete given midi synth port */
257 static void snd_seq_midisynth_delete(seq_midisynth_t *msynth)
258 {
259 	if (msynth == NULL)
260 		return;
261 
262 	if (msynth->seq_client > 0) {
263 		/* delete port */
264 		snd_seq_event_port_detach(msynth->seq_client, msynth->seq_port);
265 	}
266 
267 	if (msynth->parser)
268 		snd_midi_event_free(msynth->parser);
269 }
270 
271 /* set our client name */
272 static int set_client_name(seq_midisynth_client_t *client, snd_card_t *card,
273 			   snd_rawmidi_info_t *rmidi)
274 {
275 	snd_seq_client_info_t cinfo;
276 	const char *name;
277 
278 	memset(&cinfo, 0, sizeof(cinfo));
279 	cinfo.client = client->seq_client;
280 	cinfo.type = KERNEL_CLIENT;
281 	name = rmidi->name[0] ? (const char *)rmidi->name : "External MIDI";
282 	strlcpy(cinfo.name, name, sizeof(cinfo.name));
283 	return snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, &cinfo);
284 }
285 
286 /* register new midi synth port */
287 static int
288 snd_seq_midisynth_register_port(snd_seq_device_t *dev)
289 {
290 	seq_midisynth_client_t *client;
291 	seq_midisynth_t *msynth, *ms;
292 	snd_seq_port_info_t *port;
293 	snd_rawmidi_info_t *info;
294 	int newclient = 0;
295 	unsigned int p, ports;
296 	snd_seq_client_callback_t callbacks;
297 	snd_seq_port_callback_t pcallbacks;
298 	snd_card_t *card = dev->card;
299 	int device = dev->device;
300 	unsigned int input_count = 0, output_count = 0;
301 
302 	snd_assert(card != NULL && device >= 0 && device < SNDRV_RAWMIDI_DEVICES, return -EINVAL);
303 	info = kmalloc(sizeof(*info), GFP_KERNEL);
304 	if (! info)
305 		return -ENOMEM;
306 	info->device = device;
307 	info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
308 	info->subdevice = 0;
309 	if (snd_rawmidi_info_select(card, info) >= 0)
310 		output_count = info->subdevices_count;
311 	info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
312 	if (snd_rawmidi_info_select(card, info) >= 0) {
313 		input_count = info->subdevices_count;
314 	}
315 	ports = output_count;
316 	if (ports < input_count)
317 		ports = input_count;
318 	if (ports == 0) {
319 		kfree(info);
320 		return -ENODEV;
321 	}
322 	if (ports > (256 / SNDRV_RAWMIDI_DEVICES))
323 		ports = 256 / SNDRV_RAWMIDI_DEVICES;
324 
325 	down(&register_mutex);
326 	client = synths[card->number];
327 	if (client == NULL) {
328 		newclient = 1;
329 		client = kcalloc(1, sizeof(*client), GFP_KERNEL);
330 		if (client == NULL) {
331 			up(&register_mutex);
332 			kfree(info);
333 			return -ENOMEM;
334 		}
335 		memset(&callbacks, 0, sizeof(callbacks));
336 		callbacks.private_data = client;
337 		callbacks.allow_input = callbacks.allow_output = 1;
338 		client->seq_client = snd_seq_create_kernel_client(card, 0, &callbacks);
339 		if (client->seq_client < 0) {
340 			kfree(client);
341 			up(&register_mutex);
342 			kfree(info);
343 			return -ENOMEM;
344 		}
345 		set_client_name(client, card, info);
346 	} else if (device == 0)
347 		set_client_name(client, card, info); /* use the first device's name */
348 
349 	msynth = kcalloc(ports, sizeof(seq_midisynth_t), GFP_KERNEL);
350 	port = kmalloc(sizeof(*port), GFP_KERNEL);
351 	if (msynth == NULL || port == NULL)
352 		goto __nomem;
353 
354 	for (p = 0; p < ports; p++) {
355 		ms = &msynth[p];
356 
357 		if (snd_seq_midisynth_new(ms, card, device, p) < 0)
358 			goto __nomem;
359 
360 		/* declare port */
361 		memset(port, 0, sizeof(*port));
362 		port->addr.client = client->seq_client;
363 		port->addr.port = device * (256 / SNDRV_RAWMIDI_DEVICES) + p;
364 		port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
365 		memset(info, 0, sizeof(*info));
366 		info->device = device;
367 		if (p < output_count)
368 			info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
369 		else
370 			info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
371 		info->subdevice = p;
372 		if (snd_rawmidi_info_select(card, info) >= 0)
373 			strcpy(port->name, info->subname);
374 		if (! port->name[0]) {
375 			if (info->name[0]) {
376 				if (ports > 1)
377 					snprintf(port->name, sizeof(port->name), "%s-%d", info->name, p);
378 				else
379 					snprintf(port->name, sizeof(port->name), "%s", info->name);
380 			} else {
381 				/* last resort */
382 				if (ports > 1)
383 					sprintf(port->name, "MIDI %d-%d-%d", card->number, device, p);
384 				else
385 					sprintf(port->name, "MIDI %d-%d", card->number, device);
386 			}
387 		}
388 		if ((info->flags & SNDRV_RAWMIDI_INFO_OUTPUT) && p < output_count)
389 			port->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
390 		if ((info->flags & SNDRV_RAWMIDI_INFO_INPUT) && p < input_count)
391 			port->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
392 		if ((port->capability & (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ)) == (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ) &&
393 		    info->flags & SNDRV_RAWMIDI_INFO_DUPLEX)
394 			port->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
395 		port->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC;
396 		port->midi_channels = 16;
397 		memset(&pcallbacks, 0, sizeof(pcallbacks));
398 		pcallbacks.owner = THIS_MODULE;
399 		pcallbacks.private_data = ms;
400 		pcallbacks.subscribe = midisynth_subscribe;
401 		pcallbacks.unsubscribe = midisynth_unsubscribe;
402 		pcallbacks.use = midisynth_use;
403 		pcallbacks.unuse = midisynth_unuse;
404 		pcallbacks.event_input = event_process_midi;
405 		port->kernel = &pcallbacks;
406 		if (snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_CREATE_PORT, port)<0)
407 			goto __nomem;
408 		ms->seq_client = client->seq_client;
409 		ms->seq_port = port->addr.port;
410 	}
411 	client->ports_per_device[device] = ports;
412 	client->ports[device] = msynth;
413 	client->num_ports++;
414 	if (newclient)
415 		synths[card->number] = client;
416 	up(&register_mutex);
417 	return 0;	/* success */
418 
419       __nomem:
420 	if (msynth != NULL) {
421 	      	for (p = 0; p < ports; p++)
422 	      		snd_seq_midisynth_delete(&msynth[p]);
423 		kfree(msynth);
424 	}
425 	if (newclient) {
426 		snd_seq_delete_kernel_client(client->seq_client);
427 		kfree(client);
428 	}
429 	kfree(info);
430 	kfree(port);
431 	up(&register_mutex);
432 	return -ENOMEM;
433 }
434 
435 /* release midi synth port */
436 static int
437 snd_seq_midisynth_unregister_port(snd_seq_device_t *dev)
438 {
439 	seq_midisynth_client_t *client;
440 	seq_midisynth_t *msynth;
441 	snd_card_t *card = dev->card;
442 	int device = dev->device, p, ports;
443 
444 	down(&register_mutex);
445 	client = synths[card->number];
446 	if (client == NULL || client->ports[device] == NULL) {
447 		up(&register_mutex);
448 		return -ENODEV;
449 	}
450 	ports = client->ports_per_device[device];
451 	client->ports_per_device[device] = 0;
452 	msynth = client->ports[device];
453 	client->ports[device] = NULL;
454 	snd_runtime_check(msynth != NULL || ports <= 0, goto __skip);
455 	for (p = 0; p < ports; p++)
456 		snd_seq_midisynth_delete(&msynth[p]);
457 	kfree(msynth);
458       __skip:
459 	client->num_ports--;
460 	if (client->num_ports <= 0) {
461 		snd_seq_delete_kernel_client(client->seq_client);
462 		synths[card->number] = NULL;
463 		kfree(client);
464 	}
465 	up(&register_mutex);
466 	return 0;
467 }
468 
469 
470 static int __init alsa_seq_midi_init(void)
471 {
472 	static snd_seq_dev_ops_t ops = {
473 		snd_seq_midisynth_register_port,
474 		snd_seq_midisynth_unregister_port,
475 	};
476 	memset(&synths, 0, sizeof(synths));
477 	snd_seq_autoload_lock();
478 	snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_MIDISYNTH, &ops, 0);
479 	snd_seq_autoload_unlock();
480 	return 0;
481 }
482 
483 static void __exit alsa_seq_midi_exit(void)
484 {
485 	snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_MIDISYNTH);
486 }
487 
488 module_init(alsa_seq_midi_init)
489 module_exit(alsa_seq_midi_exit)
490