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