3 * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
4 * Jaroslav Kysela <perex@suse.cz>
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.
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.
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
23 #include <sound/driver.h>
24 #include <sound/core.h>
25 #include <linux/slab.h>
26 #include "seq_system.h"
27 #include "seq_ports.h"
28 #include "seq_clientmgr.h"
32 registration of client ports
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.
47 1) leave it this way, a client does normaly does not have more than a few
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.
58 /* return pointer to port structure - port is locked if found */
59 client_port_t *snd_seq_port_use_ptr(client_t *client, int num)
66 read_lock(&client->ports_lock);
67 list_for_each(p, &client->ports_list_head) {
68 port = list_entry(p, client_port_t, list);
69 if (port->addr.port == num) {
71 break; /* deleting now */
72 snd_use_lock_use(&port->use_lock);
73 read_unlock(&client->ports_lock);
77 read_unlock(&client->ports_lock);
78 return NULL; /* not found */
82 /* search for the next port - port is locked if found */
83 client_port_t *snd_seq_port_query_nearest(client_t *client, snd_seq_port_info_t *pinfo)
87 client_port_t *port, *found;
89 num = pinfo->addr.port;
91 read_lock(&client->ports_lock);
92 list_for_each(p, &client->ports_list_head) {
93 port = list_entry(p, client_port_t, list);
94 if (port->addr.port < num)
96 if (port->addr.port == num) {
100 if (found == NULL || port->addr.port < found->addr.port)
107 snd_use_lock_use(&found->use_lock);
109 read_unlock(&client->ports_lock);
114 /* initialize port_subs_info_t */
115 static void port_subs_info_init(port_subs_info_t *grp)
117 INIT_LIST_HEAD(&grp->list_head);
120 rwlock_init(&grp->list_lock);
121 init_rwsem(&grp->list_mutex);
127 /* create a port, port number is returned (-1 on failure) */
128 client_port_t *snd_seq_create_port(client_t *client, int port)
131 client_port_t *new_port;
136 snd_assert(client, return NULL);
138 if (client->num_ports >= SNDRV_SEQ_MAX_PORTS - 1) {
139 snd_printk(KERN_WARNING "too many ports for client %d\n", client->number);
143 /* create a new port */
144 new_port = kzalloc(sizeof(*new_port), GFP_KERNEL);
146 snd_printd("malloc failed for registering client port\n");
147 return NULL; /* failure, out of memory */
150 new_port->addr.client = client->number;
151 new_port->addr.port = -1;
152 new_port->owner = THIS_MODULE;
153 sprintf(new_port->name, "port-%d", num);
154 snd_use_lock_init(&new_port->use_lock);
155 port_subs_info_init(&new_port->c_src);
156 port_subs_info_init(&new_port->c_dest);
158 num = port >= 0 ? port : 0;
159 down(&client->ports_mutex);
160 write_lock_irqsave(&client->ports_lock, flags);
161 list_for_each(l, &client->ports_list_head) {
162 client_port_t *p = list_entry(l, client_port_t, list);
163 if (p->addr.port > num)
165 if (port < 0) /* auto-probe mode */
166 num = p->addr.port + 1;
168 /* insert the new port */
169 list_add_tail(&new_port->list, l);
171 new_port->addr.port = num; /* store the port number in the port */
172 write_unlock_irqrestore(&client->ports_lock, flags);
173 up(&client->ports_mutex);
174 sprintf(new_port->name, "port-%d", num);
184 static int subscribe_port(client_t *client, client_port_t *port, port_subs_info_t *grp, snd_seq_port_subscribe_t *info, int send_ack);
185 static int unsubscribe_port(client_t *client, client_port_t *port, port_subs_info_t *grp, snd_seq_port_subscribe_t *info, int send_ack);
188 static client_port_t *get_client_port(snd_seq_addr_t *addr, client_t **cp)
191 *cp = snd_seq_client_use_ptr(addr->client);
193 p = snd_seq_port_use_ptr(*cp, addr->port);
195 snd_seq_client_unlock(*cp);
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(client_t *client, client_port_t *port,
208 port_subs_info_t *grp, int grptype)
210 struct list_head *p, *n;
212 down_write(&grp->list_mutex);
213 list_for_each_safe(p, n, &grp->list_head) {
216 client_port_t *aport;
218 if (grptype == SRC_LIST) {
219 subs = list_entry(p, subscribers_t, src_list);
220 aport = get_client_port(&subs->info.dest, &c);
222 subs = list_entry(p, subscribers_t, dest_list);
223 aport = get_client_port(&subs->info.sender, &c);
226 unsubscribe_port(client, port, grp, &subs->info, 0);
228 /* looks like the connected port is being deleted.
229 * we decrease the counter, and when both ports are deleted
230 * remove the subscriber info
232 if (atomic_dec_and_test(&subs->ref_count))
235 /* ok we got the connected port */
236 port_subs_info_t *agrp;
237 agrp = (grptype == SRC_LIST) ? &aport->c_dest : &aport->c_src;
238 down_write(&agrp->list_mutex);
239 if (grptype == SRC_LIST)
240 list_del(&subs->dest_list);
242 list_del(&subs->src_list);
243 unsubscribe_port(c, aport, agrp, &subs->info, 1);
245 up_write(&agrp->list_mutex);
246 snd_seq_port_unlock(aport);
247 snd_seq_client_unlock(c);
250 up_write(&grp->list_mutex);
253 /* delete port data */
254 static int port_delete(client_t *client, client_port_t *port)
256 /* set closing flag and wait for all port access are gone */
258 snd_use_lock_sync(&port->use_lock);
260 /* clear subscribers info */
261 clear_subscriber_list(client, port, &port->c_src, SRC_LIST);
262 clear_subscriber_list(client, port, &port->c_dest, DEST_LIST);
264 if (port->private_free)
265 port->private_free(port->private_data);
267 snd_assert(port->c_src.count == 0,);
268 snd_assert(port->c_dest.count == 0,);
275 /* delete a port with the given port id */
276 int snd_seq_delete_port(client_t *client, int port)
280 client_port_t *found = NULL;
282 down(&client->ports_mutex);
283 write_lock_irqsave(&client->ports_lock, flags);
284 list_for_each(l, &client->ports_list_head) {
285 client_port_t *p = list_entry(l, client_port_t, list);
286 if (p->addr.port == port) {
287 /* ok found. delete from the list at first */
294 write_unlock_irqrestore(&client->ports_lock, flags);
295 up(&client->ports_mutex);
297 return port_delete(client, found);
302 /* delete the all ports belonging to the given client */
303 int snd_seq_delete_all_ports(client_t *client)
306 struct list_head deleted_list, *p, *n;
308 /* move the port list to deleted_list, and
309 * clear the port list in the client data.
311 down(&client->ports_mutex);
312 write_lock_irqsave(&client->ports_lock, flags);
313 if (! list_empty(&client->ports_list_head)) {
314 __list_add(&deleted_list,
315 client->ports_list_head.prev,
316 client->ports_list_head.next);
317 INIT_LIST_HEAD(&client->ports_list_head);
319 INIT_LIST_HEAD(&deleted_list);
321 client->num_ports = 0;
322 write_unlock_irqrestore(&client->ports_lock, flags);
324 /* remove each port in deleted_list */
325 list_for_each_safe(p, n, &deleted_list) {
326 client_port_t *port = list_entry(p, client_port_t, list);
328 snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port);
329 port_delete(client, port);
331 up(&client->ports_mutex);
335 /* set port info fields */
336 int snd_seq_set_port_info(client_port_t * port, snd_seq_port_info_t * info)
338 snd_assert(port && info, return -EINVAL);
342 strlcpy(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;
363 /* get port info fields */
364 int snd_seq_get_port_info(client_port_t * port, snd_seq_port_info_t * info)
366 snd_assert(port && info, return -EINVAL);
369 strlcpy(info->name, port->name, sizeof(info->name));
371 /* get capabilities */
372 info->capability = port->capability;
375 info->type = port->type;
377 /* information about supported channels/voices */
378 info->midi_channels = port->midi_channels;
379 info->midi_voices = port->midi_voices;
380 info->synth_voices = port->synth_voices;
382 /* get subscriber counts */
383 info->read_use = port->c_src.count;
384 info->write_use = port->c_dest.count;
388 if (port->timestamping) {
389 info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP;
391 info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL;
392 info->time_queue = port->time_queue;
401 * call callback functions (if any):
402 * the callbacks are invoked only when the first (for connection) or
403 * the last subscription (for disconnection) is done. Second or later
404 * subscription results in increment of counter, but no callback is
406 * This feature is useful if these callbacks are associated with
407 * initialization or termination of devices (see seq_midi.c).
409 * If callback_all option is set, the callback function is invoked
410 * at each connnection/disconnection.
413 static int subscribe_port(client_t *client, client_port_t *port, port_subs_info_t *grp,
414 snd_seq_port_subscribe_t *info, int send_ack)
418 if (!try_module_get(port->owner))
421 if (grp->open && (port->callback_all || grp->count == 1)) {
422 err = grp->open(port->private_data, info);
424 module_put(port->owner);
428 if (err >= 0 && send_ack && client->type == USER_CLIENT)
429 snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
430 info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
435 static int unsubscribe_port(client_t *client, client_port_t *port,
436 port_subs_info_t *grp,
437 snd_seq_port_subscribe_t *info, int send_ack)
444 if (grp->close && (port->callback_all || grp->count == 0))
445 err = grp->close(port->private_data, info);
446 if (send_ack && client->type == USER_CLIENT)
447 snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
448 info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
449 module_put(port->owner);
455 /* check if both addresses are identical */
456 static inline int addr_match(snd_seq_addr_t *r, snd_seq_addr_t *s)
458 return (r->client == s->client) && (r->port == s->port);
461 /* check the two subscribe info match */
462 /* if flags is zero, checks only sender and destination addresses */
463 static int match_subs_info(snd_seq_port_subscribe_t *r,
464 snd_seq_port_subscribe_t *s)
466 if (addr_match(&r->sender, &s->sender) &&
467 addr_match(&r->dest, &s->dest)) {
468 if (r->flags && r->flags == s->flags)
469 return r->queue == s->queue;
477 /* connect two ports */
478 int snd_seq_port_connect(client_t *connector,
479 client_t *src_client, client_port_t *src_port,
480 client_t *dest_client, client_port_t *dest_port,
481 snd_seq_port_subscribe_t *info)
483 port_subs_info_t *src = &src_port->c_src;
484 port_subs_info_t *dest = &dest_port->c_dest;
487 int err, src_called = 0;
491 subs = kzalloc(sizeof(*subs), GFP_KERNEL);
496 atomic_set(&subs->ref_count, 2);
498 down_write(&src->list_mutex);
499 down_write(&dest->list_mutex);
501 exclusive = info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE ? 1 : 0;
504 if (! list_empty(&src->list_head) || ! list_empty(&dest->list_head))
507 if (src->exclusive || dest->exclusive)
509 /* check whether already exists */
510 list_for_each(p, &src->list_head) {
511 subscribers_t *s = list_entry(p, subscribers_t, src_list);
512 if (match_subs_info(info, &s->info))
515 list_for_each(p, &dest->list_head) {
516 subscribers_t *s = list_entry(p, subscribers_t, dest_list);
517 if (match_subs_info(info, &s->info))
522 if ((err = subscribe_port(src_client, src_port, src, info,
523 connector->number != src_client->number)) < 0)
527 if ((err = subscribe_port(dest_client, dest_port, dest, info,
528 connector->number != dest_client->number)) < 0)
532 write_lock_irqsave(&src->list_lock, flags);
533 // write_lock(&dest->list_lock); // no other lock yet
534 list_add_tail(&subs->src_list, &src->list_head);
535 list_add_tail(&subs->dest_list, &dest->list_head);
536 // write_unlock(&dest->list_lock); // no other lock yet
537 write_unlock_irqrestore(&src->list_lock, flags);
539 src->exclusive = dest->exclusive = exclusive;
541 up_write(&dest->list_mutex);
542 up_write(&src->list_mutex);
547 unsubscribe_port(src_client, src_port, src, info,
548 connector->number != src_client->number);
550 up_write(&dest->list_mutex);
551 up_write(&src->list_mutex);
556 /* remove the connection */
557 int snd_seq_port_disconnect(client_t *connector,
558 client_t *src_client, client_port_t *src_port,
559 client_t *dest_client, client_port_t *dest_port,
560 snd_seq_port_subscribe_t *info)
562 port_subs_info_t *src = &src_port->c_src;
563 port_subs_info_t *dest = &dest_port->c_dest;
569 down_write(&src->list_mutex);
570 down_write(&dest->list_mutex);
572 /* look for the connection */
573 list_for_each(p, &src->list_head) {
574 subs = list_entry(p, subscribers_t, src_list);
575 if (match_subs_info(info, &subs->info)) {
576 write_lock_irqsave(&src->list_lock, flags);
577 // write_lock(&dest->list_lock); // no lock yet
578 list_del(&subs->src_list);
579 list_del(&subs->dest_list);
580 // write_unlock(&dest->list_lock);
581 write_unlock_irqrestore(&src->list_lock, flags);
582 src->exclusive = dest->exclusive = 0;
583 unsubscribe_port(src_client, src_port, src, info,
584 connector->number != src_client->number);
585 unsubscribe_port(dest_client, dest_port, dest, info,
586 connector->number != dest_client->number);
593 up_write(&dest->list_mutex);
594 up_write(&src->list_mutex);
599 /* get matched subscriber */
600 subscribers_t *snd_seq_port_get_subscription(port_subs_info_t *src_grp,
601 snd_seq_addr_t *dest_addr)
604 subscribers_t *s, *found = NULL;
606 down_read(&src_grp->list_mutex);
607 list_for_each(p, &src_grp->list_head) {
608 s = list_entry(p, subscribers_t, src_list);
609 if (addr_match(dest_addr, &s->info.dest)) {
614 up_read(&src_grp->list_mutex);
619 * Attach a device driver that wants to receive events from the
620 * sequencer. Returns the new port number on success.
621 * A driver that wants to receive the events converted to midi, will
622 * use snd_seq_midisynth_register_port().
625 int snd_seq_event_port_attach(int client,
626 snd_seq_port_callback_t *pcbp,
627 int cap, int type, int midi_channels,
628 int midi_voices, char *portname)
630 snd_seq_port_info_t portinfo;
633 /* Set up the port */
634 memset(&portinfo, 0, sizeof(portinfo));
635 portinfo.addr.client = client;
636 strlcpy(portinfo.name, portname ? portname : "Unamed port",
637 sizeof(portinfo.name));
639 portinfo.capability = cap;
640 portinfo.type = type;
641 portinfo.kernel = pcbp;
642 portinfo.midi_channels = midi_channels;
643 portinfo.midi_voices = midi_voices;
646 ret = snd_seq_kernel_client_ctl(client,
647 SNDRV_SEQ_IOCTL_CREATE_PORT,
651 ret = portinfo.addr.port;
658 * Detach the driver from a port.
661 int snd_seq_event_port_detach(int client, int port)
663 snd_seq_port_info_t portinfo;
666 memset(&portinfo, 0, sizeof(portinfo));
667 portinfo.addr.client = client;
668 portinfo.addr.port = port;
669 err = snd_seq_kernel_client_ctl(client,
670 SNDRV_SEQ_IOCTL_DELETE_PORT,