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;
73 num = pinfo->addr.port;
75 read_lock(&client->ports_lock);
76 list_for_each_entry(port, &client->ports_list_head, list) {
77 if (port->addr.port < num)
79 if (port->addr.port == num) {
83 if (found == NULL || port->addr.port < found->addr.port)
90 snd_use_lock_use(&found->use_lock);
92 read_unlock(&client->ports_lock);
97 /* initialize snd_seq_port_subs_info */
98 static void port_subs_info_init(struct snd_seq_port_subs_info *grp)
100 INIT_LIST_HEAD(&grp->list_head);
103 rwlock_init(&grp->list_lock);
104 init_rwsem(&grp->list_mutex);
110 /* create a port, port number is returned (-1 on failure);
111 * the caller needs to unref the port via snd_seq_port_unlock() appropriately
113 struct snd_seq_client_port *snd_seq_create_port(struct snd_seq_client *client,
116 struct snd_seq_client_port *new_port, *p;
120 if (snd_BUG_ON(!client))
123 if (client->num_ports >= SNDRV_SEQ_MAX_PORTS) {
124 pr_warn("ALSA: seq: too many ports for client %d\n", client->number);
128 /* create a new port */
129 new_port = kzalloc(sizeof(*new_port), GFP_KERNEL);
131 return NULL; /* failure, out of memory */
133 new_port->addr.client = client->number;
134 new_port->addr.port = -1;
135 new_port->owner = THIS_MODULE;
136 sprintf(new_port->name, "port-%d", num);
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);
142 num = port >= 0 ? port : 0;
143 mutex_lock(&client->ports_mutex);
144 write_lock_irq(&client->ports_lock);
145 list_for_each_entry(p, &client->ports_list_head, list) {
146 if (p->addr.port > num)
148 if (port < 0) /* auto-probe mode */
149 num = p->addr.port + 1;
151 /* insert the new port */
152 list_add_tail(&new_port->list, &p->list);
154 new_port->addr.port = num; /* store the port number in the port */
155 sprintf(new_port->name, "port-%d", num);
156 write_unlock_irq(&client->ports_lock);
157 mutex_unlock(&client->ports_mutex);
163 static int subscribe_port(struct snd_seq_client *client,
164 struct snd_seq_client_port *port,
165 struct snd_seq_port_subs_info *grp,
166 struct snd_seq_port_subscribe *info, int send_ack);
167 static int unsubscribe_port(struct snd_seq_client *client,
168 struct snd_seq_client_port *port,
169 struct snd_seq_port_subs_info *grp,
170 struct snd_seq_port_subscribe *info, int send_ack);
173 static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr,
174 struct snd_seq_client **cp)
176 struct snd_seq_client_port *p;
177 *cp = snd_seq_client_use_ptr(addr->client);
179 p = snd_seq_port_use_ptr(*cp, addr->port);
181 snd_seq_client_unlock(*cp);
189 static void delete_and_unsubscribe_port(struct snd_seq_client *client,
190 struct snd_seq_client_port *port,
191 struct snd_seq_subscribers *subs,
192 bool is_src, bool ack);
194 static inline struct snd_seq_subscribers *
195 get_subscriber(struct list_head *p, bool is_src)
198 return list_entry(p, struct snd_seq_subscribers, src_list);
200 return list_entry(p, struct snd_seq_subscribers, dest_list);
204 * remove all subscribers on the list
205 * this is called from port_delete, for each src and dest list.
207 static void clear_subscriber_list(struct snd_seq_client *client,
208 struct snd_seq_client_port *port,
209 struct snd_seq_port_subs_info *grp,
212 struct list_head *p, *n;
214 list_for_each_safe(p, n, &grp->list_head) {
215 struct snd_seq_subscribers *subs;
216 struct snd_seq_client *c;
217 struct snd_seq_client_port *aport;
219 subs = get_subscriber(p, is_src);
221 aport = get_client_port(&subs->info.dest, &c);
223 aport = get_client_port(&subs->info.sender, &c);
224 delete_and_unsubscribe_port(client, port, subs, is_src, false);
227 /* looks like the connected port is being deleted.
228 * we decrease the counter, and when both ports are deleted
229 * remove the subscriber info
231 if (atomic_dec_and_test(&subs->ref_count))
236 /* ok we got the connected port */
237 delete_and_unsubscribe_port(c, aport, subs, !is_src, true);
239 snd_seq_port_unlock(aport);
240 snd_seq_client_unlock(c);
244 /* delete port data */
245 static int port_delete(struct snd_seq_client *client,
246 struct snd_seq_client_port *port)
248 /* set closing flag and wait for all port access are gone */
250 snd_use_lock_sync(&port->use_lock);
252 /* clear subscribers info */
253 clear_subscriber_list(client, port, &port->c_src, true);
254 clear_subscriber_list(client, port, &port->c_dest, false);
256 if (port->private_free)
257 port->private_free(port->private_data);
259 snd_BUG_ON(port->c_src.count != 0);
260 snd_BUG_ON(port->c_dest.count != 0);
267 /* delete a port with the given port id */
268 int snd_seq_delete_port(struct snd_seq_client *client, int port)
270 struct snd_seq_client_port *found = NULL, *p;
272 mutex_lock(&client->ports_mutex);
273 write_lock_irq(&client->ports_lock);
274 list_for_each_entry(p, &client->ports_list_head, list) {
275 if (p->addr.port == port) {
276 /* ok found. delete from the list at first */
283 write_unlock_irq(&client->ports_lock);
284 mutex_unlock(&client->ports_mutex);
286 return port_delete(client, found);
291 /* delete the all ports belonging to the given client */
292 int snd_seq_delete_all_ports(struct snd_seq_client *client)
294 struct list_head deleted_list;
295 struct snd_seq_client_port *port, *tmp;
297 /* move the port list to deleted_list, and
298 * clear the port list in the client data.
300 mutex_lock(&client->ports_mutex);
301 write_lock_irq(&client->ports_lock);
302 if (! list_empty(&client->ports_list_head)) {
303 list_add(&deleted_list, &client->ports_list_head);
304 list_del_init(&client->ports_list_head);
306 INIT_LIST_HEAD(&deleted_list);
308 client->num_ports = 0;
309 write_unlock_irq(&client->ports_lock);
311 /* remove each port in deleted_list */
312 list_for_each_entry_safe(port, tmp, &deleted_list, list) {
313 list_del(&port->list);
314 snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port);
315 port_delete(client, port);
317 mutex_unlock(&client->ports_mutex);
321 /* set port info fields */
322 int snd_seq_set_port_info(struct snd_seq_client_port * port,
323 struct snd_seq_port_info * info)
325 if (snd_BUG_ON(!port || !info))
330 strlcpy(port->name, info->name, sizeof(port->name));
332 /* set capabilities */
333 port->capability = info->capability;
336 port->type = info->type;
338 /* information about supported channels/voices */
339 port->midi_channels = info->midi_channels;
340 port->midi_voices = info->midi_voices;
341 port->synth_voices = info->synth_voices;
344 port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0;
345 port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0;
346 port->time_queue = info->time_queue;
351 /* get port info fields */
352 int snd_seq_get_port_info(struct snd_seq_client_port * port,
353 struct snd_seq_port_info * info)
355 if (snd_BUG_ON(!port || !info))
359 strlcpy(info->name, port->name, sizeof(info->name));
361 /* get capabilities */
362 info->capability = port->capability;
365 info->type = port->type;
367 /* information about supported channels/voices */
368 info->midi_channels = port->midi_channels;
369 info->midi_voices = port->midi_voices;
370 info->synth_voices = port->synth_voices;
372 /* get subscriber counts */
373 info->read_use = port->c_src.count;
374 info->write_use = port->c_dest.count;
378 if (port->timestamping) {
379 info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP;
381 info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL;
382 info->time_queue = port->time_queue;
391 * call callback functions (if any):
392 * the callbacks are invoked only when the first (for connection) or
393 * the last subscription (for disconnection) is done. Second or later
394 * subscription results in increment of counter, but no callback is
396 * This feature is useful if these callbacks are associated with
397 * initialization or termination of devices (see seq_midi.c).
400 static int subscribe_port(struct snd_seq_client *client,
401 struct snd_seq_client_port *port,
402 struct snd_seq_port_subs_info *grp,
403 struct snd_seq_port_subscribe *info,
408 if (!try_module_get(port->owner))
411 if (grp->open && grp->count == 1) {
412 err = grp->open(port->private_data, info);
414 module_put(port->owner);
418 if (err >= 0 && send_ack && client->type == USER_CLIENT)
419 snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
420 info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
425 static int unsubscribe_port(struct snd_seq_client *client,
426 struct snd_seq_client_port *port,
427 struct snd_seq_port_subs_info *grp,
428 struct snd_seq_port_subscribe *info,
436 if (grp->close && grp->count == 0)
437 err = grp->close(port->private_data, info);
438 if (send_ack && client->type == USER_CLIENT)
439 snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
440 info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
441 module_put(port->owner);
447 /* check if both addresses are identical */
448 static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s)
450 return (r->client == s->client) && (r->port == s->port);
453 /* check the two subscribe info match */
454 /* if flags is zero, checks only sender and destination addresses */
455 static int match_subs_info(struct snd_seq_port_subscribe *r,
456 struct snd_seq_port_subscribe *s)
458 if (addr_match(&r->sender, &s->sender) &&
459 addr_match(&r->dest, &s->dest)) {
460 if (r->flags && r->flags == s->flags)
461 return r->queue == s->queue;
468 static int check_and_subscribe_port(struct snd_seq_client *client,
469 struct snd_seq_client_port *port,
470 struct snd_seq_subscribers *subs,
471 bool is_src, bool exclusive, bool ack)
473 struct snd_seq_port_subs_info *grp;
475 struct snd_seq_subscribers *s;
478 grp = is_src ? &port->c_src : &port->c_dest;
480 down_write(&grp->list_mutex);
482 if (!list_empty(&grp->list_head))
487 /* check whether already exists */
488 list_for_each(p, &grp->list_head) {
489 s = get_subscriber(p, is_src);
490 if (match_subs_info(&subs->info, &s->info))
495 err = subscribe_port(client, port, grp, &subs->info, ack);
502 write_lock_irq(&grp->list_lock);
504 list_add_tail(&subs->src_list, &grp->list_head);
506 list_add_tail(&subs->dest_list, &grp->list_head);
507 grp->exclusive = exclusive;
508 atomic_inc(&subs->ref_count);
509 write_unlock_irq(&grp->list_lock);
513 up_write(&grp->list_mutex);
517 /* called with grp->list_mutex held */
518 static void __delete_and_unsubscribe_port(struct snd_seq_client *client,
519 struct snd_seq_client_port *port,
520 struct snd_seq_subscribers *subs,
521 bool is_src, bool ack)
523 struct snd_seq_port_subs_info *grp;
524 struct list_head *list;
527 grp = is_src ? &port->c_src : &port->c_dest;
528 list = is_src ? &subs->src_list : &subs->dest_list;
529 write_lock_irq(&grp->list_lock);
530 empty = list_empty(list);
534 write_unlock_irq(&grp->list_lock);
537 unsubscribe_port(client, port, grp, &subs->info, ack);
540 static void delete_and_unsubscribe_port(struct snd_seq_client *client,
541 struct snd_seq_client_port *port,
542 struct snd_seq_subscribers *subs,
543 bool is_src, bool ack)
545 struct snd_seq_port_subs_info *grp;
547 grp = is_src ? &port->c_src : &port->c_dest;
548 down_write(&grp->list_mutex);
549 __delete_and_unsubscribe_port(client, port, subs, is_src, ack);
550 up_write(&grp->list_mutex);
553 /* connect two ports */
554 int snd_seq_port_connect(struct snd_seq_client *connector,
555 struct snd_seq_client *src_client,
556 struct snd_seq_client_port *src_port,
557 struct snd_seq_client *dest_client,
558 struct snd_seq_client_port *dest_port,
559 struct snd_seq_port_subscribe *info)
561 struct snd_seq_subscribers *subs;
565 subs = kzalloc(sizeof(*subs), GFP_KERNEL);
570 atomic_set(&subs->ref_count, 0);
571 INIT_LIST_HEAD(&subs->src_list);
572 INIT_LIST_HEAD(&subs->dest_list);
574 exclusive = !!(info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE);
576 err = check_and_subscribe_port(src_client, src_port, subs, true,
578 connector->number != src_client->number);
581 err = check_and_subscribe_port(dest_client, dest_port, subs, false,
583 connector->number != dest_client->number);
590 delete_and_unsubscribe_port(src_client, src_port, subs, true,
591 connector->number != src_client->number);
597 /* remove the connection */
598 int snd_seq_port_disconnect(struct snd_seq_client *connector,
599 struct snd_seq_client *src_client,
600 struct snd_seq_client_port *src_port,
601 struct snd_seq_client *dest_client,
602 struct snd_seq_client_port *dest_port,
603 struct snd_seq_port_subscribe *info)
605 struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
606 struct snd_seq_subscribers *subs;
609 /* always start from deleting the dest port for avoiding concurrent
612 down_write(&dest->list_mutex);
613 /* look for the connection */
614 list_for_each_entry(subs, &dest->list_head, dest_list) {
615 if (match_subs_info(info, &subs->info)) {
616 __delete_and_unsubscribe_port(dest_client, dest_port,
618 connector->number != dest_client->number);
623 up_write(&dest->list_mutex);
627 delete_and_unsubscribe_port(src_client, src_port, subs, true,
628 connector->number != src_client->number);
634 /* get matched subscriber */
635 int snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp,
636 struct snd_seq_addr *dest_addr,
637 struct snd_seq_port_subscribe *subs)
639 struct snd_seq_subscribers *s;
642 down_read(&src_grp->list_mutex);
643 list_for_each_entry(s, &src_grp->list_head, src_list) {
644 if (addr_match(dest_addr, &s->info.dest)) {
650 up_read(&src_grp->list_mutex);
655 * Attach a device driver that wants to receive events from the
656 * sequencer. Returns the new port number on success.
657 * A driver that wants to receive the events converted to midi, will
658 * use snd_seq_midisynth_register_port().
661 int snd_seq_event_port_attach(int client,
662 struct snd_seq_port_callback *pcbp,
663 int cap, int type, int midi_channels,
664 int midi_voices, char *portname)
666 struct snd_seq_port_info portinfo;
669 /* Set up the port */
670 memset(&portinfo, 0, sizeof(portinfo));
671 portinfo.addr.client = client;
672 strlcpy(portinfo.name, portname ? portname : "Unnamed port",
673 sizeof(portinfo.name));
675 portinfo.capability = cap;
676 portinfo.type = type;
677 portinfo.kernel = pcbp;
678 portinfo.midi_channels = midi_channels;
679 portinfo.midi_voices = midi_voices;
682 ret = snd_seq_kernel_client_ctl(client,
683 SNDRV_SEQ_IOCTL_CREATE_PORT,
687 ret = portinfo.addr.port;
691 EXPORT_SYMBOL(snd_seq_event_port_attach);
694 * Detach the driver from a port.
697 int snd_seq_event_port_detach(int client, int port)
699 struct snd_seq_port_info portinfo;
702 memset(&portinfo, 0, sizeof(portinfo));
703 portinfo.addr.client = client;
704 portinfo.addr.port = port;
705 err = snd_seq_kernel_client_ctl(client,
706 SNDRV_SEQ_IOCTL_DELETE_PORT,
711 EXPORT_SYMBOL(snd_seq_event_port_detach);