• 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 		snd_printk(KERN_WARNING "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 		snd_printd("malloc failed for registering client port\n");
148 		return NULL;	/* failure, out of memory */
149 	}
150 	/* init port data */
151 	new_port->addr.client = client->number;
152 	new_port->addr.port = -1;
153 	new_port->owner = THIS_MODULE;
154 	sprintf(new_port->name, "port-%d", num);
155 	snd_use_lock_init(&new_port->use_lock);
156 	port_subs_info_init(&new_port->c_src);
157 	port_subs_info_init(&new_port->c_dest);
158 	snd_use_lock_use(&new_port->use_lock);
159 
160 	num = port >= 0 ? port : 0;
161 	mutex_lock(&client->ports_mutex);
162 	write_lock_irqsave(&client->ports_lock, flags);
163 	list_for_each_entry(p, &client->ports_list_head, list) {
164 		if (p->addr.port > num)
165 			break;
166 		if (port < 0) /* auto-probe mode */
167 			num = p->addr.port + 1;
168 	}
169 	/* insert the new port */
170 	list_add_tail(&new_port->list, &p->list);
171 	client->num_ports++;
172 	new_port->addr.port = num;	/* store the port number in the port */
173 	sprintf(new_port->name, "port-%d", num);
174 	write_unlock_irqrestore(&client->ports_lock, flags);
175 	mutex_unlock(&client->ports_mutex);
176 
177 	return new_port;
178 }
179 
180 /* */
181 enum group_type {
182 	SRC_LIST, DEST_LIST
183 };
184 
185 static int subscribe_port(struct snd_seq_client *client,
186 			  struct snd_seq_client_port *port,
187 			  struct snd_seq_port_subs_info *grp,
188 			  struct snd_seq_port_subscribe *info, int send_ack);
189 static int unsubscribe_port(struct snd_seq_client *client,
190 			    struct snd_seq_client_port *port,
191 			    struct snd_seq_port_subs_info *grp,
192 			    struct snd_seq_port_subscribe *info, int send_ack);
193 
194 
get_client_port(struct snd_seq_addr * addr,struct snd_seq_client ** cp)195 static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr,
196 						   struct snd_seq_client **cp)
197 {
198 	struct snd_seq_client_port *p;
199 	*cp = snd_seq_client_use_ptr(addr->client);
200 	if (*cp) {
201 		p = snd_seq_port_use_ptr(*cp, addr->port);
202 		if (! p) {
203 			snd_seq_client_unlock(*cp);
204 			*cp = NULL;
205 		}
206 		return p;
207 	}
208 	return NULL;
209 }
210 
211 /*
212  * remove all subscribers on the list
213  * this is called from port_delete, for each src and dest list.
214  */
clear_subscriber_list(struct snd_seq_client * client,struct snd_seq_client_port * port,struct snd_seq_port_subs_info * grp,int grptype)215 static void clear_subscriber_list(struct snd_seq_client *client,
216 				  struct snd_seq_client_port *port,
217 				  struct snd_seq_port_subs_info *grp,
218 				  int grptype)
219 {
220 	struct list_head *p, *n;
221 
222 	list_for_each_safe(p, n, &grp->list_head) {
223 		struct snd_seq_subscribers *subs;
224 		struct snd_seq_client *c;
225 		struct snd_seq_client_port *aport;
226 
227 		if (grptype == SRC_LIST) {
228 			subs = list_entry(p, struct snd_seq_subscribers, src_list);
229 			aport = get_client_port(&subs->info.dest, &c);
230 		} else {
231 			subs = list_entry(p, struct snd_seq_subscribers, dest_list);
232 			aport = get_client_port(&subs->info.sender, &c);
233 		}
234 		list_del(p);
235 		unsubscribe_port(client, port, grp, &subs->info, 0);
236 		if (!aport) {
237 			/* looks like the connected port is being deleted.
238 			 * we decrease the counter, and when both ports are deleted
239 			 * remove the subscriber info
240 			 */
241 			if (atomic_dec_and_test(&subs->ref_count))
242 				kfree(subs);
243 		} else {
244 			/* ok we got the connected port */
245 			struct snd_seq_port_subs_info *agrp;
246 			agrp = (grptype == SRC_LIST) ? &aport->c_dest : &aport->c_src;
247 			down_write(&agrp->list_mutex);
248 			if (grptype == SRC_LIST)
249 				list_del(&subs->dest_list);
250 			else
251 				list_del(&subs->src_list);
252 			up_write(&agrp->list_mutex);
253 			unsubscribe_port(c, aport, agrp, &subs->info, 1);
254 			kfree(subs);
255 			snd_seq_port_unlock(aport);
256 			snd_seq_client_unlock(c);
257 		}
258 	}
259 }
260 
261 /* delete port data */
port_delete(struct snd_seq_client * client,struct snd_seq_client_port * port)262 static int port_delete(struct snd_seq_client *client,
263 		       struct snd_seq_client_port *port)
264 {
265 	/* set closing flag and wait for all port access are gone */
266 	port->closing = 1;
267 	snd_use_lock_sync(&port->use_lock);
268 
269 	/* clear subscribers info */
270 	clear_subscriber_list(client, port, &port->c_src, SRC_LIST);
271 	clear_subscriber_list(client, port, &port->c_dest, DEST_LIST);
272 
273 	if (port->private_free)
274 		port->private_free(port->private_data);
275 
276 	snd_BUG_ON(port->c_src.count != 0);
277 	snd_BUG_ON(port->c_dest.count != 0);
278 
279 	kfree(port);
280 	return 0;
281 }
282 
283 
284 /* delete a port with the given port id */
snd_seq_delete_port(struct snd_seq_client * client,int port)285 int snd_seq_delete_port(struct snd_seq_client *client, int port)
286 {
287 	unsigned long flags;
288 	struct snd_seq_client_port *found = NULL, *p;
289 
290 	mutex_lock(&client->ports_mutex);
291 	write_lock_irqsave(&client->ports_lock, flags);
292 	list_for_each_entry(p, &client->ports_list_head, list) {
293 		if (p->addr.port == port) {
294 			/* ok found.  delete from the list at first */
295 			list_del(&p->list);
296 			client->num_ports--;
297 			found = p;
298 			break;
299 		}
300 	}
301 	write_unlock_irqrestore(&client->ports_lock, flags);
302 	mutex_unlock(&client->ports_mutex);
303 	if (found)
304 		return port_delete(client, found);
305 	else
306 		return -ENOENT;
307 }
308 
309 /* delete the all ports belonging to the given client */
snd_seq_delete_all_ports(struct snd_seq_client * client)310 int snd_seq_delete_all_ports(struct snd_seq_client *client)
311 {
312 	unsigned long flags;
313 	struct list_head deleted_list;
314 	struct snd_seq_client_port *port, *tmp;
315 
316 	/* move the port list to deleted_list, and
317 	 * clear the port list in the client data.
318 	 */
319 	mutex_lock(&client->ports_mutex);
320 	write_lock_irqsave(&client->ports_lock, flags);
321 	if (! list_empty(&client->ports_list_head)) {
322 		list_add(&deleted_list, &client->ports_list_head);
323 		list_del_init(&client->ports_list_head);
324 	} else {
325 		INIT_LIST_HEAD(&deleted_list);
326 	}
327 	client->num_ports = 0;
328 	write_unlock_irqrestore(&client->ports_lock, flags);
329 
330 	/* remove each port in deleted_list */
331 	list_for_each_entry_safe(port, tmp, &deleted_list, list) {
332 		list_del(&port->list);
333 		snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port);
334 		port_delete(client, port);
335 	}
336 	mutex_unlock(&client->ports_mutex);
337 	return 0;
338 }
339 
340 /* set port info fields */
snd_seq_set_port_info(struct snd_seq_client_port * port,struct snd_seq_port_info * info)341 int snd_seq_set_port_info(struct snd_seq_client_port * port,
342 			  struct snd_seq_port_info * info)
343 {
344 	if (snd_BUG_ON(!port || !info))
345 		return -EINVAL;
346 
347 	/* set port name */
348 	if (info->name[0])
349 		strlcpy(port->name, info->name, sizeof(port->name));
350 
351 	/* set capabilities */
352 	port->capability = info->capability;
353 
354 	/* get port type */
355 	port->type = info->type;
356 
357 	/* information about supported channels/voices */
358 	port->midi_channels = info->midi_channels;
359 	port->midi_voices = info->midi_voices;
360 	port->synth_voices = info->synth_voices;
361 
362 	/* timestamping */
363 	port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0;
364 	port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0;
365 	port->time_queue = info->time_queue;
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 	strlcpy(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 	return 0;
405 }
406 
407 
408 
409 /*
410  * call callback functions (if any):
411  * the callbacks are invoked only when the first (for connection) or
412  * the last subscription (for disconnection) is done.  Second or later
413  * subscription results in increment of counter, but no callback is
414  * invoked.
415  * This feature is useful if these callbacks are associated with
416  * initialization or termination of devices (see seq_midi.c).
417  *
418  * If callback_all option is set, the callback function is invoked
419  * at each connection/disconnection.
420  */
421 
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)422 static int subscribe_port(struct snd_seq_client *client,
423 			  struct snd_seq_client_port *port,
424 			  struct snd_seq_port_subs_info *grp,
425 			  struct snd_seq_port_subscribe *info,
426 			  int send_ack)
427 {
428 	int err = 0;
429 
430 	if (!try_module_get(port->owner))
431 		return -EFAULT;
432 	grp->count++;
433 	if (grp->open && (port->callback_all || grp->count == 1)) {
434 		err = grp->open(port->private_data, info);
435 		if (err < 0) {
436 			module_put(port->owner);
437 			grp->count--;
438 		}
439 	}
440 	if (err >= 0 && send_ack && client->type == USER_CLIENT)
441 		snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
442 						   info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
443 
444 	return err;
445 }
446 
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)447 static int unsubscribe_port(struct snd_seq_client *client,
448 			    struct snd_seq_client_port *port,
449 			    struct snd_seq_port_subs_info *grp,
450 			    struct snd_seq_port_subscribe *info,
451 			    int send_ack)
452 {
453 	int err = 0;
454 
455 	if (! grp->count)
456 		return -EINVAL;
457 	grp->count--;
458 	if (grp->close && (port->callback_all || grp->count == 0))
459 		err = grp->close(port->private_data, info);
460 	if (send_ack && client->type == USER_CLIENT)
461 		snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
462 						   info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
463 	module_put(port->owner);
464 	return err;
465 }
466 
467 
468 
469 /* check if both addresses are identical */
addr_match(struct snd_seq_addr * r,struct snd_seq_addr * s)470 static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s)
471 {
472 	return (r->client == s->client) && (r->port == s->port);
473 }
474 
475 /* check the two subscribe info match */
476 /* 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)477 static int match_subs_info(struct snd_seq_port_subscribe *r,
478 			   struct snd_seq_port_subscribe *s)
479 {
480 	if (addr_match(&r->sender, &s->sender) &&
481 	    addr_match(&r->dest, &s->dest)) {
482 		if (r->flags && r->flags == s->flags)
483 			return r->queue == s->queue;
484 		else if (! r->flags)
485 			return 1;
486 	}
487 	return 0;
488 }
489 
490 
491 /* 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)492 int snd_seq_port_connect(struct snd_seq_client *connector,
493 			 struct snd_seq_client *src_client,
494 			 struct snd_seq_client_port *src_port,
495 			 struct snd_seq_client *dest_client,
496 			 struct snd_seq_client_port *dest_port,
497 			 struct snd_seq_port_subscribe *info)
498 {
499 	struct snd_seq_port_subs_info *src = &src_port->c_src;
500 	struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
501 	struct snd_seq_subscribers *subs, *s;
502 	int err, src_called = 0;
503 	unsigned long flags;
504 	int exclusive;
505 
506 	subs = kzalloc(sizeof(*subs), GFP_KERNEL);
507 	if (! subs)
508 		return -ENOMEM;
509 
510 	subs->info = *info;
511 	atomic_set(&subs->ref_count, 2);
512 
513 	down_write(&src->list_mutex);
514 	down_write_nested(&dest->list_mutex, SINGLE_DEPTH_NESTING);
515 
516 	exclusive = info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE ? 1 : 0;
517 	err = -EBUSY;
518 	if (exclusive) {
519 		if (! list_empty(&src->list_head) || ! list_empty(&dest->list_head))
520 			goto __error;
521 	} else {
522 		if (src->exclusive || dest->exclusive)
523 			goto __error;
524 		/* check whether already exists */
525 		list_for_each_entry(s, &src->list_head, src_list) {
526 			if (match_subs_info(info, &s->info))
527 				goto __error;
528 		}
529 		list_for_each_entry(s, &dest->list_head, dest_list) {
530 			if (match_subs_info(info, &s->info))
531 				goto __error;
532 		}
533 	}
534 
535 	if ((err = subscribe_port(src_client, src_port, src, info,
536 				  connector->number != src_client->number)) < 0)
537 		goto __error;
538 	src_called = 1;
539 
540 	if ((err = subscribe_port(dest_client, dest_port, dest, info,
541 				  connector->number != dest_client->number)) < 0)
542 		goto __error;
543 
544 	/* add to list */
545 	write_lock_irqsave(&src->list_lock, flags);
546 	// write_lock(&dest->list_lock); // no other lock yet
547 	list_add_tail(&subs->src_list, &src->list_head);
548 	list_add_tail(&subs->dest_list, &dest->list_head);
549 	// write_unlock(&dest->list_lock); // no other lock yet
550 	write_unlock_irqrestore(&src->list_lock, flags);
551 
552 	src->exclusive = dest->exclusive = exclusive;
553 
554 	up_write(&dest->list_mutex);
555 	up_write(&src->list_mutex);
556 	return 0;
557 
558  __error:
559 	if (src_called)
560 		unsubscribe_port(src_client, src_port, src, info,
561 				 connector->number != src_client->number);
562 	kfree(subs);
563 	up_write(&dest->list_mutex);
564 	up_write(&src->list_mutex);
565 	return err;
566 }
567 
568 
569 /* 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)570 int snd_seq_port_disconnect(struct snd_seq_client *connector,
571 			    struct snd_seq_client *src_client,
572 			    struct snd_seq_client_port *src_port,
573 			    struct snd_seq_client *dest_client,
574 			    struct snd_seq_client_port *dest_port,
575 			    struct snd_seq_port_subscribe *info)
576 {
577 	struct snd_seq_port_subs_info *src = &src_port->c_src;
578 	struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
579 	struct snd_seq_subscribers *subs;
580 	int err = -ENOENT;
581 	unsigned long flags;
582 
583 	down_write(&src->list_mutex);
584 	down_write_nested(&dest->list_mutex, SINGLE_DEPTH_NESTING);
585 
586 	/* look for the connection */
587 	list_for_each_entry(subs, &src->list_head, src_list) {
588 		if (match_subs_info(info, &subs->info)) {
589 			write_lock_irqsave(&src->list_lock, flags);
590 			// write_lock(&dest->list_lock);  // no lock yet
591 			list_del(&subs->src_list);
592 			list_del(&subs->dest_list);
593 			// write_unlock(&dest->list_lock);
594 			write_unlock_irqrestore(&src->list_lock, flags);
595 			src->exclusive = dest->exclusive = 0;
596 			unsubscribe_port(src_client, src_port, src, info,
597 					 connector->number != src_client->number);
598 			unsubscribe_port(dest_client, dest_port, dest, info,
599 					 connector->number != dest_client->number);
600 			kfree(subs);
601 			err = 0;
602 			break;
603 		}
604 	}
605 
606 	up_write(&dest->list_mutex);
607 	up_write(&src->list_mutex);
608 	return err;
609 }
610 
611 
612 /* get matched subscriber */
snd_seq_port_get_subscription(struct snd_seq_port_subs_info * src_grp,struct snd_seq_addr * dest_addr)613 struct snd_seq_subscribers *snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp,
614 							  struct snd_seq_addr *dest_addr)
615 {
616 	struct snd_seq_subscribers *s, *found = NULL;
617 
618 	down_read(&src_grp->list_mutex);
619 	list_for_each_entry(s, &src_grp->list_head, src_list) {
620 		if (addr_match(dest_addr, &s->info.dest)) {
621 			found = s;
622 			break;
623 		}
624 	}
625 	up_read(&src_grp->list_mutex);
626 	return found;
627 }
628 
629 /*
630  * Attach a device driver that wants to receive events from the
631  * sequencer.  Returns the new port number on success.
632  * A driver that wants to receive the events converted to midi, will
633  * use snd_seq_midisynth_register_port().
634  */
635 /* 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)636 int snd_seq_event_port_attach(int client,
637 			      struct snd_seq_port_callback *pcbp,
638 			      int cap, int type, int midi_channels,
639 			      int midi_voices, char *portname)
640 {
641 	struct snd_seq_port_info portinfo;
642 	int  ret;
643 
644 	/* Set up the port */
645 	memset(&portinfo, 0, sizeof(portinfo));
646 	portinfo.addr.client = client;
647 	strlcpy(portinfo.name, portname ? portname : "Unamed port",
648 		sizeof(portinfo.name));
649 
650 	portinfo.capability = cap;
651 	portinfo.type = type;
652 	portinfo.kernel = pcbp;
653 	portinfo.midi_channels = midi_channels;
654 	portinfo.midi_voices = midi_voices;
655 
656 	/* Create it */
657 	ret = snd_seq_kernel_client_ctl(client,
658 					SNDRV_SEQ_IOCTL_CREATE_PORT,
659 					&portinfo);
660 
661 	if (ret >= 0)
662 		ret = portinfo.addr.port;
663 
664 	return ret;
665 }
666 
667 EXPORT_SYMBOL(snd_seq_event_port_attach);
668 
669 /*
670  * Detach the driver from a port.
671  */
672 /* exported */
snd_seq_event_port_detach(int client,int port)673 int snd_seq_event_port_detach(int client, int port)
674 {
675 	struct snd_seq_port_info portinfo;
676 	int  err;
677 
678 	memset(&portinfo, 0, sizeof(portinfo));
679 	portinfo.addr.client = client;
680 	portinfo.addr.port   = port;
681 	err = snd_seq_kernel_client_ctl(client,
682 					SNDRV_SEQ_IOCTL_DELETE_PORT,
683 					&portinfo);
684 
685 	return err;
686 }
687 
688 EXPORT_SYMBOL(snd_seq_event_port_detach);
689