3 * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
4 * Jaroslav Kysela <perex@perex.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/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"
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
,
62 struct snd_seq_client_port
*port
;
66 read_lock(&client
->ports_lock
);
67 list_for_each_entry(port
, &client
->ports_list_head
, list
) {
68 if (port
->addr
.port
== num
) {
70 break; /* deleting now */
71 snd_use_lock_use(&port
->use_lock
);
72 read_unlock(&client
->ports_lock
);
76 read_unlock(&client
->ports_lock
);
77 return NULL
; /* not found */
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
)
86 struct snd_seq_client_port
*port
, *found
;
88 num
= pinfo
->addr
.port
;
90 read_lock(&client
->ports_lock
);
91 list_for_each_entry(port
, &client
->ports_list_head
, list
) {
92 if (port
->addr
.port
< num
)
94 if (port
->addr
.port
== num
) {
98 if (found
== NULL
|| port
->addr
.port
< found
->addr
.port
)
105 snd_use_lock_use(&found
->use_lock
);
107 read_unlock(&client
->ports_lock
);
112 /* initialize snd_seq_port_subs_info */
113 static void port_subs_info_init(struct snd_seq_port_subs_info
*grp
)
115 INIT_LIST_HEAD(&grp
->list_head
);
118 rwlock_init(&grp
->list_lock
);
119 init_rwsem(&grp
->list_mutex
);
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
,
130 struct snd_seq_client_port
*new_port
, *p
;
134 if (snd_BUG_ON(!client
))
137 if (client
->num_ports
>= SNDRV_SEQ_MAX_PORTS
) {
138 pr_warn("ALSA: seq: too many ports for client %d\n", client
->number
);
142 /* create a new port */
143 new_port
= kzalloc(sizeof(*new_port
), GFP_KERNEL
);
145 return NULL
; /* failure, out of memory */
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
);
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
)
161 if (port
< 0) /* auto-probe mode */
162 num
= p
->addr
.port
+ 1;
164 /* insert the new port */
165 list_add_tail(&new_port
->list
, &p
->list
);
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
);
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
);
190 static struct snd_seq_client_port
*get_client_port(struct snd_seq_addr
*addr
,
191 struct snd_seq_client
**cp
)
193 struct snd_seq_client_port
*p
;
194 *cp
= snd_seq_client_use_ptr(addr
->client
);
196 p
= snd_seq_port_use_ptr(*cp
, addr
->port
);
198 snd_seq_client_unlock(*cp
);
207 * remove all subscribers on the list
208 * this is called from port_delete, for each src and dest list.
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
,
215 struct list_head
*p
, *n
;
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
;
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
);
226 subs
= list_entry(p
, struct snd_seq_subscribers
, dest_list
);
227 aport
= get_client_port(&subs
->info
.sender
, &c
);
230 unsubscribe_port(client
, port
, grp
, &subs
->info
, 0);
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
236 if (atomic_dec_and_test(&subs
->ref_count
))
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
);
246 list_del(&subs
->src_list
);
247 up_write(&agrp
->list_mutex
);
248 unsubscribe_port(c
, aport
, agrp
, &subs
->info
, 1);
250 snd_seq_port_unlock(aport
);
251 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
, SRC_LIST
);
266 clear_subscriber_list(client
, port
, &port
->c_dest
, DEST_LIST
);
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
)
283 struct snd_seq_client_port
*found
= NULL
, *p
;
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 */
296 write_unlock_irqrestore(&client
->ports_lock
, flags
);
297 mutex_unlock(&client
->ports_mutex
);
299 return port_delete(client
, found
);
304 /* delete the all ports belonging to the given client */
305 int snd_seq_delete_all_ports(struct snd_seq_client
*client
)
308 struct list_head deleted_list
;
309 struct snd_seq_client_port
*port
, *tmp
;
311 /* move the port list to deleted_list, and
312 * clear the port list in the client data.
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
);
320 INIT_LIST_HEAD(&deleted_list
);
322 client
->num_ports
= 0;
323 write_unlock_irqrestore(&client
->ports_lock
, flags
);
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
);
331 mutex_unlock(&client
->ports_mutex
);
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
)
339 if (snd_BUG_ON(!port
|| !info
))
344 strlcpy(port
->name
, info
->name
, sizeof(port
->name
));
346 /* set capabilities */
347 port
->capability
= info
->capability
;
350 port
->type
= info
->type
;
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
;
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
;
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
)
369 if (snd_BUG_ON(!port
|| !info
))
373 strlcpy(info
->name
, port
->name
, sizeof(info
->name
));
375 /* get capabilities */
376 info
->capability
= port
->capability
;
379 info
->type
= port
->type
;
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
;
386 /* get subscriber counts */
387 info
->read_use
= port
->c_src
.count
;
388 info
->write_use
= port
->c_dest
.count
;
392 if (port
->timestamping
) {
393 info
->flags
|= SNDRV_SEQ_PORT_FLG_TIMESTAMP
;
395 info
->flags
|= SNDRV_SEQ_PORT_FLG_TIME_REAL
;
396 info
->time_queue
= port
->time_queue
;
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
410 * This feature is useful if these callbacks are associated with
411 * initialization or termination of devices (see seq_midi.c).
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
,
422 if (!try_module_get(port
->owner
))
425 if (grp
->open
&& grp
->count
== 1) {
426 err
= grp
->open(port
->private_data
, info
);
428 module_put(port
->owner
);
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
);
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
,
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
);
461 /* check if both addresses are identical */
462 static inline int addr_match(struct snd_seq_addr
*r
, struct snd_seq_addr
*s
)
464 return (r
->client
== s
->client
) && (r
->port
== s
->port
);
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
)
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
;
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
)
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;
498 subs
= kzalloc(sizeof(*subs
), GFP_KERNEL
);
503 atomic_set(&subs
->ref_count
, 2);
505 down_write(&src
->list_mutex
);
506 down_write_nested(&dest
->list_mutex
, SINGLE_DEPTH_NESTING
);
508 exclusive
= info
->flags
& SNDRV_SEQ_PORT_SUBS_EXCLUSIVE
? 1 : 0;
511 if (! list_empty(&src
->list_head
) || ! list_empty(&dest
->list_head
))
514 if (src
->exclusive
|| dest
->exclusive
)
516 /* check whether already exists */
517 list_for_each_entry(s
, &src
->list_head
, src_list
) {
518 if (match_subs_info(info
, &s
->info
))
521 list_for_each_entry(s
, &dest
->list_head
, dest_list
) {
522 if (match_subs_info(info
, &s
->info
))
527 if ((err
= subscribe_port(src_client
, src_port
, src
, info
,
528 connector
->number
!= src_client
->number
)) < 0)
532 if ((err
= subscribe_port(dest_client
, dest_port
, dest
, info
,
533 connector
->number
!= dest_client
->number
)) < 0)
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
);
544 src
->exclusive
= dest
->exclusive
= exclusive
;
546 up_write(&dest
->list_mutex
);
547 up_write(&src
->list_mutex
);
552 unsubscribe_port(src_client
, src_port
, src
, info
,
553 connector
->number
!= src_client
->number
);
555 up_write(&dest
->list_mutex
);
556 up_write(&src
->list_mutex
);
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
)
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
;
575 down_write(&src
->list_mutex
);
576 down_write_nested(&dest
->list_mutex
, SINGLE_DEPTH_NESTING
);
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
);
598 up_write(&dest
->list_mutex
);
599 up_write(&src
->list_mutex
);
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
)
608 struct snd_seq_subscribers
*s
, *found
= NULL
;
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
)) {
617 up_read(&src_grp
->list_mutex
);
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().
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
)
633 struct snd_seq_port_info portinfo
;
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
));
642 portinfo
.capability
= cap
;
643 portinfo
.type
= type
;
644 portinfo
.kernel
= pcbp
;
645 portinfo
.midi_channels
= midi_channels
;
646 portinfo
.midi_voices
= midi_voices
;
649 ret
= snd_seq_kernel_client_ctl(client
,
650 SNDRV_SEQ_IOCTL_CREATE_PORT
,
654 ret
= portinfo
.addr
.port
;
659 EXPORT_SYMBOL(snd_seq_event_port_attach
);
662 * Detach the driver from a port.
665 int snd_seq_event_port_detach(int client
, int port
)
667 struct snd_seq_port_info portinfo
;
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
,
680 EXPORT_SYMBOL(snd_seq_event_port_detach
);