WorkStruct: make allyesconfig
[linux-2.6/linux-2.6-openrd.git] / sound / core / seq / seq_ports.c
blob8c64b58ff77bf3afb9e7f82e30209a15c68f0dc9
1 /*
2 * ALSA sequencer Ports
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
37 /*
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.
45 Possible actions:
47 1) leave it this way, a client does normaly does not have more than a few
48 ports
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,
60 int num)
62 struct list_head *p;
63 struct snd_seq_client_port *port;
65 if (client == NULL)
66 return NULL;
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) {
71 if (port->closing)
72 break; /* deleting now */
73 snd_use_lock_use(&port->use_lock);
74 read_unlock(&client->ports_lock);
75 return port;
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)
87 int num;
88 struct list_head *p;
89 struct snd_seq_client_port *port, *found;
91 num = pinfo->addr.port;
92 found = NULL;
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)
97 continue;
98 if (port->addr.port == num) {
99 found = port;
100 break;
102 if (found == NULL || port->addr.port < found->addr.port)
103 found = port;
105 if (found) {
106 if (found->closing)
107 found = NULL;
108 else
109 snd_use_lock_use(&found->use_lock);
111 read_unlock(&client->ports_lock);
112 return found;
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);
120 grp->count = 0;
121 grp->exclusive = 0;
122 rwlock_init(&grp->list_lock);
123 init_rwsem(&grp->list_mutex);
124 grp->open = NULL;
125 grp->close = NULL;
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,
131 int port)
133 unsigned long flags;
134 struct snd_seq_client_port *new_port;
135 struct list_head *l;
136 int num = -1;
138 /* sanity check */
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);
143 return NULL;
146 /* create a new port */
147 new_port = kzalloc(sizeof(*new_port), GFP_KERNEL);
148 if (! new_port) {
149 snd_printd("malloc failed for registering client port\n");
150 return NULL; /* failure, out of memory */
152 /* init port data */
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)
167 break;
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);
173 client->num_ports++;
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);
179 return new_port;
182 /* */
183 enum group_type {
184 SRC_LIST, DEST_LIST
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);
202 if (*cp) {
203 p = snd_seq_port_use_ptr(*cp, addr->port);
204 if (! p) {
205 snd_seq_client_unlock(*cp);
206 *cp = NULL;
208 return p;
210 return NULL;
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,
220 int grptype)
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);
232 } else {
233 subs = list_entry(p, struct snd_seq_subscribers, dest_list);
234 aport = get_client_port(&subs->info.sender, &c);
236 list_del(p);
237 unsubscribe_port(client, port, grp, &subs->info, 0);
238 if (!aport) {
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))
244 kfree(subs);
245 } else {
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);
252 else
253 list_del(&subs->src_list);
254 unsubscribe_port(c, aport, agrp, &subs->info, 1);
255 kfree(subs);
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 */
268 port->closing = 1;
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,);
281 kfree(port);
282 return 0;
286 /* delete a port with the given port id */
287 int snd_seq_delete_port(struct snd_seq_client *client, int port)
289 unsigned long flags;
290 struct list_head *l;
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 */
299 list_del(l);
300 client->num_ports--;
301 found = p;
302 break;
305 write_unlock_irqrestore(&client->ports_lock, flags);
306 mutex_unlock(&client->ports_mutex);
307 if (found)
308 return port_delete(client, found);
309 else
310 return -ENOENT;
313 /* delete the all ports belonging to the given client */
314 int snd_seq_delete_all_ports(struct snd_seq_client *client)
316 unsigned long flags;
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);
327 } else {
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);
336 list_del(p);
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);
341 return 0;
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);
350 /* set port name */
351 if (info->name[0])
352 strlcpy(port->name, info->name, sizeof(port->name));
354 /* set capabilities */
355 port->capability = info->capability;
357 /* get port type */
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;
365 /* timestamping */
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;
370 return 0;
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);
379 /* get port name */
380 strlcpy(info->name, port->name, sizeof(info->name));
382 /* get capabilities */
383 info->capability = port->capability;
385 /* get port type */
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;
397 /* timestamping */
398 info->flags = 0;
399 if (port->timestamping) {
400 info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP;
401 if (port->time_real)
402 info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL;
403 info->time_queue = port->time_queue;
406 return 0;
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
416 * invoked.
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,
428 int send_ack)
430 int err = 0;
432 if (!try_module_get(port->owner))
433 return -EFAULT;
434 grp->count++;
435 if (grp->open && (port->callback_all || grp->count == 1)) {
436 err = grp->open(port->private_data, info);
437 if (err < 0) {
438 module_put(port->owner);
439 grp->count--;
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);
446 return err;
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,
453 int send_ack)
455 int err = 0;
457 if (! grp->count)
458 return -EINVAL;
459 grp->count--;
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);
466 return err;
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;
486 else if (! r->flags)
487 return 1;
489 return 0;
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;
504 struct list_head *p;
505 int err, src_called = 0;
506 unsigned long flags;
507 int exclusive;
509 subs = kzalloc(sizeof(*subs), GFP_KERNEL);
510 if (! subs)
511 return -ENOMEM;
513 subs->info = *info;
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;
520 err = -EBUSY;
521 if (exclusive) {
522 if (! list_empty(&src->list_head) || ! list_empty(&dest->list_head))
523 goto __error;
524 } else {
525 if (src->exclusive || dest->exclusive)
526 goto __error;
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))
531 goto __error;
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))
536 goto __error;
540 if ((err = subscribe_port(src_client, src_port, src, info,
541 connector->number != src_client->number)) < 0)
542 goto __error;
543 src_called = 1;
545 if ((err = subscribe_port(dest_client, dest_port, dest, info,
546 connector->number != dest_client->number)) < 0)
547 goto __error;
549 /* add to list */
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);
561 return 0;
563 __error:
564 if (src_called)
565 unsubscribe_port(src_client, src_port, src, info,
566 connector->number != src_client->number);
567 kfree(subs);
568 up_write(&dest->list_mutex);
569 up_write(&src->list_mutex);
570 return err;
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;
585 struct list_head *p;
586 int err = -ENOENT;
587 unsigned long flags;
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);
607 kfree(subs);
608 err = 0;
609 break;
613 up_write(&dest->list_mutex);
614 up_write(&src->list_mutex);
615 return err;
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)
623 struct list_head *p;
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)) {
630 found = s;
631 break;
634 up_read(&src_grp->list_mutex);
635 return found;
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().
644 /* exported */
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;
651 int ret;
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;
665 /* Create it */
666 ret = snd_seq_kernel_client_ctl(client,
667 SNDRV_SEQ_IOCTL_CREATE_PORT,
668 &portinfo);
670 if (ret >= 0)
671 ret = portinfo.addr.port;
673 return ret;
676 EXPORT_SYMBOL(snd_seq_event_port_attach);
679 * Detach the driver from a port.
681 /* exported */
682 int snd_seq_event_port_detach(int client, int port)
684 struct snd_seq_port_info portinfo;
685 int err;
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,
692 &portinfo);
694 return err;
697 EXPORT_SYMBOL(snd_seq_event_port_detach);