seq_virmidi.c 14.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
 *  Virtual Raw MIDI client on Sequencer
 *
 *  Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>,
 *                        Jaroslav Kysela <perex@perex.cz>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

/*
 * Virtual Raw MIDI client
 *
 * The virtual rawmidi client is a sequencer client which associate
 * a rawmidi device file.  The created rawmidi device file can be
 * accessed as a normal raw midi, but its MIDI source and destination
 * are arbitrary.  For example, a user-client software synth connected
 * to this port can be used as a normal midi device as well.
 *
 * The virtual rawmidi device accepts also multiple opens.  Each file
 * has its own input buffer, so that no conflict would occur.  The drain
 * of input/output buffer acts only to the local buffer.
 *
 */

#include <linux/init.h>
#include <linux/wait.h>
40
#include <linux/module.h>
L
Linus Torvalds 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include <linux/slab.h>
#include <sound/core.h>
#include <sound/rawmidi.h>
#include <sound/info.h>
#include <sound/control.h>
#include <sound/minors.h>
#include <sound/seq_kernel.h>
#include <sound/seq_midi_event.h>
#include <sound/seq_virmidi.h>

MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
MODULE_DESCRIPTION("Virtual Raw MIDI client on Sequencer");
MODULE_LICENSE("GPL");

/*
 * initialize an event record
 */
58 59
static void snd_virmidi_init_event(struct snd_virmidi *vmidi,
				   struct snd_seq_event *ev)
L
Linus Torvalds 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
{
	memset(ev, 0, sizeof(*ev));
	ev->source.port = vmidi->port;
	switch (vmidi->seq_mode) {
	case SNDRV_VIRMIDI_SEQ_DISPATCH:
		ev->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
		break;
	case SNDRV_VIRMIDI_SEQ_ATTACH:
		/* FIXME: source and destination are same - not good.. */
		ev->dest.client = vmidi->client;
		ev->dest.port = vmidi->port;
		break;
	}
	ev->type = SNDRV_SEQ_EVENT_NONE;
}

/*
 * decode input event and put to read buffer of each opened file
 */
79
static int snd_virmidi_dev_receive_event(struct snd_virmidi_dev *rdev,
80 81
					 struct snd_seq_event *ev,
					 bool atomic)
L
Linus Torvalds 已提交
82
{
83
	struct snd_virmidi *vmidi;
L
Linus Torvalds 已提交
84 85 86
	unsigned char msg[4];
	int len;

87 88 89 90
	if (atomic)
		read_lock(&rdev->filelist_lock);
	else
		down_read(&rdev->filelist_sem);
91
	list_for_each_entry(vmidi, &rdev->filelist, list) {
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100 101 102 103
		if (!vmidi->trigger)
			continue;
		if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
			if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
				continue;
			snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)snd_rawmidi_receive, vmidi->substream);
		} else {
			len = snd_midi_event_decode(vmidi->parser, msg, sizeof(msg), ev);
			if (len > 0)
				snd_rawmidi_receive(vmidi->substream, msg, len);
		}
	}
104 105 106 107
	if (atomic)
		read_unlock(&rdev->filelist_lock);
	else
		up_read(&rdev->filelist_sem);
L
Linus Torvalds 已提交
108 109 110 111 112 113 114 115 116 117 118

	return 0;
}

/*
 * receive an event from the remote virmidi port
 *
 * for rawmidi inputs, you can call this function from the event
 * handler of a remote port which is attached to the virmidi via
 * SNDRV_VIRMIDI_SEQ_ATTACH.
 */
119
#if 0
120
int snd_virmidi_receive(struct snd_rawmidi *rmidi, struct snd_seq_event *ev)
L
Linus Torvalds 已提交
121
{
122
	struct snd_virmidi_dev *rdev;
L
Linus Torvalds 已提交
123 124

	rdev = rmidi->private_data;
125
	return snd_virmidi_dev_receive_event(rdev, ev, true);
L
Linus Torvalds 已提交
126
}
127
#endif  /*  0  */
L
Linus Torvalds 已提交
128 129 130 131

/*
 * event handler of virmidi port
 */
132
static int snd_virmidi_event_input(struct snd_seq_event *ev, int direct,
L
Linus Torvalds 已提交
133 134
				   void *private_data, int atomic, int hop)
{
135
	struct snd_virmidi_dev *rdev;
L
Linus Torvalds 已提交
136 137 138 139

	rdev = private_data;
	if (!(rdev->flags & SNDRV_VIRMIDI_USE))
		return 0; /* ignored */
140
	return snd_virmidi_dev_receive_event(rdev, ev, atomic);
L
Linus Torvalds 已提交
141 142 143 144 145
}

/*
 * trigger rawmidi stream for input
 */
146
static void snd_virmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
L
Linus Torvalds 已提交
147
{
148
	struct snd_virmidi *vmidi = substream->runtime->private_data;
L
Linus Torvalds 已提交
149 150 151 152 153 154 155 156 157 158 159

	if (up) {
		vmidi->trigger = 1;
	} else {
		vmidi->trigger = 0;
	}
}

/*
 * trigger rawmidi stream for output
 */
160
static void snd_virmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
L
Linus Torvalds 已提交
161
{
162
	struct snd_virmidi *vmidi = substream->runtime->private_data;
L
Linus Torvalds 已提交
163 164
	int count, res;
	unsigned char buf[32], *pbuf;
165
	unsigned long flags;
166
	bool check_resched = !in_atomic();
L
Linus Torvalds 已提交
167 168 169 170 171

	if (up) {
		vmidi->trigger = 1;
		if (vmidi->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH &&
		    !(vmidi->rdev->flags & SNDRV_VIRMIDI_SUBSCRIBE)) {
172 173 174 175 176
			while (snd_rawmidi_transmit(substream, buf,
						    sizeof(buf)) > 0) {
				/* ignored */
			}
			return;
L
Linus Torvalds 已提交
177
		}
178
		spin_lock_irqsave(&substream->runtime->lock, flags);
L
Linus Torvalds 已提交
179
		if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) {
180
			if (snd_seq_kernel_client_dispatch(vmidi->client, &vmidi->event, in_atomic(), 0) < 0)
181
				goto out;
L
Linus Torvalds 已提交
182 183 184
			vmidi->event.type = SNDRV_SEQ_EVENT_NONE;
		}
		while (1) {
185
			count = __snd_rawmidi_transmit_peek(substream, buf, sizeof(buf));
L
Linus Torvalds 已提交
186 187 188 189 190 191 192 193 194
			if (count <= 0)
				break;
			pbuf = buf;
			while (count > 0) {
				res = snd_midi_event_encode(vmidi->parser, pbuf, count, &vmidi->event);
				if (res < 0) {
					snd_midi_event_reset_encode(vmidi->parser);
					continue;
				}
195
				__snd_rawmidi_transmit_ack(substream, res);
L
Linus Torvalds 已提交
196 197 198
				pbuf += res;
				count -= res;
				if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) {
199
					if (snd_seq_kernel_client_dispatch(vmidi->client, &vmidi->event, in_atomic(), 0) < 0)
200
						goto out;
L
Linus Torvalds 已提交
201 202 203
					vmidi->event.type = SNDRV_SEQ_EVENT_NONE;
				}
			}
204 205 206 207 208 209 210 211 212
			if (!check_resched)
				continue;
			/* do temporary unlock & cond_resched() for avoiding
			 * CPU soft lockup, which may happen via a write from
			 * a huge rawmidi buffer
			 */
			spin_unlock_irqrestore(&substream->runtime->lock, flags);
			cond_resched();
			spin_lock_irqsave(&substream->runtime->lock, flags);
L
Linus Torvalds 已提交
213
		}
214 215
	out:
		spin_unlock_irqrestore(&substream->runtime->lock, flags);
L
Linus Torvalds 已提交
216 217 218 219 220 221 222 223
	} else {
		vmidi->trigger = 0;
	}
}

/*
 * open rawmidi handle for input
 */
224
static int snd_virmidi_input_open(struct snd_rawmidi_substream *substream)
L
Linus Torvalds 已提交
225
{
226 227 228
	struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
	struct snd_rawmidi_runtime *runtime = substream->runtime;
	struct snd_virmidi *vmidi;
L
Linus Torvalds 已提交
229

230
	vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
L
Linus Torvalds 已提交
231 232 233 234 235 236 237 238 239 240 241
	if (vmidi == NULL)
		return -ENOMEM;
	vmidi->substream = substream;
	if (snd_midi_event_new(0, &vmidi->parser) < 0) {
		kfree(vmidi);
		return -ENOMEM;
	}
	vmidi->seq_mode = rdev->seq_mode;
	vmidi->client = rdev->client;
	vmidi->port = rdev->port;	
	runtime->private_data = vmidi;
242 243
	down_write(&rdev->filelist_sem);
	write_lock_irq(&rdev->filelist_lock);
L
Linus Torvalds 已提交
244
	list_add_tail(&vmidi->list, &rdev->filelist);
245 246
	write_unlock_irq(&rdev->filelist_lock);
	up_write(&rdev->filelist_sem);
L
Linus Torvalds 已提交
247 248 249 250 251 252 253
	vmidi->rdev = rdev;
	return 0;
}

/*
 * open rawmidi handle for output
 */
254
static int snd_virmidi_output_open(struct snd_rawmidi_substream *substream)
L
Linus Torvalds 已提交
255
{
256 257 258
	struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
	struct snd_rawmidi_runtime *runtime = substream->runtime;
	struct snd_virmidi *vmidi;
L
Linus Torvalds 已提交
259

260
	vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
L
Linus Torvalds 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	if (vmidi == NULL)
		return -ENOMEM;
	vmidi->substream = substream;
	if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &vmidi->parser) < 0) {
		kfree(vmidi);
		return -ENOMEM;
	}
	vmidi->seq_mode = rdev->seq_mode;
	vmidi->client = rdev->client;
	vmidi->port = rdev->port;
	snd_virmidi_init_event(vmidi, &vmidi->event);
	vmidi->rdev = rdev;
	runtime->private_data = vmidi;
	return 0;
}

/*
 * close rawmidi handle for input
 */
280
static int snd_virmidi_input_close(struct snd_rawmidi_substream *substream)
L
Linus Torvalds 已提交
281
{
282
	struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
283
	struct snd_virmidi *vmidi = substream->runtime->private_data;
284

285
	down_write(&rdev->filelist_sem);
286
	write_lock_irq(&rdev->filelist_lock);
L
Linus Torvalds 已提交
287
	list_del(&vmidi->list);
288
	write_unlock_irq(&rdev->filelist_lock);
289
	up_write(&rdev->filelist_sem);
290
	snd_midi_event_free(vmidi->parser);
L
Linus Torvalds 已提交
291 292 293 294 295 296 297 298
	substream->runtime->private_data = NULL;
	kfree(vmidi);
	return 0;
}

/*
 * close rawmidi handle for output
 */
299
static int snd_virmidi_output_close(struct snd_rawmidi_substream *substream)
L
Linus Torvalds 已提交
300
{
301
	struct snd_virmidi *vmidi = substream->runtime->private_data;
L
Linus Torvalds 已提交
302 303 304 305 306 307 308 309 310
	snd_midi_event_free(vmidi->parser);
	substream->runtime->private_data = NULL;
	kfree(vmidi);
	return 0;
}

/*
 * subscribe callback - allow output to rawmidi device
 */
311 312
static int snd_virmidi_subscribe(void *private_data,
				 struct snd_seq_port_subscribe *info)
L
Linus Torvalds 已提交
313
{
314
	struct snd_virmidi_dev *rdev;
L
Linus Torvalds 已提交
315 316 317 318 319 320 321 322 323 324 325

	rdev = private_data;
	if (!try_module_get(rdev->card->module))
		return -EFAULT;
	rdev->flags |= SNDRV_VIRMIDI_SUBSCRIBE;
	return 0;
}

/*
 * unsubscribe callback - disallow output to rawmidi device
 */
326 327
static int snd_virmidi_unsubscribe(void *private_data,
				   struct snd_seq_port_subscribe *info)
L
Linus Torvalds 已提交
328
{
329
	struct snd_virmidi_dev *rdev;
L
Linus Torvalds 已提交
330 331 332 333 334 335 336 337 338 339 340

	rdev = private_data;
	rdev->flags &= ~SNDRV_VIRMIDI_SUBSCRIBE;
	module_put(rdev->card->module);
	return 0;
}


/*
 * use callback - allow input to rawmidi device
 */
341 342
static int snd_virmidi_use(void *private_data,
			   struct snd_seq_port_subscribe *info)
L
Linus Torvalds 已提交
343
{
344
	struct snd_virmidi_dev *rdev;
L
Linus Torvalds 已提交
345 346 347 348 349 350 351 352 353 354 355

	rdev = private_data;
	if (!try_module_get(rdev->card->module))
		return -EFAULT;
	rdev->flags |= SNDRV_VIRMIDI_USE;
	return 0;
}

/*
 * unuse callback - disallow input to rawmidi device
 */
356 357
static int snd_virmidi_unuse(void *private_data,
			     struct snd_seq_port_subscribe *info)
L
Linus Torvalds 已提交
358
{
359
	struct snd_virmidi_dev *rdev;
L
Linus Torvalds 已提交
360 361 362 363 364 365 366 367 368 369 370 371

	rdev = private_data;
	rdev->flags &= ~SNDRV_VIRMIDI_USE;
	module_put(rdev->card->module);
	return 0;
}


/*
 *  Register functions
 */

372
static const struct snd_rawmidi_ops snd_virmidi_input_ops = {
L
Linus Torvalds 已提交
373 374 375 376 377
	.open = snd_virmidi_input_open,
	.close = snd_virmidi_input_close,
	.trigger = snd_virmidi_input_trigger,
};

378
static const struct snd_rawmidi_ops snd_virmidi_output_ops = {
L
Linus Torvalds 已提交
379 380 381 382 383 384 385 386
	.open = snd_virmidi_output_open,
	.close = snd_virmidi_output_close,
	.trigger = snd_virmidi_output_trigger,
};

/*
 * create a sequencer client and a port
 */
387
static int snd_virmidi_dev_attach_seq(struct snd_virmidi_dev *rdev)
L
Linus Torvalds 已提交
388 389
{
	int client;
390 391
	struct snd_seq_port_callback pcallbacks;
	struct snd_seq_port_info *pinfo;
L
Linus Torvalds 已提交
392 393 394 395 396
	int err;

	if (rdev->client >= 0)
		return 0;

397
	pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
398
	if (!pinfo) {
L
Linus Torvalds 已提交
399 400 401 402
		err = -ENOMEM;
		goto __error;
	}

403 404 405 406
	client = snd_seq_create_kernel_client(rdev->card, rdev->device,
					      "%s %d-%d", rdev->rmidi->name,
					      rdev->card->number,
					      rdev->device);
L
Linus Torvalds 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419
	if (client < 0) {
		err = client;
		goto __error;
	}
	rdev->client = client;

	/* create a port */
	pinfo->addr.client = client;
	sprintf(pinfo->name, "VirMIDI %d-%d", rdev->card->number, rdev->device);
	/* set all capabilities */
	pinfo->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
	pinfo->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
	pinfo->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
420 421 422
	pinfo->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
		| SNDRV_SEQ_PORT_TYPE_SOFTWARE
		| SNDRV_SEQ_PORT_TYPE_PORT;
L
Linus Torvalds 已提交
423 424 425 426 427 428 429 430 431 432
	pinfo->midi_channels = 16;
	memset(&pcallbacks, 0, sizeof(pcallbacks));
	pcallbacks.owner = THIS_MODULE;
	pcallbacks.private_data = rdev;
	pcallbacks.subscribe = snd_virmidi_subscribe;
	pcallbacks.unsubscribe = snd_virmidi_unsubscribe;
	pcallbacks.use = snd_virmidi_use;
	pcallbacks.unuse = snd_virmidi_unuse;
	pcallbacks.event_input = snd_virmidi_event_input;
	pinfo->kernel = &pcallbacks;
433
	err = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, pinfo);
L
Linus Torvalds 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
	if (err < 0) {
		snd_seq_delete_kernel_client(client);
		rdev->client = -1;
		goto __error;
	}

	rdev->port = pinfo->addr.port;
	err = 0; /* success */

 __error:
	kfree(pinfo);
	return err;
}


/*
 * release the sequencer client
 */
452
static void snd_virmidi_dev_detach_seq(struct snd_virmidi_dev *rdev)
L
Linus Torvalds 已提交
453 454 455 456 457 458 459 460 461 462
{
	if (rdev->client >= 0) {
		snd_seq_delete_kernel_client(rdev->client);
		rdev->client = -1;
	}
}

/*
 * register the device
 */
463
static int snd_virmidi_dev_register(struct snd_rawmidi *rmidi)
L
Linus Torvalds 已提交
464
{
465
	struct snd_virmidi_dev *rdev = rmidi->private_data;
L
Linus Torvalds 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479
	int err;

	switch (rdev->seq_mode) {
	case SNDRV_VIRMIDI_SEQ_DISPATCH:
		err = snd_virmidi_dev_attach_seq(rdev);
		if (err < 0)
			return err;
		break;
	case SNDRV_VIRMIDI_SEQ_ATTACH:
		if (rdev->client == 0)
			return -EINVAL;
		/* should check presence of port more strictly.. */
		break;
	default:
480
		pr_err("ALSA: seq_virmidi: seq_mode is not set: %d\n", rdev->seq_mode);
L
Linus Torvalds 已提交
481 482 483 484 485 486 487 488 489
		return -EINVAL;
	}
	return 0;
}


/*
 * unregister the device
 */
490
static int snd_virmidi_dev_unregister(struct snd_rawmidi *rmidi)
L
Linus Torvalds 已提交
491
{
492
	struct snd_virmidi_dev *rdev = rmidi->private_data;
L
Linus Torvalds 已提交
493 494 495 496 497 498 499 500 501

	if (rdev->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH)
		snd_virmidi_dev_detach_seq(rdev);
	return 0;
}

/*
 *
 */
502
static const struct snd_rawmidi_global_ops snd_virmidi_global_ops = {
L
Linus Torvalds 已提交
503 504 505 506 507 508 509
	.dev_register = snd_virmidi_dev_register,
	.dev_unregister = snd_virmidi_dev_unregister,
};

/*
 * free device
 */
510
static void snd_virmidi_free(struct snd_rawmidi *rmidi)
L
Linus Torvalds 已提交
511
{
512
	struct snd_virmidi_dev *rdev = rmidi->private_data;
L
Linus Torvalds 已提交
513 514 515 516 517 518 519 520
	kfree(rdev);
}

/*
 * create a new device
 *
 */
/* exported */
521
int snd_virmidi_new(struct snd_card *card, int device, struct snd_rawmidi **rrmidi)
L
Linus Torvalds 已提交
522
{
523 524
	struct snd_rawmidi *rmidi;
	struct snd_virmidi_dev *rdev;
L
Linus Torvalds 已提交
525 526 527 528 529 530 531 532 533
	int err;
	
	*rrmidi = NULL;
	if ((err = snd_rawmidi_new(card, "VirMidi", device,
				   16,	/* may be configurable */
				   16,	/* may be configurable */
				   &rmidi)) < 0)
		return err;
	strcpy(rmidi->name, rmidi->id);
534
	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
L
Linus Torvalds 已提交
535 536 537 538 539 540 541 542
	if (rdev == NULL) {
		snd_device_free(card, rmidi);
		return -ENOMEM;
	}
	rdev->card = card;
	rdev->rmidi = rmidi;
	rdev->device = device;
	rdev->client = -1;
543
	init_rwsem(&rdev->filelist_sem);
L
Linus Torvalds 已提交
544 545 546 547 548 549 550 551 552 553 554 555 556 557
	rwlock_init(&rdev->filelist_lock);
	INIT_LIST_HEAD(&rdev->filelist);
	rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
	rmidi->private_data = rdev;
	rmidi->private_free = snd_virmidi_free;
	rmidi->ops = &snd_virmidi_global_ops;
	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_virmidi_input_ops);
	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_virmidi_output_ops);
	rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT |
			    SNDRV_RAWMIDI_INFO_OUTPUT |
			    SNDRV_RAWMIDI_INFO_DUPLEX;
	*rrmidi = rmidi;
	return 0;
}
558
EXPORT_SYMBOL(snd_virmidi_new);
L
Linus Torvalds 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

/*
 *  ENTRY functions
 */

static int __init alsa_virmidi_init(void)
{
	return 0;
}

static void __exit alsa_virmidi_exit(void)
{
}

module_init(alsa_virmidi_init)
module_exit(alsa_virmidi_exit)