Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * ALSA sequencer Ports | |
3 | * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl> | |
c1017a4c | 4 | * Jaroslav Kysela <perex@perex.cz> |
1da177e4 LT |
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 | ||
1da177e4 LT |
23 | #include <sound/core.h> |
24 | #include <linux/slab.h> | |
da155d5b | 25 | #include <linux/module.h> |
1da177e4 LT |
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 */ | |
c7e0b5bf TI |
59 | struct snd_seq_client_port *snd_seq_port_use_ptr(struct snd_seq_client *client, |
60 | int num) | |
1da177e4 | 61 | { |
c7e0b5bf | 62 | struct snd_seq_client_port *port; |
1da177e4 LT |
63 | |
64 | if (client == NULL) | |
65 | return NULL; | |
66 | read_lock(&client->ports_lock); | |
9244b2c3 | 67 | list_for_each_entry(port, &client->ports_list_head, list) { |
1da177e4 LT |
68 | 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 */ | |
c7e0b5bf TI |
82 | struct snd_seq_client_port *snd_seq_port_query_nearest(struct snd_seq_client *client, |
83 | struct snd_seq_port_info *pinfo) | |
1da177e4 LT |
84 | { |
85 | int num; | |
c7e0b5bf | 86 | struct snd_seq_client_port *port, *found; |
1da177e4 LT |
87 | |
88 | num = pinfo->addr.port; | |
89 | found = NULL; | |
90 | read_lock(&client->ports_lock); | |
9244b2c3 | 91 | list_for_each_entry(port, &client->ports_list_head, list) { |
1da177e4 LT |
92 | 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 | ||
c7e0b5bf TI |
112 | /* initialize snd_seq_port_subs_info */ |
113 | static void port_subs_info_init(struct snd_seq_port_subs_info *grp) | |
1da177e4 LT |
114 | { |
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) */ | |
c7e0b5bf TI |
126 | struct snd_seq_client_port *snd_seq_create_port(struct snd_seq_client *client, |
127 | int port) | |
1da177e4 LT |
128 | { |
129 | unsigned long flags; | |
9244b2c3 | 130 | struct snd_seq_client_port *new_port, *p; |
1da177e4 LT |
131 | int num = -1; |
132 | ||
133 | /* sanity check */ | |
7eaa943c TI |
134 | if (snd_BUG_ON(!client)) |
135 | return NULL; | |
1da177e4 LT |
136 | |
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 */ | |
ecca82b4 | 143 | new_port = kzalloc(sizeof(*new_port), GFP_KERNEL); |
1da177e4 LT |
144 | 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; | |
1a60d4c5 | 158 | mutex_lock(&client->ports_mutex); |
1da177e4 | 159 | write_lock_irqsave(&client->ports_lock, flags); |
9244b2c3 | 160 | list_for_each_entry(p, &client->ports_list_head, list) { |
1da177e4 LT |
161 | 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 */ | |
9244b2c3 | 167 | list_add_tail(&new_port->list, &p->list); |
1da177e4 LT |
168 | client->num_ports++; |
169 | new_port->addr.port = num; /* store the port number in the port */ | |
170 | write_unlock_irqrestore(&client->ports_lock, flags); | |
1a60d4c5 | 171 | mutex_unlock(&client->ports_mutex); |
1da177e4 LT |
172 | sprintf(new_port->name, "port-%d", num); |
173 | ||
174 | return new_port; | |
175 | } | |
176 | ||
177 | /* */ | |
c7e0b5bf TI |
178 | static int subscribe_port(struct snd_seq_client *client, |
179 | struct snd_seq_client_port *port, | |
180 | struct snd_seq_port_subs_info *grp, | |
181 | struct snd_seq_port_subscribe *info, int send_ack); | |
182 | static int unsubscribe_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); | |
1da177e4 LT |
186 | |
187 | ||
c7e0b5bf TI |
188 | static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr, |
189 | struct snd_seq_client **cp) | |
1da177e4 | 190 | { |
c7e0b5bf | 191 | struct snd_seq_client_port *p; |
1da177e4 LT |
192 | *cp = snd_seq_client_use_ptr(addr->client); |
193 | if (*cp) { | |
194 | p = snd_seq_port_use_ptr(*cp, addr->port); | |
195 | if (! p) { | |
196 | snd_seq_client_unlock(*cp); | |
197 | *cp = NULL; | |
198 | } | |
199 | return p; | |
200 | } | |
201 | return NULL; | |
202 | } | |
203 | ||
e53ec492 TI |
204 | static void delete_and_unsubscribe_port(struct snd_seq_client *client, |
205 | struct snd_seq_client_port *port, | |
206 | struct snd_seq_subscribers *subs, | |
207 | bool is_src, bool ack); | |
208 | ||
209 | static inline struct snd_seq_subscribers * | |
210 | get_subscriber(struct list_head *p, bool is_src) | |
211 | { | |
212 | if (is_src) | |
213 | return list_entry(p, struct snd_seq_subscribers, src_list); | |
214 | else | |
215 | return list_entry(p, struct snd_seq_subscribers, dest_list); | |
216 | } | |
217 | ||
1da177e4 LT |
218 | /* |
219 | * remove all subscribers on the list | |
220 | * this is called from port_delete, for each src and dest list. | |
221 | */ | |
c7e0b5bf TI |
222 | static void clear_subscriber_list(struct snd_seq_client *client, |
223 | struct snd_seq_client_port *port, | |
224 | struct snd_seq_port_subs_info *grp, | |
e53ec492 | 225 | int is_src) |
1da177e4 LT |
226 | { |
227 | struct list_head *p, *n; | |
228 | ||
1da177e4 | 229 | list_for_each_safe(p, n, &grp->list_head) { |
c7e0b5bf TI |
230 | struct snd_seq_subscribers *subs; |
231 | struct snd_seq_client *c; | |
232 | struct snd_seq_client_port *aport; | |
1da177e4 | 233 | |
e53ec492 TI |
234 | subs = get_subscriber(p, is_src); |
235 | if (is_src) | |
1da177e4 | 236 | aport = get_client_port(&subs->info.dest, &c); |
e53ec492 | 237 | else |
1da177e4 | 238 | aport = get_client_port(&subs->info.sender, &c); |
e53ec492 TI |
239 | delete_and_unsubscribe_port(client, port, subs, is_src, false); |
240 | ||
1da177e4 LT |
241 | if (!aport) { |
242 | /* looks like the connected port is being deleted. | |
243 | * we decrease the counter, and when both ports are deleted | |
244 | * remove the subscriber info | |
245 | */ | |
246 | if (atomic_dec_and_test(&subs->ref_count)) | |
247 | kfree(subs); | |
e53ec492 | 248 | continue; |
1da177e4 | 249 | } |
e53ec492 TI |
250 | |
251 | /* ok we got the connected port */ | |
252 | delete_and_unsubscribe_port(c, aport, subs, !is_src, true); | |
253 | kfree(subs); | |
254 | snd_seq_port_unlock(aport); | |
255 | snd_seq_client_unlock(c); | |
1da177e4 | 256 | } |
1da177e4 LT |
257 | } |
258 | ||
259 | /* delete port data */ | |
c7e0b5bf TI |
260 | static int port_delete(struct snd_seq_client *client, |
261 | struct snd_seq_client_port *port) | |
1da177e4 LT |
262 | { |
263 | /* set closing flag and wait for all port access are gone */ | |
264 | port->closing = 1; | |
265 | snd_use_lock_sync(&port->use_lock); | |
266 | ||
267 | /* clear subscribers info */ | |
e53ec492 TI |
268 | clear_subscriber_list(client, port, &port->c_src, true); |
269 | clear_subscriber_list(client, port, &port->c_dest, false); | |
1da177e4 LT |
270 | |
271 | if (port->private_free) | |
272 | port->private_free(port->private_data); | |
273 | ||
7eaa943c TI |
274 | snd_BUG_ON(port->c_src.count != 0); |
275 | snd_BUG_ON(port->c_dest.count != 0); | |
1da177e4 LT |
276 | |
277 | kfree(port); | |
278 | return 0; | |
279 | } | |
280 | ||
281 | ||
282 | /* delete a port with the given port id */ | |
c7e0b5bf | 283 | int snd_seq_delete_port(struct snd_seq_client *client, int port) |
1da177e4 LT |
284 | { |
285 | unsigned long flags; | |
9244b2c3 | 286 | struct snd_seq_client_port *found = NULL, *p; |
1da177e4 | 287 | |
1a60d4c5 | 288 | mutex_lock(&client->ports_mutex); |
1da177e4 | 289 | write_lock_irqsave(&client->ports_lock, flags); |
9244b2c3 | 290 | list_for_each_entry(p, &client->ports_list_head, list) { |
1da177e4 LT |
291 | if (p->addr.port == port) { |
292 | /* ok found. delete from the list at first */ | |
9244b2c3 | 293 | list_del(&p->list); |
1da177e4 LT |
294 | client->num_ports--; |
295 | found = p; | |
296 | break; | |
297 | } | |
298 | } | |
299 | write_unlock_irqrestore(&client->ports_lock, flags); | |
1a60d4c5 | 300 | mutex_unlock(&client->ports_mutex); |
1da177e4 LT |
301 | if (found) |
302 | return port_delete(client, found); | |
303 | else | |
304 | return -ENOENT; | |
305 | } | |
306 | ||
307 | /* delete the all ports belonging to the given client */ | |
c7e0b5bf | 308 | int snd_seq_delete_all_ports(struct snd_seq_client *client) |
1da177e4 LT |
309 | { |
310 | unsigned long flags; | |
9244b2c3 JB |
311 | struct list_head deleted_list; |
312 | struct snd_seq_client_port *port, *tmp; | |
1da177e4 LT |
313 | |
314 | /* move the port list to deleted_list, and | |
315 | * clear the port list in the client data. | |
316 | */ | |
1a60d4c5 | 317 | mutex_lock(&client->ports_mutex); |
1da177e4 LT |
318 | write_lock_irqsave(&client->ports_lock, flags); |
319 | if (! list_empty(&client->ports_list_head)) { | |
be7ee278 TI |
320 | list_add(&deleted_list, &client->ports_list_head); |
321 | list_del_init(&client->ports_list_head); | |
1da177e4 LT |
322 | } else { |
323 | INIT_LIST_HEAD(&deleted_list); | |
324 | } | |
325 | client->num_ports = 0; | |
326 | write_unlock_irqrestore(&client->ports_lock, flags); | |
327 | ||
328 | /* remove each port in deleted_list */ | |
9244b2c3 JB |
329 | list_for_each_entry_safe(port, tmp, &deleted_list, list) { |
330 | list_del(&port->list); | |
1da177e4 LT |
331 | snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port); |
332 | port_delete(client, port); | |
333 | } | |
1a60d4c5 | 334 | mutex_unlock(&client->ports_mutex); |
1da177e4 LT |
335 | return 0; |
336 | } | |
337 | ||
338 | /* set port info fields */ | |
c7e0b5bf TI |
339 | int snd_seq_set_port_info(struct snd_seq_client_port * port, |
340 | struct snd_seq_port_info * info) | |
1da177e4 | 341 | { |
7eaa943c TI |
342 | if (snd_BUG_ON(!port || !info)) |
343 | return -EINVAL; | |
1da177e4 LT |
344 | |
345 | /* set port name */ | |
346 | if (info->name[0]) | |
347 | strlcpy(port->name, info->name, sizeof(port->name)); | |
348 | ||
349 | /* set capabilities */ | |
350 | port->capability = info->capability; | |
351 | ||
352 | /* get port type */ | |
353 | port->type = info->type; | |
354 | ||
355 | /* information about supported channels/voices */ | |
356 | port->midi_channels = info->midi_channels; | |
357 | port->midi_voices = info->midi_voices; | |
358 | port->synth_voices = info->synth_voices; | |
359 | ||
360 | /* timestamping */ | |
361 | port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0; | |
362 | port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0; | |
363 | port->time_queue = info->time_queue; | |
364 | ||
365 | return 0; | |
366 | } | |
367 | ||
368 | /* get port info fields */ | |
c7e0b5bf TI |
369 | int snd_seq_get_port_info(struct snd_seq_client_port * port, |
370 | struct snd_seq_port_info * info) | |
1da177e4 | 371 | { |
7eaa943c TI |
372 | if (snd_BUG_ON(!port || !info)) |
373 | return -EINVAL; | |
1da177e4 LT |
374 | |
375 | /* get port name */ | |
376 | strlcpy(info->name, port->name, sizeof(info->name)); | |
377 | ||
378 | /* get capabilities */ | |
379 | info->capability = port->capability; | |
380 | ||
381 | /* get port type */ | |
382 | info->type = port->type; | |
383 | ||
384 | /* information about supported channels/voices */ | |
385 | info->midi_channels = port->midi_channels; | |
386 | info->midi_voices = port->midi_voices; | |
387 | info->synth_voices = port->synth_voices; | |
388 | ||
389 | /* get subscriber counts */ | |
390 | info->read_use = port->c_src.count; | |
391 | info->write_use = port->c_dest.count; | |
392 | ||
393 | /* timestamping */ | |
394 | info->flags = 0; | |
395 | if (port->timestamping) { | |
396 | info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP; | |
397 | if (port->time_real) | |
398 | info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL; | |
399 | info->time_queue = port->time_queue; | |
400 | } | |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
405 | ||
406 | ||
407 | /* | |
408 | * call callback functions (if any): | |
409 | * the callbacks are invoked only when the first (for connection) or | |
410 | * the last subscription (for disconnection) is done. Second or later | |
411 | * subscription results in increment of counter, but no callback is | |
412 | * invoked. | |
413 | * This feature is useful if these callbacks are associated with | |
414 | * initialization or termination of devices (see seq_midi.c). | |
415 | * | |
416 | * If callback_all option is set, the callback function is invoked | |
b6aa63ee | 417 | * at each connection/disconnection. |
1da177e4 LT |
418 | */ |
419 | ||
c7e0b5bf TI |
420 | static int subscribe_port(struct snd_seq_client *client, |
421 | struct snd_seq_client_port *port, | |
422 | struct snd_seq_port_subs_info *grp, | |
423 | struct snd_seq_port_subscribe *info, | |
424 | int send_ack) | |
1da177e4 LT |
425 | { |
426 | int err = 0; | |
427 | ||
428 | if (!try_module_get(port->owner)) | |
429 | return -EFAULT; | |
430 | grp->count++; | |
431 | if (grp->open && (port->callback_all || grp->count == 1)) { | |
432 | err = grp->open(port->private_data, info); | |
433 | if (err < 0) { | |
434 | module_put(port->owner); | |
435 | grp->count--; | |
436 | } | |
437 | } | |
438 | if (err >= 0 && send_ack && client->type == USER_CLIENT) | |
439 | snd_seq_client_notify_subscription(port->addr.client, port->addr.port, | |
440 | info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED); | |
441 | ||
442 | return err; | |
443 | } | |
444 | ||
c7e0b5bf TI |
445 | static int unsubscribe_port(struct snd_seq_client *client, |
446 | struct snd_seq_client_port *port, | |
447 | struct snd_seq_port_subs_info *grp, | |
448 | struct snd_seq_port_subscribe *info, | |
449 | int send_ack) | |
1da177e4 LT |
450 | { |
451 | int err = 0; | |
452 | ||
453 | if (! grp->count) | |
454 | return -EINVAL; | |
455 | grp->count--; | |
456 | if (grp->close && (port->callback_all || grp->count == 0)) | |
457 | err = grp->close(port->private_data, info); | |
458 | if (send_ack && client->type == USER_CLIENT) | |
459 | snd_seq_client_notify_subscription(port->addr.client, port->addr.port, | |
460 | info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED); | |
461 | module_put(port->owner); | |
462 | return err; | |
463 | } | |
464 | ||
465 | ||
466 | ||
467 | /* check if both addresses are identical */ | |
c7e0b5bf | 468 | static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s) |
1da177e4 LT |
469 | { |
470 | return (r->client == s->client) && (r->port == s->port); | |
471 | } | |
472 | ||
473 | /* check the two subscribe info match */ | |
474 | /* if flags is zero, checks only sender and destination addresses */ | |
c7e0b5bf TI |
475 | static int match_subs_info(struct snd_seq_port_subscribe *r, |
476 | struct snd_seq_port_subscribe *s) | |
1da177e4 LT |
477 | { |
478 | if (addr_match(&r->sender, &s->sender) && | |
479 | addr_match(&r->dest, &s->dest)) { | |
480 | if (r->flags && r->flags == s->flags) | |
481 | return r->queue == s->queue; | |
482 | else if (! r->flags) | |
483 | return 1; | |
484 | } | |
485 | return 0; | |
486 | } | |
487 | ||
e53ec492 TI |
488 | static int check_and_subscribe_port(struct snd_seq_client *client, |
489 | struct snd_seq_client_port *port, | |
490 | struct snd_seq_subscribers *subs, | |
491 | bool is_src, bool exclusive, bool ack) | |
1da177e4 | 492 | { |
e53ec492 TI |
493 | struct snd_seq_port_subs_info *grp; |
494 | struct list_head *p; | |
495 | struct snd_seq_subscribers *s; | |
496 | int err; | |
1da177e4 | 497 | |
e53ec492 | 498 | grp = is_src ? &port->c_src : &port->c_dest; |
1da177e4 | 499 | err = -EBUSY; |
e53ec492 | 500 | down_write(&grp->list_mutex); |
1da177e4 | 501 | if (exclusive) { |
e53ec492 | 502 | if (!list_empty(&grp->list_head)) |
1da177e4 LT |
503 | goto __error; |
504 | } else { | |
e53ec492 | 505 | if (grp->exclusive) |
1da177e4 LT |
506 | goto __error; |
507 | /* check whether already exists */ | |
e53ec492 TI |
508 | list_for_each(p, &grp->list_head) { |
509 | s = get_subscriber(p, is_src); | |
510 | if (match_subs_info(&subs->info, &s->info)) | |
1da177e4 LT |
511 | goto __error; |
512 | } | |
513 | } | |
514 | ||
e53ec492 TI |
515 | err = subscribe_port(client, port, grp, &subs->info, ack); |
516 | if (err < 0) { | |
517 | grp->exclusive = 0; | |
1da177e4 | 518 | goto __error; |
e53ec492 | 519 | } |
1da177e4 LT |
520 | |
521 | /* add to list */ | |
e53ec492 TI |
522 | write_lock_irq(&grp->list_lock); |
523 | if (is_src) | |
524 | list_add_tail(&subs->src_list, &grp->list_head); | |
525 | else | |
526 | list_add_tail(&subs->dest_list, &grp->list_head); | |
527 | grp->exclusive = exclusive; | |
528 | atomic_inc(&subs->ref_count); | |
529 | write_unlock_irq(&grp->list_lock); | |
530 | err = 0; | |
531 | ||
532 | __error: | |
533 | up_write(&grp->list_mutex); | |
534 | return err; | |
535 | } | |
1da177e4 | 536 | |
e53ec492 TI |
537 | static void delete_and_unsubscribe_port(struct snd_seq_client *client, |
538 | struct snd_seq_client_port *port, | |
539 | struct snd_seq_subscribers *subs, | |
540 | bool is_src, bool ack) | |
541 | { | |
542 | struct snd_seq_port_subs_info *grp; | |
549584e9 TI |
543 | struct list_head *list; |
544 | bool empty; | |
e53ec492 TI |
545 | |
546 | grp = is_src ? &port->c_src : &port->c_dest; | |
549584e9 | 547 | list = is_src ? &subs->src_list : &subs->dest_list; |
e53ec492 TI |
548 | down_write(&grp->list_mutex); |
549 | write_lock_irq(&grp->list_lock); | |
549584e9 TI |
550 | empty = list_empty(list); |
551 | if (!empty) | |
552 | list_del_init(list); | |
e53ec492 TI |
553 | grp->exclusive = 0; |
554 | write_unlock_irq(&grp->list_lock); | |
555 | up_write(&grp->list_mutex); | |
556 | ||
549584e9 TI |
557 | if (!empty) |
558 | unsubscribe_port(client, port, grp, &subs->info, ack); | |
e53ec492 TI |
559 | } |
560 | ||
561 | /* connect two ports */ | |
562 | int snd_seq_port_connect(struct snd_seq_client *connector, | |
563 | struct snd_seq_client *src_client, | |
564 | struct snd_seq_client_port *src_port, | |
565 | struct snd_seq_client *dest_client, | |
566 | struct snd_seq_client_port *dest_port, | |
567 | struct snd_seq_port_subscribe *info) | |
568 | { | |
569 | struct snd_seq_subscribers *subs; | |
570 | bool exclusive; | |
571 | int err; | |
572 | ||
573 | subs = kzalloc(sizeof(*subs), GFP_KERNEL); | |
574 | if (!subs) | |
575 | return -ENOMEM; | |
576 | ||
577 | subs->info = *info; | |
578 | atomic_set(&subs->ref_count, 0); | |
579 | INIT_LIST_HEAD(&subs->src_list); | |
580 | INIT_LIST_HEAD(&subs->dest_list); | |
581 | ||
582 | exclusive = !!(info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE); | |
583 | ||
584 | err = check_and_subscribe_port(src_client, src_port, subs, true, | |
585 | exclusive, | |
586 | connector->number != src_client->number); | |
587 | if (err < 0) | |
588 | goto error; | |
589 | err = check_and_subscribe_port(dest_client, dest_port, subs, false, | |
590 | exclusive, | |
591 | connector->number != dest_client->number); | |
592 | if (err < 0) | |
593 | goto error_dest; | |
1da177e4 | 594 | |
1da177e4 LT |
595 | return 0; |
596 | ||
e53ec492 TI |
597 | error_dest: |
598 | delete_and_unsubscribe_port(src_client, src_port, subs, true, | |
599 | connector->number != src_client->number); | |
600 | error: | |
1da177e4 | 601 | kfree(subs); |
1da177e4 LT |
602 | return err; |
603 | } | |
604 | ||
1da177e4 | 605 | /* remove the connection */ |
c7e0b5bf TI |
606 | int snd_seq_port_disconnect(struct snd_seq_client *connector, |
607 | struct snd_seq_client *src_client, | |
608 | struct snd_seq_client_port *src_port, | |
609 | struct snd_seq_client *dest_client, | |
610 | struct snd_seq_client_port *dest_port, | |
611 | struct snd_seq_port_subscribe *info) | |
1da177e4 | 612 | { |
c7e0b5bf | 613 | struct snd_seq_port_subs_info *src = &src_port->c_src; |
c7e0b5bf | 614 | struct snd_seq_subscribers *subs; |
1da177e4 | 615 | int err = -ENOENT; |
1da177e4 LT |
616 | |
617 | down_write(&src->list_mutex); | |
1da177e4 | 618 | /* look for the connection */ |
9244b2c3 | 619 | list_for_each_entry(subs, &src->list_head, src_list) { |
1da177e4 | 620 | if (match_subs_info(info, &subs->info)) { |
e53ec492 | 621 | atomic_dec(&subs->ref_count); /* mark as not ready */ |
1da177e4 LT |
622 | err = 0; |
623 | break; | |
624 | } | |
625 | } | |
1da177e4 | 626 | up_write(&src->list_mutex); |
e53ec492 TI |
627 | if (err < 0) |
628 | return err; | |
629 | ||
630 | delete_and_unsubscribe_port(src_client, src_port, subs, true, | |
631 | connector->number != src_client->number); | |
632 | delete_and_unsubscribe_port(dest_client, dest_port, subs, false, | |
633 | connector->number != dest_client->number); | |
634 | kfree(subs); | |
635 | return 0; | |
1da177e4 LT |
636 | } |
637 | ||
638 | ||
639 | /* get matched subscriber */ | |
c7e0b5bf TI |
640 | struct snd_seq_subscribers *snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp, |
641 | struct snd_seq_addr *dest_addr) | |
1da177e4 | 642 | { |
c7e0b5bf | 643 | struct snd_seq_subscribers *s, *found = NULL; |
1da177e4 LT |
644 | |
645 | down_read(&src_grp->list_mutex); | |
9244b2c3 | 646 | list_for_each_entry(s, &src_grp->list_head, src_list) { |
1da177e4 LT |
647 | if (addr_match(dest_addr, &s->info.dest)) { |
648 | found = s; | |
649 | break; | |
650 | } | |
651 | } | |
652 | up_read(&src_grp->list_mutex); | |
653 | return found; | |
654 | } | |
655 | ||
656 | /* | |
657 | * Attach a device driver that wants to receive events from the | |
658 | * sequencer. Returns the new port number on success. | |
659 | * A driver that wants to receive the events converted to midi, will | |
660 | * use snd_seq_midisynth_register_port(). | |
661 | */ | |
662 | /* exported */ | |
663 | int snd_seq_event_port_attach(int client, | |
c7e0b5bf | 664 | struct snd_seq_port_callback *pcbp, |
1da177e4 LT |
665 | int cap, int type, int midi_channels, |
666 | int midi_voices, char *portname) | |
667 | { | |
c7e0b5bf | 668 | struct snd_seq_port_info portinfo; |
1da177e4 LT |
669 | int ret; |
670 | ||
671 | /* Set up the port */ | |
672 | memset(&portinfo, 0, sizeof(portinfo)); | |
673 | portinfo.addr.client = client; | |
674 | strlcpy(portinfo.name, portname ? portname : "Unamed port", | |
675 | sizeof(portinfo.name)); | |
676 | ||
677 | portinfo.capability = cap; | |
678 | portinfo.type = type; | |
679 | portinfo.kernel = pcbp; | |
680 | portinfo.midi_channels = midi_channels; | |
681 | portinfo.midi_voices = midi_voices; | |
682 | ||
683 | /* Create it */ | |
684 | ret = snd_seq_kernel_client_ctl(client, | |
685 | SNDRV_SEQ_IOCTL_CREATE_PORT, | |
686 | &portinfo); | |
687 | ||
688 | if (ret >= 0) | |
689 | ret = portinfo.addr.port; | |
690 | ||
691 | return ret; | |
692 | } | |
693 | ||
91715ed9 | 694 | EXPORT_SYMBOL(snd_seq_event_port_attach); |
1da177e4 LT |
695 | |
696 | /* | |
697 | * Detach the driver from a port. | |
698 | */ | |
699 | /* exported */ | |
700 | int snd_seq_event_port_detach(int client, int port) | |
701 | { | |
c7e0b5bf | 702 | struct snd_seq_port_info portinfo; |
1da177e4 LT |
703 | int err; |
704 | ||
705 | memset(&portinfo, 0, sizeof(portinfo)); | |
706 | portinfo.addr.client = client; | |
707 | portinfo.addr.port = port; | |
708 | err = snd_seq_kernel_client_ctl(client, | |
709 | SNDRV_SEQ_IOCTL_DELETE_PORT, | |
710 | &portinfo); | |
711 | ||
712 | return err; | |
713 | } | |
91715ed9 TI |
714 | |
715 | EXPORT_SYMBOL(snd_seq_event_port_detach); |