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