blob: 1c32a53d6bd8d91084fb1a560277199557f40e35 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALSA sequencer Ports
3 * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <sound/core.h>
24#include <linux/slab.h>
25#include "seq_system.h"
26#include "seq_ports.h"
27#include "seq_clientmgr.h"
28
29/*
30
31 registration of client ports
32
33 */
34
35
36/*
37
38NOTE: the current implementation of the port structure as a linked list is
39not optimal for clients that have many ports. For sending messages to all
40subscribers of a port we first need to find the address of the port
41structure, which means we have to traverse the list. A direct access table
42(array) would be better, but big preallocated arrays waste memory.
43
44Possible actions:
45
461) leave it this way, a client does normaly does not have more than a few
47ports
48
492) replace the linked list of ports by a array of pointers which is
50dynamicly kmalloced. When a port is added or deleted we can simply allocate
51a new array, copy the corresponding pointers, and delete the old one. We
52then only need a pointer to this array, and an integer that tells us how
53much elements are in array.
54
55*/
56
57/* return pointer to port structure - port is locked if found */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010058struct snd_seq_client_port *snd_seq_port_use_ptr(struct snd_seq_client *client,
59 int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010061 struct snd_seq_client_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 if (client == NULL)
64 return NULL;
65 read_lock(&client->ports_lock);
Johannes Berg9244b2c2006-10-05 16:02:22 +020066 list_for_each_entry(port, &client->ports_list_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (port->addr.port == num) {
68 if (port->closing)
69 break; /* deleting now */
70 snd_use_lock_use(&port->use_lock);
71 read_unlock(&client->ports_lock);
72 return port;
73 }
74 }
75 read_unlock(&client->ports_lock);
76 return NULL; /* not found */
77}
78
79
80/* search for the next port - port is locked if found */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010081struct snd_seq_client_port *snd_seq_port_query_nearest(struct snd_seq_client *client,
82 struct snd_seq_port_info *pinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 int num;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010085 struct snd_seq_client_port *port, *found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 num = pinfo->addr.port;
88 found = NULL;
89 read_lock(&client->ports_lock);
Johannes Berg9244b2c2006-10-05 16:02:22 +020090 list_for_each_entry(port, &client->ports_list_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (port->addr.port < num)
92 continue;
93 if (port->addr.port == num) {
94 found = port;
95 break;
96 }
97 if (found == NULL || port->addr.port < found->addr.port)
98 found = port;
99 }
100 if (found) {
101 if (found->closing)
102 found = NULL;
103 else
104 snd_use_lock_use(&found->use_lock);
105 }
106 read_unlock(&client->ports_lock);
107 return found;
108}
109
110
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100111/* initialize snd_seq_port_subs_info */
112static void port_subs_info_init(struct snd_seq_port_subs_info *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 INIT_LIST_HEAD(&grp->list_head);
115 grp->count = 0;
116 grp->exclusive = 0;
117 rwlock_init(&grp->list_lock);
118 init_rwsem(&grp->list_mutex);
119 grp->open = NULL;
120 grp->close = NULL;
121}
122
123
124/* create a port, port number is returned (-1 on failure) */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100125struct snd_seq_client_port *snd_seq_create_port(struct snd_seq_client *client,
126 int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 unsigned long flags;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200129 struct snd_seq_client_port *new_port, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 int num = -1;
131
132 /* sanity check */
133 snd_assert(client, return NULL);
134
135 if (client->num_ports >= SNDRV_SEQ_MAX_PORTS - 1) {
136 snd_printk(KERN_WARNING "too many ports for client %d\n", client->number);
137 return NULL;
138 }
139
140 /* create a new port */
Takashi Iwaiecca82b2005-09-09 14:20:49 +0200141 new_port = kzalloc(sizeof(*new_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (! new_port) {
143 snd_printd("malloc failed for registering client port\n");
144 return NULL; /* failure, out of memory */
145 }
146 /* init port data */
147 new_port->addr.client = client->number;
148 new_port->addr.port = -1;
149 new_port->owner = THIS_MODULE;
150 sprintf(new_port->name, "port-%d", num);
151 snd_use_lock_init(&new_port->use_lock);
152 port_subs_info_init(&new_port->c_src);
153 port_subs_info_init(&new_port->c_dest);
154
155 num = port >= 0 ? port : 0;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100156 mutex_lock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 write_lock_irqsave(&client->ports_lock, flags);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200158 list_for_each_entry(p, &client->ports_list_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (p->addr.port > num)
160 break;
161 if (port < 0) /* auto-probe mode */
162 num = p->addr.port + 1;
163 }
164 /* insert the new port */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200165 list_add_tail(&new_port->list, &p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 client->num_ports++;
167 new_port->addr.port = num; /* store the port number in the port */
168 write_unlock_irqrestore(&client->ports_lock, flags);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100169 mutex_unlock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 sprintf(new_port->name, "port-%d", num);
171
172 return new_port;
173}
174
175/* */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100176enum group_type {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 SRC_LIST, DEST_LIST
178};
179
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100180static int subscribe_port(struct snd_seq_client *client,
181 struct snd_seq_client_port *port,
182 struct snd_seq_port_subs_info *grp,
183 struct snd_seq_port_subscribe *info, int send_ack);
184static int unsubscribe_port(struct snd_seq_client *client,
185 struct snd_seq_client_port *port,
186 struct snd_seq_port_subs_info *grp,
187 struct snd_seq_port_subscribe *info, int send_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100190static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr,
191 struct snd_seq_client **cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100193 struct snd_seq_client_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *cp = snd_seq_client_use_ptr(addr->client);
195 if (*cp) {
196 p = snd_seq_port_use_ptr(*cp, addr->port);
197 if (! p) {
198 snd_seq_client_unlock(*cp);
199 *cp = NULL;
200 }
201 return p;
202 }
203 return NULL;
204}
205
206/*
207 * remove all subscribers on the list
208 * this is called from port_delete, for each src and dest list.
209 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100210static void clear_subscriber_list(struct snd_seq_client *client,
211 struct snd_seq_client_port *port,
212 struct snd_seq_port_subs_info *grp,
213 int grptype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 struct list_head *p, *n;
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 list_for_each_safe(p, n, &grp->list_head) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100218 struct snd_seq_subscribers *subs;
219 struct snd_seq_client *c;
220 struct snd_seq_client_port *aport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 if (grptype == SRC_LIST) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100223 subs = list_entry(p, struct snd_seq_subscribers, src_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 aport = get_client_port(&subs->info.dest, &c);
225 } else {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100226 subs = list_entry(p, struct snd_seq_subscribers, dest_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 aport = get_client_port(&subs->info.sender, &c);
228 }
229 list_del(p);
230 unsubscribe_port(client, port, grp, &subs->info, 0);
231 if (!aport) {
232 /* looks like the connected port is being deleted.
233 * we decrease the counter, and when both ports are deleted
234 * remove the subscriber info
235 */
236 if (atomic_dec_and_test(&subs->ref_count))
237 kfree(subs);
238 } else {
239 /* ok we got the connected port */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100240 struct snd_seq_port_subs_info *agrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 agrp = (grptype == SRC_LIST) ? &aport->c_dest : &aport->c_src;
242 down_write(&agrp->list_mutex);
243 if (grptype == SRC_LIST)
244 list_del(&subs->dest_list);
245 else
246 list_del(&subs->src_list);
Takashi Iwai6116ea02007-02-07 14:07:08 +0100247 up_write(&agrp->list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 unsubscribe_port(c, aport, agrp, &subs->info, 1);
249 kfree(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 snd_seq_port_unlock(aport);
251 snd_seq_client_unlock(c);
252 }
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256/* delete port data */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100257static int port_delete(struct snd_seq_client *client,
258 struct snd_seq_client_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 /* set closing flag and wait for all port access are gone */
261 port->closing = 1;
262 snd_use_lock_sync(&port->use_lock);
263
264 /* clear subscribers info */
265 clear_subscriber_list(client, port, &port->c_src, SRC_LIST);
266 clear_subscriber_list(client, port, &port->c_dest, DEST_LIST);
267
268 if (port->private_free)
269 port->private_free(port->private_data);
270
271 snd_assert(port->c_src.count == 0,);
272 snd_assert(port->c_dest.count == 0,);
273
274 kfree(port);
275 return 0;
276}
277
278
279/* delete a port with the given port id */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100280int snd_seq_delete_port(struct snd_seq_client *client, int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 unsigned long flags;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200283 struct snd_seq_client_port *found = NULL, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100285 mutex_lock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 write_lock_irqsave(&client->ports_lock, flags);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200287 list_for_each_entry(p, &client->ports_list_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (p->addr.port == port) {
289 /* ok found. delete from the list at first */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200290 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 client->num_ports--;
292 found = p;
293 break;
294 }
295 }
296 write_unlock_irqrestore(&client->ports_lock, flags);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100297 mutex_unlock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (found)
299 return port_delete(client, found);
300 else
301 return -ENOENT;
302}
303
304/* delete the all ports belonging to the given client */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100305int snd_seq_delete_all_ports(struct snd_seq_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 unsigned long flags;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200308 struct list_head deleted_list;
309 struct snd_seq_client_port *port, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* move the port list to deleted_list, and
312 * clear the port list in the client data.
313 */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100314 mutex_lock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 write_lock_irqsave(&client->ports_lock, flags);
316 if (! list_empty(&client->ports_list_head)) {
Takashi Iwaibe7ee272006-06-27 13:07:04 +0200317 list_add(&deleted_list, &client->ports_list_head);
318 list_del_init(&client->ports_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 } else {
320 INIT_LIST_HEAD(&deleted_list);
321 }
322 client->num_ports = 0;
323 write_unlock_irqrestore(&client->ports_lock, flags);
324
325 /* remove each port in deleted_list */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200326 list_for_each_entry_safe(port, tmp, &deleted_list, list) {
327 list_del(&port->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port);
329 port_delete(client, port);
330 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100331 mutex_unlock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return 0;
333}
334
335/* set port info fields */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100336int snd_seq_set_port_info(struct snd_seq_client_port * port,
337 struct snd_seq_port_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 snd_assert(port && info, return -EINVAL);
340
341 /* set port name */
342 if (info->name[0])
343 strlcpy(port->name, info->name, sizeof(port->name));
344
345 /* set capabilities */
346 port->capability = info->capability;
347
348 /* get port type */
349 port->type = info->type;
350
351 /* information about supported channels/voices */
352 port->midi_channels = info->midi_channels;
353 port->midi_voices = info->midi_voices;
354 port->synth_voices = info->synth_voices;
355
356 /* timestamping */
357 port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0;
358 port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0;
359 port->time_queue = info->time_queue;
360
361 return 0;
362}
363
364/* get port info fields */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100365int snd_seq_get_port_info(struct snd_seq_client_port * port,
366 struct snd_seq_port_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 snd_assert(port && info, return -EINVAL);
369
370 /* get port name */
371 strlcpy(info->name, port->name, sizeof(info->name));
372
373 /* get capabilities */
374 info->capability = port->capability;
375
376 /* get port type */
377 info->type = port->type;
378
379 /* information about supported channels/voices */
380 info->midi_channels = port->midi_channels;
381 info->midi_voices = port->midi_voices;
382 info->synth_voices = port->synth_voices;
383
384 /* get subscriber counts */
385 info->read_use = port->c_src.count;
386 info->write_use = port->c_dest.count;
387
388 /* timestamping */
389 info->flags = 0;
390 if (port->timestamping) {
391 info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP;
392 if (port->time_real)
393 info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL;
394 info->time_queue = port->time_queue;
395 }
396
397 return 0;
398}
399
400
401
402/*
403 * call callback functions (if any):
404 * the callbacks are invoked only when the first (for connection) or
405 * the last subscription (for disconnection) is done. Second or later
406 * subscription results in increment of counter, but no callback is
407 * invoked.
408 * This feature is useful if these callbacks are associated with
409 * initialization or termination of devices (see seq_midi.c).
410 *
411 * If callback_all option is set, the callback function is invoked
412 * at each connnection/disconnection.
413 */
414
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100415static int subscribe_port(struct snd_seq_client *client,
416 struct snd_seq_client_port *port,
417 struct snd_seq_port_subs_info *grp,
418 struct snd_seq_port_subscribe *info,
419 int send_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 int err = 0;
422
423 if (!try_module_get(port->owner))
424 return -EFAULT;
425 grp->count++;
426 if (grp->open && (port->callback_all || grp->count == 1)) {
427 err = grp->open(port->private_data, info);
428 if (err < 0) {
429 module_put(port->owner);
430 grp->count--;
431 }
432 }
433 if (err >= 0 && send_ack && client->type == USER_CLIENT)
434 snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
435 info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
436
437 return err;
438}
439
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100440static int unsubscribe_port(struct snd_seq_client *client,
441 struct snd_seq_client_port *port,
442 struct snd_seq_port_subs_info *grp,
443 struct snd_seq_port_subscribe *info,
444 int send_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 int err = 0;
447
448 if (! grp->count)
449 return -EINVAL;
450 grp->count--;
451 if (grp->close && (port->callback_all || grp->count == 0))
452 err = grp->close(port->private_data, info);
453 if (send_ack && client->type == USER_CLIENT)
454 snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
455 info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
456 module_put(port->owner);
457 return err;
458}
459
460
461
462/* check if both addresses are identical */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100463static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 return (r->client == s->client) && (r->port == s->port);
466}
467
468/* check the two subscribe info match */
469/* if flags is zero, checks only sender and destination addresses */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100470static int match_subs_info(struct snd_seq_port_subscribe *r,
471 struct snd_seq_port_subscribe *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 if (addr_match(&r->sender, &s->sender) &&
474 addr_match(&r->dest, &s->dest)) {
475 if (r->flags && r->flags == s->flags)
476 return r->queue == s->queue;
477 else if (! r->flags)
478 return 1;
479 }
480 return 0;
481}
482
483
484/* connect two ports */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100485int snd_seq_port_connect(struct snd_seq_client *connector,
486 struct snd_seq_client *src_client,
487 struct snd_seq_client_port *src_port,
488 struct snd_seq_client *dest_client,
489 struct snd_seq_client_port *dest_port,
490 struct snd_seq_port_subscribe *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100492 struct snd_seq_port_subs_info *src = &src_port->c_src;
493 struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200494 struct snd_seq_subscribers *subs, *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 int err, src_called = 0;
496 unsigned long flags;
497 int exclusive;
498
Takashi Iwaiecca82b2005-09-09 14:20:49 +0200499 subs = kzalloc(sizeof(*subs), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (! subs)
501 return -ENOMEM;
502
503 subs->info = *info;
504 atomic_set(&subs->ref_count, 2);
505
506 down_write(&src->list_mutex);
Ingo Molnard8371f02006-07-03 00:25:21 -0700507 down_write_nested(&dest->list_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 exclusive = info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE ? 1 : 0;
510 err = -EBUSY;
511 if (exclusive) {
512 if (! list_empty(&src->list_head) || ! list_empty(&dest->list_head))
513 goto __error;
514 } else {
515 if (src->exclusive || dest->exclusive)
516 goto __error;
517 /* check whether already exists */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200518 list_for_each_entry(s, &src->list_head, src_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (match_subs_info(info, &s->info))
520 goto __error;
521 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200522 list_for_each_entry(s, &dest->list_head, dest_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (match_subs_info(info, &s->info))
524 goto __error;
525 }
526 }
527
528 if ((err = subscribe_port(src_client, src_port, src, info,
529 connector->number != src_client->number)) < 0)
530 goto __error;
531 src_called = 1;
532
533 if ((err = subscribe_port(dest_client, dest_port, dest, info,
534 connector->number != dest_client->number)) < 0)
535 goto __error;
536
537 /* add to list */
538 write_lock_irqsave(&src->list_lock, flags);
539 // write_lock(&dest->list_lock); // no other lock yet
540 list_add_tail(&subs->src_list, &src->list_head);
541 list_add_tail(&subs->dest_list, &dest->list_head);
542 // write_unlock(&dest->list_lock); // no other lock yet
543 write_unlock_irqrestore(&src->list_lock, flags);
544
545 src->exclusive = dest->exclusive = exclusive;
546
547 up_write(&dest->list_mutex);
548 up_write(&src->list_mutex);
549 return 0;
550
551 __error:
552 if (src_called)
553 unsubscribe_port(src_client, src_port, src, info,
554 connector->number != src_client->number);
555 kfree(subs);
556 up_write(&dest->list_mutex);
557 up_write(&src->list_mutex);
558 return err;
559}
560
561
562/* remove the connection */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100563int snd_seq_port_disconnect(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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100570 struct snd_seq_port_subs_info *src = &src_port->c_src;
571 struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
572 struct snd_seq_subscribers *subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int err = -ENOENT;
574 unsigned long flags;
575
576 down_write(&src->list_mutex);
Ingo Molnard8371f02006-07-03 00:25:21 -0700577 down_write_nested(&dest->list_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 /* look for the connection */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200580 list_for_each_entry(subs, &src->list_head, src_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (match_subs_info(info, &subs->info)) {
582 write_lock_irqsave(&src->list_lock, flags);
583 // write_lock(&dest->list_lock); // no lock yet
584 list_del(&subs->src_list);
585 list_del(&subs->dest_list);
586 // write_unlock(&dest->list_lock);
587 write_unlock_irqrestore(&src->list_lock, flags);
588 src->exclusive = dest->exclusive = 0;
589 unsubscribe_port(src_client, src_port, src, info,
590 connector->number != src_client->number);
591 unsubscribe_port(dest_client, dest_port, dest, info,
592 connector->number != dest_client->number);
593 kfree(subs);
594 err = 0;
595 break;
596 }
597 }
598
599 up_write(&dest->list_mutex);
600 up_write(&src->list_mutex);
601 return err;
602}
603
604
605/* get matched subscriber */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100606struct snd_seq_subscribers *snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp,
607 struct snd_seq_addr *dest_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100609 struct snd_seq_subscribers *s, *found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 down_read(&src_grp->list_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200612 list_for_each_entry(s, &src_grp->list_head, src_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (addr_match(dest_addr, &s->info.dest)) {
614 found = s;
615 break;
616 }
617 }
618 up_read(&src_grp->list_mutex);
619 return found;
620}
621
622/*
623 * Attach a device driver that wants to receive events from the
624 * sequencer. Returns the new port number on success.
625 * A driver that wants to receive the events converted to midi, will
626 * use snd_seq_midisynth_register_port().
627 */
628/* exported */
629int snd_seq_event_port_attach(int client,
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100630 struct snd_seq_port_callback *pcbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int cap, int type, int midi_channels,
632 int midi_voices, char *portname)
633{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100634 struct snd_seq_port_info portinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int ret;
636
637 /* Set up the port */
638 memset(&portinfo, 0, sizeof(portinfo));
639 portinfo.addr.client = client;
640 strlcpy(portinfo.name, portname ? portname : "Unamed port",
641 sizeof(portinfo.name));
642
643 portinfo.capability = cap;
644 portinfo.type = type;
645 portinfo.kernel = pcbp;
646 portinfo.midi_channels = midi_channels;
647 portinfo.midi_voices = midi_voices;
648
649 /* Create it */
650 ret = snd_seq_kernel_client_ctl(client,
651 SNDRV_SEQ_IOCTL_CREATE_PORT,
652 &portinfo);
653
654 if (ret >= 0)
655 ret = portinfo.addr.port;
656
657 return ret;
658}
659
Takashi Iwai91715ed2006-04-28 15:13:39 +0200660EXPORT_SYMBOL(snd_seq_event_port_attach);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662/*
663 * Detach the driver from a port.
664 */
665/* exported */
666int snd_seq_event_port_detach(int client, int port)
667{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100668 struct snd_seq_port_info portinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 int err;
670
671 memset(&portinfo, 0, sizeof(portinfo));
672 portinfo.addr.client = client;
673 portinfo.addr.port = port;
674 err = snd_seq_kernel_client_ctl(client,
675 SNDRV_SEQ_IOCTL_DELETE_PORT,
676 &portinfo);
677
678 return err;
679}
Takashi Iwai91715ed2006-04-28 15:13:39 +0200680
681EXPORT_SYMBOL(snd_seq_event_port_detach);