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 down(&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 up(&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 down_write(&grp
->list_mutex
);
225 list_for_each_safe(p
, n
, &grp
->list_head
) {
226 struct snd_seq_subscribers
*subs
;
227 struct snd_seq_client
*c
;
228 struct snd_seq_client_port
*aport
;
230 if (grptype
== SRC_LIST
) {
231 subs
= list_entry(p
, struct snd_seq_subscribers
, src_list
);
232 aport
= get_client_port(&subs
->info
.dest
, &c
);
234 subs
= list_entry(p
, struct snd_seq_subscribers
, dest_list
);
235 aport
= get_client_port(&subs
->info
.sender
, &c
);
238 unsubscribe_port(client
, port
, grp
, &subs
->info
, 0);
240 /* looks like the connected port is being deleted.
241 * we decrease the counter, and when both ports are deleted
242 * remove the subscriber info
244 if (atomic_dec_and_test(&subs
->ref_count
))
247 /* ok we got the connected port */
248 struct snd_seq_port_subs_info
*agrp
;
249 agrp
= (grptype
== SRC_LIST
) ? &aport
->c_dest
: &aport
->c_src
;
250 down_write(&agrp
->list_mutex
);
251 if (grptype
== SRC_LIST
)
252 list_del(&subs
->dest_list
);
254 list_del(&subs
->src_list
);
255 unsubscribe_port(c
, aport
, agrp
, &subs
->info
, 1);
257 up_write(&agrp
->list_mutex
);
258 snd_seq_port_unlock(aport
);
259 snd_seq_client_unlock(c
);
262 up_write(&grp
->list_mutex
);
265 /* delete port data */
266 static int port_delete(struct snd_seq_client
*client
,
267 struct snd_seq_client_port
*port
)
269 /* set closing flag and wait for all port access are gone */
271 snd_use_lock_sync(&port
->use_lock
);
273 /* clear subscribers info */
274 clear_subscriber_list(client
, port
, &port
->c_src
, SRC_LIST
);
275 clear_subscriber_list(client
, port
, &port
->c_dest
, DEST_LIST
);
277 if (port
->private_free
)
278 port
->private_free(port
->private_data
);
280 snd_assert(port
->c_src
.count
== 0,);
281 snd_assert(port
->c_dest
.count
== 0,);
288 /* delete a port with the given port id */
289 int snd_seq_delete_port(struct snd_seq_client
*client
, int port
)
293 struct snd_seq_client_port
*found
= NULL
;
295 down(&client
->ports_mutex
);
296 write_lock_irqsave(&client
->ports_lock
, flags
);
297 list_for_each(l
, &client
->ports_list_head
) {
298 struct snd_seq_client_port
*p
= list_entry(l
, struct snd_seq_client_port
, list
);
299 if (p
->addr
.port
== port
) {
300 /* ok found. delete from the list at first */
307 write_unlock_irqrestore(&client
->ports_lock
, flags
);
308 up(&client
->ports_mutex
);
310 return port_delete(client
, found
);
315 /* delete the all ports belonging to the given client */
316 int snd_seq_delete_all_ports(struct snd_seq_client
*client
)
319 struct list_head deleted_list
, *p
, *n
;
321 /* move the port list to deleted_list, and
322 * clear the port list in the client data.
324 down(&client
->ports_mutex
);
325 write_lock_irqsave(&client
->ports_lock
, flags
);
326 if (! list_empty(&client
->ports_list_head
)) {
327 __list_add(&deleted_list
,
328 client
->ports_list_head
.prev
,
329 client
->ports_list_head
.next
);
330 INIT_LIST_HEAD(&client
->ports_list_head
);
332 INIT_LIST_HEAD(&deleted_list
);
334 client
->num_ports
= 0;
335 write_unlock_irqrestore(&client
->ports_lock
, flags
);
337 /* remove each port in deleted_list */
338 list_for_each_safe(p
, n
, &deleted_list
) {
339 struct snd_seq_client_port
*port
= list_entry(p
, struct snd_seq_client_port
, list
);
341 snd_seq_system_client_ev_port_exit(port
->addr
.client
, port
->addr
.port
);
342 port_delete(client
, port
);
344 up(&client
->ports_mutex
);
348 /* set port info fields */
349 int snd_seq_set_port_info(struct snd_seq_client_port
* port
,
350 struct snd_seq_port_info
* info
)
352 snd_assert(port
&& info
, return -EINVAL
);
356 strlcpy(port
->name
, info
->name
, sizeof(port
->name
));
358 /* set capabilities */
359 port
->capability
= info
->capability
;
362 port
->type
= info
->type
;
364 /* information about supported channels/voices */
365 port
->midi_channels
= info
->midi_channels
;
366 port
->midi_voices
= info
->midi_voices
;
367 port
->synth_voices
= info
->synth_voices
;
370 port
->timestamping
= (info
->flags
& SNDRV_SEQ_PORT_FLG_TIMESTAMP
) ? 1 : 0;
371 port
->time_real
= (info
->flags
& SNDRV_SEQ_PORT_FLG_TIME_REAL
) ? 1 : 0;
372 port
->time_queue
= info
->time_queue
;
377 /* get port info fields */
378 int snd_seq_get_port_info(struct snd_seq_client_port
* port
,
379 struct snd_seq_port_info
* info
)
381 snd_assert(port
&& info
, return -EINVAL
);
384 strlcpy(info
->name
, port
->name
, sizeof(info
->name
));
386 /* get capabilities */
387 info
->capability
= port
->capability
;
390 info
->type
= port
->type
;
392 /* information about supported channels/voices */
393 info
->midi_channels
= port
->midi_channels
;
394 info
->midi_voices
= port
->midi_voices
;
395 info
->synth_voices
= port
->synth_voices
;
397 /* get subscriber counts */
398 info
->read_use
= port
->c_src
.count
;
399 info
->write_use
= port
->c_dest
.count
;
403 if (port
->timestamping
) {
404 info
->flags
|= SNDRV_SEQ_PORT_FLG_TIMESTAMP
;
406 info
->flags
|= SNDRV_SEQ_PORT_FLG_TIME_REAL
;
407 info
->time_queue
= port
->time_queue
;
416 * call callback functions (if any):
417 * the callbacks are invoked only when the first (for connection) or
418 * the last subscription (for disconnection) is done. Second or later
419 * subscription results in increment of counter, but no callback is
421 * This feature is useful if these callbacks are associated with
422 * initialization or termination of devices (see seq_midi.c).
424 * If callback_all option is set, the callback function is invoked
425 * at each connnection/disconnection.
428 static int subscribe_port(struct snd_seq_client
*client
,
429 struct snd_seq_client_port
*port
,
430 struct snd_seq_port_subs_info
*grp
,
431 struct snd_seq_port_subscribe
*info
,
436 if (!try_module_get(port
->owner
))
439 if (grp
->open
&& (port
->callback_all
|| grp
->count
== 1)) {
440 err
= grp
->open(port
->private_data
, info
);
442 module_put(port
->owner
);
446 if (err
>= 0 && send_ack
&& client
->type
== USER_CLIENT
)
447 snd_seq_client_notify_subscription(port
->addr
.client
, port
->addr
.port
,
448 info
, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED
);
453 static int unsubscribe_port(struct snd_seq_client
*client
,
454 struct snd_seq_client_port
*port
,
455 struct snd_seq_port_subs_info
*grp
,
456 struct snd_seq_port_subscribe
*info
,
464 if (grp
->close
&& (port
->callback_all
|| grp
->count
== 0))
465 err
= grp
->close(port
->private_data
, info
);
466 if (send_ack
&& client
->type
== USER_CLIENT
)
467 snd_seq_client_notify_subscription(port
->addr
.client
, port
->addr
.port
,
468 info
, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED
);
469 module_put(port
->owner
);
475 /* check if both addresses are identical */
476 static inline int addr_match(struct snd_seq_addr
*r
, struct snd_seq_addr
*s
)
478 return (r
->client
== s
->client
) && (r
->port
== s
->port
);
481 /* check the two subscribe info match */
482 /* if flags is zero, checks only sender and destination addresses */
483 static int match_subs_info(struct snd_seq_port_subscribe
*r
,
484 struct snd_seq_port_subscribe
*s
)
486 if (addr_match(&r
->sender
, &s
->sender
) &&
487 addr_match(&r
->dest
, &s
->dest
)) {
488 if (r
->flags
&& r
->flags
== s
->flags
)
489 return r
->queue
== s
->queue
;
497 /* connect two ports */
498 int snd_seq_port_connect(struct snd_seq_client
*connector
,
499 struct snd_seq_client
*src_client
,
500 struct snd_seq_client_port
*src_port
,
501 struct snd_seq_client
*dest_client
,
502 struct snd_seq_client_port
*dest_port
,
503 struct snd_seq_port_subscribe
*info
)
505 struct snd_seq_port_subs_info
*src
= &src_port
->c_src
;
506 struct snd_seq_port_subs_info
*dest
= &dest_port
->c_dest
;
507 struct snd_seq_subscribers
*subs
;
509 int err
, src_called
= 0;
513 subs
= kzalloc(sizeof(*subs
), GFP_KERNEL
);
518 atomic_set(&subs
->ref_count
, 2);
520 down_write(&src
->list_mutex
);
521 down_write(&dest
->list_mutex
);
523 exclusive
= info
->flags
& SNDRV_SEQ_PORT_SUBS_EXCLUSIVE
? 1 : 0;
526 if (! list_empty(&src
->list_head
) || ! list_empty(&dest
->list_head
))
529 if (src
->exclusive
|| dest
->exclusive
)
531 /* check whether already exists */
532 list_for_each(p
, &src
->list_head
) {
533 struct snd_seq_subscribers
*s
= list_entry(p
, struct snd_seq_subscribers
, src_list
);
534 if (match_subs_info(info
, &s
->info
))
537 list_for_each(p
, &dest
->list_head
) {
538 struct snd_seq_subscribers
*s
= list_entry(p
, struct snd_seq_subscribers
, dest_list
);
539 if (match_subs_info(info
, &s
->info
))
544 if ((err
= subscribe_port(src_client
, src_port
, src
, info
,
545 connector
->number
!= src_client
->number
)) < 0)
549 if ((err
= subscribe_port(dest_client
, dest_port
, dest
, info
,
550 connector
->number
!= dest_client
->number
)) < 0)
554 write_lock_irqsave(&src
->list_lock
, flags
);
555 // write_lock(&dest->list_lock); // no other lock yet
556 list_add_tail(&subs
->src_list
, &src
->list_head
);
557 list_add_tail(&subs
->dest_list
, &dest
->list_head
);
558 // write_unlock(&dest->list_lock); // no other lock yet
559 write_unlock_irqrestore(&src
->list_lock
, flags
);
561 src
->exclusive
= dest
->exclusive
= exclusive
;
563 up_write(&dest
->list_mutex
);
564 up_write(&src
->list_mutex
);
569 unsubscribe_port(src_client
, src_port
, src
, info
,
570 connector
->number
!= src_client
->number
);
572 up_write(&dest
->list_mutex
);
573 up_write(&src
->list_mutex
);
578 /* remove the connection */
579 int snd_seq_port_disconnect(struct snd_seq_client
*connector
,
580 struct snd_seq_client
*src_client
,
581 struct snd_seq_client_port
*src_port
,
582 struct snd_seq_client
*dest_client
,
583 struct snd_seq_client_port
*dest_port
,
584 struct snd_seq_port_subscribe
*info
)
586 struct snd_seq_port_subs_info
*src
= &src_port
->c_src
;
587 struct snd_seq_port_subs_info
*dest
= &dest_port
->c_dest
;
588 struct snd_seq_subscribers
*subs
;
593 down_write(&src
->list_mutex
);
594 down_write(&dest
->list_mutex
);
596 /* look for the connection */
597 list_for_each(p
, &src
->list_head
) {
598 subs
= list_entry(p
, struct snd_seq_subscribers
, src_list
);
599 if (match_subs_info(info
, &subs
->info
)) {
600 write_lock_irqsave(&src
->list_lock
, flags
);
601 // write_lock(&dest->list_lock); // no lock yet
602 list_del(&subs
->src_list
);
603 list_del(&subs
->dest_list
);
604 // write_unlock(&dest->list_lock);
605 write_unlock_irqrestore(&src
->list_lock
, flags
);
606 src
->exclusive
= dest
->exclusive
= 0;
607 unsubscribe_port(src_client
, src_port
, src
, info
,
608 connector
->number
!= src_client
->number
);
609 unsubscribe_port(dest_client
, dest_port
, dest
, info
,
610 connector
->number
!= dest_client
->number
);
617 up_write(&dest
->list_mutex
);
618 up_write(&src
->list_mutex
);
623 /* get matched subscriber */
624 struct snd_seq_subscribers
*snd_seq_port_get_subscription(struct snd_seq_port_subs_info
*src_grp
,
625 struct snd_seq_addr
*dest_addr
)
628 struct snd_seq_subscribers
*s
, *found
= NULL
;
630 down_read(&src_grp
->list_mutex
);
631 list_for_each(p
, &src_grp
->list_head
) {
632 s
= list_entry(p
, struct snd_seq_subscribers
, src_list
);
633 if (addr_match(dest_addr
, &s
->info
.dest
)) {
638 up_read(&src_grp
->list_mutex
);
643 * Attach a device driver that wants to receive events from the
644 * sequencer. Returns the new port number on success.
645 * A driver that wants to receive the events converted to midi, will
646 * use snd_seq_midisynth_register_port().
649 int snd_seq_event_port_attach(int client
,
650 struct snd_seq_port_callback
*pcbp
,
651 int cap
, int type
, int midi_channels
,
652 int midi_voices
, char *portname
)
654 struct snd_seq_port_info portinfo
;
657 /* Set up the port */
658 memset(&portinfo
, 0, sizeof(portinfo
));
659 portinfo
.addr
.client
= client
;
660 strlcpy(portinfo
.name
, portname
? portname
: "Unamed port",
661 sizeof(portinfo
.name
));
663 portinfo
.capability
= cap
;
664 portinfo
.type
= type
;
665 portinfo
.kernel
= pcbp
;
666 portinfo
.midi_channels
= midi_channels
;
667 portinfo
.midi_voices
= midi_voices
;
670 ret
= snd_seq_kernel_client_ctl(client
,
671 SNDRV_SEQ_IOCTL_CREATE_PORT
,
675 ret
= portinfo
.addr
.port
;
682 * Detach the driver from a port.
685 int snd_seq_event_port_detach(int client
, int port
)
687 struct snd_seq_port_info portinfo
;
690 memset(&portinfo
, 0, sizeof(portinfo
));
691 portinfo
.addr
.client
= client
;
692 portinfo
.addr
.port
= port
;
693 err
= snd_seq_kernel_client_ctl(client
,
694 SNDRV_SEQ_IOCTL_DELETE_PORT
,