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