xref: /openbmc/linux/sound/core/seq/seq_virmidi.c (revision 89b4ab21)
1 /*
2  *  Virtual Raw MIDI client on Sequencer
3  *
4  *  Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>,
5  *                        Jaroslav Kysela <perex@perex.cz>
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 2 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  *
21  */
22 
23 /*
24  * Virtual Raw MIDI client
25  *
26  * The virtual rawmidi client is a sequencer client which associate
27  * a rawmidi device file.  The created rawmidi device file can be
28  * accessed as a normal raw midi, but its MIDI source and destination
29  * are arbitrary.  For example, a user-client software synth connected
30  * to this port can be used as a normal midi device as well.
31  *
32  * The virtual rawmidi device accepts also multiple opens.  Each file
33  * has its own input buffer, so that no conflict would occur.  The drain
34  * of input/output buffer acts only to the local buffer.
35  *
36  */
37 
38 #include <linux/init.h>
39 #include <linux/wait.h>
40 #include <linux/module.h>
41 #include <linux/slab.h>
42 #include <sound/core.h>
43 #include <sound/rawmidi.h>
44 #include <sound/info.h>
45 #include <sound/control.h>
46 #include <sound/minors.h>
47 #include <sound/seq_kernel.h>
48 #include <sound/seq_midi_event.h>
49 #include <sound/seq_virmidi.h>
50 
51 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
52 MODULE_DESCRIPTION("Virtual Raw MIDI client on Sequencer");
53 MODULE_LICENSE("GPL");
54 
55 /*
56  * initialize an event record
57  */
58 static void snd_virmidi_init_event(struct snd_virmidi *vmidi,
59 				   struct snd_seq_event *ev)
60 {
61 	memset(ev, 0, sizeof(*ev));
62 	ev->source.port = vmidi->port;
63 	switch (vmidi->seq_mode) {
64 	case SNDRV_VIRMIDI_SEQ_DISPATCH:
65 		ev->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
66 		break;
67 	case SNDRV_VIRMIDI_SEQ_ATTACH:
68 		/* FIXME: source and destination are same - not good.. */
69 		ev->dest.client = vmidi->client;
70 		ev->dest.port = vmidi->port;
71 		break;
72 	}
73 	ev->type = SNDRV_SEQ_EVENT_NONE;
74 }
75 
76 /*
77  * decode input event and put to read buffer of each opened file
78  */
79 static int snd_virmidi_dev_receive_event(struct snd_virmidi_dev *rdev,
80 					 struct snd_seq_event *ev,
81 					 bool atomic)
82 {
83 	struct snd_virmidi *vmidi;
84 	unsigned char msg[4];
85 	int len;
86 
87 	if (atomic)
88 		read_lock(&rdev->filelist_lock);
89 	else
90 		down_read(&rdev->filelist_sem);
91 	list_for_each_entry(vmidi, &rdev->filelist, list) {
92 		if (!READ_ONCE(vmidi->trigger))
93 			continue;
94 		if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
95 			if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
96 				continue;
97 			snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)snd_rawmidi_receive, vmidi->substream);
98 		} else {
99 			len = snd_midi_event_decode(vmidi->parser, msg, sizeof(msg), ev);
100 			if (len > 0)
101 				snd_rawmidi_receive(vmidi->substream, msg, len);
102 		}
103 	}
104 	if (atomic)
105 		read_unlock(&rdev->filelist_lock);
106 	else
107 		up_read(&rdev->filelist_sem);
108 
109 	return 0;
110 }
111 
112 /*
113  * receive an event from the remote virmidi port
114  *
115  * for rawmidi inputs, you can call this function from the event
116  * handler of a remote port which is attached to the virmidi via
117  * SNDRV_VIRMIDI_SEQ_ATTACH.
118  */
119 #if 0
120 int snd_virmidi_receive(struct snd_rawmidi *rmidi, struct snd_seq_event *ev)
121 {
122 	struct snd_virmidi_dev *rdev;
123 
124 	rdev = rmidi->private_data;
125 	return snd_virmidi_dev_receive_event(rdev, ev, true);
126 }
127 #endif  /*  0  */
128 
129 /*
130  * event handler of virmidi port
131  */
132 static int snd_virmidi_event_input(struct snd_seq_event *ev, int direct,
133 				   void *private_data, int atomic, int hop)
134 {
135 	struct snd_virmidi_dev *rdev;
136 
137 	rdev = private_data;
138 	if (!(rdev->flags & SNDRV_VIRMIDI_USE))
139 		return 0; /* ignored */
140 	return snd_virmidi_dev_receive_event(rdev, ev, atomic);
141 }
142 
143 /*
144  * trigger rawmidi stream for input
145  */
146 static void snd_virmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
147 {
148 	struct snd_virmidi *vmidi = substream->runtime->private_data;
149 
150 	WRITE_ONCE(vmidi->trigger, !!up);
151 }
152 
153 /* process rawmidi bytes and send events;
154  * we need no lock here for vmidi->event since it's handled only in this work
155  */
156 static void snd_vmidi_output_work(struct work_struct *work)
157 {
158 	struct snd_virmidi *vmidi;
159 	struct snd_rawmidi_substream *substream;
160 	unsigned char input;
161 	int ret;
162 
163 	vmidi = container_of(work, struct snd_virmidi, output_work);
164 	substream = vmidi->substream;
165 
166 	/* discard the outputs in dispatch mode unless subscribed */
167 	if (vmidi->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH &&
168 	    !(vmidi->rdev->flags & SNDRV_VIRMIDI_SUBSCRIBE)) {
169 		while (!snd_rawmidi_transmit_empty(substream))
170 			snd_rawmidi_transmit_ack(substream, 1);
171 		return;
172 	}
173 
174 	while (READ_ONCE(vmidi->trigger)) {
175 		if (snd_rawmidi_transmit(substream, &input, 1) != 1)
176 			break;
177 		if (snd_midi_event_encode_byte(vmidi->parser, input,
178 					       &vmidi->event) <= 0)
179 			continue;
180 		if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) {
181 			ret = snd_seq_kernel_client_dispatch(vmidi->client,
182 							     &vmidi->event,
183 							     false, 0);
184 			vmidi->event.type = SNDRV_SEQ_EVENT_NONE;
185 			if (ret < 0)
186 				break;
187 		}
188 		/* rawmidi input might be huge, allow to have a break */
189 		cond_resched();
190 	}
191 }
192 
193 /*
194  * trigger rawmidi stream for output
195  */
196 static void snd_virmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
197 {
198 	struct snd_virmidi *vmidi = substream->runtime->private_data;
199 
200 	WRITE_ONCE(vmidi->trigger, !!up);
201 	if (up)
202 		queue_work(system_highpri_wq, &vmidi->output_work);
203 }
204 
205 /*
206  * open rawmidi handle for input
207  */
208 static int snd_virmidi_input_open(struct snd_rawmidi_substream *substream)
209 {
210 	struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
211 	struct snd_rawmidi_runtime *runtime = substream->runtime;
212 	struct snd_virmidi *vmidi;
213 
214 	vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
215 	if (vmidi == NULL)
216 		return -ENOMEM;
217 	vmidi->substream = substream;
218 	if (snd_midi_event_new(0, &vmidi->parser) < 0) {
219 		kfree(vmidi);
220 		return -ENOMEM;
221 	}
222 	vmidi->seq_mode = rdev->seq_mode;
223 	vmidi->client = rdev->client;
224 	vmidi->port = rdev->port;
225 	runtime->private_data = vmidi;
226 	down_write(&rdev->filelist_sem);
227 	write_lock_irq(&rdev->filelist_lock);
228 	list_add_tail(&vmidi->list, &rdev->filelist);
229 	write_unlock_irq(&rdev->filelist_lock);
230 	up_write(&rdev->filelist_sem);
231 	vmidi->rdev = rdev;
232 	return 0;
233 }
234 
235 /*
236  * open rawmidi handle for output
237  */
238 static int snd_virmidi_output_open(struct snd_rawmidi_substream *substream)
239 {
240 	struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
241 	struct snd_rawmidi_runtime *runtime = substream->runtime;
242 	struct snd_virmidi *vmidi;
243 
244 	vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
245 	if (vmidi == NULL)
246 		return -ENOMEM;
247 	vmidi->substream = substream;
248 	if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &vmidi->parser) < 0) {
249 		kfree(vmidi);
250 		return -ENOMEM;
251 	}
252 	vmidi->seq_mode = rdev->seq_mode;
253 	vmidi->client = rdev->client;
254 	vmidi->port = rdev->port;
255 	snd_virmidi_init_event(vmidi, &vmidi->event);
256 	vmidi->rdev = rdev;
257 	INIT_WORK(&vmidi->output_work, snd_vmidi_output_work);
258 	runtime->private_data = vmidi;
259 	return 0;
260 }
261 
262 /*
263  * close rawmidi handle for input
264  */
265 static int snd_virmidi_input_close(struct snd_rawmidi_substream *substream)
266 {
267 	struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
268 	struct snd_virmidi *vmidi = substream->runtime->private_data;
269 
270 	down_write(&rdev->filelist_sem);
271 	write_lock_irq(&rdev->filelist_lock);
272 	list_del(&vmidi->list);
273 	write_unlock_irq(&rdev->filelist_lock);
274 	up_write(&rdev->filelist_sem);
275 	snd_midi_event_free(vmidi->parser);
276 	substream->runtime->private_data = NULL;
277 	kfree(vmidi);
278 	return 0;
279 }
280 
281 /*
282  * close rawmidi handle for output
283  */
284 static int snd_virmidi_output_close(struct snd_rawmidi_substream *substream)
285 {
286 	struct snd_virmidi *vmidi = substream->runtime->private_data;
287 
288 	WRITE_ONCE(vmidi->trigger, false); /* to be sure */
289 	cancel_work_sync(&vmidi->output_work);
290 	snd_midi_event_free(vmidi->parser);
291 	substream->runtime->private_data = NULL;
292 	kfree(vmidi);
293 	return 0;
294 }
295 
296 /*
297  * subscribe callback - allow output to rawmidi device
298  */
299 static int snd_virmidi_subscribe(void *private_data,
300 				 struct snd_seq_port_subscribe *info)
301 {
302 	struct snd_virmidi_dev *rdev;
303 
304 	rdev = private_data;
305 	if (!try_module_get(rdev->card->module))
306 		return -EFAULT;
307 	rdev->flags |= SNDRV_VIRMIDI_SUBSCRIBE;
308 	return 0;
309 }
310 
311 /*
312  * unsubscribe callback - disallow output to rawmidi device
313  */
314 static int snd_virmidi_unsubscribe(void *private_data,
315 				   struct snd_seq_port_subscribe *info)
316 {
317 	struct snd_virmidi_dev *rdev;
318 
319 	rdev = private_data;
320 	rdev->flags &= ~SNDRV_VIRMIDI_SUBSCRIBE;
321 	module_put(rdev->card->module);
322 	return 0;
323 }
324 
325 
326 /*
327  * use callback - allow input to rawmidi device
328  */
329 static int snd_virmidi_use(void *private_data,
330 			   struct snd_seq_port_subscribe *info)
331 {
332 	struct snd_virmidi_dev *rdev;
333 
334 	rdev = private_data;
335 	if (!try_module_get(rdev->card->module))
336 		return -EFAULT;
337 	rdev->flags |= SNDRV_VIRMIDI_USE;
338 	return 0;
339 }
340 
341 /*
342  * unuse callback - disallow input to rawmidi device
343  */
344 static int snd_virmidi_unuse(void *private_data,
345 			     struct snd_seq_port_subscribe *info)
346 {
347 	struct snd_virmidi_dev *rdev;
348 
349 	rdev = private_data;
350 	rdev->flags &= ~SNDRV_VIRMIDI_USE;
351 	module_put(rdev->card->module);
352 	return 0;
353 }
354 
355 
356 /*
357  *  Register functions
358  */
359 
360 static const struct snd_rawmidi_ops snd_virmidi_input_ops = {
361 	.open = snd_virmidi_input_open,
362 	.close = snd_virmidi_input_close,
363 	.trigger = snd_virmidi_input_trigger,
364 };
365 
366 static const struct snd_rawmidi_ops snd_virmidi_output_ops = {
367 	.open = snd_virmidi_output_open,
368 	.close = snd_virmidi_output_close,
369 	.trigger = snd_virmidi_output_trigger,
370 };
371 
372 /*
373  * create a sequencer client and a port
374  */
375 static int snd_virmidi_dev_attach_seq(struct snd_virmidi_dev *rdev)
376 {
377 	int client;
378 	struct snd_seq_port_callback pcallbacks;
379 	struct snd_seq_port_info *pinfo;
380 	int err;
381 
382 	if (rdev->client >= 0)
383 		return 0;
384 
385 	pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
386 	if (!pinfo) {
387 		err = -ENOMEM;
388 		goto __error;
389 	}
390 
391 	client = snd_seq_create_kernel_client(rdev->card, rdev->device,
392 					      "%s %d-%d", rdev->rmidi->name,
393 					      rdev->card->number,
394 					      rdev->device);
395 	if (client < 0) {
396 		err = client;
397 		goto __error;
398 	}
399 	rdev->client = client;
400 
401 	/* create a port */
402 	pinfo->addr.client = client;
403 	sprintf(pinfo->name, "VirMIDI %d-%d", rdev->card->number, rdev->device);
404 	/* set all capabilities */
405 	pinfo->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
406 	pinfo->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
407 	pinfo->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
408 	pinfo->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
409 		| SNDRV_SEQ_PORT_TYPE_SOFTWARE
410 		| SNDRV_SEQ_PORT_TYPE_PORT;
411 	pinfo->midi_channels = 16;
412 	memset(&pcallbacks, 0, sizeof(pcallbacks));
413 	pcallbacks.owner = THIS_MODULE;
414 	pcallbacks.private_data = rdev;
415 	pcallbacks.subscribe = snd_virmidi_subscribe;
416 	pcallbacks.unsubscribe = snd_virmidi_unsubscribe;
417 	pcallbacks.use = snd_virmidi_use;
418 	pcallbacks.unuse = snd_virmidi_unuse;
419 	pcallbacks.event_input = snd_virmidi_event_input;
420 	pinfo->kernel = &pcallbacks;
421 	err = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, pinfo);
422 	if (err < 0) {
423 		snd_seq_delete_kernel_client(client);
424 		rdev->client = -1;
425 		goto __error;
426 	}
427 
428 	rdev->port = pinfo->addr.port;
429 	err = 0; /* success */
430 
431  __error:
432 	kfree(pinfo);
433 	return err;
434 }
435 
436 
437 /*
438  * release the sequencer client
439  */
440 static void snd_virmidi_dev_detach_seq(struct snd_virmidi_dev *rdev)
441 {
442 	if (rdev->client >= 0) {
443 		snd_seq_delete_kernel_client(rdev->client);
444 		rdev->client = -1;
445 	}
446 }
447 
448 /*
449  * register the device
450  */
451 static int snd_virmidi_dev_register(struct snd_rawmidi *rmidi)
452 {
453 	struct snd_virmidi_dev *rdev = rmidi->private_data;
454 	int err;
455 
456 	switch (rdev->seq_mode) {
457 	case SNDRV_VIRMIDI_SEQ_DISPATCH:
458 		err = snd_virmidi_dev_attach_seq(rdev);
459 		if (err < 0)
460 			return err;
461 		break;
462 	case SNDRV_VIRMIDI_SEQ_ATTACH:
463 		if (rdev->client == 0)
464 			return -EINVAL;
465 		/* should check presence of port more strictly.. */
466 		break;
467 	default:
468 		pr_err("ALSA: seq_virmidi: seq_mode is not set: %d\n", rdev->seq_mode);
469 		return -EINVAL;
470 	}
471 	return 0;
472 }
473 
474 
475 /*
476  * unregister the device
477  */
478 static int snd_virmidi_dev_unregister(struct snd_rawmidi *rmidi)
479 {
480 	struct snd_virmidi_dev *rdev = rmidi->private_data;
481 
482 	if (rdev->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH)
483 		snd_virmidi_dev_detach_seq(rdev);
484 	return 0;
485 }
486 
487 /*
488  *
489  */
490 static const struct snd_rawmidi_global_ops snd_virmidi_global_ops = {
491 	.dev_register = snd_virmidi_dev_register,
492 	.dev_unregister = snd_virmidi_dev_unregister,
493 };
494 
495 /*
496  * free device
497  */
498 static void snd_virmidi_free(struct snd_rawmidi *rmidi)
499 {
500 	struct snd_virmidi_dev *rdev = rmidi->private_data;
501 	kfree(rdev);
502 }
503 
504 /*
505  * create a new device
506  *
507  */
508 /* exported */
509 int snd_virmidi_new(struct snd_card *card, int device, struct snd_rawmidi **rrmidi)
510 {
511 	struct snd_rawmidi *rmidi;
512 	struct snd_virmidi_dev *rdev;
513 	int err;
514 
515 	*rrmidi = NULL;
516 	if ((err = snd_rawmidi_new(card, "VirMidi", device,
517 				   16,	/* may be configurable */
518 				   16,	/* may be configurable */
519 				   &rmidi)) < 0)
520 		return err;
521 	strcpy(rmidi->name, rmidi->id);
522 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
523 	if (rdev == NULL) {
524 		snd_device_free(card, rmidi);
525 		return -ENOMEM;
526 	}
527 	rdev->card = card;
528 	rdev->rmidi = rmidi;
529 	rdev->device = device;
530 	rdev->client = -1;
531 	init_rwsem(&rdev->filelist_sem);
532 	rwlock_init(&rdev->filelist_lock);
533 	INIT_LIST_HEAD(&rdev->filelist);
534 	rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
535 	rmidi->private_data = rdev;
536 	rmidi->private_free = snd_virmidi_free;
537 	rmidi->ops = &snd_virmidi_global_ops;
538 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_virmidi_input_ops);
539 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_virmidi_output_ops);
540 	rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT |
541 			    SNDRV_RAWMIDI_INFO_OUTPUT |
542 			    SNDRV_RAWMIDI_INFO_DUPLEX;
543 	*rrmidi = rmidi;
544 	return 0;
545 }
546 EXPORT_SYMBOL(snd_virmidi_new);
547 
548 /*
549  *  ENTRY functions
550  */
551 
552 static int __init alsa_virmidi_init(void)
553 {
554 	return 0;
555 }
556 
557 static void __exit alsa_virmidi_exit(void)
558 {
559 }
560 
561 module_init(alsa_virmidi_init)
562 module_exit(alsa_virmidi_exit)
563