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 struct snd_seq_client_port *snd_seq_port_use_ptr(struct snd_seq_client *client,
63 struct snd_seq_client_port *port;
67 read_lock(&client->ports_lock);
68 list_for_each(p, &client->ports_list_head) {
69 port = list_entry(p, struct snd_seq_client_port, list);
70 if (port->addr.port == num) {
72 break; /* deleting now */
73 snd_use_lock_use(&port->use_lock);
74 read_unlock(&client->ports_lock);
78 read_unlock(&client->ports_lock);
79 return NULL; /* not found */
83 /* search for the next port - port is locked if found */
84 struct snd_seq_client_port *snd_seq_port_query_nearest(struct snd_seq_client *client,
85 struct snd_seq_port_info *pinfo)
89 struct snd_seq_client_port *port, *found;
91 num = pinfo->addr.port;
93 read_lock(&client->ports_lock);
94 list_for_each(p, &client->ports_list_head) {
95 port = list_entry(p, struct snd_seq_client_port, list);
96 if (port->addr.port < num)
98 if (port->addr.port == num) {
102 if (found == NULL || port->addr.port < found->addr.port)
109 snd_use_lock_use(&found->use_lock);
111 read_unlock(&client->ports_lock);
116 /* initialize snd_seq_port_subs_info */
117 static void port_subs_info_init(struct snd_seq_port_subs_info *grp)
119 INIT_LIST_HEAD(&grp->list_head);
122 rwlock_init(&grp->list_lock);
123 init_rwsem(&grp->list_mutex);
129 /* create a port, port number is returned (-1 on failure) */
130 struct snd_seq_client_port *snd_seq_create_port(struct snd_seq_client *client,
134 struct snd_seq_client_port *new_port;
139 snd_assert(client, return NULL);
141 if (client->num_ports >= SNDRV_SEQ_MAX_PORTS - 1) {
142 snd_printk(KERN_WARNING "too many ports for client %d\n", client->number);
146 /* create a new port */
147 new_port = kzalloc(sizeof(*new_port), GFP_KERNEL);
149 snd_printd("malloc failed for registering client port\n");
150 return NULL; /* failure, out of memory */
153 new_port->addr.client = client->number;
154 new_port->addr.port = -1;
155 new_port->owner = THIS_MODULE;
156 sprintf(new_port->name, "port-%d", num);
157 snd_use_lock_init(&new_port->use_lock);
158 port_subs_info_init(&new_port->c_src);
159 port_subs_info_init(&new_port->c_dest);
161 num = port >= 0 ? port : 0;
162 mutex_lock(&client->ports_mutex);
163 write_lock_irqsave(&client->ports_lock, flags);
164 list_for_each(l, &client->ports_list_head) {
165 struct snd_seq_client_port *p = list_entry(l, struct snd_seq_client_port, list);
166 if (p->addr.port > num)
168 if (port < 0) /* auto-probe mode */
169 num = p->addr.port + 1;
171 /* insert the new port */
172 list_add_tail(&new_port->list, l);
174 new_port->addr.port = num; /* store the port number in the port */
175 write_unlock_irqrestore(&client->ports_lock, flags);
176 mutex_unlock(&client->ports_mutex);
177 sprintf(new_port->name, "port-%d", num);
187 static int subscribe_port(struct snd_seq_client *client,
188 struct snd_seq_client_port *port,
189 struct snd_seq_port_subs_info *grp,
190 struct snd_seq_port_subscribe *info, int send_ack);
191 static int unsubscribe_port(struct snd_seq_client *client,
192 struct snd_seq_client_port *port,
193 struct snd_seq_port_subs_info *grp,
194 struct snd_seq_port_subscribe *info, int send_ack);
197 static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr,
198 struct snd_seq_client **cp)
200 struct snd_seq_client_port *p;
201 *cp = snd_seq_client_use_ptr(addr->client);
203 p = snd_seq_port_use_ptr(*cp, addr->port);
205 snd_seq_client_unlock(*cp);
214 * remove all subscribers on the list
215 * this is called from port_delete, for each src and dest list.
217 static void clear_subscriber_list(struct snd_seq_client *client,
218 struct snd_seq_client_port *port,
219 struct snd_seq_port_subs_info *grp,
222 struct list_head *p, *n;
224 list_for_each_safe(p, n, &grp->list_head) {
225 struct snd_seq_subscribers *subs;
226 struct snd_seq_client *c;
227 struct snd_seq_client_port *aport;
229 if (grptype == SRC_LIST) {
230 subs = list_entry(p, struct snd_seq_subscribers, src_list);
231 aport = get_client_port(&subs->info.dest, &c);
233 subs = list_entry(p, struct snd_seq_subscribers, dest_list);
234 aport = get_client_port(&subs->info.sender, &c);
237 unsubscribe_port(client, port, grp, &subs->info, 0);
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))
246 /* ok we got the connected port */
247 struct snd_seq_port_subs_info *agrp;
248 agrp = (grptype == SRC_LIST) ? &aport->c_dest : &aport->c_src;
249 down_write(&agrp->list_mutex);
250 if (grptype == SRC_LIST)
251 list_del(&subs->dest_list);
253 list_del(&subs->src_list);
254 unsubscribe_port(c, aport, agrp, &subs->info, 1);
256 up_write(&agrp->list_mutex);
257 snd_seq_port_unlock(aport);
258 snd_seq_client_unlock(c);
263 /* delete port data */
264 static int port_delete(struct snd_seq_client *client,
265 struct snd_seq_client_port *port)
267 /* set closing flag and wait for all port access are gone */
269 snd_use_lock_sync(&port->use_lock);
271 /* clear subscribers info */
272 clear_subscriber_list(client, port, &port->c_src, SRC_LIST);
273 clear_subscriber_list(client, port, &port->c_dest, DEST_LIST);
275 if (port->private_free)
276 port->private_free(port->private_data);
278 snd_assert(port->c_src.count == 0,);
279 snd_assert(port->c_dest.count == 0,);
286 /* delete a port with the given port id */
287 int snd_seq_delete_port(struct snd_seq_client *client, int port)
291 struct snd_seq_client_port *found = NULL;
293 mutex_lock(&client->ports_mutex);
294 write_lock_irqsave(&client->ports_lock, flags);
295 list_for_each(l, &client->ports_list_head) {
296 struct snd_seq_client_port *p = list_entry(l, struct snd_seq_client_port, list);
297 if (p->addr.port == port) {
298 /* ok found. delete from the list at first */
305 write_unlock_irqrestore(&client->ports_lock, flags);
306 mutex_unlock(&client->ports_mutex);
308 return port_delete(client, found);
313 /* delete the all ports belonging to the given client */
314 int snd_seq_delete_all_ports(struct snd_seq_client *client)
317 struct list_head deleted_list, *p, *n;
319 /* move the port list to deleted_list, and
320 * clear the port list in the client data.
322 mutex_lock(&client->ports_mutex);
323 write_lock_irqsave(&client->ports_lock, flags);
324 if (! list_empty(&client->ports_list_head)) {
325 __list_add(&deleted_list,
326 client->ports_list_head.prev,
327 client->ports_list_head.next);
328 INIT_LIST_HEAD(&client->ports_list_head);
330 INIT_LIST_HEAD(&deleted_list);
332 client->num_ports = 0;
333 write_unlock_irqrestore(&client->ports_lock, flags);
335 /* remove each port in deleted_list */
336 list_for_each_safe(p, n, &deleted_list) {
337 struct snd_seq_client_port *port = list_entry(p, struct snd_seq_client_port, list);
339 snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port);
340 port_delete(client, port);
342 mutex_unlock(&client->ports_mutex);
346 /* set port info fields */
347 int snd_seq_set_port_info(struct snd_seq_client_port * port,
348 struct snd_seq_port_info * info)
350 snd_assert(port && info, return -EINVAL);
354 strlcpy(port->name, info->name, sizeof(port->name));
356 /* set capabilities */
357 port->capability = info->capability;
360 port->type = info->type;
362 /* information about supported channels/voices */
363 port->midi_channels = info->midi_channels;
364 port->midi_voices = info->midi_voices;
365 port->synth_voices = info->synth_voices;
368 port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0;
369 port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0;
370 port->time_queue = info->time_queue;
375 /* get port info fields */
376 int snd_seq_get_port_info(struct snd_seq_client_port * port,
377 struct snd_seq_port_info * info)
379 snd_assert(port && info, return -EINVAL);
382 strlcpy(info->name, port->name, sizeof(info->name));
384 /* get capabilities */
385 info->capability = port->capability;
388 info->type = port->type;
390 /* information about supported channels/voices */
391 info->midi_channels = port->midi_channels;
392 info->midi_voices = port->midi_voices;
393 info->synth_voices = port->synth_voices;
395 /* get subscriber counts */
396 info->read_use = port->c_src.count;
397 info->write_use = port->c_dest.count;
401 if (port->timestamping) {
402 info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP;
404 info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL;
405 info->time_queue = port->time_queue;
414 * call callback functions (if any):
415 * the callbacks are invoked only when the first (for connection) or
416 * the last subscription (for disconnection) is done. Second or later
417 * subscription results in increment of counter, but no callback is
419 * This feature is useful if these callbacks are associated with
420 * initialization or termination of devices (see seq_midi.c).
422 * If callback_all option is set, the callback function is invoked
423 * at each connnection/disconnection.
426 static int subscribe_port(struct snd_seq_client *client,
427 struct snd_seq_client_port *port,
428 struct snd_seq_port_subs_info *grp,
429 struct snd_seq_port_subscribe *info,
434 if (!try_module_get(port->owner))
437 if (grp->open && (port->callback_all || grp->count == 1)) {
438 err = grp->open(port->private_data, info);
440 module_put(port->owner);
444 if (err >= 0 && send_ack && client->type == USER_CLIENT)
445 snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
446 info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
451 static int unsubscribe_port(struct snd_seq_client *client,
452 struct snd_seq_client_port *port,
453 struct snd_seq_port_subs_info *grp,
454 struct snd_seq_port_subscribe *info,
462 if (grp->close && (port->callback_all || grp->count == 0))
463 err = grp->close(port->private_data, info);
464 if (send_ack && client->type == USER_CLIENT)
465 snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
466 info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
467 module_put(port->owner);
473 /* check if both addresses are identical */
474 static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s)
476 return (r->client == s->client) && (r->port == s->port);
479 /* check the two subscribe info match */
480 /* if flags is zero, checks only sender and destination addresses */
481 static int match_subs_info(struct snd_seq_port_subscribe *r,
482 struct snd_seq_port_subscribe *s)
484 if (addr_match(&r->sender, &s->sender) &&
485 addr_match(&r->dest, &s->dest)) {
486 if (r->flags && r->flags == s->flags)
487 return r->queue == s->queue;
495 /* connect two ports */
496 int snd_seq_port_connect(struct snd_seq_client *connector,
497 struct snd_seq_client *src_client,
498 struct snd_seq_client_port *src_port,
499 struct snd_seq_client *dest_client,
500 struct snd_seq_client_port *dest_port,
501 struct snd_seq_port_subscribe *info)
503 struct snd_seq_port_subs_info *src = &src_port->c_src;
504 struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
505 struct snd_seq_subscribers *subs;
507 int err, src_called = 0;
511 subs = kzalloc(sizeof(*subs), GFP_KERNEL);
516 atomic_set(&subs->ref_count, 2);
518 down_write(&src->list_mutex);
519 down_write(&dest->list_mutex);
521 exclusive = info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE ? 1 : 0;
524 if (! list_empty(&src->list_head) || ! list_empty(&dest->list_head))
527 if (src->exclusive || dest->exclusive)
529 /* check whether already exists */
530 list_for_each(p, &src->list_head) {
531 struct snd_seq_subscribers *s = list_entry(p, struct snd_seq_subscribers, src_list);
532 if (match_subs_info(info, &s->info))
535 list_for_each(p, &dest->list_head) {
536 struct snd_seq_subscribers *s = list_entry(p, struct snd_seq_subscribers, dest_list);
537 if (match_subs_info(info, &s->info))
542 if ((err = subscribe_port(src_client, src_port, src, info,
543 connector->number != src_client->number)) < 0)
547 if ((err = subscribe_port(dest_client, dest_port, dest, info,
548 connector->number != dest_client->number)) < 0)
552 write_lock_irqsave(&src->list_lock, flags);
553 // write_lock(&dest->list_lock); // no other lock yet
554 list_add_tail(&subs->src_list, &src->list_head);
555 list_add_tail(&subs->dest_list, &dest->list_head);
556 // write_unlock(&dest->list_lock); // no other lock yet
557 write_unlock_irqrestore(&src->list_lock, flags);
559 src->exclusive = dest->exclusive = exclusive;
561 up_write(&dest->list_mutex);
562 up_write(&src->list_mutex);
567 unsubscribe_port(src_client, src_port, src, info,
568 connector->number != src_client->number);
570 up_write(&dest->list_mutex);
571 up_write(&src->list_mutex);
576 /* remove the connection */
577 int snd_seq_port_disconnect(struct snd_seq_client *connector,
578 struct snd_seq_client *src_client,
579 struct snd_seq_client_port *src_port,
580 struct snd_seq_client *dest_client,
581 struct snd_seq_client_port *dest_port,
582 struct snd_seq_port_subscribe *info)
584 struct snd_seq_port_subs_info *src = &src_port->c_src;
585 struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
586 struct snd_seq_subscribers *subs;
591 down_write(&src->list_mutex);
592 down_write(&dest->list_mutex);
594 /* look for the connection */
595 list_for_each(p, &src->list_head) {
596 subs = list_entry(p, struct snd_seq_subscribers, src_list);
597 if (match_subs_info(info, &subs->info)) {
598 write_lock_irqsave(&src->list_lock, flags);
599 // write_lock(&dest->list_lock); // no lock yet
600 list_del(&subs->src_list);
601 list_del(&subs->dest_list);
602 // write_unlock(&dest->list_lock);
603 write_unlock_irqrestore(&src->list_lock, flags);
604 src->exclusive = dest->exclusive = 0;
605 unsubscribe_port(src_client, src_port, src, info,
606 connector->number != src_client->number);
607 unsubscribe_port(dest_client, dest_port, dest, info,
608 connector->number != dest_client->number);
615 up_write(&dest->list_mutex);
616 up_write(&src->list_mutex);
621 /* get matched subscriber */
622 struct snd_seq_subscribers *snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp,
623 struct snd_seq_addr *dest_addr)
626 struct snd_seq_subscribers *s, *found = NULL;
628 down_read(&src_grp->list_mutex);
629 list_for_each(p, &src_grp->list_head) {
630 s = list_entry(p, struct snd_seq_subscribers, src_list);
631 if (addr_match(dest_addr, &s->info.dest)) {
636 up_read(&src_grp->list_mutex);
641 * Attach a device driver that wants to receive events from the
642 * sequencer. Returns the new port number on success.
643 * A driver that wants to receive the events converted to midi, will
644 * use snd_seq_midisynth_register_port().
647 int snd_seq_event_port_attach(int client,
648 struct snd_seq_port_callback *pcbp,
649 int cap, int type, int midi_channels,
650 int midi_voices, char *portname)
652 struct snd_seq_port_info portinfo;
655 /* Set up the port */
656 memset(&portinfo, 0, sizeof(portinfo));
657 portinfo.addr.client = client;
658 strlcpy(portinfo.name, portname ? portname : "Unamed port",
659 sizeof(portinfo.name));
661 portinfo.capability = cap;
662 portinfo.type = type;
663 portinfo.kernel = pcbp;
664 portinfo.midi_channels = midi_channels;
665 portinfo.midi_voices = midi_voices;
668 ret = snd_seq_kernel_client_ctl(client,
669 SNDRV_SEQ_IOCTL_CREATE_PORT,
673 ret = portinfo.addr.port;
678 EXPORT_SYMBOL(snd_seq_event_port_attach);
681 * Detach the driver from a port.
684 int snd_seq_event_port_detach(int client, int port)
686 struct snd_seq_port_info portinfo;
689 memset(&portinfo, 0, sizeof(portinfo));
690 portinfo.addr.client = client;
691 portinfo.addr.port = port;
692 err = snd_seq_kernel_client_ctl(client,
693 SNDRV_SEQ_IOCTL_DELETE_PORT,
699 EXPORT_SYMBOL(snd_seq_event_port_detach);