Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / sound / core / seq / seq_ports.c
1 /*
2  *   ALSA sequencer Ports
3  *   Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
4  *                         Jaroslav Kysela <perex@suse.cz>
5  *
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22
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"
29
30 /*
31
32    registration of client ports
33
34  */
35
36
37 /* 
38
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.
44
45 Possible actions:
46
47 1) leave it this way, a client does normaly does not have more than a few
48 ports
49
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.
55
56 */
57
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)
61 {
62         struct list_head *p;
63         struct snd_seq_client_port *port;
64
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;
76                 }
77         }
78         read_unlock(&client->ports_lock);
79         return NULL;            /* not found */
80 }
81
82
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)
86 {
87         int num;
88         struct list_head *p;
89         struct snd_seq_client_port *port, *found;
90
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;
101                 }
102                 if (found == NULL || port->addr.port < found->addr.port)
103                         found = port;
104         }
105         if (found) {
106                 if (found->closing)
107                         found = NULL;
108                 else
109                         snd_use_lock_use(&found->use_lock);
110         }
111         read_unlock(&client->ports_lock);
112         return found;
113 }
114
115
116 /* initialize snd_seq_port_subs_info */
117 static void port_subs_info_init(struct snd_seq_port_subs_info *grp)
118 {
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;
126 }
127
128
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)
132 {
133         unsigned long flags;
134         struct snd_seq_client_port *new_port;
135         struct list_head *l;
136         int num = -1;
137         
138         /* sanity check */
139         snd_assert(client, return NULL);
140
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;
144         }
145
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 */
151         }
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);
160
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;
170         }
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);
178
179         return new_port;
180 }
181
182 /* */
183 enum group_type {
184         SRC_LIST, DEST_LIST
185 };
186
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);
195
196
197 static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr,
198                                                    struct snd_seq_client **cp)
199 {
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;
207                 }
208                 return p;
209         }
210         return NULL;
211 }
212
213 /*
214  * remove all subscribers on the list
215  * this is called from port_delete, for each src and dest list.
216  */
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)
221 {
222         struct list_head *p, *n;
223
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;
229
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);
233                 } else {
234                         subs = list_entry(p, struct snd_seq_subscribers, dest_list);
235                         aport = get_client_port(&subs->info.sender, &c);
236                 }
237                 list_del(p);
238                 unsubscribe_port(client, port, grp, &subs->info, 0);
239                 if (!aport) {
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
243                          */
244                         if (atomic_dec_and_test(&subs->ref_count))
245                                 kfree(subs);
246                 } else {
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);
253                         else
254                                 list_del(&subs->src_list);
255                         unsubscribe_port(c, aport, agrp, &subs->info, 1);
256                         kfree(subs);
257                         up_write(&agrp->list_mutex);
258                         snd_seq_port_unlock(aport);
259                         snd_seq_client_unlock(c);
260                 }
261         }
262         up_write(&grp->list_mutex);
263 }
264
265 /* delete port data */
266 static int port_delete(struct snd_seq_client *client,
267                        struct snd_seq_client_port *port)
268 {
269         /* set closing flag and wait for all port access are gone */
270         port->closing = 1;
271         snd_use_lock_sync(&port->use_lock); 
272
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);
276
277         if (port->private_free)
278                 port->private_free(port->private_data);
279
280         snd_assert(port->c_src.count == 0,);
281         snd_assert(port->c_dest.count == 0,);
282
283         kfree(port);
284         return 0;
285 }
286
287
288 /* delete a port with the given port id */
289 int snd_seq_delete_port(struct snd_seq_client *client, int port)
290 {
291         unsigned long flags;
292         struct list_head *l;
293         struct snd_seq_client_port *found = NULL;
294
295         mutex_lock(&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 */
301                         list_del(l);
302                         client->num_ports--;
303                         found = p;
304                         break;
305                 }
306         }
307         write_unlock_irqrestore(&client->ports_lock, flags);
308         mutex_unlock(&client->ports_mutex);
309         if (found)
310                 return port_delete(client, found);
311         else
312                 return -ENOENT;
313 }
314
315 /* delete the all ports belonging to the given client */
316 int snd_seq_delete_all_ports(struct snd_seq_client *client)
317 {
318         unsigned long flags;
319         struct list_head deleted_list, *p, *n;
320         
321         /* move the port list to deleted_list, and
322          * clear the port list in the client data.
323          */
324         mutex_lock(&client->ports_mutex);
325         write_lock_irqsave(&client->ports_lock, flags);
326         if (! list_empty(&client->ports_list_head)) {
327                 list_add(&deleted_list, &client->ports_list_head);
328                 list_del_init(&client->ports_list_head);
329         } else {
330                 INIT_LIST_HEAD(&deleted_list);
331         }
332         client->num_ports = 0;
333         write_unlock_irqrestore(&client->ports_lock, flags);
334
335         /* remove each port in deleted_list */
336         list_for_each_safe(p, n, &deleted_list) {
337                 struct snd_seq_client_port *port = list_entry(p, struct snd_seq_client_port, list);
338                 list_del(p);
339                 snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port);
340                 port_delete(client, port);
341         }
342         mutex_unlock(&client->ports_mutex);
343         return 0;
344 }
345
346 /* set port info fields */
347 int snd_seq_set_port_info(struct snd_seq_client_port * port,
348                           struct snd_seq_port_info * info)
349 {
350         snd_assert(port && info, return -EINVAL);
351
352         /* set port name */
353         if (info->name[0])
354                 strlcpy(port->name, info->name, sizeof(port->name));
355         
356         /* set capabilities */
357         port->capability = info->capability;
358         
359         /* get port type */
360         port->type = info->type;
361
362         /* information about supported channels/voices */
363         port->midi_channels = info->midi_channels;
364         port->midi_voices = info->midi_voices;
365         port->synth_voices = info->synth_voices;
366
367         /* timestamping */
368         port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0;
369         port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0;
370         port->time_queue = info->time_queue;
371
372         return 0;
373 }
374
375 /* get port info fields */
376 int snd_seq_get_port_info(struct snd_seq_client_port * port,
377                           struct snd_seq_port_info * info)
378 {
379         snd_assert(port && info, return -EINVAL);
380
381         /* get port name */
382         strlcpy(info->name, port->name, sizeof(info->name));
383         
384         /* get capabilities */
385         info->capability = port->capability;
386
387         /* get port type */
388         info->type = port->type;
389
390         /* information about supported channels/voices */
391         info->midi_channels = port->midi_channels;
392         info->midi_voices = port->midi_voices;
393         info->synth_voices = port->synth_voices;
394
395         /* get subscriber counts */
396         info->read_use = port->c_src.count;
397         info->write_use = port->c_dest.count;
398         
399         /* timestamping */
400         info->flags = 0;
401         if (port->timestamping) {
402                 info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP;
403                 if (port->time_real)
404                         info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL;
405                 info->time_queue = port->time_queue;
406         }
407
408         return 0;
409 }
410
411
412
413 /*
414  * call callback functions (if any):
415  * the callbacks are invoked only when the first (for connection) or
416  * the last subscription (for disconnection) is done.  Second or later
417  * subscription results in increment of counter, but no callback is
418  * invoked.
419  * This feature is useful if these callbacks are associated with
420  * initialization or termination of devices (see seq_midi.c).
421  *
422  * If callback_all option is set, the callback function is invoked
423  * at each connnection/disconnection. 
424  */
425
426 static int subscribe_port(struct snd_seq_client *client,
427                           struct snd_seq_client_port *port,
428                           struct snd_seq_port_subs_info *grp,
429                           struct snd_seq_port_subscribe *info,
430                           int send_ack)
431 {
432         int err = 0;
433
434         if (!try_module_get(port->owner))
435                 return -EFAULT;
436         grp->count++;
437         if (grp->open && (port->callback_all || grp->count == 1)) {
438                 err = grp->open(port->private_data, info);
439                 if (err < 0) {
440                         module_put(port->owner);
441                         grp->count--;
442                 }
443         }
444         if (err >= 0 && send_ack && client->type == USER_CLIENT)
445                 snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
446                                                    info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
447
448         return err;
449 }
450
451 static int unsubscribe_port(struct snd_seq_client *client,
452                             struct snd_seq_client_port *port,
453                             struct snd_seq_port_subs_info *grp,
454                             struct snd_seq_port_subscribe *info,
455                             int send_ack)
456 {
457         int err = 0;
458
459         if (! grp->count)
460                 return -EINVAL;
461         grp->count--;
462         if (grp->close && (port->callback_all || grp->count == 0))
463                 err = grp->close(port->private_data, info);
464         if (send_ack && client->type == USER_CLIENT)
465                 snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
466                                                    info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
467         module_put(port->owner);
468         return err;
469 }
470
471
472
473 /* check if both addresses are identical */
474 static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s)
475 {
476         return (r->client == s->client) && (r->port == s->port);
477 }
478
479 /* check the two subscribe info match */
480 /* if flags is zero, checks only sender and destination addresses */
481 static int match_subs_info(struct snd_seq_port_subscribe *r,
482                            struct snd_seq_port_subscribe *s)
483 {
484         if (addr_match(&r->sender, &s->sender) &&
485             addr_match(&r->dest, &s->dest)) {
486                 if (r->flags && r->flags == s->flags)
487                         return r->queue == s->queue;
488                 else if (! r->flags)
489                         return 1;
490         }
491         return 0;
492 }
493
494
495 /* connect two ports */
496 int snd_seq_port_connect(struct snd_seq_client *connector,
497                          struct snd_seq_client *src_client,
498                          struct snd_seq_client_port *src_port,
499                          struct snd_seq_client *dest_client,
500                          struct snd_seq_client_port *dest_port,
501                          struct snd_seq_port_subscribe *info)
502 {
503         struct snd_seq_port_subs_info *src = &src_port->c_src;
504         struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
505         struct snd_seq_subscribers *subs;
506         struct list_head *p;
507         int err, src_called = 0;
508         unsigned long flags;
509         int exclusive;
510
511         subs = kzalloc(sizeof(*subs), GFP_KERNEL);
512         if (! subs)
513                 return -ENOMEM;
514
515         subs->info = *info;
516         atomic_set(&subs->ref_count, 2);
517
518         down_write(&src->list_mutex);
519         down_write(&dest->list_mutex);
520
521         exclusive = info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE ? 1 : 0;
522         err = -EBUSY;
523         if (exclusive) {
524                 if (! list_empty(&src->list_head) || ! list_empty(&dest->list_head))
525                         goto __error;
526         } else {
527                 if (src->exclusive || dest->exclusive)
528                         goto __error;
529                 /* check whether already exists */
530                 list_for_each(p, &src->list_head) {
531                         struct snd_seq_subscribers *s = list_entry(p, struct snd_seq_subscribers, src_list);
532                         if (match_subs_info(info, &s->info))
533                                 goto __error;
534                 }
535                 list_for_each(p, &dest->list_head) {
536                         struct snd_seq_subscribers *s = list_entry(p, struct snd_seq_subscribers, dest_list);
537                         if (match_subs_info(info, &s->info))
538                                 goto __error;
539                 }
540         }
541
542         if ((err = subscribe_port(src_client, src_port, src, info,
543                                   connector->number != src_client->number)) < 0)
544                 goto __error;
545         src_called = 1;
546
547         if ((err = subscribe_port(dest_client, dest_port, dest, info,
548                                   connector->number != dest_client->number)) < 0)
549                 goto __error;
550
551         /* add to list */
552         write_lock_irqsave(&src->list_lock, flags);
553         // write_lock(&dest->list_lock); // no other lock yet
554         list_add_tail(&subs->src_list, &src->list_head);
555         list_add_tail(&subs->dest_list, &dest->list_head);
556         // write_unlock(&dest->list_lock); // no other lock yet
557         write_unlock_irqrestore(&src->list_lock, flags);
558
559         src->exclusive = dest->exclusive = exclusive;
560
561         up_write(&dest->list_mutex);
562         up_write(&src->list_mutex);
563         return 0;
564
565  __error:
566         if (src_called)
567                 unsubscribe_port(src_client, src_port, src, info,
568                                  connector->number != src_client->number);
569         kfree(subs);
570         up_write(&dest->list_mutex);
571         up_write(&src->list_mutex);
572         return err;
573 }
574
575
576 /* remove the connection */
577 int snd_seq_port_disconnect(struct snd_seq_client *connector,
578                             struct snd_seq_client *src_client,
579                             struct snd_seq_client_port *src_port,
580                             struct snd_seq_client *dest_client,
581                             struct snd_seq_client_port *dest_port,
582                             struct snd_seq_port_subscribe *info)
583 {
584         struct snd_seq_port_subs_info *src = &src_port->c_src;
585         struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
586         struct snd_seq_subscribers *subs;
587         struct list_head *p;
588         int err = -ENOENT;
589         unsigned long flags;
590
591         down_write(&src->list_mutex);
592         down_write(&dest->list_mutex);
593
594         /* look for the connection */
595         list_for_each(p, &src->list_head) {
596                 subs = list_entry(p, struct snd_seq_subscribers, src_list);
597                 if (match_subs_info(info, &subs->info)) {
598                         write_lock_irqsave(&src->list_lock, flags);
599                         // write_lock(&dest->list_lock);  // no lock yet
600                         list_del(&subs->src_list);
601                         list_del(&subs->dest_list);
602                         // write_unlock(&dest->list_lock);
603                         write_unlock_irqrestore(&src->list_lock, flags);
604                         src->exclusive = dest->exclusive = 0;
605                         unsubscribe_port(src_client, src_port, src, info,
606                                          connector->number != src_client->number);
607                         unsubscribe_port(dest_client, dest_port, dest, info,
608                                          connector->number != dest_client->number);
609                         kfree(subs);
610                         err = 0;
611                         break;
612                 }
613         }
614
615         up_write(&dest->list_mutex);
616         up_write(&src->list_mutex);
617         return err;
618 }
619
620
621 /* get matched subscriber */
622 struct snd_seq_subscribers *snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp,
623                                                           struct snd_seq_addr *dest_addr)
624 {
625         struct list_head *p;
626         struct snd_seq_subscribers *s, *found = NULL;
627
628         down_read(&src_grp->list_mutex);
629         list_for_each(p, &src_grp->list_head) {
630                 s = list_entry(p, struct snd_seq_subscribers, src_list);
631                 if (addr_match(dest_addr, &s->info.dest)) {
632                         found = s;
633                         break;
634                 }
635         }
636         up_read(&src_grp->list_mutex);
637         return found;
638 }
639
640 /*
641  * Attach a device driver that wants to receive events from the
642  * sequencer.  Returns the new port number on success.
643  * A driver that wants to receive the events converted to midi, will
644  * use snd_seq_midisynth_register_port().
645  */
646 /* exported */
647 int snd_seq_event_port_attach(int client,
648                               struct snd_seq_port_callback *pcbp,
649                               int cap, int type, int midi_channels,
650                               int midi_voices, char *portname)
651 {
652         struct snd_seq_port_info portinfo;
653         int  ret;
654
655         /* Set up the port */
656         memset(&portinfo, 0, sizeof(portinfo));
657         portinfo.addr.client = client;
658         strlcpy(portinfo.name, portname ? portname : "Unamed port",
659                 sizeof(portinfo.name));
660
661         portinfo.capability = cap;
662         portinfo.type = type;
663         portinfo.kernel = pcbp;
664         portinfo.midi_channels = midi_channels;
665         portinfo.midi_voices = midi_voices;
666
667         /* Create it */
668         ret = snd_seq_kernel_client_ctl(client,
669                                         SNDRV_SEQ_IOCTL_CREATE_PORT,
670                                         &portinfo);
671
672         if (ret >= 0)
673                 ret = portinfo.addr.port;
674
675         return ret;
676 }
677
678
679 /*
680  * Detach the driver from a port.
681  */
682 /* exported */
683 int snd_seq_event_port_detach(int client, int port)
684 {
685         struct snd_seq_port_info portinfo;
686         int  err;
687
688         memset(&portinfo, 0, sizeof(portinfo));
689         portinfo.addr.client = client;
690         portinfo.addr.port   = port;
691         err = snd_seq_kernel_client_ctl(client,
692                                         SNDRV_SEQ_IOCTL_DELETE_PORT,
693                                         &portinfo);
694
695         return err;
696 }