blob: 9516e5ce3aadd8cb50e857a187a469284cb91550 [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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040025#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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
39NOTE: the current implementation of the port structure as a linked list is
40not optimal for clients that have many ports. For sending messages to all
41subscribers of a port we first need to find the address of the port
42structure, which means we have to traverse the list. A direct access table
43(array) would be better, but big preallocated arrays waste memory.
44
45Possible actions:
46
471) leave it this way, a client does normaly does not have more than a few
48ports
49
502) replace the linked list of ports by a array of pointers which is
51dynamicly kmalloced. When a port is added or deleted we can simply allocate
52a new array, copy the corresponding pointers, and delete the old one. We
53then only need a pointer to this array, and an integer that tells us how
54much elements are in array.
55
56*/
57
58/* return pointer to port structure - port is locked if found */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010059struct snd_seq_client_port *snd_seq_port_use_ptr(struct snd_seq_client *client,
60 int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010062 struct snd_seq_client_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 if (client == NULL)
65 return NULL;
66 read_lock(&client->ports_lock);
Johannes Berg9244b2c2006-10-05 16:02:22 +020067 list_for_each_entry(port, &client->ports_list_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (port->addr.port == num) {
69 if (port->closing)
70 break; /* deleting now */
71 snd_use_lock_use(&port->use_lock);
72 read_unlock(&client->ports_lock);
73 return port;
74 }
75 }
76 read_unlock(&client->ports_lock);
77 return NULL; /* not found */
78}
79
80
81/* search for the next port - port is locked if found */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010082struct snd_seq_client_port *snd_seq_port_query_nearest(struct snd_seq_client *client,
83 struct snd_seq_port_info *pinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 int num;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010086 struct snd_seq_client_port *port, *found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 num = pinfo->addr.port;
89 found = NULL;
90 read_lock(&client->ports_lock);
Johannes Berg9244b2c2006-10-05 16:02:22 +020091 list_for_each_entry(port, &client->ports_list_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (port->addr.port < num)
93 continue;
94 if (port->addr.port == num) {
95 found = port;
96 break;
97 }
98 if (found == NULL || port->addr.port < found->addr.port)
99 found = port;
100 }
101 if (found) {
102 if (found->closing)
103 found = NULL;
104 else
105 snd_use_lock_use(&found->use_lock);
106 }
107 read_unlock(&client->ports_lock);
108 return found;
109}
110
111
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100112/* initialize snd_seq_port_subs_info */
113static void port_subs_info_init(struct snd_seq_port_subs_info *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 INIT_LIST_HEAD(&grp->list_head);
116 grp->count = 0;
117 grp->exclusive = 0;
118 rwlock_init(&grp->list_lock);
119 init_rwsem(&grp->list_mutex);
120 grp->open = NULL;
121 grp->close = NULL;
122}
123
124
125/* create a port, port number is returned (-1 on failure) */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100126struct snd_seq_client_port *snd_seq_create_port(struct snd_seq_client *client,
127 int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 unsigned long flags;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200130 struct snd_seq_client_port *new_port, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int num = -1;
132
133 /* sanity check */
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200134 if (snd_BUG_ON(!client))
135 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 if (client->num_ports >= SNDRV_SEQ_MAX_PORTS - 1) {
138 snd_printk(KERN_WARNING "too many ports for client %d\n", client->number);
139 return NULL;
140 }
141
142 /* create a new port */
Takashi Iwaiecca82b2005-09-09 14:20:49 +0200143 new_port = kzalloc(sizeof(*new_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (! new_port) {
145 snd_printd("malloc failed for registering client port\n");
146 return NULL; /* failure, out of memory */
147 }
148 /* init port data */
149 new_port->addr.client = client->number;
150 new_port->addr.port = -1;
151 new_port->owner = THIS_MODULE;
152 sprintf(new_port->name, "port-%d", num);
153 snd_use_lock_init(&new_port->use_lock);
154 port_subs_info_init(&new_port->c_src);
155 port_subs_info_init(&new_port->c_dest);
156
157 num = port >= 0 ? port : 0;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100158 mutex_lock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 write_lock_irqsave(&client->ports_lock, flags);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200160 list_for_each_entry(p, &client->ports_list_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (p->addr.port > num)
162 break;
163 if (port < 0) /* auto-probe mode */
164 num = p->addr.port + 1;
165 }
166 /* insert the new port */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200167 list_add_tail(&new_port->list, &p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 client->num_ports++;
169 new_port->addr.port = num; /* store the port number in the port */
170 write_unlock_irqrestore(&client->ports_lock, flags);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100171 mutex_unlock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 sprintf(new_port->name, "port-%d", num);
173
174 return new_port;
175}
176
177/* */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100178enum group_type {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 SRC_LIST, DEST_LIST
180};
181
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100182static int subscribe_port(struct snd_seq_client *client,
183 struct snd_seq_client_port *port,
184 struct snd_seq_port_subs_info *grp,
185 struct snd_seq_port_subscribe *info, int send_ack);
186static int unsubscribe_port(struct snd_seq_client *client,
187 struct snd_seq_client_port *port,
188 struct snd_seq_port_subs_info *grp,
189 struct snd_seq_port_subscribe *info, int send_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100192static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr,
193 struct snd_seq_client **cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100195 struct snd_seq_client_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *cp = snd_seq_client_use_ptr(addr->client);
197 if (*cp) {
198 p = snd_seq_port_use_ptr(*cp, addr->port);
199 if (! p) {
200 snd_seq_client_unlock(*cp);
201 *cp = NULL;
202 }
203 return p;
204 }
205 return NULL;
206}
207
208/*
209 * remove all subscribers on the list
210 * this is called from port_delete, for each src and dest list.
211 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100212static void clear_subscriber_list(struct snd_seq_client *client,
213 struct snd_seq_client_port *port,
214 struct snd_seq_port_subs_info *grp,
215 int grptype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct list_head *p, *n;
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 list_for_each_safe(p, n, &grp->list_head) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100220 struct snd_seq_subscribers *subs;
221 struct snd_seq_client *c;
222 struct snd_seq_client_port *aport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 if (grptype == SRC_LIST) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100225 subs = list_entry(p, struct snd_seq_subscribers, src_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 aport = get_client_port(&subs->info.dest, &c);
227 } else {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100228 subs = list_entry(p, struct snd_seq_subscribers, dest_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 aport = get_client_port(&subs->info.sender, &c);
230 }
231 list_del(p);
232 unsubscribe_port(client, port, grp, &subs->info, 0);
233 if (!aport) {
234 /* looks like the connected port is being deleted.
235 * we decrease the counter, and when both ports are deleted
236 * remove the subscriber info
237 */
238 if (atomic_dec_and_test(&subs->ref_count))
239 kfree(subs);
240 } else {
241 /* ok we got the connected port */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100242 struct snd_seq_port_subs_info *agrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 agrp = (grptype == SRC_LIST) ? &aport->c_dest : &aport->c_src;
244 down_write(&agrp->list_mutex);
245 if (grptype == SRC_LIST)
246 list_del(&subs->dest_list);
247 else
248 list_del(&subs->src_list);
Takashi Iwai6116ea02007-02-07 14:07:08 +0100249 up_write(&agrp->list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 unsubscribe_port(c, aport, agrp, &subs->info, 1);
251 kfree(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 snd_seq_port_unlock(aport);
253 snd_seq_client_unlock(c);
254 }
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/* delete port data */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100259static int port_delete(struct snd_seq_client *client,
260 struct snd_seq_client_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 /* set closing flag and wait for all port access are gone */
263 port->closing = 1;
264 snd_use_lock_sync(&port->use_lock);
265
266 /* clear subscribers info */
267 clear_subscriber_list(client, port, &port->c_src, SRC_LIST);
268 clear_subscriber_list(client, port, &port->c_dest, DEST_LIST);
269
270 if (port->private_free)
271 port->private_free(port->private_data);
272
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200273 snd_BUG_ON(port->c_src.count != 0);
274 snd_BUG_ON(port->c_dest.count != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 kfree(port);
277 return 0;
278}
279
280
281/* delete a port with the given port id */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100282int snd_seq_delete_port(struct snd_seq_client *client, int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 unsigned long flags;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200285 struct snd_seq_client_port *found = NULL, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100287 mutex_lock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 write_lock_irqsave(&client->ports_lock, flags);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200289 list_for_each_entry(p, &client->ports_list_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (p->addr.port == port) {
291 /* ok found. delete from the list at first */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200292 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 client->num_ports--;
294 found = p;
295 break;
296 }
297 }
298 write_unlock_irqrestore(&client->ports_lock, flags);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100299 mutex_unlock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (found)
301 return port_delete(client, found);
302 else
303 return -ENOENT;
304}
305
306/* delete the all ports belonging to the given client */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100307int snd_seq_delete_all_ports(struct snd_seq_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 unsigned long flags;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200310 struct list_head deleted_list;
311 struct snd_seq_client_port *port, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 /* move the port list to deleted_list, and
314 * clear the port list in the client data.
315 */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100316 mutex_lock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 write_lock_irqsave(&client->ports_lock, flags);
318 if (! list_empty(&client->ports_list_head)) {
Takashi Iwaibe7ee272006-06-27 13:07:04 +0200319 list_add(&deleted_list, &client->ports_list_head);
320 list_del_init(&client->ports_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 } else {
322 INIT_LIST_HEAD(&deleted_list);
323 }
324 client->num_ports = 0;
325 write_unlock_irqrestore(&client->ports_lock, flags);
326
327 /* remove each port in deleted_list */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200328 list_for_each_entry_safe(port, tmp, &deleted_list, list) {
329 list_del(&port->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port);
331 port_delete(client, port);
332 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100333 mutex_unlock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return 0;
335}
336
337/* set port info fields */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100338int snd_seq_set_port_info(struct snd_seq_client_port * port,
339 struct snd_seq_port_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200341 if (snd_BUG_ON(!port || !info))
342 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /* set port name */
345 if (info->name[0])
346 strlcpy(port->name, info->name, sizeof(port->name));
347
348 /* set capabilities */
349 port->capability = info->capability;
350
351 /* get port type */
352 port->type = info->type;
353
354 /* information about supported channels/voices */
355 port->midi_channels = info->midi_channels;
356 port->midi_voices = info->midi_voices;
357 port->synth_voices = info->synth_voices;
358
359 /* timestamping */
360 port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0;
361 port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0;
362 port->time_queue = info->time_queue;
363
364 return 0;
365}
366
367/* get port info fields */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100368int snd_seq_get_port_info(struct snd_seq_client_port * port,
369 struct snd_seq_port_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200371 if (snd_BUG_ON(!port || !info))
372 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /* get port name */
375 strlcpy(info->name, port->name, sizeof(info->name));
376
377 /* get capabilities */
378 info->capability = port->capability;
379
380 /* get port type */
381 info->type = port->type;
382
383 /* information about supported channels/voices */
384 info->midi_channels = port->midi_channels;
385 info->midi_voices = port->midi_voices;
386 info->synth_voices = port->synth_voices;
387
388 /* get subscriber counts */
389 info->read_use = port->c_src.count;
390 info->write_use = port->c_dest.count;
391
392 /* timestamping */
393 info->flags = 0;
394 if (port->timestamping) {
395 info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP;
396 if (port->time_real)
397 info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL;
398 info->time_queue = port->time_queue;
399 }
400
401 return 0;
402}
403
404
405
406/*
407 * call callback functions (if any):
408 * the callbacks are invoked only when the first (for connection) or
409 * the last subscription (for disconnection) is done. Second or later
410 * subscription results in increment of counter, but no callback is
411 * invoked.
412 * This feature is useful if these callbacks are associated with
413 * initialization or termination of devices (see seq_midi.c).
414 *
415 * If callback_all option is set, the callback function is invoked
Justin P. Mattockb6aa63e2011-02-26 20:34:10 -0800416 * at each connection/disconnection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
418
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100419static int subscribe_port(struct snd_seq_client *client,
420 struct snd_seq_client_port *port,
421 struct snd_seq_port_subs_info *grp,
422 struct snd_seq_port_subscribe *info,
423 int send_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 int err = 0;
426
427 if (!try_module_get(port->owner))
428 return -EFAULT;
429 grp->count++;
430 if (grp->open && (port->callback_all || grp->count == 1)) {
431 err = grp->open(port->private_data, info);
432 if (err < 0) {
433 module_put(port->owner);
434 grp->count--;
435 }
436 }
437 if (err >= 0 && send_ack && client->type == USER_CLIENT)
438 snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
439 info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
440
441 return err;
442}
443
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100444static int unsubscribe_port(struct snd_seq_client *client,
445 struct snd_seq_client_port *port,
446 struct snd_seq_port_subs_info *grp,
447 struct snd_seq_port_subscribe *info,
448 int send_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 int err = 0;
451
452 if (! grp->count)
453 return -EINVAL;
454 grp->count--;
455 if (grp->close && (port->callback_all || grp->count == 0))
456 err = grp->close(port->private_data, info);
457 if (send_ack && client->type == USER_CLIENT)
458 snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
459 info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
460 module_put(port->owner);
461 return err;
462}
463
464
465
466/* check if both addresses are identical */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100467static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 return (r->client == s->client) && (r->port == s->port);
470}
471
472/* check the two subscribe info match */
473/* if flags is zero, checks only sender and destination addresses */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100474static int match_subs_info(struct snd_seq_port_subscribe *r,
475 struct snd_seq_port_subscribe *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 if (addr_match(&r->sender, &s->sender) &&
478 addr_match(&r->dest, &s->dest)) {
479 if (r->flags && r->flags == s->flags)
480 return r->queue == s->queue;
481 else if (! r->flags)
482 return 1;
483 }
484 return 0;
485}
486
487
488/* connect two ports */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100489int snd_seq_port_connect(struct snd_seq_client *connector,
490 struct snd_seq_client *src_client,
491 struct snd_seq_client_port *src_port,
492 struct snd_seq_client *dest_client,
493 struct snd_seq_client_port *dest_port,
494 struct snd_seq_port_subscribe *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100496 struct snd_seq_port_subs_info *src = &src_port->c_src;
497 struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200498 struct snd_seq_subscribers *subs, *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 int err, src_called = 0;
500 unsigned long flags;
501 int exclusive;
502
Takashi Iwaiecca82b2005-09-09 14:20:49 +0200503 subs = kzalloc(sizeof(*subs), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (! subs)
505 return -ENOMEM;
506
507 subs->info = *info;
508 atomic_set(&subs->ref_count, 2);
509
510 down_write(&src->list_mutex);
Ingo Molnard8371f02006-07-03 00:25:21 -0700511 down_write_nested(&dest->list_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 exclusive = info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE ? 1 : 0;
514 err = -EBUSY;
515 if (exclusive) {
516 if (! list_empty(&src->list_head) || ! list_empty(&dest->list_head))
517 goto __error;
518 } else {
519 if (src->exclusive || dest->exclusive)
520 goto __error;
521 /* check whether already exists */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200522 list_for_each_entry(s, &src->list_head, src_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (match_subs_info(info, &s->info))
524 goto __error;
525 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200526 list_for_each_entry(s, &dest->list_head, dest_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (match_subs_info(info, &s->info))
528 goto __error;
529 }
530 }
531
532 if ((err = subscribe_port(src_client, src_port, src, info,
533 connector->number != src_client->number)) < 0)
534 goto __error;
535 src_called = 1;
536
537 if ((err = subscribe_port(dest_client, dest_port, dest, info,
538 connector->number != dest_client->number)) < 0)
539 goto __error;
540
541 /* add to list */
542 write_lock_irqsave(&src->list_lock, flags);
543 // write_lock(&dest->list_lock); // no other lock yet
544 list_add_tail(&subs->src_list, &src->list_head);
545 list_add_tail(&subs->dest_list, &dest->list_head);
546 // write_unlock(&dest->list_lock); // no other lock yet
547 write_unlock_irqrestore(&src->list_lock, flags);
548
549 src->exclusive = dest->exclusive = exclusive;
550
551 up_write(&dest->list_mutex);
552 up_write(&src->list_mutex);
553 return 0;
554
555 __error:
556 if (src_called)
557 unsubscribe_port(src_client, src_port, src, info,
558 connector->number != src_client->number);
559 kfree(subs);
560 up_write(&dest->list_mutex);
561 up_write(&src->list_mutex);
562 return err;
563}
564
565
566/* remove the connection */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100567int snd_seq_port_disconnect(struct snd_seq_client *connector,
568 struct snd_seq_client *src_client,
569 struct snd_seq_client_port *src_port,
570 struct snd_seq_client *dest_client,
571 struct snd_seq_client_port *dest_port,
572 struct snd_seq_port_subscribe *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100574 struct snd_seq_port_subs_info *src = &src_port->c_src;
575 struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
576 struct snd_seq_subscribers *subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 int err = -ENOENT;
578 unsigned long flags;
579
580 down_write(&src->list_mutex);
Ingo Molnard8371f02006-07-03 00:25:21 -0700581 down_write_nested(&dest->list_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* look for the connection */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200584 list_for_each_entry(subs, &src->list_head, src_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (match_subs_info(info, &subs->info)) {
586 write_lock_irqsave(&src->list_lock, flags);
587 // write_lock(&dest->list_lock); // no lock yet
588 list_del(&subs->src_list);
589 list_del(&subs->dest_list);
590 // write_unlock(&dest->list_lock);
591 write_unlock_irqrestore(&src->list_lock, flags);
592 src->exclusive = dest->exclusive = 0;
593 unsubscribe_port(src_client, src_port, src, info,
594 connector->number != src_client->number);
595 unsubscribe_port(dest_client, dest_port, dest, info,
596 connector->number != dest_client->number);
597 kfree(subs);
598 err = 0;
599 break;
600 }
601 }
602
603 up_write(&dest->list_mutex);
604 up_write(&src->list_mutex);
605 return err;
606}
607
608
609/* get matched subscriber */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100610struct snd_seq_subscribers *snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp,
611 struct snd_seq_addr *dest_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100613 struct snd_seq_subscribers *s, *found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 down_read(&src_grp->list_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200616 list_for_each_entry(s, &src_grp->list_head, src_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (addr_match(dest_addr, &s->info.dest)) {
618 found = s;
619 break;
620 }
621 }
622 up_read(&src_grp->list_mutex);
623 return found;
624}
625
626/*
627 * Attach a device driver that wants to receive events from the
628 * sequencer. Returns the new port number on success.
629 * A driver that wants to receive the events converted to midi, will
630 * use snd_seq_midisynth_register_port().
631 */
632/* exported */
633int snd_seq_event_port_attach(int client,
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100634 struct snd_seq_port_callback *pcbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int cap, int type, int midi_channels,
636 int midi_voices, char *portname)
637{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100638 struct snd_seq_port_info portinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int ret;
640
641 /* Set up the port */
642 memset(&portinfo, 0, sizeof(portinfo));
643 portinfo.addr.client = client;
644 strlcpy(portinfo.name, portname ? portname : "Unamed port",
645 sizeof(portinfo.name));
646
647 portinfo.capability = cap;
648 portinfo.type = type;
649 portinfo.kernel = pcbp;
650 portinfo.midi_channels = midi_channels;
651 portinfo.midi_voices = midi_voices;
652
653 /* Create it */
654 ret = snd_seq_kernel_client_ctl(client,
655 SNDRV_SEQ_IOCTL_CREATE_PORT,
656 &portinfo);
657
658 if (ret >= 0)
659 ret = portinfo.addr.port;
660
661 return ret;
662}
663
Takashi Iwai91715ed2006-04-28 15:13:39 +0200664EXPORT_SYMBOL(snd_seq_event_port_attach);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666/*
667 * Detach the driver from a port.
668 */
669/* exported */
670int snd_seq_event_port_detach(int client, int port)
671{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100672 struct snd_seq_port_info portinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int err;
674
675 memset(&portinfo, 0, sizeof(portinfo));
676 portinfo.addr.client = client;
677 portinfo.addr.port = port;
678 err = snd_seq_kernel_client_ctl(client,
679 SNDRV_SEQ_IOCTL_DELETE_PORT,
680 &portinfo);
681
682 return err;
683}
Takashi Iwai91715ed2006-04-28 15:13:39 +0200684
685EXPORT_SYMBOL(snd_seq_event_port_detach);