seq_ports.c 18.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *   ALSA sequencer Ports
 *   Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
4
 *                         Jaroslav Kysela <perex@perex.cz>
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 *
 *   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
 *
 */

#include <sound/core.h>
#include <linux/slab.h>
25
#include <linux/module.h>
L
Linus Torvalds 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include "seq_system.h"
#include "seq_ports.h"
#include "seq_clientmgr.h"

/*

   registration of client ports

 */


/* 

NOTE: the current implementation of the port structure as a linked list is
not optimal for clients that have many ports. For sending messages to all
subscribers of a port we first need to find the address of the port
structure, which means we have to traverse the list. A direct access table
(array) would be better, but big preallocated arrays waste memory.

Possible actions:

1) leave it this way, a client does normaly does not have more than a few
ports

2) replace the linked list of ports by a array of pointers which is
dynamicly kmalloced. When a port is added or deleted we can simply allocate
a new array, copy the corresponding pointers, and delete the old one. We
then only need a pointer to this array, and an integer that tells us how
much elements are in array.

*/

/* return pointer to port structure - port is locked if found */
59 60
struct snd_seq_client_port *snd_seq_port_use_ptr(struct snd_seq_client *client,
						 int num)
L
Linus Torvalds 已提交
61
{
62
	struct snd_seq_client_port *port;
L
Linus Torvalds 已提交
63 64 65 66

	if (client == NULL)
		return NULL;
	read_lock(&client->ports_lock);
67
	list_for_each_entry(port, &client->ports_list_head, list) {
L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81
		if (port->addr.port == num) {
			if (port->closing)
				break; /* deleting now */
			snd_use_lock_use(&port->use_lock);
			read_unlock(&client->ports_lock);
			return port;
		}
	}
	read_unlock(&client->ports_lock);
	return NULL;		/* not found */
}


/* search for the next port - port is locked if found */
82 83
struct snd_seq_client_port *snd_seq_port_query_nearest(struct snd_seq_client *client,
						       struct snd_seq_port_info *pinfo)
L
Linus Torvalds 已提交
84 85
{
	int num;
86
	struct snd_seq_client_port *port, *found;
L
Linus Torvalds 已提交
87 88 89 90

	num = pinfo->addr.port;
	found = NULL;
	read_lock(&client->ports_lock);
91
	list_for_each_entry(port, &client->ports_list_head, list) {
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
		if (port->addr.port < num)
			continue;
		if (port->addr.port == num) {
			found = port;
			break;
		}
		if (found == NULL || port->addr.port < found->addr.port)
			found = port;
	}
	if (found) {
		if (found->closing)
			found = NULL;
		else
			snd_use_lock_use(&found->use_lock);
	}
	read_unlock(&client->ports_lock);
	return found;
}


112 113
/* initialize snd_seq_port_subs_info */
static void port_subs_info_init(struct snd_seq_port_subs_info *grp)
L
Linus Torvalds 已提交
114 115 116 117 118 119 120 121 122 123 124
{
	INIT_LIST_HEAD(&grp->list_head);
	grp->count = 0;
	grp->exclusive = 0;
	rwlock_init(&grp->list_lock);
	init_rwsem(&grp->list_mutex);
	grp->open = NULL;
	grp->close = NULL;
}


125 126 127
/* create a port, port number is returned (-1 on failure);
 * the caller needs to unref the port via snd_seq_port_unlock() appropriately
 */
128 129
struct snd_seq_client_port *snd_seq_create_port(struct snd_seq_client *client,
						int port)
L
Linus Torvalds 已提交
130 131
{
	unsigned long flags;
132
	struct snd_seq_client_port *new_port, *p;
L
Linus Torvalds 已提交
133 134 135
	int num = -1;
	
	/* sanity check */
136 137
	if (snd_BUG_ON(!client))
		return NULL;
L
Linus Torvalds 已提交
138

139
	if (client->num_ports >= SNDRV_SEQ_MAX_PORTS) {
140
		pr_warn("ALSA: seq: too many ports for client %d\n", client->number);
L
Linus Torvalds 已提交
141 142 143 144
		return NULL;
	}

	/* create a new port */
145
	new_port = kzalloc(sizeof(*new_port), GFP_KERNEL);
146
	if (!new_port)
L
Linus Torvalds 已提交
147 148 149 150 151 152 153 154 155
		return NULL;	/* failure, out of memory */
	/* init port data */
	new_port->addr.client = client->number;
	new_port->addr.port = -1;
	new_port->owner = THIS_MODULE;
	sprintf(new_port->name, "port-%d", num);
	snd_use_lock_init(&new_port->use_lock);
	port_subs_info_init(&new_port->c_src);
	port_subs_info_init(&new_port->c_dest);
156
	snd_use_lock_use(&new_port->use_lock);
L
Linus Torvalds 已提交
157 158

	num = port >= 0 ? port : 0;
159
	mutex_lock(&client->ports_mutex);
L
Linus Torvalds 已提交
160
	write_lock_irqsave(&client->ports_lock, flags);
161
	list_for_each_entry(p, &client->ports_list_head, list) {
L
Linus Torvalds 已提交
162 163 164 165 166 167
		if (p->addr.port > num)
			break;
		if (port < 0) /* auto-probe mode */
			num = p->addr.port + 1;
	}
	/* insert the new port */
168
	list_add_tail(&new_port->list, &p->list);
L
Linus Torvalds 已提交
169 170
	client->num_ports++;
	new_port->addr.port = num;	/* store the port number in the port */
171
	sprintf(new_port->name, "port-%d", num);
L
Linus Torvalds 已提交
172
	write_unlock_irqrestore(&client->ports_lock, flags);
173
	mutex_unlock(&client->ports_mutex);
L
Linus Torvalds 已提交
174 175 176 177 178

	return new_port;
}

/* */
179 180 181 182 183 184 185 186
static int subscribe_port(struct snd_seq_client *client,
			  struct snd_seq_client_port *port,
			  struct snd_seq_port_subs_info *grp,
			  struct snd_seq_port_subscribe *info, int send_ack);
static int unsubscribe_port(struct snd_seq_client *client,
			    struct snd_seq_client_port *port,
			    struct snd_seq_port_subs_info *grp,
			    struct snd_seq_port_subscribe *info, int send_ack);
L
Linus Torvalds 已提交
187 188


189 190
static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr,
						   struct snd_seq_client **cp)
L
Linus Torvalds 已提交
191
{
192
	struct snd_seq_client_port *p;
L
Linus Torvalds 已提交
193 194 195 196 197 198 199 200 201 202 203 204
	*cp = snd_seq_client_use_ptr(addr->client);
	if (*cp) {
		p = snd_seq_port_use_ptr(*cp, addr->port);
		if (! p) {
			snd_seq_client_unlock(*cp);
			*cp = NULL;
		}
		return p;
	}
	return NULL;
}

205 206 207 208 209 210 211 212 213 214 215 216 217 218
static void delete_and_unsubscribe_port(struct snd_seq_client *client,
					struct snd_seq_client_port *port,
					struct snd_seq_subscribers *subs,
					bool is_src, bool ack);

static inline struct snd_seq_subscribers *
get_subscriber(struct list_head *p, bool is_src)
{
	if (is_src)
		return list_entry(p, struct snd_seq_subscribers, src_list);
	else
		return list_entry(p, struct snd_seq_subscribers, dest_list);
}

L
Linus Torvalds 已提交
219 220 221 222
/*
 * remove all subscribers on the list
 * this is called from port_delete, for each src and dest list.
 */
223 224 225
static void clear_subscriber_list(struct snd_seq_client *client,
				  struct snd_seq_client_port *port,
				  struct snd_seq_port_subs_info *grp,
226
				  int is_src)
L
Linus Torvalds 已提交
227 228 229 230
{
	struct list_head *p, *n;

	list_for_each_safe(p, n, &grp->list_head) {
231 232 233
		struct snd_seq_subscribers *subs;
		struct snd_seq_client *c;
		struct snd_seq_client_port *aport;
L
Linus Torvalds 已提交
234

235 236
		subs = get_subscriber(p, is_src);
		if (is_src)
L
Linus Torvalds 已提交
237
			aport = get_client_port(&subs->info.dest, &c);
238
		else
L
Linus Torvalds 已提交
239
			aport = get_client_port(&subs->info.sender, &c);
240 241
		delete_and_unsubscribe_port(client, port, subs, is_src, false);

L
Linus Torvalds 已提交
242 243 244 245 246 247 248
		if (!aport) {
			/* looks like the connected port is being deleted.
			 * we decrease the counter, and when both ports are deleted
			 * remove the subscriber info
			 */
			if (atomic_dec_and_test(&subs->ref_count))
				kfree(subs);
249
			continue;
L
Linus Torvalds 已提交
250
		}
251 252 253 254 255 256

		/* ok we got the connected port */
		delete_and_unsubscribe_port(c, aport, subs, !is_src, true);
		kfree(subs);
		snd_seq_port_unlock(aport);
		snd_seq_client_unlock(c);
L
Linus Torvalds 已提交
257 258 259 260
	}
}

/* delete port data */
261 262
static int port_delete(struct snd_seq_client *client,
		       struct snd_seq_client_port *port)
L
Linus Torvalds 已提交
263 264 265 266 267 268
{
	/* set closing flag and wait for all port access are gone */
	port->closing = 1;
	snd_use_lock_sync(&port->use_lock); 

	/* clear subscribers info */
269 270
	clear_subscriber_list(client, port, &port->c_src, true);
	clear_subscriber_list(client, port, &port->c_dest, false);
L
Linus Torvalds 已提交
271 272 273 274

	if (port->private_free)
		port->private_free(port->private_data);

275 276
	snd_BUG_ON(port->c_src.count != 0);
	snd_BUG_ON(port->c_dest.count != 0);
L
Linus Torvalds 已提交
277 278 279 280 281 282 283

	kfree(port);
	return 0;
}


/* delete a port with the given port id */
284
int snd_seq_delete_port(struct snd_seq_client *client, int port)
L
Linus Torvalds 已提交
285 286
{
	unsigned long flags;
287
	struct snd_seq_client_port *found = NULL, *p;
L
Linus Torvalds 已提交
288

289
	mutex_lock(&client->ports_mutex);
L
Linus Torvalds 已提交
290
	write_lock_irqsave(&client->ports_lock, flags);
291
	list_for_each_entry(p, &client->ports_list_head, list) {
L
Linus Torvalds 已提交
292 293
		if (p->addr.port == port) {
			/* ok found.  delete from the list at first */
294
			list_del(&p->list);
L
Linus Torvalds 已提交
295 296 297 298 299 300
			client->num_ports--;
			found = p;
			break;
		}
	}
	write_unlock_irqrestore(&client->ports_lock, flags);
301
	mutex_unlock(&client->ports_mutex);
L
Linus Torvalds 已提交
302 303 304 305 306 307 308
	if (found)
		return port_delete(client, found);
	else
		return -ENOENT;
}

/* delete the all ports belonging to the given client */
309
int snd_seq_delete_all_ports(struct snd_seq_client *client)
L
Linus Torvalds 已提交
310 311
{
	unsigned long flags;
312 313
	struct list_head deleted_list;
	struct snd_seq_client_port *port, *tmp;
L
Linus Torvalds 已提交
314 315 316 317
	
	/* move the port list to deleted_list, and
	 * clear the port list in the client data.
	 */
318
	mutex_lock(&client->ports_mutex);
L
Linus Torvalds 已提交
319 320
	write_lock_irqsave(&client->ports_lock, flags);
	if (! list_empty(&client->ports_list_head)) {
321 322
		list_add(&deleted_list, &client->ports_list_head);
		list_del_init(&client->ports_list_head);
L
Linus Torvalds 已提交
323 324 325 326 327 328 329
	} else {
		INIT_LIST_HEAD(&deleted_list);
	}
	client->num_ports = 0;
	write_unlock_irqrestore(&client->ports_lock, flags);

	/* remove each port in deleted_list */
330 331
	list_for_each_entry_safe(port, tmp, &deleted_list, list) {
		list_del(&port->list);
L
Linus Torvalds 已提交
332 333 334
		snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port);
		port_delete(client, port);
	}
335
	mutex_unlock(&client->ports_mutex);
L
Linus Torvalds 已提交
336 337 338 339
	return 0;
}

/* set port info fields */
340 341
int snd_seq_set_port_info(struct snd_seq_client_port * port,
			  struct snd_seq_port_info * info)
L
Linus Torvalds 已提交
342
{
343 344
	if (snd_BUG_ON(!port || !info))
		return -EINVAL;
L
Linus Torvalds 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

	/* set port name */
	if (info->name[0])
		strlcpy(port->name, info->name, sizeof(port->name));
	
	/* set capabilities */
	port->capability = info->capability;
	
	/* get port type */
	port->type = info->type;

	/* information about supported channels/voices */
	port->midi_channels = info->midi_channels;
	port->midi_voices = info->midi_voices;
	port->synth_voices = info->synth_voices;

	/* timestamping */
	port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0;
	port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0;
	port->time_queue = info->time_queue;

	return 0;
}

/* get port info fields */
370 371
int snd_seq_get_port_info(struct snd_seq_client_port * port,
			  struct snd_seq_port_info * info)
L
Linus Torvalds 已提交
372
{
373 374
	if (snd_BUG_ON(!port || !info))
		return -EINVAL;
L
Linus Torvalds 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

	/* get port name */
	strlcpy(info->name, port->name, sizeof(info->name));
	
	/* get capabilities */
	info->capability = port->capability;

	/* get port type */
	info->type = port->type;

	/* information about supported channels/voices */
	info->midi_channels = port->midi_channels;
	info->midi_voices = port->midi_voices;
	info->synth_voices = port->synth_voices;

	/* get subscriber counts */
	info->read_use = port->c_src.count;
	info->write_use = port->c_dest.count;
	
	/* timestamping */
	info->flags = 0;
	if (port->timestamping) {
		info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP;
		if (port->time_real)
			info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL;
		info->time_queue = port->time_queue;
	}

	return 0;
}



/*
 * call callback functions (if any):
 * the callbacks are invoked only when the first (for connection) or
 * the last subscription (for disconnection) is done.  Second or later
 * subscription results in increment of counter, but no callback is
 * invoked.
 * This feature is useful if these callbacks are associated with
 * initialization or termination of devices (see seq_midi.c).
 */

418 419 420 421 422
static int subscribe_port(struct snd_seq_client *client,
			  struct snd_seq_client_port *port,
			  struct snd_seq_port_subs_info *grp,
			  struct snd_seq_port_subscribe *info,
			  int send_ack)
L
Linus Torvalds 已提交
423 424 425 426 427 428
{
	int err = 0;

	if (!try_module_get(port->owner))
		return -EFAULT;
	grp->count++;
429
	if (grp->open && grp->count == 1) {
L
Linus Torvalds 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442
		err = grp->open(port->private_data, info);
		if (err < 0) {
			module_put(port->owner);
			grp->count--;
		}
	}
	if (err >= 0 && send_ack && client->type == USER_CLIENT)
		snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
						   info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);

	return err;
}

443 444 445 446 447
static int unsubscribe_port(struct snd_seq_client *client,
			    struct snd_seq_client_port *port,
			    struct snd_seq_port_subs_info *grp,
			    struct snd_seq_port_subscribe *info,
			    int send_ack)
L
Linus Torvalds 已提交
448 449 450 451 452 453
{
	int err = 0;

	if (! grp->count)
		return -EINVAL;
	grp->count--;
454
	if (grp->close && grp->count == 0)
L
Linus Torvalds 已提交
455 456 457 458 459 460 461 462 463 464 465
		err = grp->close(port->private_data, info);
	if (send_ack && client->type == USER_CLIENT)
		snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
						   info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
	module_put(port->owner);
	return err;
}



/* check if both addresses are identical */
466
static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s)
L
Linus Torvalds 已提交
467 468 469 470 471 472
{
	return (r->client == s->client) && (r->port == s->port);
}

/* check the two subscribe info match */
/* if flags is zero, checks only sender and destination addresses */
473 474
static int match_subs_info(struct snd_seq_port_subscribe *r,
			   struct snd_seq_port_subscribe *s)
L
Linus Torvalds 已提交
475 476 477 478 479 480 481 482 483 484 485
{
	if (addr_match(&r->sender, &s->sender) &&
	    addr_match(&r->dest, &s->dest)) {
		if (r->flags && r->flags == s->flags)
			return r->queue == s->queue;
		else if (! r->flags)
			return 1;
	}
	return 0;
}

486 487 488 489
static int check_and_subscribe_port(struct snd_seq_client *client,
				    struct snd_seq_client_port *port,
				    struct snd_seq_subscribers *subs,
				    bool is_src, bool exclusive, bool ack)
L
Linus Torvalds 已提交
490
{
491 492 493 494
	struct snd_seq_port_subs_info *grp;
	struct list_head *p;
	struct snd_seq_subscribers *s;
	int err;
L
Linus Torvalds 已提交
495

496
	grp = is_src ? &port->c_src : &port->c_dest;
L
Linus Torvalds 已提交
497
	err = -EBUSY;
498
	down_write(&grp->list_mutex);
L
Linus Torvalds 已提交
499
	if (exclusive) {
500
		if (!list_empty(&grp->list_head))
L
Linus Torvalds 已提交
501 502
			goto __error;
	} else {
503
		if (grp->exclusive)
L
Linus Torvalds 已提交
504 505
			goto __error;
		/* check whether already exists */
506 507 508
		list_for_each(p, &grp->list_head) {
			s = get_subscriber(p, is_src);
			if (match_subs_info(&subs->info, &s->info))
L
Linus Torvalds 已提交
509 510 511 512
				goto __error;
		}
	}

513 514 515
	err = subscribe_port(client, port, grp, &subs->info, ack);
	if (err < 0) {
		grp->exclusive = 0;
L
Linus Torvalds 已提交
516
		goto __error;
517
	}
L
Linus Torvalds 已提交
518 519

	/* add to list */
520 521 522 523 524 525 526 527 528 529 530 531 532 533
	write_lock_irq(&grp->list_lock);
	if (is_src)
		list_add_tail(&subs->src_list, &grp->list_head);
	else
		list_add_tail(&subs->dest_list, &grp->list_head);
	grp->exclusive = exclusive;
	atomic_inc(&subs->ref_count);
	write_unlock_irq(&grp->list_lock);
	err = 0;

 __error:
	up_write(&grp->list_mutex);
	return err;
}
L
Linus Torvalds 已提交
534

535 536 537 538 539 540
static void delete_and_unsubscribe_port(struct snd_seq_client *client,
					struct snd_seq_client_port *port,
					struct snd_seq_subscribers *subs,
					bool is_src, bool ack)
{
	struct snd_seq_port_subs_info *grp;
541 542
	struct list_head *list;
	bool empty;
543 544

	grp = is_src ? &port->c_src : &port->c_dest;
545
	list = is_src ? &subs->src_list : &subs->dest_list;
546 547
	down_write(&grp->list_mutex);
	write_lock_irq(&grp->list_lock);
548 549 550
	empty = list_empty(list);
	if (!empty)
		list_del_init(list);
551 552 553 554
	grp->exclusive = 0;
	write_unlock_irq(&grp->list_lock);
	up_write(&grp->list_mutex);

555 556
	if (!empty)
		unsubscribe_port(client, port, grp, &subs->info, ack);
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
}

/* connect two ports */
int snd_seq_port_connect(struct snd_seq_client *connector,
			 struct snd_seq_client *src_client,
			 struct snd_seq_client_port *src_port,
			 struct snd_seq_client *dest_client,
			 struct snd_seq_client_port *dest_port,
			 struct snd_seq_port_subscribe *info)
{
	struct snd_seq_subscribers *subs;
	bool exclusive;
	int err;

	subs = kzalloc(sizeof(*subs), GFP_KERNEL);
	if (!subs)
		return -ENOMEM;

	subs->info = *info;
	atomic_set(&subs->ref_count, 0);
	INIT_LIST_HEAD(&subs->src_list);
	INIT_LIST_HEAD(&subs->dest_list);

	exclusive = !!(info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE);

	err = check_and_subscribe_port(src_client, src_port, subs, true,
				       exclusive,
				       connector->number != src_client->number);
	if (err < 0)
		goto error;
	err = check_and_subscribe_port(dest_client, dest_port, subs, false,
				       exclusive,
				       connector->number != dest_client->number);
	if (err < 0)
		goto error_dest;
L
Linus Torvalds 已提交
592 593 594

	return 0;

595 596 597 598
 error_dest:
	delete_and_unsubscribe_port(src_client, src_port, subs, true,
				    connector->number != src_client->number);
 error:
L
Linus Torvalds 已提交
599 600 601 602 603
	kfree(subs);
	return err;
}

/* remove the connection */
604 605 606 607 608 609
int snd_seq_port_disconnect(struct snd_seq_client *connector,
			    struct snd_seq_client *src_client,
			    struct snd_seq_client_port *src_port,
			    struct snd_seq_client *dest_client,
			    struct snd_seq_client_port *dest_port,
			    struct snd_seq_port_subscribe *info)
L
Linus Torvalds 已提交
610
{
611 612
	struct snd_seq_port_subs_info *src = &src_port->c_src;
	struct snd_seq_subscribers *subs;
L
Linus Torvalds 已提交
613 614 615 616
	int err = -ENOENT;

	down_write(&src->list_mutex);
	/* look for the connection */
617
	list_for_each_entry(subs, &src->list_head, src_list) {
L
Linus Torvalds 已提交
618
		if (match_subs_info(info, &subs->info)) {
619
			atomic_dec(&subs->ref_count); /* mark as not ready */
L
Linus Torvalds 已提交
620 621 622 623 624
			err = 0;
			break;
		}
	}
	up_write(&src->list_mutex);
625 626 627 628 629 630 631 632 633
	if (err < 0)
		return err;

	delete_and_unsubscribe_port(src_client, src_port, subs, true,
				    connector->number != src_client->number);
	delete_and_unsubscribe_port(dest_client, dest_port, subs, false,
				    connector->number != dest_client->number);
	kfree(subs);
	return 0;
L
Linus Torvalds 已提交
634 635 636 637
}


/* get matched subscriber */
638 639
struct snd_seq_subscribers *snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp,
							  struct snd_seq_addr *dest_addr)
L
Linus Torvalds 已提交
640
{
641
	struct snd_seq_subscribers *s, *found = NULL;
L
Linus Torvalds 已提交
642 643

	down_read(&src_grp->list_mutex);
644
	list_for_each_entry(s, &src_grp->list_head, src_list) {
L
Linus Torvalds 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
		if (addr_match(dest_addr, &s->info.dest)) {
			found = s;
			break;
		}
	}
	up_read(&src_grp->list_mutex);
	return found;
}

/*
 * Attach a device driver that wants to receive events from the
 * sequencer.  Returns the new port number on success.
 * A driver that wants to receive the events converted to midi, will
 * use snd_seq_midisynth_register_port().
 */
/* exported */
int snd_seq_event_port_attach(int client,
662
			      struct snd_seq_port_callback *pcbp,
L
Linus Torvalds 已提交
663 664 665
			      int cap, int type, int midi_channels,
			      int midi_voices, char *portname)
{
666
	struct snd_seq_port_info portinfo;
L
Linus Torvalds 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
	int  ret;

	/* Set up the port */
	memset(&portinfo, 0, sizeof(portinfo));
	portinfo.addr.client = client;
	strlcpy(portinfo.name, portname ? portname : "Unamed port",
		sizeof(portinfo.name));

	portinfo.capability = cap;
	portinfo.type = type;
	portinfo.kernel = pcbp;
	portinfo.midi_channels = midi_channels;
	portinfo.midi_voices = midi_voices;

	/* Create it */
	ret = snd_seq_kernel_client_ctl(client,
					SNDRV_SEQ_IOCTL_CREATE_PORT,
					&portinfo);

	if (ret >= 0)
		ret = portinfo.addr.port;

	return ret;
}
691
EXPORT_SYMBOL(snd_seq_event_port_attach);
L
Linus Torvalds 已提交
692 693 694 695 696 697 698

/*
 * Detach the driver from a port.
 */
/* exported */
int snd_seq_event_port_detach(int client, int port)
{
699
	struct snd_seq_port_info portinfo;
L
Linus Torvalds 已提交
700 701 702 703 704 705 706 707 708 709 710
	int  err;

	memset(&portinfo, 0, sizeof(portinfo));
	portinfo.addr.client = client;
	portinfo.addr.port   = port;
	err = snd_seq_kernel_client_ctl(client,
					SNDRV_SEQ_IOCTL_DELETE_PORT,
					&portinfo);

	return err;
}
711
EXPORT_SYMBOL(snd_seq_event_port_detach);