xref: /linux/sound/core/seq/seq_ports.c (revision 7ca1731b8a129cafe258c578fa87b7c414c6d004)
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 */
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(rcu)();
52 	list_for_each_entry_rcu(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 */
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(rcu)();
75 	list_for_each_entry_rcu(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 */
99 static void port_subs_info_init(struct snd_seq_port_subs_info *grp)
100 {
101 	INIT_HLIST_HEAD(&grp->list_head);
102 	grp->count = 0;
103 	grp->exclusive = 0;
104 	init_rwsem(&grp->list_mutex);
105 	grp->open = NULL;
106 	grp->close = NULL;
107 }
108 
109 
110 /* create a port, 0 on success or a negative error code is returned
111  * the caller needs to unref the port via snd_seq_port_unlock() appropriately
112  */
113 int snd_seq_create_port(struct snd_seq_client *client,
114 			struct snd_seq_client_port **port_ret)
115 {
116 	struct snd_seq_client_port *new_port;
117 
118 	*port_ret = NULL;
119 
120 	/* sanity check */
121 	if (snd_BUG_ON(!client))
122 		return -EINVAL;
123 
124 	if (client->num_ports >= SNDRV_SEQ_MAX_PORTS) {
125 		pr_warn("ALSA: seq: too many ports for client %d\n", client->number);
126 		return -EINVAL;
127 	}
128 
129 	/* create a new port */
130 	new_port = kzalloc_obj(*new_port);
131 	if (!new_port)
132 		return -ENOMEM;	/* failure, out of memory */
133 	/* init port data */
134 	new_port->addr.client = client->number;
135 	new_port->addr.port = -1;
136 	new_port->owner = THIS_MODULE;
137 	snd_use_lock_init(&new_port->use_lock);
138 	port_subs_info_init(&new_port->c_src);
139 	port_subs_info_init(&new_port->c_dest);
140 	snd_use_lock_use(&new_port->use_lock);
141 
142 	*port_ret = new_port;
143 
144 	return 0;
145 }
146 
147 /* insert the port; return the port address or a negative error code */
148 int snd_seq_insert_port(struct snd_seq_client *client, int port,
149 			struct snd_seq_client_port *new_port)
150 {
151 	struct snd_seq_client_port *p;
152 	int num;
153 
154 	num = max(port, 0);
155 	guard(mutex)(&client->ports_mutex);
156 	struct list_head *insert_before = &client->ports_list_head;
157 	list_for_each_entry(p, &client->ports_list_head, list) {
158 		if (p->addr.port == port)
159 			return -EBUSY;
160 		if (p->addr.port > num) {
161 			insert_before = &p->list;
162 			break;
163 		}
164 		if (port < 0) /* auto-probe mode */
165 			num = p->addr.port + 1;
166 	}
167 	/* finish initializing the port before publishing it to RCU readers */
168 	new_port->addr.port = num;	/* store the port number in the port */
169 	if (!new_port->name[0])
170 		sprintf(new_port->name, "port-%d", num);
171 	/* insert the new port */
172 	list_add_tail_rcu(&new_port->list, insert_before);
173 	client->num_ports++;
174 
175 	return num;
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 
189 static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr,
190 						   struct snd_seq_client **cp)
191 {
192 	*cp = snd_seq_client_use_ptr(addr->client);
193 	if (!*cp)
194 		return NULL;
195 	return snd_seq_port_use_ptr(*cp, addr->port);
196 }
197 
198 static void delete_and_unsubscribe_port(struct snd_seq_client *client,
199 					struct snd_seq_client_port *port,
200 					struct snd_seq_subscribers *subs,
201 					bool is_src, bool ack);
202 
203 static inline struct snd_seq_subscribers *
204 get_subscriber(struct hlist_node *p, bool is_src)
205 {
206 	if (is_src)
207 		return hlist_entry(p, struct snd_seq_subscribers, src_list);
208 	else
209 		return hlist_entry(p, struct snd_seq_subscribers, dest_list);
210 }
211 
212 /*
213  * remove all subscribers on the list
214  * this is called from port_delete, for each src and dest list.
215  */
216 static void clear_subscriber_list(struct snd_seq_client *client,
217 				  struct snd_seq_client_port *port,
218 				  struct snd_seq_port_subs_info *grp,
219 				  int is_src)
220 {
221 	struct hlist_node *p, *n;
222 
223 	hlist_for_each_safe(p, n, &grp->list_head) {
224 		struct snd_seq_subscribers *subs;
225 
226 		subs = get_subscriber(p, is_src);
227 		struct snd_seq_client *c __free(snd_seq_client) = NULL;
228 		struct snd_seq_client_port *aport __free(snd_seq_port) =
229 			is_src ?
230 			get_client_port(&subs->info.dest, &c) :
231 			get_client_port(&subs->info.sender, &c);
232 		delete_and_unsubscribe_port(client, port, subs, is_src, false);
233 
234 		if (!aport) {
235 			/* looks like the connected port is being deleted.
236 			 * we decrease the counter, and when both ports are deleted
237 			 * remove the subscriber info
238 			 */
239 			if (atomic_dec_and_test(&subs->ref_count))
240 				kfree_rcu(subs, rcu);
241 			continue;
242 		}
243 
244 		/* ok we got the connected port */
245 		delete_and_unsubscribe_port(c, aport, subs, !is_src, true);
246 		kfree_rcu(subs, rcu);
247 	}
248 }
249 
250 /* delete port data */
251 static void port_free(struct snd_seq_client *client,
252 		      struct snd_seq_client_port *port)
253 {
254 	snd_use_lock_sync(&port->use_lock);
255 
256 	/* clear subscribers info */
257 	clear_subscriber_list(client, port, &port->c_src, true);
258 	clear_subscriber_list(client, port, &port->c_dest, false);
259 
260 	if (port->private_free)
261 		port->private_free(port->private_data);
262 
263 	snd_BUG_ON(port->c_src.count != 0);
264 	snd_BUG_ON(port->c_dest.count != 0);
265 
266 	kfree(port);
267 }
268 
269 static int port_delete(struct snd_seq_client *client,
270 		       struct snd_seq_client_port *port)
271 {
272 	/* set closing flag and wait for all port access are gone */
273 	port->closing = 1;
274 	/* the port has already been unlinked from the client's port list;
275 	 * wait for a grace period so that RCU readers still traversing the
276 	 * list can no longer take a new use_lock reference, then drain the
277 	 * outstanding references before freeing
278 	 */
279 	synchronize_rcu();
280 	port_free(client, port);
281 	return 0;
282 }
283 
284 
285 /* delete a port with the given port id */
286 int snd_seq_delete_port(struct snd_seq_client *client, int port)
287 {
288 	struct snd_seq_client_port *found = NULL, *p;
289 
290 	scoped_guard(mutex, &client->ports_mutex) {
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_rcu(&p->list);
295 				client->num_ports--;
296 				found = p;
297 				break;
298 			}
299 		}
300 	}
301 	if (found)
302 		return port_delete(client, found);
303 	else
304 		return -ENOENT;
305 }
306 
307 /* delete the all ports belonging to the given client */
308 int snd_seq_delete_all_ports(struct snd_seq_client *client)
309 {
310 	struct snd_seq_client_port *port, *tmp;
311 	LIST_HEAD(deleted);
312 
313 	/* unlink all ports first and wait for a single RCU grace period
314 	 * before draining and freeing them, so concurrent lockless readers
315 	 * can no longer take a new use_lock reference on any of them
316 	 */
317 	guard(mutex)(&client->ports_mutex);
318 	list_for_each_entry_safe(port, tmp, &client->ports_list_head, list) {
319 		list_del_rcu(&port->list);
320 		client->num_ports--;
321 		port->closing = 1;
322 		list_add_tail(&port->delete_list, &deleted);
323 		snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port);
324 	}
325 	if (list_empty(&deleted))
326 		return 0;
327 	synchronize_rcu();
328 	list_for_each_entry_safe(port, tmp, &deleted, delete_list)
329 		port_free(client, port);
330 	return 0;
331 }
332 
333 /* set port info fields */
334 int snd_seq_set_port_info(struct snd_seq_client_port * port,
335 			  struct snd_seq_port_info * info)
336 {
337 	if (snd_BUG_ON(!port || !info))
338 		return -EINVAL;
339 
340 	/* set port name */
341 	if (info->name[0])
342 		strscpy(port->name, info->name, sizeof(port->name));
343 
344 	/* set capabilities */
345 	port->capability = info->capability;
346 
347 	/* get port type */
348 	port->type = info->type;
349 
350 	/* information about supported channels/voices */
351 	port->midi_channels = info->midi_channels;
352 	port->midi_voices = info->midi_voices;
353 	port->synth_voices = info->synth_voices;
354 
355 	/* timestamping */
356 	port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0;
357 	port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0;
358 	port->time_queue = info->time_queue;
359 
360 	/* UMP direction and group */
361 	port->direction = info->direction;
362 	port->ump_group = info->ump_group;
363 	if (port->ump_group > SNDRV_UMP_MAX_GROUPS)
364 		port->ump_group = 0;
365 
366 	/* fill default port direction */
367 	if (!port->direction) {
368 		if (info->capability & SNDRV_SEQ_PORT_CAP_READ)
369 			port->direction |= SNDRV_SEQ_PORT_DIR_INPUT;
370 		if (info->capability & SNDRV_SEQ_PORT_CAP_WRITE)
371 			port->direction |= SNDRV_SEQ_PORT_DIR_OUTPUT;
372 	}
373 
374 	port->is_midi1 = !!(info->flags & SNDRV_SEQ_PORT_FLG_IS_MIDI1);
375 
376 	return 0;
377 }
378 
379 /* get port info fields */
380 int snd_seq_get_port_info(struct snd_seq_client_port * port,
381 			  struct snd_seq_port_info * info)
382 {
383 	if (snd_BUG_ON(!port || !info))
384 		return -EINVAL;
385 
386 	/* get port name */
387 	strscpy(info->name, port->name, sizeof(info->name));
388 
389 	/* get capabilities */
390 	info->capability = port->capability;
391 
392 	/* get port type */
393 	info->type = port->type;
394 
395 	/* information about supported channels/voices */
396 	info->midi_channels = port->midi_channels;
397 	info->midi_voices = port->midi_voices;
398 	info->synth_voices = port->synth_voices;
399 
400 	/* get subscriber counts */
401 	info->read_use = port->c_src.count;
402 	info->write_use = port->c_dest.count;
403 
404 	/* timestamping */
405 	info->flags = 0;
406 	if (port->timestamping) {
407 		info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP;
408 		if (port->time_real)
409 			info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL;
410 		info->time_queue = port->time_queue;
411 	}
412 
413 	if (port->is_midi1)
414 		info->flags |= SNDRV_SEQ_PORT_FLG_IS_MIDI1;
415 
416 	/* UMP direction and group */
417 	info->direction = port->direction;
418 	info->ump_group = port->ump_group;
419 
420 	return 0;
421 }
422 
423 
424 
425 /*
426  * call callback functions (if any):
427  * the callbacks are invoked only when the first (for connection) or
428  * the last subscription (for disconnection) is done.  Second or later
429  * subscription results in increment of counter, but no callback is
430  * invoked.
431  * This feature is useful if these callbacks are associated with
432  * initialization or termination of devices (see seq_midi.c).
433  */
434 
435 static int subscribe_port(struct snd_seq_client *client,
436 			  struct snd_seq_client_port *port,
437 			  struct snd_seq_port_subs_info *grp,
438 			  struct snd_seq_port_subscribe *info,
439 			  int send_ack)
440 {
441 	int err = 0;
442 
443 	if (!try_module_get(port->owner))
444 		return -EFAULT;
445 	grp->count++;
446 	if (grp->open && grp->count == 1) {
447 		err = grp->open(port->private_data, info);
448 		if (err < 0) {
449 			module_put(port->owner);
450 			grp->count--;
451 		}
452 	}
453 	if (err >= 0 && send_ack && client->type == USER_CLIENT)
454 		snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
455 						   info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
456 
457 	return err;
458 }
459 
460 static int unsubscribe_port(struct snd_seq_client *client,
461 			    struct snd_seq_client_port *port,
462 			    struct snd_seq_port_subs_info *grp,
463 			    struct snd_seq_port_subscribe *info,
464 			    int send_ack)
465 {
466 	int err = 0;
467 
468 	if (! grp->count)
469 		return -EINVAL;
470 	grp->count--;
471 	if (grp->close && grp->count == 0)
472 		err = grp->close(port->private_data, info);
473 	if (send_ack && client->type == USER_CLIENT)
474 		snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
475 						   info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
476 	module_put(port->owner);
477 	return err;
478 }
479 
480 
481 
482 /* check if both addresses are identical */
483 static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s)
484 {
485 	return (r->client == s->client) && (r->port == s->port);
486 }
487 
488 /* check the two subscribe info match */
489 /* if flags is zero, checks only sender and destination addresses */
490 static int match_subs_info(struct snd_seq_port_subscribe *r,
491 			   struct snd_seq_port_subscribe *s)
492 {
493 	if (addr_match(&r->sender, &s->sender) &&
494 	    addr_match(&r->dest, &s->dest)) {
495 		if (r->flags && r->flags == s->flags)
496 			return r->queue == s->queue;
497 		else if (! r->flags)
498 			return 1;
499 	}
500 	return 0;
501 }
502 
503 static int check_and_subscribe_port(struct snd_seq_client *client,
504 				    struct snd_seq_client_port *port,
505 				    struct snd_seq_subscribers *subs,
506 				    bool is_src, bool exclusive, bool ack)
507 {
508 	struct snd_seq_port_subs_info *grp;
509 	struct hlist_node *p;
510 	struct snd_seq_subscribers *s;
511 	int err;
512 
513 	grp = is_src ? &port->c_src : &port->c_dest;
514 	guard(rwsem_write)(&grp->list_mutex);
515 	if (exclusive) {
516 		if (!hlist_empty(&grp->list_head))
517 			return -EBUSY;
518 	} else {
519 		if (grp->exclusive)
520 			return -EBUSY;
521 		/* check whether already exists */
522 		hlist_for_each(p, &grp->list_head) {
523 			s = get_subscriber(p, is_src);
524 			if (match_subs_info(&subs->info, &s->info))
525 				return -EBUSY;
526 		}
527 	}
528 
529 	err = subscribe_port(client, port, grp, &subs->info, ack);
530 	if (err < 0) {
531 		grp->exclusive = 0;
532 		return err;
533 	}
534 
535 	/* add to list */
536 	if (is_src)
537 		hlist_add_tail_rcu(&subs->src_list, &grp->list_head);
538 	else
539 		hlist_add_tail_rcu(&subs->dest_list, &grp->list_head);
540 	grp->exclusive = exclusive;
541 	atomic_inc(&subs->ref_count);
542 
543 	return 0;
544 }
545 
546 /* called with grp->list_mutex held */
547 static void __delete_and_unsubscribe_port(struct snd_seq_client *client,
548 					  struct snd_seq_client_port *port,
549 					  struct snd_seq_subscribers *subs,
550 					  bool is_src, bool ack)
551 {
552 	struct snd_seq_port_subs_info *grp;
553 	struct hlist_node *list;
554 	bool empty;
555 
556 	grp = is_src ? &port->c_src : &port->c_dest;
557 	list = is_src ? &subs->src_list : &subs->dest_list;
558 	empty = hlist_unhashed(list);
559 	if (!empty)
560 		hlist_del_init_rcu(list);
561 	grp->exclusive = 0;
562 
563 	if (!empty)
564 		unsubscribe_port(client, port, grp, &subs->info, ack);
565 }
566 
567 static void delete_and_unsubscribe_port(struct snd_seq_client *client,
568 					struct snd_seq_client_port *port,
569 					struct snd_seq_subscribers *subs,
570 					bool is_src, bool ack)
571 {
572 	struct snd_seq_port_subs_info *grp;
573 
574 	grp = is_src ? &port->c_src : &port->c_dest;
575 	guard(rwsem_write)(&grp->list_mutex);
576 	__delete_and_unsubscribe_port(client, port, subs, is_src, ack);
577 }
578 
579 /* connect two ports */
580 int snd_seq_port_connect(struct snd_seq_client *connector,
581 			 struct snd_seq_client *src_client,
582 			 struct snd_seq_client_port *src_port,
583 			 struct snd_seq_client *dest_client,
584 			 struct snd_seq_client_port *dest_port,
585 			 struct snd_seq_port_subscribe *info)
586 {
587 	struct snd_seq_subscribers *subs;
588 	bool exclusive;
589 	int err;
590 
591 	subs = kzalloc_obj(*subs);
592 	if (!subs)
593 		return -ENOMEM;
594 
595 	subs->info = *info;
596 	atomic_set(&subs->ref_count, 0);
597 	INIT_HLIST_NODE(&subs->src_list);
598 	INIT_HLIST_NODE(&subs->dest_list);
599 
600 	exclusive = !!(info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE);
601 
602 	err = check_and_subscribe_port(src_client, src_port, subs, true,
603 				       exclusive,
604 				       connector->number != src_client->number);
605 	if (err < 0)
606 		goto error;
607 	err = check_and_subscribe_port(dest_client, dest_port, subs, false,
608 				       exclusive,
609 				       connector->number != dest_client->number);
610 	if (err < 0)
611 		goto error_dest;
612 
613 	return 0;
614 
615  error_dest:
616 	delete_and_unsubscribe_port(src_client, src_port, subs, true,
617 				    connector->number != src_client->number);
618  error:
619 	kfree_rcu(subs, rcu);
620 	return err;
621 }
622 
623 /* remove the connection */
624 int snd_seq_port_disconnect(struct snd_seq_client *connector,
625 			    struct snd_seq_client *src_client,
626 			    struct snd_seq_client_port *src_port,
627 			    struct snd_seq_client *dest_client,
628 			    struct snd_seq_client_port *dest_port,
629 			    struct snd_seq_port_subscribe *info)
630 {
631 	struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
632 	struct snd_seq_subscribers *subs;
633 	int err = -ENOENT;
634 
635 	/* always start from deleting the dest port for avoiding concurrent
636 	 * deletions
637 	 */
638 	scoped_guard(rwsem_write, &dest->list_mutex) {
639 		/* look for the connection */
640 		hlist_for_each_entry(subs, &dest->list_head, dest_list) {
641 			if (match_subs_info(info, &subs->info)) {
642 				__delete_and_unsubscribe_port(dest_client, dest_port,
643 							      subs, false,
644 							      connector->number != dest_client->number);
645 				err = 0;
646 				break;
647 			}
648 		}
649 	}
650 	if (err < 0)
651 		return err;
652 
653 	delete_and_unsubscribe_port(src_client, src_port, subs, true,
654 				    connector->number != src_client->number);
655 	kfree_rcu(subs, rcu);
656 	return 0;
657 }
658 
659 
660 /* get matched subscriber */
661 int snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp,
662 				  struct snd_seq_addr *dest_addr,
663 				  struct snd_seq_port_subscribe *subs)
664 {
665 	struct snd_seq_subscribers *s;
666 	int err = -ENOENT;
667 
668 	guard(rwsem_read)(&src_grp->list_mutex);
669 	hlist_for_each_entry(s, &src_grp->list_head, src_list) {
670 		if (addr_match(dest_addr, &s->info.dest)) {
671 			*subs = s->info;
672 			err = 0;
673 			break;
674 		}
675 	}
676 	return err;
677 }
678 
679 /*
680  * Attach a device driver that wants to receive events from the
681  * sequencer.  Returns the new port number on success.
682  * A driver that wants to receive the events converted to midi, will
683  * use snd_seq_midisynth_register_port().
684  */
685 /* exported */
686 int snd_seq_event_port_attach(int client,
687 			      struct snd_seq_port_callback *pcbp,
688 			      int cap, int type, int midi_channels,
689 			      int midi_voices, char *portname)
690 {
691 	struct snd_seq_port_info portinfo;
692 	int  ret;
693 
694 	/* Set up the port */
695 	memset(&portinfo, 0, sizeof(portinfo));
696 	portinfo.addr.client = client;
697 	strscpy(portinfo.name, portname ? portname : "Unnamed port",
698 		sizeof(portinfo.name));
699 
700 	portinfo.capability = cap;
701 	portinfo.type = type;
702 	portinfo.kernel = pcbp;
703 	portinfo.midi_channels = midi_channels;
704 	portinfo.midi_voices = midi_voices;
705 
706 	/* Create it */
707 	ret = snd_seq_kernel_client_ctl(client,
708 					SNDRV_SEQ_IOCTL_CREATE_PORT,
709 					&portinfo);
710 
711 	if (ret >= 0)
712 		ret = portinfo.addr.port;
713 
714 	return ret;
715 }
716 EXPORT_SYMBOL(snd_seq_event_port_attach);
717 
718 /*
719  * Detach the driver from a port.
720  */
721 /* exported */
722 int snd_seq_event_port_detach(int client, int port)
723 {
724 	struct snd_seq_port_info portinfo;
725 	int  err;
726 
727 	memset(&portinfo, 0, sizeof(portinfo));
728 	portinfo.addr.client = client;
729 	portinfo.addr.port   = port;
730 	err = snd_seq_kernel_client_ctl(client,
731 					SNDRV_SEQ_IOCTL_DELETE_PORT,
732 					&portinfo);
733 
734 	return err;
735 }
736 EXPORT_SYMBOL(snd_seq_event_port_detach);
737