blob: 8c64b58ff77bf3afb9e7f82e30209a15c68f0dc9 [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>
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
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{
62 struct list_head *p;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010063 struct snd_seq_client_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 if (client == NULL)
66 return NULL;
67 read_lock(&client->ports_lock);
68 list_for_each(p, &client->ports_list_head) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010069 port = list_entry(p, struct snd_seq_client_port, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 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 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010084struct snd_seq_client_port *snd_seq_port_query_nearest(struct snd_seq_client *client,
85 struct snd_seq_port_info *pinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 int num;
88 struct list_head *p;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010089 struct snd_seq_client_port *port, *found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 num = pinfo->addr.port;
92 found = NULL;
93 read_lock(&client->ports_lock);
94 list_for_each(p, &client->ports_list_head) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010095 port = list_entry(p, struct snd_seq_client_port, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 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
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100116/* initialize snd_seq_port_subs_info */
117static void port_subs_info_init(struct snd_seq_port_subs_info *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
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) */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100130struct snd_seq_client_port *snd_seq_create_port(struct snd_seq_client *client,
131 int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 unsigned long flags;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100134 struct snd_seq_client_port *new_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 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 */
Takashi Iwaiecca82b2005-09-09 14:20:49 +0200147 new_port = kzalloc(sizeof(*new_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 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;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100162 mutex_lock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 write_lock_irqsave(&client->ports_lock, flags);
164 list_for_each(l, &client->ports_list_head) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100165 struct snd_seq_client_port *p = list_entry(l, struct snd_seq_client_port, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 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);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100176 mutex_unlock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 sprintf(new_port->name, "port-%d", num);
178
179 return new_port;
180}
181
182/* */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100183enum group_type {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 SRC_LIST, DEST_LIST
185};
186
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100187static 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);
191static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100197static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr,
198 struct snd_seq_client **cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100200 struct snd_seq_client_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 *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 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100217static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 struct list_head *p, *n;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 list_for_each_safe(p, n, &grp->list_head) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100225 struct snd_seq_subscribers *subs;
226 struct snd_seq_client *c;
227 struct snd_seq_client_port *aport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (grptype == SRC_LIST) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100230 subs = list_entry(p, struct snd_seq_subscribers, src_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 aport = get_client_port(&subs->info.dest, &c);
232 } else {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100233 subs = list_entry(p, struct snd_seq_subscribers, dest_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 aport = get_client_port(&subs->info.sender, &c);
235 }
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
242 */
243 if (atomic_dec_and_test(&subs->ref_count))
244 kfree(subs);
245 } else {
246 /* ok we got the connected port */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100247 struct snd_seq_port_subs_info *agrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 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);
259 }
260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263/* delete port data */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100264static int port_delete(struct snd_seq_client *client,
265 struct snd_seq_client_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 /* set closing flag and wait for all port access are gone */
268 port->closing = 1;
269 snd_use_lock_sync(&port->use_lock);
270
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);
274
275 if (port->private_free)
276 port->private_free(port->private_data);
277
278 snd_assert(port->c_src.count == 0,);
279 snd_assert(port->c_dest.count == 0,);
280
281 kfree(port);
282 return 0;
283}
284
285
286/* delete a port with the given port id */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100287int snd_seq_delete_port(struct snd_seq_client *client, int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 unsigned long flags;
290 struct list_head *l;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100291 struct snd_seq_client_port *found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100293 mutex_lock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 write_lock_irqsave(&client->ports_lock, flags);
295 list_for_each(l, &client->ports_list_head) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100296 struct snd_seq_client_port *p = list_entry(l, struct snd_seq_client_port, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 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;
303 }
304 }
305 write_unlock_irqrestore(&client->ports_lock, flags);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100306 mutex_unlock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (found)
308 return port_delete(client, found);
309 else
310 return -ENOENT;
311}
312
313/* delete the all ports belonging to the given client */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100314int snd_seq_delete_all_ports(struct snd_seq_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 unsigned long flags;
317 struct list_head deleted_list, *p, *n;
318
319 /* move the port list to deleted_list, and
320 * clear the port list in the client data.
321 */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100322 mutex_lock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 write_lock_irqsave(&client->ports_lock, flags);
324 if (! list_empty(&client->ports_list_head)) {
Takashi Iwaibe7ee272006-06-27 13:07:04 +0200325 list_add(&deleted_list, &client->ports_list_head);
326 list_del_init(&client->ports_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 } else {
328 INIT_LIST_HEAD(&deleted_list);
329 }
330 client->num_ports = 0;
331 write_unlock_irqrestore(&client->ports_lock, flags);
332
333 /* remove each port in deleted_list */
334 list_for_each_safe(p, n, &deleted_list) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100335 struct snd_seq_client_port *port = list_entry(p, struct snd_seq_client_port, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 list_del(p);
337 snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port);
338 port_delete(client, port);
339 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100340 mutex_unlock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return 0;
342}
343
344/* set port info fields */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100345int snd_seq_set_port_info(struct snd_seq_client_port * port,
346 struct snd_seq_port_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 snd_assert(port && info, return -EINVAL);
349
350 /* set port name */
351 if (info->name[0])
352 strlcpy(port->name, info->name, sizeof(port->name));
353
354 /* set capabilities */
355 port->capability = info->capability;
356
357 /* get port type */
358 port->type = info->type;
359
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;
364
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;
369
370 return 0;
371}
372
373/* get port info fields */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100374int snd_seq_get_port_info(struct snd_seq_client_port * port,
375 struct snd_seq_port_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 snd_assert(port && info, return -EINVAL);
378
379 /* get port name */
380 strlcpy(info->name, port->name, sizeof(info->name));
381
382 /* get capabilities */
383 info->capability = port->capability;
384
385 /* get port type */
386 info->type = port->type;
387
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;
392
393 /* get subscriber counts */
394 info->read_use = port->c_src.count;
395 info->write_use = port->c_dest.count;
396
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;
404 }
405
406 return 0;
407}
408
409
410
411/*
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).
419 *
420 * If callback_all option is set, the callback function is invoked
421 * at each connnection/disconnection.
422 */
423
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100424static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 int err = 0;
431
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--;
440 }
441 }
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);
445
446 return err;
447}
448
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100449static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 int err = 0;
456
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;
467}
468
469
470
471/* check if both addresses are identical */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100472static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 return (r->client == s->client) && (r->port == s->port);
475}
476
477/* check the two subscribe info match */
478/* if flags is zero, checks only sender and destination addresses */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100479static int match_subs_info(struct snd_seq_port_subscribe *r,
480 struct snd_seq_port_subscribe *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
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;
488 }
489 return 0;
490}
491
492
493/* connect two ports */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100494int 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100501 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct list_head *p;
505 int err, src_called = 0;
506 unsigned long flags;
507 int exclusive;
508
Takashi Iwaiecca82b2005-09-09 14:20:49 +0200509 subs = kzalloc(sizeof(*subs), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (! subs)
511 return -ENOMEM;
512
513 subs->info = *info;
514 atomic_set(&subs->ref_count, 2);
515
516 down_write(&src->list_mutex);
Ingo Molnard8371f02006-07-03 00:25:21 -0700517 down_write_nested(&dest->list_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
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) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100529 struct snd_seq_subscribers *s = list_entry(p, struct snd_seq_subscribers, src_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (match_subs_info(info, &s->info))
531 goto __error;
532 }
533 list_for_each(p, &dest->list_head) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100534 struct snd_seq_subscribers *s = list_entry(p, struct snd_seq_subscribers, dest_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (match_subs_info(info, &s->info))
536 goto __error;
537 }
538 }
539
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;
544
545 if ((err = subscribe_port(dest_client, dest_port, dest, info,
546 connector->number != dest_client->number)) < 0)
547 goto __error;
548
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);
556
557 src->exclusive = dest->exclusive = exclusive;
558
559 up_write(&dest->list_mutex);
560 up_write(&src->list_mutex);
561 return 0;
562
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;
571}
572
573
574/* remove the connection */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100575int 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100582 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct list_head *p;
586 int err = -ENOENT;
587 unsigned long flags;
588
589 down_write(&src->list_mutex);
Ingo Molnard8371f02006-07-03 00:25:21 -0700590 down_write_nested(&dest->list_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /* look for the connection */
593 list_for_each(p, &src->list_head) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100594 subs = list_entry(p, struct snd_seq_subscribers, src_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 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;
610 }
611 }
612
613 up_write(&dest->list_mutex);
614 up_write(&src->list_mutex);
615 return err;
616}
617
618
619/* get matched subscriber */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100620struct snd_seq_subscribers *snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp,
621 struct snd_seq_addr *dest_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 struct list_head *p;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100624 struct snd_seq_subscribers *s, *found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 down_read(&src_grp->list_mutex);
627 list_for_each(p, &src_grp->list_head) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100628 s = list_entry(p, struct snd_seq_subscribers, src_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (addr_match(dest_addr, &s->info.dest)) {
630 found = s;
631 break;
632 }
633 }
634 up_read(&src_grp->list_mutex);
635 return found;
636}
637
638/*
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().
643 */
644/* exported */
645int snd_seq_event_port_attach(int client,
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100646 struct snd_seq_port_callback *pcbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 int cap, int type, int midi_channels,
648 int midi_voices, char *portname)
649{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100650 struct snd_seq_port_info portinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 int ret;
652
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));
658
659 portinfo.capability = cap;
660 portinfo.type = type;
661 portinfo.kernel = pcbp;
662 portinfo.midi_channels = midi_channels;
663 portinfo.midi_voices = midi_voices;
664
665 /* Create it */
666 ret = snd_seq_kernel_client_ctl(client,
667 SNDRV_SEQ_IOCTL_CREATE_PORT,
668 &portinfo);
669
670 if (ret >= 0)
671 ret = portinfo.addr.port;
672
673 return ret;
674}
675
Takashi Iwai91715ed2006-04-28 15:13:39 +0200676EXPORT_SYMBOL(snd_seq_event_port_attach);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678/*
679 * Detach the driver from a port.
680 */
681/* exported */
682int snd_seq_event_port_detach(int client, int port)
683{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100684 struct snd_seq_port_info portinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int err;
686
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);
693
694 return err;
695}
Takashi Iwai91715ed2006-04-28 15:13:39 +0200696
697EXPORT_SYMBOL(snd_seq_event_port_detach);