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
, &client
->ports_list_head
);
326 list_del_init(&client
->ports_list_head
);
328 INIT_LIST_HEAD(&deleted_list
);
330 client
->num_ports
= 0;
331 write_unlock_irqrestore(&client
->ports_lock
, flags
);
333 /* remove each port in deleted_list */
334 list_for_each_safe(p
, n
, &deleted_list
) {
335 struct snd_seq_client_port
*port
= list_entry(p
, struct snd_seq_client_port
, list
);
337 snd_seq_system_client_ev_port_exit(port
->addr
.client
, port
->addr
.port
);
338 port_delete(client
, port
);
340 mutex_unlock(&client
->ports_mutex
);
344 /* set port info fields */
345 int snd_seq_set_port_info(struct snd_seq_client_port
* port
,
346 struct snd_seq_port_info
* info
)
348 snd_assert(port
&& info
, return -EINVAL
);
352 strlcpy(port
->name
, info
->name
, sizeof(port
->name
));
354 /* set capabilities */
355 port
->capability
= info
->capability
;
358 port
->type
= info
->type
;
360 /* information about supported channels/voices */
361 port
->midi_channels
= info
->midi_channels
;
362 port
->midi_voices
= info
->midi_voices
;
363 port
->synth_voices
= info
->synth_voices
;
366 port
->timestamping
= (info
->flags
& SNDRV_SEQ_PORT_FLG_TIMESTAMP
) ? 1 : 0;
367 port
->time_real
= (info
->flags
& SNDRV_SEQ_PORT_FLG_TIME_REAL
) ? 1 : 0;
368 port
->time_queue
= info
->time_queue
;
373 /* get port info fields */
374 int snd_seq_get_port_info(struct snd_seq_client_port
* port
,
375 struct snd_seq_port_info
* info
)
377 snd_assert(port
&& info
, return -EINVAL
);
380 strlcpy(info
->name
, port
->name
, sizeof(info
->name
));
382 /* get capabilities */
383 info
->capability
= port
->capability
;
386 info
->type
= port
->type
;
388 /* information about supported channels/voices */
389 info
->midi_channels
= port
->midi_channels
;
390 info
->midi_voices
= port
->midi_voices
;
391 info
->synth_voices
= port
->synth_voices
;
393 /* get subscriber counts */
394 info
->read_use
= port
->c_src
.count
;
395 info
->write_use
= port
->c_dest
.count
;
399 if (port
->timestamping
) {
400 info
->flags
|= SNDRV_SEQ_PORT_FLG_TIMESTAMP
;
402 info
->flags
|= SNDRV_SEQ_PORT_FLG_TIME_REAL
;
403 info
->time_queue
= port
->time_queue
;
412 * call callback functions (if any):
413 * the callbacks are invoked only when the first (for connection) or
414 * the last subscription (for disconnection) is done. Second or later
415 * subscription results in increment of counter, but no callback is
417 * This feature is useful if these callbacks are associated with
418 * initialization or termination of devices (see seq_midi.c).
420 * If callback_all option is set, the callback function is invoked
421 * at each connnection/disconnection.
424 static int subscribe_port(struct snd_seq_client
*client
,
425 struct snd_seq_client_port
*port
,
426 struct snd_seq_port_subs_info
*grp
,
427 struct snd_seq_port_subscribe
*info
,
432 if (!try_module_get(port
->owner
))
435 if (grp
->open
&& (port
->callback_all
|| grp
->count
== 1)) {
436 err
= grp
->open(port
->private_data
, info
);
438 module_put(port
->owner
);
442 if (err
>= 0 && send_ack
&& client
->type
== USER_CLIENT
)
443 snd_seq_client_notify_subscription(port
->addr
.client
, port
->addr
.port
,
444 info
, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED
);
449 static int unsubscribe_port(struct snd_seq_client
*client
,
450 struct snd_seq_client_port
*port
,
451 struct snd_seq_port_subs_info
*grp
,
452 struct snd_seq_port_subscribe
*info
,
460 if (grp
->close
&& (port
->callback_all
|| grp
->count
== 0))
461 err
= grp
->close(port
->private_data
, info
);
462 if (send_ack
&& client
->type
== USER_CLIENT
)
463 snd_seq_client_notify_subscription(port
->addr
.client
, port
->addr
.port
,
464 info
, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED
);
465 module_put(port
->owner
);
471 /* check if both addresses are identical */
472 static inline int addr_match(struct snd_seq_addr
*r
, struct snd_seq_addr
*s
)
474 return (r
->client
== s
->client
) && (r
->port
== s
->port
);
477 /* check the two subscribe info match */
478 /* if flags is zero, checks only sender and destination addresses */
479 static int match_subs_info(struct snd_seq_port_subscribe
*r
,
480 struct snd_seq_port_subscribe
*s
)
482 if (addr_match(&r
->sender
, &s
->sender
) &&
483 addr_match(&r
->dest
, &s
->dest
)) {
484 if (r
->flags
&& r
->flags
== s
->flags
)
485 return r
->queue
== s
->queue
;
493 /* connect two ports */
494 int snd_seq_port_connect(struct snd_seq_client
*connector
,
495 struct snd_seq_client
*src_client
,
496 struct snd_seq_client_port
*src_port
,
497 struct snd_seq_client
*dest_client
,
498 struct snd_seq_client_port
*dest_port
,
499 struct snd_seq_port_subscribe
*info
)
501 struct snd_seq_port_subs_info
*src
= &src_port
->c_src
;
502 struct snd_seq_port_subs_info
*dest
= &dest_port
->c_dest
;
503 struct snd_seq_subscribers
*subs
;
505 int err
, src_called
= 0;
509 subs
= kzalloc(sizeof(*subs
), GFP_KERNEL
);
514 atomic_set(&subs
->ref_count
, 2);
516 down_write(&src
->list_mutex
);
517 down_write_nested(&dest
->list_mutex
, SINGLE_DEPTH_NESTING
);
519 exclusive
= info
->flags
& SNDRV_SEQ_PORT_SUBS_EXCLUSIVE
? 1 : 0;
522 if (! list_empty(&src
->list_head
) || ! list_empty(&dest
->list_head
))
525 if (src
->exclusive
|| dest
->exclusive
)
527 /* check whether already exists */
528 list_for_each(p
, &src
->list_head
) {
529 struct snd_seq_subscribers
*s
= list_entry(p
, struct snd_seq_subscribers
, src_list
);
530 if (match_subs_info(info
, &s
->info
))
533 list_for_each(p
, &dest
->list_head
) {
534 struct snd_seq_subscribers
*s
= list_entry(p
, struct snd_seq_subscribers
, dest_list
);
535 if (match_subs_info(info
, &s
->info
))
540 if ((err
= subscribe_port(src_client
, src_port
, src
, info
,
541 connector
->number
!= src_client
->number
)) < 0)
545 if ((err
= subscribe_port(dest_client
, dest_port
, dest
, info
,
546 connector
->number
!= dest_client
->number
)) < 0)
550 write_lock_irqsave(&src
->list_lock
, flags
);
551 // write_lock(&dest->list_lock); // no other lock yet
552 list_add_tail(&subs
->src_list
, &src
->list_head
);
553 list_add_tail(&subs
->dest_list
, &dest
->list_head
);
554 // write_unlock(&dest->list_lock); // no other lock yet
555 write_unlock_irqrestore(&src
->list_lock
, flags
);
557 src
->exclusive
= dest
->exclusive
= exclusive
;
559 up_write(&dest
->list_mutex
);
560 up_write(&src
->list_mutex
);
565 unsubscribe_port(src_client
, src_port
, src
, info
,
566 connector
->number
!= src_client
->number
);
568 up_write(&dest
->list_mutex
);
569 up_write(&src
->list_mutex
);
574 /* remove the connection */
575 int snd_seq_port_disconnect(struct snd_seq_client
*connector
,
576 struct snd_seq_client
*src_client
,
577 struct snd_seq_client_port
*src_port
,
578 struct snd_seq_client
*dest_client
,
579 struct snd_seq_client_port
*dest_port
,
580 struct snd_seq_port_subscribe
*info
)
582 struct snd_seq_port_subs_info
*src
= &src_port
->c_src
;
583 struct snd_seq_port_subs_info
*dest
= &dest_port
->c_dest
;
584 struct snd_seq_subscribers
*subs
;
589 down_write(&src
->list_mutex
);
590 down_write_nested(&dest
->list_mutex
, SINGLE_DEPTH_NESTING
);
592 /* look for the connection */
593 list_for_each(p
, &src
->list_head
) {
594 subs
= list_entry(p
, struct snd_seq_subscribers
, src_list
);
595 if (match_subs_info(info
, &subs
->info
)) {
596 write_lock_irqsave(&src
->list_lock
, flags
);
597 // write_lock(&dest->list_lock); // no lock yet
598 list_del(&subs
->src_list
);
599 list_del(&subs
->dest_list
);
600 // write_unlock(&dest->list_lock);
601 write_unlock_irqrestore(&src
->list_lock
, flags
);
602 src
->exclusive
= dest
->exclusive
= 0;
603 unsubscribe_port(src_client
, src_port
, src
, info
,
604 connector
->number
!= src_client
->number
);
605 unsubscribe_port(dest_client
, dest_port
, dest
, info
,
606 connector
->number
!= dest_client
->number
);
613 up_write(&dest
->list_mutex
);
614 up_write(&src
->list_mutex
);
619 /* get matched subscriber */
620 struct snd_seq_subscribers
*snd_seq_port_get_subscription(struct snd_seq_port_subs_info
*src_grp
,
621 struct snd_seq_addr
*dest_addr
)
624 struct snd_seq_subscribers
*s
, *found
= NULL
;
626 down_read(&src_grp
->list_mutex
);
627 list_for_each(p
, &src_grp
->list_head
) {
628 s
= list_entry(p
, struct snd_seq_subscribers
, src_list
);
629 if (addr_match(dest_addr
, &s
->info
.dest
)) {
634 up_read(&src_grp
->list_mutex
);
639 * Attach a device driver that wants to receive events from the
640 * sequencer. Returns the new port number on success.
641 * A driver that wants to receive the events converted to midi, will
642 * use snd_seq_midisynth_register_port().
645 int snd_seq_event_port_attach(int client
,
646 struct snd_seq_port_callback
*pcbp
,
647 int cap
, int type
, int midi_channels
,
648 int midi_voices
, char *portname
)
650 struct snd_seq_port_info portinfo
;
653 /* Set up the port */
654 memset(&portinfo
, 0, sizeof(portinfo
));
655 portinfo
.addr
.client
= client
;
656 strlcpy(portinfo
.name
, portname
? portname
: "Unamed port",
657 sizeof(portinfo
.name
));
659 portinfo
.capability
= cap
;
660 portinfo
.type
= type
;
661 portinfo
.kernel
= pcbp
;
662 portinfo
.midi_channels
= midi_channels
;
663 portinfo
.midi_voices
= midi_voices
;
666 ret
= snd_seq_kernel_client_ctl(client
,
667 SNDRV_SEQ_IOCTL_CREATE_PORT
,
671 ret
= portinfo
.addr
.port
;
676 EXPORT_SYMBOL(snd_seq_event_port_attach
);
679 * Detach the driver from a port.
682 int snd_seq_event_port_detach(int client
, int port
)
684 struct snd_seq_port_info portinfo
;
687 memset(&portinfo
, 0, sizeof(portinfo
));
688 portinfo
.addr
.client
= client
;
689 portinfo
.addr
.port
= port
;
690 err
= snd_seq_kernel_client_ctl(client
,
691 SNDRV_SEQ_IOCTL_DELETE_PORT
,
697 EXPORT_SYMBOL(snd_seq_event_port_detach
);