1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
5 * Jaroslav Kysela <perex@perex.cz>
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"
17 registration of client ports
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.
32 1) leave it this way, a client does normaly does not have more than a few
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.
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,
47 struct snd_seq_client_port *port;
51 read_lock(&client->ports_lock);
52 list_for_each_entry(port, &client->ports_list_head, list) {
53 if (port->addr.port == num) {
55 break; /* deleting now */
56 snd_use_lock_use(&port->use_lock);
57 read_unlock(&client->ports_lock);
61 read_unlock(&client->ports_lock);
62 return NULL; /* not found */
66 /* search for the next port - port is locked if found */
67 struct snd_seq_client_port *snd_seq_port_query_nearest(struct snd_seq_client *client,
68 struct snd_seq_port_info *pinfo)
71 struct snd_seq_client_port *port, *found;
72 bool check_inactive = (pinfo->capability & SNDRV_SEQ_PORT_CAP_INACTIVE);
74 num = pinfo->addr.port;
76 read_lock(&client->ports_lock);
77 list_for_each_entry(port, &client->ports_list_head, list) {
78 if ((port->capability & SNDRV_SEQ_PORT_CAP_INACTIVE) &&
80 continue; /* skip inactive ports */
81 if (port->addr.port < num)
83 if (port->addr.port == num) {
87 if (found == NULL || port->addr.port < found->addr.port)
94 snd_use_lock_use(&found->use_lock);
96 read_unlock(&client->ports_lock);
101 /* initialize snd_seq_port_subs_info */
102 static void port_subs_info_init(struct snd_seq_port_subs_info *grp)
104 INIT_LIST_HEAD(&grp->list_head);
107 rwlock_init(&grp->list_lock);
108 init_rwsem(&grp->list_mutex);
114 /* create a port, port number or a negative error code is returned
115 * the caller needs to unref the port via snd_seq_port_unlock() appropriately
117 int snd_seq_create_port(struct snd_seq_client *client, int port,
118 struct snd_seq_client_port **port_ret)
120 struct snd_seq_client_port *new_port, *p;
126 if (snd_BUG_ON(!client))
129 if (client->num_ports >= SNDRV_SEQ_MAX_PORTS) {
130 pr_warn("ALSA: seq: too many ports for client %d\n", client->number);
134 /* create a new port */
135 new_port = kzalloc(sizeof(*new_port), GFP_KERNEL);
137 return -ENOMEM; /* failure, out of memory */
139 new_port->addr.client = client->number;
140 new_port->addr.port = -1;
141 new_port->owner = THIS_MODULE;
142 snd_use_lock_init(&new_port->use_lock);
143 port_subs_info_init(&new_port->c_src);
144 port_subs_info_init(&new_port->c_dest);
145 snd_use_lock_use(&new_port->use_lock);
148 mutex_lock(&client->ports_mutex);
149 write_lock_irq(&client->ports_lock);
150 list_for_each_entry(p, &client->ports_list_head, list) {
151 if (p->addr.port == port) {
156 if (p->addr.port > num)
158 if (port < 0) /* auto-probe mode */
159 num = p->addr.port + 1;
161 /* insert the new port */
162 list_add_tail(&new_port->list, &p->list);
164 new_port->addr.port = num; /* store the port number in the port */
165 sprintf(new_port->name, "port-%d", num);
166 *port_ret = new_port;
168 write_unlock_irq(&client->ports_lock);
169 mutex_unlock(&client->ports_mutex);
175 static int subscribe_port(struct snd_seq_client *client,
176 struct snd_seq_client_port *port,
177 struct snd_seq_port_subs_info *grp,
178 struct snd_seq_port_subscribe *info, int send_ack);
179 static int unsubscribe_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);
185 static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr,
186 struct snd_seq_client **cp)
188 struct snd_seq_client_port *p;
189 *cp = snd_seq_client_use_ptr(addr->client);
191 p = snd_seq_port_use_ptr(*cp, addr->port);
193 snd_seq_client_unlock(*cp);
201 static void delete_and_unsubscribe_port(struct snd_seq_client *client,
202 struct snd_seq_client_port *port,
203 struct snd_seq_subscribers *subs,
204 bool is_src, bool ack);
206 static inline struct snd_seq_subscribers *
207 get_subscriber(struct list_head *p, bool is_src)
210 return list_entry(p, struct snd_seq_subscribers, src_list);
212 return list_entry(p, struct snd_seq_subscribers, dest_list);
216 * remove all subscribers on the list
217 * this is called from port_delete, for each src and dest list.
219 static void clear_subscriber_list(struct snd_seq_client *client,
220 struct snd_seq_client_port *port,
221 struct snd_seq_port_subs_info *grp,
224 struct list_head *p, *n;
226 list_for_each_safe(p, n, &grp->list_head) {
227 struct snd_seq_subscribers *subs;
228 struct snd_seq_client *c;
229 struct snd_seq_client_port *aport;
231 subs = get_subscriber(p, is_src);
233 aport = get_client_port(&subs->info.dest, &c);
235 aport = get_client_port(&subs->info.sender, &c);
236 delete_and_unsubscribe_port(client, port, subs, is_src, false);
239 /* looks like the connected port is being deleted.
240 * we decrease the counter, and when both ports are deleted
241 * remove the subscriber info
243 if (atomic_dec_and_test(&subs->ref_count))
248 /* ok we got the connected port */
249 delete_and_unsubscribe_port(c, aport, subs, !is_src, true);
251 snd_seq_port_unlock(aport);
252 snd_seq_client_unlock(c);
256 /* delete port data */
257 static int port_delete(struct snd_seq_client *client,
258 struct snd_seq_client_port *port)
260 /* set closing flag and wait for all port access are gone */
262 snd_use_lock_sync(&port->use_lock);
264 /* clear subscribers info */
265 clear_subscriber_list(client, port, &port->c_src, true);
266 clear_subscriber_list(client, port, &port->c_dest, false);
268 if (port->private_free)
269 port->private_free(port->private_data);
271 snd_BUG_ON(port->c_src.count != 0);
272 snd_BUG_ON(port->c_dest.count != 0);
279 /* delete a port with the given port id */
280 int snd_seq_delete_port(struct snd_seq_client *client, int port)
282 struct snd_seq_client_port *found = NULL, *p;
284 mutex_lock(&client->ports_mutex);
285 write_lock_irq(&client->ports_lock);
286 list_for_each_entry(p, &client->ports_list_head, list) {
287 if (p->addr.port == port) {
288 /* ok found. delete from the list at first */
295 write_unlock_irq(&client->ports_lock);
296 mutex_unlock(&client->ports_mutex);
298 return port_delete(client, found);
303 /* delete the all ports belonging to the given client */
304 int snd_seq_delete_all_ports(struct snd_seq_client *client)
306 struct list_head deleted_list;
307 struct snd_seq_client_port *port, *tmp;
309 /* move the port list to deleted_list, and
310 * clear the port list in the client data.
312 mutex_lock(&client->ports_mutex);
313 write_lock_irq(&client->ports_lock);
314 if (! list_empty(&client->ports_list_head)) {
315 list_add(&deleted_list, &client->ports_list_head);
316 list_del_init(&client->ports_list_head);
318 INIT_LIST_HEAD(&deleted_list);
320 client->num_ports = 0;
321 write_unlock_irq(&client->ports_lock);
323 /* remove each port in deleted_list */
324 list_for_each_entry_safe(port, tmp, &deleted_list, list) {
325 list_del(&port->list);
326 snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port);
327 port_delete(client, port);
329 mutex_unlock(&client->ports_mutex);
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)
337 if (snd_BUG_ON(!port || !info))
342 strscpy(port->name, info->name, sizeof(port->name));
344 /* set capabilities */
345 port->capability = info->capability;
348 port->type = info->type;
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;
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;
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)
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;
377 /* get port info fields */
378 int snd_seq_get_port_info(struct snd_seq_client_port * port,
379 struct snd_seq_port_info * info)
381 if (snd_BUG_ON(!port || !info))
385 strscpy(info->name, port->name, sizeof(info->name));
387 /* get capabilities */
388 info->capability = port->capability;
391 info->type = port->type;
393 /* information about supported channels/voices */
394 info->midi_channels = port->midi_channels;
395 info->midi_voices = port->midi_voices;
396 info->synth_voices = port->synth_voices;
398 /* get subscriber counts */
399 info->read_use = port->c_src.count;
400 info->write_use = port->c_dest.count;
404 if (port->timestamping) {
405 info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP;
407 info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL;
408 info->time_queue = port->time_queue;
411 /* UMP direction and group */
412 info->direction = port->direction;
413 info->ump_group = port->ump_group;
421 * call callback functions (if any):
422 * the callbacks are invoked only when the first (for connection) or
423 * the last subscription (for disconnection) is done. Second or later
424 * subscription results in increment of counter, but no callback is
426 * This feature is useful if these callbacks are associated with
427 * initialization or termination of devices (see seq_midi.c).
430 static int subscribe_port(struct snd_seq_client *client,
431 struct snd_seq_client_port *port,
432 struct snd_seq_port_subs_info *grp,
433 struct snd_seq_port_subscribe *info,
438 if (!try_module_get(port->owner))
441 if (grp->open && grp->count == 1) {
442 err = grp->open(port->private_data, info);
444 module_put(port->owner);
448 if (err >= 0 && send_ack && client->type == USER_CLIENT)
449 snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
450 info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
455 static int unsubscribe_port(struct snd_seq_client *client,
456 struct snd_seq_client_port *port,
457 struct snd_seq_port_subs_info *grp,
458 struct snd_seq_port_subscribe *info,
466 if (grp->close && grp->count == 0)
467 err = grp->close(port->private_data, info);
468 if (send_ack && client->type == USER_CLIENT)
469 snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
470 info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
471 module_put(port->owner);
477 /* check if both addresses are identical */
478 static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s)
480 return (r->client == s->client) && (r->port == s->port);
483 /* check the two subscribe info match */
484 /* if flags is zero, checks only sender and destination addresses */
485 static int match_subs_info(struct snd_seq_port_subscribe *r,
486 struct snd_seq_port_subscribe *s)
488 if (addr_match(&r->sender, &s->sender) &&
489 addr_match(&r->dest, &s->dest)) {
490 if (r->flags && r->flags == s->flags)
491 return r->queue == s->queue;
498 static int check_and_subscribe_port(struct snd_seq_client *client,
499 struct snd_seq_client_port *port,
500 struct snd_seq_subscribers *subs,
501 bool is_src, bool exclusive, bool ack)
503 struct snd_seq_port_subs_info *grp;
505 struct snd_seq_subscribers *s;
508 grp = is_src ? &port->c_src : &port->c_dest;
510 down_write(&grp->list_mutex);
512 if (!list_empty(&grp->list_head))
517 /* check whether already exists */
518 list_for_each(p, &grp->list_head) {
519 s = get_subscriber(p, is_src);
520 if (match_subs_info(&subs->info, &s->info))
525 err = subscribe_port(client, port, grp, &subs->info, ack);
532 write_lock_irq(&grp->list_lock);
534 list_add_tail(&subs->src_list, &grp->list_head);
536 list_add_tail(&subs->dest_list, &grp->list_head);
537 grp->exclusive = exclusive;
538 atomic_inc(&subs->ref_count);
539 write_unlock_irq(&grp->list_lock);
543 up_write(&grp->list_mutex);
547 /* called with grp->list_mutex held */
548 static void __delete_and_unsubscribe_port(struct snd_seq_client *client,
549 struct snd_seq_client_port *port,
550 struct snd_seq_subscribers *subs,
551 bool is_src, bool ack)
553 struct snd_seq_port_subs_info *grp;
554 struct list_head *list;
557 grp = is_src ? &port->c_src : &port->c_dest;
558 list = is_src ? &subs->src_list : &subs->dest_list;
559 write_lock_irq(&grp->list_lock);
560 empty = list_empty(list);
564 write_unlock_irq(&grp->list_lock);
567 unsubscribe_port(client, port, grp, &subs->info, ack);
570 static void delete_and_unsubscribe_port(struct snd_seq_client *client,
571 struct snd_seq_client_port *port,
572 struct snd_seq_subscribers *subs,
573 bool is_src, bool ack)
575 struct snd_seq_port_subs_info *grp;
577 grp = is_src ? &port->c_src : &port->c_dest;
578 down_write(&grp->list_mutex);
579 __delete_and_unsubscribe_port(client, port, subs, is_src, ack);
580 up_write(&grp->list_mutex);
583 /* connect two ports */
584 int snd_seq_port_connect(struct snd_seq_client *connector,
585 struct snd_seq_client *src_client,
586 struct snd_seq_client_port *src_port,
587 struct snd_seq_client *dest_client,
588 struct snd_seq_client_port *dest_port,
589 struct snd_seq_port_subscribe *info)
591 struct snd_seq_subscribers *subs;
595 subs = kzalloc(sizeof(*subs), GFP_KERNEL);
600 atomic_set(&subs->ref_count, 0);
601 INIT_LIST_HEAD(&subs->src_list);
602 INIT_LIST_HEAD(&subs->dest_list);
604 exclusive = !!(info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE);
606 err = check_and_subscribe_port(src_client, src_port, subs, true,
608 connector->number != src_client->number);
611 err = check_and_subscribe_port(dest_client, dest_port, subs, false,
613 connector->number != dest_client->number);
620 delete_and_unsubscribe_port(src_client, src_port, subs, true,
621 connector->number != src_client->number);
627 /* remove the connection */
628 int snd_seq_port_disconnect(struct snd_seq_client *connector,
629 struct snd_seq_client *src_client,
630 struct snd_seq_client_port *src_port,
631 struct snd_seq_client *dest_client,
632 struct snd_seq_client_port *dest_port,
633 struct snd_seq_port_subscribe *info)
635 struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
636 struct snd_seq_subscribers *subs;
639 /* always start from deleting the dest port for avoiding concurrent
642 down_write(&dest->list_mutex);
643 /* look for the connection */
644 list_for_each_entry(subs, &dest->list_head, dest_list) {
645 if (match_subs_info(info, &subs->info)) {
646 __delete_and_unsubscribe_port(dest_client, dest_port,
648 connector->number != dest_client->number);
653 up_write(&dest->list_mutex);
657 delete_and_unsubscribe_port(src_client, src_port, subs, true,
658 connector->number != src_client->number);
664 /* get matched subscriber */
665 int snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp,
666 struct snd_seq_addr *dest_addr,
667 struct snd_seq_port_subscribe *subs)
669 struct snd_seq_subscribers *s;
672 down_read(&src_grp->list_mutex);
673 list_for_each_entry(s, &src_grp->list_head, src_list) {
674 if (addr_match(dest_addr, &s->info.dest)) {
680 up_read(&src_grp->list_mutex);
685 * Attach a device driver that wants to receive events from the
686 * sequencer. Returns the new port number on success.
687 * A driver that wants to receive the events converted to midi, will
688 * use snd_seq_midisynth_register_port().
691 int snd_seq_event_port_attach(int client,
692 struct snd_seq_port_callback *pcbp,
693 int cap, int type, int midi_channels,
694 int midi_voices, char *portname)
696 struct snd_seq_port_info portinfo;
699 /* Set up the port */
700 memset(&portinfo, 0, sizeof(portinfo));
701 portinfo.addr.client = client;
702 strscpy(portinfo.name, portname ? portname : "Unnamed port",
703 sizeof(portinfo.name));
705 portinfo.capability = cap;
706 portinfo.type = type;
707 portinfo.kernel = pcbp;
708 portinfo.midi_channels = midi_channels;
709 portinfo.midi_voices = midi_voices;
712 ret = snd_seq_kernel_client_ctl(client,
713 SNDRV_SEQ_IOCTL_CREATE_PORT,
717 ret = portinfo.addr.port;
721 EXPORT_SYMBOL(snd_seq_event_port_attach);
724 * Detach the driver from a port.
727 int snd_seq_event_port_detach(int client, int port)
729 struct snd_seq_port_info portinfo;
732 memset(&portinfo, 0, sizeof(portinfo));
733 portinfo.addr.client = client;
734 portinfo.addr.port = port;
735 err = snd_seq_kernel_client_ctl(client,
736 SNDRV_SEQ_IOCTL_DELETE_PORT,
741 EXPORT_SYMBOL(snd_seq_event_port_detach);