• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *   ALSA sequencer Ports
3  *   Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
4  *                         Jaroslav Kysela <perex@perex.cz>
5  *
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 #include <sound/core.h>
24 #include <linux/slab.h>
25 #include <linux/module.h>
26 #include "seq_system.h"
27 #include "seq_ports.h"
28 #include "seq_clientmgr.h"
29 
30 /*
31 
32    registration of client ports
33 
34  */
35 
36 
37 /*
38 
39 NOTE: the current implementation of the port structure as a linked list is
40 not optimal for clients that have many ports. For sending messages to all
41 subscribers of a port we first need to find the address of the port
42 structure, which means we have to traverse the list. A direct access table
43 (array) would be better, but big preallocated arrays waste memory.
44 
45 Possible actions:
46 
47 1) leave it this way, a client does normaly does not have more than a few
48 ports
49 
50 2) replace the linked list of ports by a array of pointers which is
51 dynamicly kmalloced. When a port is added or deleted we can simply allocate
52 a new array, copy the corresponding pointers, and delete the old one. We
53 then only need a pointer to this array, and an integer that tells us how
54 much elements are in array.
55 
56 */
57 
58 /* return pointer to port structure - port is locked if found */
snd_seq_port_use_ptr(struct snd_seq_client * client,int num)59 struct snd_seq_client_port *snd_seq_port_use_ptr(struct snd_seq_client *client,
60 						 int num)
61 {
62 	struct snd_seq_client_port *port;
63 
64 	if (client == NULL)
65 		return NULL;
66 	read_lock(&client->ports_lock);
67 	list_for_each_entry(port, &client->ports_list_head, list) {
68 		if (port->addr.port == num) {
69 			if (port->closing)
70 				break; /* deleting now */
71 			snd_use_lock_use(&port->use_lock);
72 			read_unlock(&client->ports_lock);
73 			return port;
74 		}
75 	}
76 	read_unlock(&client->ports_lock);
77 	return NULL;		/* not found */
78 }
79 
80 
81 /* search for the next port - port is locked if found */
snd_seq_port_query_nearest(struct snd_seq_client * client,struct snd_seq_port_info * pinfo)82 struct snd_seq_client_port *snd_seq_port_query_nearest(struct snd_seq_client *client,
83 						       struct snd_seq_port_info *pinfo)
84 {
85 	int num;
86 	struct snd_seq_client_port *port, *found;
87 
88 	num = pinfo->addr.port;
89 	found = NULL;
90 	read_lock(&client->ports_lock);
91 	list_for_each_entry(port, &client->ports_list_head, list) {
92 		if (port->addr.port < num)
93 			continue;
94 		if (port->addr.port == num) {
95 			found = port;
96 			break;
97 		}
98 		if (found == NULL || port->addr.port < found->addr.port)
99 			found = port;
100 	}
101 	if (found) {
102 		if (found->closing)
103 			found = NULL;
104 		else
105 			snd_use_lock_use(&found->use_lock);
106 	}
107 	read_unlock(&client->ports_lock);
108 	return found;
109 }
110 
111 
112 /* initialize snd_seq_port_subs_info */
port_subs_info_init(struct snd_seq_port_subs_info * grp)113 static void port_subs_info_init(struct snd_seq_port_subs_info *grp)
114 {
115 	INIT_LIST_HEAD(&grp->list_head);
116 	grp->count = 0;
117 	grp->exclusive = 0;
118 	rwlock_init(&grp->list_lock);
119 	init_rwsem(&grp->list_mutex);
120 	grp->open = NULL;
121 	grp->close = NULL;
122 }
123 
124 
125 /* create a port, port number is returned (-1 on failure);
126  * the caller needs to unref the port via snd_seq_port_unlock() appropriately
127  */
snd_seq_create_port(struct snd_seq_client * client,int port)128 struct snd_seq_client_port *snd_seq_create_port(struct snd_seq_client *client,
129 						int port)
130 {
131 	unsigned long flags;
132 	struct snd_seq_client_port *new_port, *p;
133 	int num = -1;
134 
135 	/* sanity check */
136 	if (snd_BUG_ON(!client))
137 		return NULL;
138 
139 	if (client->num_ports >= SNDRV_SEQ_MAX_PORTS - 1) {
140 		pr_warn("ALSA: seq: too many ports for client %d\n", client->number);
141 		return NULL;
142 	}
143 
144 	/* create a new port */
145 	new_port = kzalloc(sizeof(*new_port), GFP_KERNEL);
146 	if (!new_port)
147 		return NULL;	/* failure, out of memory */
148 	/* init port data */
149 	new_port->addr.client = client->number;
150 	new_port->addr.port = -1;
151 	new_port->owner = THIS_MODULE;
152 	sprintf(new_port->name, "port-%d", num);
153 	snd_use_lock_init(&new_port->use_lock);
154 	port_subs_info_init(&new_port->c_src);
155 	port_subs_info_init(&new_port->c_dest);
156 	snd_use_lock_use(&new_port->use_lock);
157 
158 	num = port >= 0 ? port : 0;
159 	mutex_lock(&client->ports_mutex);
160 	write_lock_irqsave(&client->ports_lock, flags);
161 	list_for_each_entry(p, &client->ports_list_head, list) {
162 		if (p->addr.port > num)
163 			break;
164 		if (port < 0) /* auto-probe mode */
165 			num = p->addr.port + 1;
166 	}
167 	/* insert the new port */
168 	list_add_tail(&new_port->list, &p->list);
169 	client->num_ports++;
170 	new_port->addr.port = num;	/* store the port number in the port */
171 	sprintf(new_port->name, "port-%d", num);
172 	write_unlock_irqrestore(&client->ports_lock, flags);
173 	mutex_unlock(&client->ports_mutex);
174 
175 	return new_port;
176 }
177 
178 /* */
179 static int subscribe_port(struct snd_seq_client *client,
180 			  struct snd_seq_client_port *port,
181 			  struct snd_seq_port_subs_info *grp,
182 			  struct snd_seq_port_subscribe *info, int send_ack);
183 static int unsubscribe_port(struct snd_seq_client *client,
184 			    struct snd_seq_client_port *port,
185 			    struct snd_seq_port_subs_info *grp,
186 			    struct snd_seq_port_subscribe *info, int send_ack);
187 
188 
get_client_port(struct snd_seq_addr * addr,struct snd_seq_client ** cp)189 static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr,
190 						   struct snd_seq_client **cp)
191 {
192 	struct snd_seq_client_port *p;
193 	*cp = snd_seq_client_use_ptr(addr->client);
194 	if (*cp) {
195 		p = snd_seq_port_use_ptr(*cp, addr->port);
196 		if (! p) {
197 			snd_seq_client_unlock(*cp);
198 			*cp = NULL;
199 		}
200 		return p;
201 	}
202 	return NULL;
203 }
204 
205 static void delete_and_unsubscribe_port(struct snd_seq_client *client,
206 					struct snd_seq_client_port *port,
207 					struct snd_seq_subscribers *subs,
208 					bool is_src, bool ack);
209 
210 static inline struct snd_seq_subscribers *
get_subscriber(struct list_head * p,bool is_src)211 get_subscriber(struct list_head *p, bool is_src)
212 {
213 	if (is_src)
214 		return list_entry(p, struct snd_seq_subscribers, src_list);
215 	else
216 		return list_entry(p, struct snd_seq_subscribers, dest_list);
217 }
218 
219 /*
220  * remove all subscribers on the list
221  * this is called from port_delete, for each src and dest list.
222  */
clear_subscriber_list(struct snd_seq_client * client,struct snd_seq_client_port * port,struct snd_seq_port_subs_info * grp,int is_src)223 static void clear_subscriber_list(struct snd_seq_client *client,
224 				  struct snd_seq_client_port *port,
225 				  struct snd_seq_port_subs_info *grp,
226 				  int is_src)
227 {
228 	struct list_head *p, *n;
229 
230 	list_for_each_safe(p, n, &grp->list_head) {
231 		struct snd_seq_subscribers *subs;
232 		struct snd_seq_client *c;
233 		struct snd_seq_client_port *aport;
234 
235 		subs = get_subscriber(p, is_src);
236 		if (is_src)
237 			aport = get_client_port(&subs->info.dest, &c);
238 		else
239 			aport = get_client_port(&subs->info.sender, &c);
240 		delete_and_unsubscribe_port(client, port, subs, is_src, false);
241 
242 		if (!aport) {
243 			/* looks like the connected port is being deleted.
244 			 * we decrease the counter, and when both ports are deleted
245 			 * remove the subscriber info
246 			 */
247 			if (atomic_dec_and_test(&subs->ref_count))
248 				kfree(subs);
249 			continue;
250 		}
251 
252 		/* ok we got the connected port */
253 		delete_and_unsubscribe_port(c, aport, subs, !is_src, true);
254 		kfree(subs);
255 		snd_seq_port_unlock(aport);
256 		snd_seq_client_unlock(c);
257 	}
258 }
259 
260 /* delete port data */
port_delete(struct snd_seq_client * client,struct snd_seq_client_port * port)261 static int port_delete(struct snd_seq_client *client,
262 		       struct snd_seq_client_port *port)
263 {
264 	/* set closing flag and wait for all port access are gone */
265 	port->closing = 1;
266 	snd_use_lock_sync(&port->use_lock);
267 
268 	/* clear subscribers info */
269 	clear_subscriber_list(client, port, &port->c_src, true);
270 	clear_subscriber_list(client, port, &port->c_dest, false);
271 
272 	if (port->private_free)
273 		port->private_free(port->private_data);
274 
275 	snd_BUG_ON(port->c_src.count != 0);
276 	snd_BUG_ON(port->c_dest.count != 0);
277 
278 	kfree(port);
279 	return 0;
280 }
281 
282 
283 /* delete a port with the given port id */
snd_seq_delete_port(struct snd_seq_client * client,int port)284 int snd_seq_delete_port(struct snd_seq_client *client, int port)
285 {
286 	unsigned long flags;
287 	struct snd_seq_client_port *found = NULL, *p;
288 
289 	mutex_lock(&client->ports_mutex);
290 	write_lock_irqsave(&client->ports_lock, flags);
291 	list_for_each_entry(p, &client->ports_list_head, list) {
292 		if (p->addr.port == port) {
293 			/* ok found.  delete from the list at first */
294 			list_del(&p->list);
295 			client->num_ports--;
296 			found = p;
297 			break;
298 		}
299 	}
300 	write_unlock_irqrestore(&client->ports_lock, flags);
301 	mutex_unlock(&client->ports_mutex);
302 	if (found)
303 		return port_delete(client, found);
304 	else
305 		return -ENOENT;
306 }
307 
308 /* delete the all ports belonging to the given client */
snd_seq_delete_all_ports(struct snd_seq_client * client)309 int snd_seq_delete_all_ports(struct snd_seq_client *client)
310 {
311 	unsigned long flags;
312 	struct list_head deleted_list;
313 	struct snd_seq_client_port *port, *tmp;
314 
315 	/* move the port list to deleted_list, and
316 	 * clear the port list in the client data.
317 	 */
318 	mutex_lock(&client->ports_mutex);
319 	write_lock_irqsave(&client->ports_lock, flags);
320 	if (! list_empty(&client->ports_list_head)) {
321 		list_add(&deleted_list, &client->ports_list_head);
322 		list_del_init(&client->ports_list_head);
323 	} else {
324 		INIT_LIST_HEAD(&deleted_list);
325 	}
326 	client->num_ports = 0;
327 	write_unlock_irqrestore(&client->ports_lock, flags);
328 
329 	/* remove each port in deleted_list */
330 	list_for_each_entry_safe(port, tmp, &deleted_list, list) {
331 		list_del(&port->list);
332 		snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port);
333 		port_delete(client, port);
334 	}
335 	mutex_unlock(&client->ports_mutex);
336 	return 0;
337 }
338 
339 /* set port info fields */
snd_seq_set_port_info(struct snd_seq_client_port * port,struct snd_seq_port_info * info)340 int snd_seq_set_port_info(struct snd_seq_client_port * port,
341 			  struct snd_seq_port_info * info)
342 {
343 	if (snd_BUG_ON(!port || !info))
344 		return -EINVAL;
345 
346 	/* set port name */
347 	if (info->name[0])
348 		strlcpy(port->name, info->name, sizeof(port->name));
349 
350 	/* set capabilities */
351 	port->capability = info->capability;
352 
353 	/* get port type */
354 	port->type = info->type;
355 
356 	/* information about supported channels/voices */
357 	port->midi_channels = info->midi_channels;
358 	port->midi_voices = info->midi_voices;
359 	port->synth_voices = info->synth_voices;
360 
361 	/* timestamping */
362 	port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0;
363 	port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0;
364 	port->time_queue = info->time_queue;
365 
366 	return 0;
367 }
368 
369 /* get port info fields */
snd_seq_get_port_info(struct snd_seq_client_port * port,struct snd_seq_port_info * info)370 int snd_seq_get_port_info(struct snd_seq_client_port * port,
371 			  struct snd_seq_port_info * info)
372 {
373 	if (snd_BUG_ON(!port || !info))
374 		return -EINVAL;
375 
376 	/* get port name */
377 	strlcpy(info->name, port->name, sizeof(info->name));
378 
379 	/* get capabilities */
380 	info->capability = port->capability;
381 
382 	/* get port type */
383 	info->type = port->type;
384 
385 	/* information about supported channels/voices */
386 	info->midi_channels = port->midi_channels;
387 	info->midi_voices = port->midi_voices;
388 	info->synth_voices = port->synth_voices;
389 
390 	/* get subscriber counts */
391 	info->read_use = port->c_src.count;
392 	info->write_use = port->c_dest.count;
393 
394 	/* timestamping */
395 	info->flags = 0;
396 	if (port->timestamping) {
397 		info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP;
398 		if (port->time_real)
399 			info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL;
400 		info->time_queue = port->time_queue;
401 	}
402 
403 	return 0;
404 }
405 
406 
407 
408 /*
409  * call callback functions (if any):
410  * the callbacks are invoked only when the first (for connection) or
411  * the last subscription (for disconnection) is done.  Second or later
412  * subscription results in increment of counter, but no callback is
413  * invoked.
414  * This feature is useful if these callbacks are associated with
415  * initialization or termination of devices (see seq_midi.c).
416  *
417  * If callback_all option is set, the callback function is invoked
418  * at each connection/disconnection.
419  */
420 
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)421 static int subscribe_port(struct snd_seq_client *client,
422 			  struct snd_seq_client_port *port,
423 			  struct snd_seq_port_subs_info *grp,
424 			  struct snd_seq_port_subscribe *info,
425 			  int send_ack)
426 {
427 	int err = 0;
428 
429 	if (!try_module_get(port->owner))
430 		return -EFAULT;
431 	grp->count++;
432 	if (grp->open && (port->callback_all || grp->count == 1)) {
433 		err = grp->open(port->private_data, info);
434 		if (err < 0) {
435 			module_put(port->owner);
436 			grp->count--;
437 		}
438 	}
439 	if (err >= 0 && send_ack && client->type == USER_CLIENT)
440 		snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
441 						   info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
442 
443 	return err;
444 }
445 
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)446 static int unsubscribe_port(struct snd_seq_client *client,
447 			    struct snd_seq_client_port *port,
448 			    struct snd_seq_port_subs_info *grp,
449 			    struct snd_seq_port_subscribe *info,
450 			    int send_ack)
451 {
452 	int err = 0;
453 
454 	if (! grp->count)
455 		return -EINVAL;
456 	grp->count--;
457 	if (grp->close && (port->callback_all || grp->count == 0))
458 		err = grp->close(port->private_data, info);
459 	if (send_ack && client->type == USER_CLIENT)
460 		snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
461 						   info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
462 	module_put(port->owner);
463 	return err;
464 }
465 
466 
467 
468 /* check if both addresses are identical */
addr_match(struct snd_seq_addr * r,struct snd_seq_addr * s)469 static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s)
470 {
471 	return (r->client == s->client) && (r->port == s->port);
472 }
473 
474 /* check the two subscribe info match */
475 /* if flags is zero, checks only sender and destination addresses */
match_subs_info(struct snd_seq_port_subscribe * r,struct snd_seq_port_subscribe * s)476 static int match_subs_info(struct snd_seq_port_subscribe *r,
477 			   struct snd_seq_port_subscribe *s)
478 {
479 	if (addr_match(&r->sender, &s->sender) &&
480 	    addr_match(&r->dest, &s->dest)) {
481 		if (r->flags && r->flags == s->flags)
482 			return r->queue == s->queue;
483 		else if (! r->flags)
484 			return 1;
485 	}
486 	return 0;
487 }
488 
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)489 static int check_and_subscribe_port(struct snd_seq_client *client,
490 				    struct snd_seq_client_port *port,
491 				    struct snd_seq_subscribers *subs,
492 				    bool is_src, bool exclusive, bool ack)
493 {
494 	struct snd_seq_port_subs_info *grp;
495 	struct list_head *p;
496 	struct snd_seq_subscribers *s;
497 	int err;
498 
499 	grp = is_src ? &port->c_src : &port->c_dest;
500 	err = -EBUSY;
501 	down_write(&grp->list_mutex);
502 	if (exclusive) {
503 		if (!list_empty(&grp->list_head))
504 			goto __error;
505 	} else {
506 		if (grp->exclusive)
507 			goto __error;
508 		/* check whether already exists */
509 		list_for_each(p, &grp->list_head) {
510 			s = get_subscriber(p, is_src);
511 			if (match_subs_info(&subs->info, &s->info))
512 				goto __error;
513 		}
514 	}
515 
516 	err = subscribe_port(client, port, grp, &subs->info, ack);
517 	if (err < 0) {
518 		grp->exclusive = 0;
519 		goto __error;
520 	}
521 
522 	/* add to list */
523 	write_lock_irq(&grp->list_lock);
524 	if (is_src)
525 		list_add_tail(&subs->src_list, &grp->list_head);
526 	else
527 		list_add_tail(&subs->dest_list, &grp->list_head);
528 	grp->exclusive = exclusive;
529 	atomic_inc(&subs->ref_count);
530 	write_unlock_irq(&grp->list_lock);
531 	err = 0;
532 
533  __error:
534 	up_write(&grp->list_mutex);
535 	return err;
536 }
537 
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)538 static void delete_and_unsubscribe_port(struct snd_seq_client *client,
539 					struct snd_seq_client_port *port,
540 					struct snd_seq_subscribers *subs,
541 					bool is_src, bool ack)
542 {
543 	struct snd_seq_port_subs_info *grp;
544 	struct list_head *list;
545 	bool empty;
546 
547 	grp = is_src ? &port->c_src : &port->c_dest;
548 	list = is_src ? &subs->src_list : &subs->dest_list;
549 	down_write(&grp->list_mutex);
550 	write_lock_irq(&grp->list_lock);
551 	empty = list_empty(list);
552 	if (!empty)
553 		list_del_init(list);
554 	grp->exclusive = 0;
555 	write_unlock_irq(&grp->list_lock);
556 	up_write(&grp->list_mutex);
557 
558 	if (!empty)
559 		unsubscribe_port(client, port, grp, &subs->info, ack);
560 }
561 
562 /* connect two ports */
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)563 int snd_seq_port_connect(struct snd_seq_client *connector,
564 			 struct snd_seq_client *src_client,
565 			 struct snd_seq_client_port *src_port,
566 			 struct snd_seq_client *dest_client,
567 			 struct snd_seq_client_port *dest_port,
568 			 struct snd_seq_port_subscribe *info)
569 {
570 	struct snd_seq_subscribers *subs;
571 	bool exclusive;
572 	int err;
573 
574 	subs = kzalloc(sizeof(*subs), GFP_KERNEL);
575 	if (!subs)
576 		return -ENOMEM;
577 
578 	subs->info = *info;
579 	atomic_set(&subs->ref_count, 0);
580 	INIT_LIST_HEAD(&subs->src_list);
581 	INIT_LIST_HEAD(&subs->dest_list);
582 
583 	exclusive = !!(info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE);
584 
585 	err = check_and_subscribe_port(src_client, src_port, subs, true,
586 				       exclusive,
587 				       connector->number != src_client->number);
588 	if (err < 0)
589 		goto error;
590 	err = check_and_subscribe_port(dest_client, dest_port, subs, false,
591 				       exclusive,
592 				       connector->number != dest_client->number);
593 	if (err < 0)
594 		goto error_dest;
595 
596 	return 0;
597 
598  error_dest:
599 	delete_and_unsubscribe_port(src_client, src_port, subs, true,
600 				    connector->number != src_client->number);
601  error:
602 	kfree(subs);
603 	return err;
604 }
605 
606 /* remove the connection */
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)607 int snd_seq_port_disconnect(struct snd_seq_client *connector,
608 			    struct snd_seq_client *src_client,
609 			    struct snd_seq_client_port *src_port,
610 			    struct snd_seq_client *dest_client,
611 			    struct snd_seq_client_port *dest_port,
612 			    struct snd_seq_port_subscribe *info)
613 {
614 	struct snd_seq_port_subs_info *src = &src_port->c_src;
615 	struct snd_seq_subscribers *subs;
616 	int err = -ENOENT;
617 
618 	down_write(&src->list_mutex);
619 	/* look for the connection */
620 	list_for_each_entry(subs, &src->list_head, src_list) {
621 		if (match_subs_info(info, &subs->info)) {
622 			atomic_dec(&subs->ref_count); /* mark as not ready */
623 			err = 0;
624 			break;
625 		}
626 	}
627 	up_write(&src->list_mutex);
628 	if (err < 0)
629 		return err;
630 
631 	delete_and_unsubscribe_port(src_client, src_port, subs, true,
632 				    connector->number != src_client->number);
633 	delete_and_unsubscribe_port(dest_client, dest_port, subs, false,
634 				    connector->number != dest_client->number);
635 	kfree(subs);
636 	return 0;
637 }
638 
639 
640 /* get matched subscriber */
snd_seq_port_get_subscription(struct snd_seq_port_subs_info * src_grp,struct snd_seq_addr * dest_addr)641 struct snd_seq_subscribers *snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp,
642 							  struct snd_seq_addr *dest_addr)
643 {
644 	struct snd_seq_subscribers *s, *found = NULL;
645 
646 	down_read(&src_grp->list_mutex);
647 	list_for_each_entry(s, &src_grp->list_head, src_list) {
648 		if (addr_match(dest_addr, &s->info.dest)) {
649 			found = s;
650 			break;
651 		}
652 	}
653 	up_read(&src_grp->list_mutex);
654 	return found;
655 }
656 
657 /*
658  * Attach a device driver that wants to receive events from the
659  * sequencer.  Returns the new port number on success.
660  * A driver that wants to receive the events converted to midi, will
661  * use snd_seq_midisynth_register_port().
662  */
663 /* exported */
snd_seq_event_port_attach(int client,struct snd_seq_port_callback * pcbp,int cap,int type,int midi_channels,int midi_voices,char * portname)664 int snd_seq_event_port_attach(int client,
665 			      struct snd_seq_port_callback *pcbp,
666 			      int cap, int type, int midi_channels,
667 			      int midi_voices, char *portname)
668 {
669 	struct snd_seq_port_info portinfo;
670 	int  ret;
671 
672 	/* Set up the port */
673 	memset(&portinfo, 0, sizeof(portinfo));
674 	portinfo.addr.client = client;
675 	strlcpy(portinfo.name, portname ? portname : "Unamed port",
676 		sizeof(portinfo.name));
677 
678 	portinfo.capability = cap;
679 	portinfo.type = type;
680 	portinfo.kernel = pcbp;
681 	portinfo.midi_channels = midi_channels;
682 	portinfo.midi_voices = midi_voices;
683 
684 	/* Create it */
685 	ret = snd_seq_kernel_client_ctl(client,
686 					SNDRV_SEQ_IOCTL_CREATE_PORT,
687 					&portinfo);
688 
689 	if (ret >= 0)
690 		ret = portinfo.addr.port;
691 
692 	return ret;
693 }
694 
695 EXPORT_SYMBOL(snd_seq_event_port_attach);
696 
697 /*
698  * Detach the driver from a port.
699  */
700 /* exported */
snd_seq_event_port_detach(int client,int port)701 int snd_seq_event_port_detach(int client, int port)
702 {
703 	struct snd_seq_port_info portinfo;
704 	int  err;
705 
706 	memset(&portinfo, 0, sizeof(portinfo));
707 	portinfo.addr.client = client;
708 	portinfo.addr.port   = port;
709 	err = snd_seq_kernel_client_ctl(client,
710 					SNDRV_SEQ_IOCTL_DELETE_PORT,
711 					&portinfo);
712 
713 	return err;
714 }
715 
716 EXPORT_SYMBOL(snd_seq_event_port_detach);
717