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 * the caller needs to unref the port via snd_seq_port_unlock() appropriately
128 struct snd_seq_client_port
*snd_seq_create_port(struct snd_seq_client
*client
,
132 struct snd_seq_client_port
*new_port
, *p
;
136 if (snd_BUG_ON(!client
))
139 if (client
->num_ports
>= SNDRV_SEQ_MAX_PORTS
- 1) {
140 pr_warn("ALSA: seq: too many ports for client %d\n", client
->number
);
144 /* create a new port */
145 new_port
= kzalloc(sizeof(*new_port
), GFP_KERNEL
);
147 return NULL
; /* failure, out of memory */
149 new_port
->addr
.client
= client
->number
;
150 new_port
->addr
.port
= -1;
151 new_port
->owner
= THIS_MODULE
;
152 sprintf(new_port
->name
, "port-%d", num
);
153 snd_use_lock_init(&new_port
->use_lock
);
154 port_subs_info_init(&new_port
->c_src
);
155 port_subs_info_init(&new_port
->c_dest
);
156 snd_use_lock_use(&new_port
->use_lock
);
158 num
= port
>= 0 ? port
: 0;
159 mutex_lock(&client
->ports_mutex
);
160 write_lock_irqsave(&client
->ports_lock
, flags
);
161 list_for_each_entry(p
, &client
->ports_list_head
, list
) {
162 if (p
->addr
.port
> num
)
164 if (port
< 0) /* auto-probe mode */
165 num
= p
->addr
.port
+ 1;
167 /* insert the new port */
168 list_add_tail(&new_port
->list
, &p
->list
);
170 new_port
->addr
.port
= num
; /* store the port number in the port */
171 sprintf(new_port
->name
, "port-%d", num
);
172 write_unlock_irqrestore(&client
->ports_lock
, flags
);
173 mutex_unlock(&client
->ports_mutex
);
179 static int subscribe_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
);
183 static int unsubscribe_port(struct snd_seq_client
*client
,
184 struct snd_seq_client_port
*port
,
185 struct snd_seq_port_subs_info
*grp
,
186 struct snd_seq_port_subscribe
*info
, int send_ack
);
189 static struct snd_seq_client_port
*get_client_port(struct snd_seq_addr
*addr
,
190 struct snd_seq_client
**cp
)
192 struct snd_seq_client_port
*p
;
193 *cp
= snd_seq_client_use_ptr(addr
->client
);
195 p
= snd_seq_port_use_ptr(*cp
, addr
->port
);
197 snd_seq_client_unlock(*cp
);
205 static void delete_and_unsubscribe_port(struct snd_seq_client
*client
,
206 struct snd_seq_client_port
*port
,
207 struct snd_seq_subscribers
*subs
,
208 bool is_src
, bool ack
);
210 static inline struct snd_seq_subscribers
*
211 get_subscriber(struct list_head
*p
, bool is_src
)
214 return list_entry(p
, struct snd_seq_subscribers
, src_list
);
216 return list_entry(p
, struct snd_seq_subscribers
, dest_list
);
220 * remove all subscribers on the list
221 * this is called from port_delete, for each src and dest list.
223 static void clear_subscriber_list(struct snd_seq_client
*client
,
224 struct snd_seq_client_port
*port
,
225 struct snd_seq_port_subs_info
*grp
,
228 struct list_head
*p
, *n
;
230 list_for_each_safe(p
, n
, &grp
->list_head
) {
231 struct snd_seq_subscribers
*subs
;
232 struct snd_seq_client
*c
;
233 struct snd_seq_client_port
*aport
;
235 subs
= get_subscriber(p
, is_src
);
237 aport
= get_client_port(&subs
->info
.dest
, &c
);
239 aport
= get_client_port(&subs
->info
.sender
, &c
);
240 delete_and_unsubscribe_port(client
, port
, subs
, is_src
, false);
243 /* looks like the connected port is being deleted.
244 * we decrease the counter, and when both ports are deleted
245 * remove the subscriber info
247 if (atomic_dec_and_test(&subs
->ref_count
))
252 /* ok we got the connected port */
253 delete_and_unsubscribe_port(c
, aport
, subs
, !is_src
, true);
255 snd_seq_port_unlock(aport
);
256 snd_seq_client_unlock(c
);
260 /* delete port data */
261 static int port_delete(struct snd_seq_client
*client
,
262 struct snd_seq_client_port
*port
)
264 /* set closing flag and wait for all port access are gone */
266 snd_use_lock_sync(&port
->use_lock
);
268 /* clear subscribers info */
269 clear_subscriber_list(client
, port
, &port
->c_src
, true);
270 clear_subscriber_list(client
, port
, &port
->c_dest
, false);
272 if (port
->private_free
)
273 port
->private_free(port
->private_data
);
275 snd_BUG_ON(port
->c_src
.count
!= 0);
276 snd_BUG_ON(port
->c_dest
.count
!= 0);
283 /* delete a port with the given port id */
284 int snd_seq_delete_port(struct snd_seq_client
*client
, int port
)
287 struct snd_seq_client_port
*found
= NULL
, *p
;
289 mutex_lock(&client
->ports_mutex
);
290 write_lock_irqsave(&client
->ports_lock
, flags
);
291 list_for_each_entry(p
, &client
->ports_list_head
, list
) {
292 if (p
->addr
.port
== port
) {
293 /* ok found. delete from the list at first */
300 write_unlock_irqrestore(&client
->ports_lock
, flags
);
301 mutex_unlock(&client
->ports_mutex
);
303 return port_delete(client
, found
);
308 /* delete the all ports belonging to the given client */
309 int snd_seq_delete_all_ports(struct snd_seq_client
*client
)
312 struct list_head deleted_list
;
313 struct snd_seq_client_port
*port
, *tmp
;
315 /* move the port list to deleted_list, and
316 * clear the port list in the client data.
318 mutex_lock(&client
->ports_mutex
);
319 write_lock_irqsave(&client
->ports_lock
, flags
);
320 if (! list_empty(&client
->ports_list_head
)) {
321 list_add(&deleted_list
, &client
->ports_list_head
);
322 list_del_init(&client
->ports_list_head
);
324 INIT_LIST_HEAD(&deleted_list
);
326 client
->num_ports
= 0;
327 write_unlock_irqrestore(&client
->ports_lock
, flags
);
329 /* remove each port in deleted_list */
330 list_for_each_entry_safe(port
, tmp
, &deleted_list
, list
) {
331 list_del(&port
->list
);
332 snd_seq_system_client_ev_port_exit(port
->addr
.client
, port
->addr
.port
);
333 port_delete(client
, port
);
335 mutex_unlock(&client
->ports_mutex
);
339 /* set port info fields */
340 int snd_seq_set_port_info(struct snd_seq_client_port
* port
,
341 struct snd_seq_port_info
* info
)
343 if (snd_BUG_ON(!port
|| !info
))
348 strlcpy(port
->name
, info
->name
, sizeof(port
->name
));
350 /* set capabilities */
351 port
->capability
= info
->capability
;
354 port
->type
= info
->type
;
356 /* information about supported channels/voices */
357 port
->midi_channels
= info
->midi_channels
;
358 port
->midi_voices
= info
->midi_voices
;
359 port
->synth_voices
= info
->synth_voices
;
362 port
->timestamping
= (info
->flags
& SNDRV_SEQ_PORT_FLG_TIMESTAMP
) ? 1 : 0;
363 port
->time_real
= (info
->flags
& SNDRV_SEQ_PORT_FLG_TIME_REAL
) ? 1 : 0;
364 port
->time_queue
= info
->time_queue
;
369 /* get port info fields */
370 int snd_seq_get_port_info(struct snd_seq_client_port
* port
,
371 struct snd_seq_port_info
* info
)
373 if (snd_BUG_ON(!port
|| !info
))
377 strlcpy(info
->name
, port
->name
, sizeof(info
->name
));
379 /* get capabilities */
380 info
->capability
= port
->capability
;
383 info
->type
= port
->type
;
385 /* information about supported channels/voices */
386 info
->midi_channels
= port
->midi_channels
;
387 info
->midi_voices
= port
->midi_voices
;
388 info
->synth_voices
= port
->synth_voices
;
390 /* get subscriber counts */
391 info
->read_use
= port
->c_src
.count
;
392 info
->write_use
= port
->c_dest
.count
;
396 if (port
->timestamping
) {
397 info
->flags
|= SNDRV_SEQ_PORT_FLG_TIMESTAMP
;
399 info
->flags
|= SNDRV_SEQ_PORT_FLG_TIME_REAL
;
400 info
->time_queue
= port
->time_queue
;
409 * call callback functions (if any):
410 * the callbacks are invoked only when the first (for connection) or
411 * the last subscription (for disconnection) is done. Second or later
412 * subscription results in increment of counter, but no callback is
414 * This feature is useful if these callbacks are associated with
415 * initialization or termination of devices (see seq_midi.c).
417 * If callback_all option is set, the callback function is invoked
418 * at each connection/disconnection.
421 static int subscribe_port(struct snd_seq_client
*client
,
422 struct snd_seq_client_port
*port
,
423 struct snd_seq_port_subs_info
*grp
,
424 struct snd_seq_port_subscribe
*info
,
429 if (!try_module_get(port
->owner
))
432 if (grp
->open
&& (port
->callback_all
|| grp
->count
== 1)) {
433 err
= grp
->open(port
->private_data
, info
);
435 module_put(port
->owner
);
439 if (err
>= 0 && send_ack
&& client
->type
== USER_CLIENT
)
440 snd_seq_client_notify_subscription(port
->addr
.client
, port
->addr
.port
,
441 info
, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED
);
446 static int unsubscribe_port(struct snd_seq_client
*client
,
447 struct snd_seq_client_port
*port
,
448 struct snd_seq_port_subs_info
*grp
,
449 struct snd_seq_port_subscribe
*info
,
457 if (grp
->close
&& (port
->callback_all
|| grp
->count
== 0))
458 err
= grp
->close(port
->private_data
, info
);
459 if (send_ack
&& client
->type
== USER_CLIENT
)
460 snd_seq_client_notify_subscription(port
->addr
.client
, port
->addr
.port
,
461 info
, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED
);
462 module_put(port
->owner
);
468 /* check if both addresses are identical */
469 static inline int addr_match(struct snd_seq_addr
*r
, struct snd_seq_addr
*s
)
471 return (r
->client
== s
->client
) && (r
->port
== s
->port
);
474 /* check the two subscribe info match */
475 /* if flags is zero, checks only sender and destination addresses */
476 static int match_subs_info(struct snd_seq_port_subscribe
*r
,
477 struct snd_seq_port_subscribe
*s
)
479 if (addr_match(&r
->sender
, &s
->sender
) &&
480 addr_match(&r
->dest
, &s
->dest
)) {
481 if (r
->flags
&& r
->flags
== s
->flags
)
482 return r
->queue
== s
->queue
;
489 static int check_and_subscribe_port(struct snd_seq_client
*client
,
490 struct snd_seq_client_port
*port
,
491 struct snd_seq_subscribers
*subs
,
492 bool is_src
, bool exclusive
, bool ack
)
494 struct snd_seq_port_subs_info
*grp
;
496 struct snd_seq_subscribers
*s
;
499 grp
= is_src
? &port
->c_src
: &port
->c_dest
;
501 down_write(&grp
->list_mutex
);
503 if (!list_empty(&grp
->list_head
))
508 /* check whether already exists */
509 list_for_each(p
, &grp
->list_head
) {
510 s
= get_subscriber(p
, is_src
);
511 if (match_subs_info(&subs
->info
, &s
->info
))
516 err
= subscribe_port(client
, port
, grp
, &subs
->info
, ack
);
523 write_lock_irq(&grp
->list_lock
);
525 list_add_tail(&subs
->src_list
, &grp
->list_head
);
527 list_add_tail(&subs
->dest_list
, &grp
->list_head
);
528 grp
->exclusive
= exclusive
;
529 atomic_inc(&subs
->ref_count
);
530 write_unlock_irq(&grp
->list_lock
);
534 up_write(&grp
->list_mutex
);
538 static void delete_and_unsubscribe_port(struct snd_seq_client
*client
,
539 struct snd_seq_client_port
*port
,
540 struct snd_seq_subscribers
*subs
,
541 bool is_src
, bool ack
)
543 struct snd_seq_port_subs_info
*grp
;
544 struct list_head
*list
;
547 grp
= is_src
? &port
->c_src
: &port
->c_dest
;
548 list
= is_src
? &subs
->src_list
: &subs
->dest_list
;
549 down_write(&grp
->list_mutex
);
550 write_lock_irq(&grp
->list_lock
);
551 empty
= list_empty(list
);
555 write_unlock_irq(&grp
->list_lock
);
556 up_write(&grp
->list_mutex
);
559 unsubscribe_port(client
, port
, grp
, &subs
->info
, ack
);
562 /* connect two ports */
563 int snd_seq_port_connect(struct snd_seq_client
*connector
,
564 struct snd_seq_client
*src_client
,
565 struct snd_seq_client_port
*src_port
,
566 struct snd_seq_client
*dest_client
,
567 struct snd_seq_client_port
*dest_port
,
568 struct snd_seq_port_subscribe
*info
)
570 struct snd_seq_subscribers
*subs
;
574 subs
= kzalloc(sizeof(*subs
), GFP_KERNEL
);
579 atomic_set(&subs
->ref_count
, 0);
580 INIT_LIST_HEAD(&subs
->src_list
);
581 INIT_LIST_HEAD(&subs
->dest_list
);
583 exclusive
= !!(info
->flags
& SNDRV_SEQ_PORT_SUBS_EXCLUSIVE
);
585 err
= check_and_subscribe_port(src_client
, src_port
, subs
, true,
587 connector
->number
!= src_client
->number
);
590 err
= check_and_subscribe_port(dest_client
, dest_port
, subs
, false,
592 connector
->number
!= dest_client
->number
);
599 delete_and_unsubscribe_port(src_client
, src_port
, subs
, true,
600 connector
->number
!= src_client
->number
);
606 /* remove the connection */
607 int snd_seq_port_disconnect(struct snd_seq_client
*connector
,
608 struct snd_seq_client
*src_client
,
609 struct snd_seq_client_port
*src_port
,
610 struct snd_seq_client
*dest_client
,
611 struct snd_seq_client_port
*dest_port
,
612 struct snd_seq_port_subscribe
*info
)
614 struct snd_seq_port_subs_info
*src
= &src_port
->c_src
;
615 struct snd_seq_subscribers
*subs
;
618 down_write(&src
->list_mutex
);
619 /* look for the connection */
620 list_for_each_entry(subs
, &src
->list_head
, src_list
) {
621 if (match_subs_info(info
, &subs
->info
)) {
622 atomic_dec(&subs
->ref_count
); /* mark as not ready */
627 up_write(&src
->list_mutex
);
631 delete_and_unsubscribe_port(src_client
, src_port
, subs
, true,
632 connector
->number
!= src_client
->number
);
633 delete_and_unsubscribe_port(dest_client
, dest_port
, subs
, false,
634 connector
->number
!= dest_client
->number
);
640 /* get matched subscriber */
641 struct snd_seq_subscribers
*snd_seq_port_get_subscription(struct snd_seq_port_subs_info
*src_grp
,
642 struct snd_seq_addr
*dest_addr
)
644 struct snd_seq_subscribers
*s
, *found
= NULL
;
646 down_read(&src_grp
->list_mutex
);
647 list_for_each_entry(s
, &src_grp
->list_head
, src_list
) {
648 if (addr_match(dest_addr
, &s
->info
.dest
)) {
653 up_read(&src_grp
->list_mutex
);
658 * Attach a device driver that wants to receive events from the
659 * sequencer. Returns the new port number on success.
660 * A driver that wants to receive the events converted to midi, will
661 * use snd_seq_midisynth_register_port().
664 int snd_seq_event_port_attach(int client
,
665 struct snd_seq_port_callback
*pcbp
,
666 int cap
, int type
, int midi_channels
,
667 int midi_voices
, char *portname
)
669 struct snd_seq_port_info portinfo
;
672 /* Set up the port */
673 memset(&portinfo
, 0, sizeof(portinfo
));
674 portinfo
.addr
.client
= client
;
675 strlcpy(portinfo
.name
, portname
? portname
: "Unamed port",
676 sizeof(portinfo
.name
));
678 portinfo
.capability
= cap
;
679 portinfo
.type
= type
;
680 portinfo
.kernel
= pcbp
;
681 portinfo
.midi_channels
= midi_channels
;
682 portinfo
.midi_voices
= midi_voices
;
685 ret
= snd_seq_kernel_client_ctl(client
,
686 SNDRV_SEQ_IOCTL_CREATE_PORT
,
690 ret
= portinfo
.addr
.port
;
695 EXPORT_SYMBOL(snd_seq_event_port_attach
);
698 * Detach the driver from a port.
701 int snd_seq_event_port_detach(int client
, int port
)
703 struct snd_seq_port_info portinfo
;
706 memset(&portinfo
, 0, sizeof(portinfo
));
707 portinfo
.addr
.client
= client
;
708 portinfo
.addr
.port
= port
;
709 err
= snd_seq_kernel_client_ctl(client
,
710 SNDRV_SEQ_IOCTL_DELETE_PORT
,
716 EXPORT_SYMBOL(snd_seq_event_port_detach
);