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