1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * ALSA sequencer Ports 4 * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl> 5 * Jaroslav Kysela <perex@perex.cz> 6 */ 7 8 #include <sound/core.h> 9 #include <linux/slab.h> 10 #include <linux/module.h> 11 #include "seq_system.h" 12 #include "seq_ports.h" 13 #include "seq_clientmgr.h" 14 15 /* 16 17 registration of client ports 18 19 */ 20 21 22 /* 23 24 NOTE: the current implementation of the port structure as a linked list is 25 not optimal for clients that have many ports. For sending messages to all 26 subscribers of a port we first need to find the address of the port 27 structure, which means we have to traverse the list. A direct access table 28 (array) would be better, but big preallocated arrays waste memory. 29 30 Possible actions: 31 32 1) leave it this way, a client does normaly does not have more than a few 33 ports 34 35 2) replace the linked list of ports by a array of pointers which is 36 dynamicly kmalloced. When a port is added or deleted we can simply allocate 37 a new array, copy the corresponding pointers, and delete the old one. We 38 then only need a pointer to this array, and an integer that tells us how 39 much elements are in array. 40 41 */ 42 43 /* return pointer to port structure - port is locked if found */ 44 struct snd_seq_client_port *snd_seq_port_use_ptr(struct snd_seq_client *client, 45 int num) 46 { 47 struct snd_seq_client_port *port; 48 49 if (client == NULL) 50 return NULL; 51 guard(read_lock)(&client->ports_lock); 52 list_for_each_entry(port, &client->ports_list_head, list) { 53 if (port->addr.port == num) { 54 if (port->closing) 55 break; /* deleting now */ 56 snd_use_lock_use(&port->use_lock); 57 return port; 58 } 59 } 60 return NULL; /* not found */ 61 } 62 63 64 /* search for the next port - port is locked if found */ 65 struct snd_seq_client_port *snd_seq_port_query_nearest(struct snd_seq_client *client, 66 struct snd_seq_port_info *pinfo) 67 { 68 int num; 69 struct snd_seq_client_port *port, *found; 70 bool check_inactive = (pinfo->capability & SNDRV_SEQ_PORT_CAP_INACTIVE); 71 72 num = pinfo->addr.port; 73 found = NULL; 74 guard(read_lock)(&client->ports_lock); 75 list_for_each_entry(port, &client->ports_list_head, list) { 76 if ((port->capability & SNDRV_SEQ_PORT_CAP_INACTIVE) && 77 !check_inactive) 78 continue; /* skip inactive ports */ 79 if (port->addr.port < num) 80 continue; 81 if (port->addr.port == num) { 82 found = port; 83 break; 84 } 85 if (found == NULL || port->addr.port < found->addr.port) 86 found = port; 87 } 88 if (found) { 89 if (found->closing) 90 found = NULL; 91 else 92 snd_use_lock_use(&found->use_lock); 93 } 94 return found; 95 } 96 97 98 /* initialize snd_seq_port_subs_info */ 99 static void port_subs_info_init(struct snd_seq_port_subs_info *grp) 100 { 101 INIT_LIST_HEAD(&grp->list_head); 102 grp->count = 0; 103 grp->exclusive = 0; 104 rwlock_init(&grp->list_lock); 105 init_rwsem(&grp->list_mutex); 106 grp->open = NULL; 107 grp->close = NULL; 108 } 109 110 111 /* create a port, port number or a negative error code is returned 112 * the caller needs to unref the port via snd_seq_port_unlock() appropriately 113 */ 114 int snd_seq_create_port(struct snd_seq_client *client, int port, 115 struct snd_seq_client_port **port_ret) 116 { 117 struct snd_seq_client_port *new_port, *p; 118 int num; 119 120 *port_ret = NULL; 121 122 /* sanity check */ 123 if (snd_BUG_ON(!client)) 124 return -EINVAL; 125 126 if (client->num_ports >= SNDRV_SEQ_MAX_PORTS) { 127 pr_warn("ALSA: seq: too many ports for client %d\n", client->number); 128 return -EINVAL; 129 } 130 131 /* create a new port */ 132 new_port = kzalloc_obj(*new_port); 133 if (!new_port) 134 return -ENOMEM; /* failure, out of memory */ 135 /* init port data */ 136 new_port->addr.client = client->number; 137 new_port->addr.port = -1; 138 new_port->owner = THIS_MODULE; 139 snd_use_lock_init(&new_port->use_lock); 140 port_subs_info_init(&new_port->c_src); 141 port_subs_info_init(&new_port->c_dest); 142 snd_use_lock_use(&new_port->use_lock); 143 144 num = max(port, 0); 145 guard(mutex)(&client->ports_mutex); 146 guard(write_lock_irq)(&client->ports_lock); 147 struct list_head *insert_before = &client->ports_list_head; 148 list_for_each_entry(p, &client->ports_list_head, list) { 149 if (p->addr.port == port) { 150 kfree(new_port); 151 return -EBUSY; 152 } 153 if (p->addr.port > num) { 154 insert_before = &p->list; 155 break; 156 } 157 if (port < 0) /* auto-probe mode */ 158 num = p->addr.port + 1; 159 } 160 /* insert the new port */ 161 list_add_tail(&new_port->list, insert_before); 162 client->num_ports++; 163 new_port->addr.port = num; /* store the port number in the port */ 164 sprintf(new_port->name, "port-%d", num); 165 *port_ret = new_port; 166 167 return num; 168 } 169 170 /* */ 171 static int subscribe_port(struct snd_seq_client *client, 172 struct snd_seq_client_port *port, 173 struct snd_seq_port_subs_info *grp, 174 struct snd_seq_port_subscribe *info, int send_ack); 175 static int unsubscribe_port(struct snd_seq_client *client, 176 struct snd_seq_client_port *port, 177 struct snd_seq_port_subs_info *grp, 178 struct snd_seq_port_subscribe *info, int send_ack); 179 180 181 static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr, 182 struct snd_seq_client **cp) 183 { 184 *cp = snd_seq_client_use_ptr(addr->client); 185 if (!*cp) 186 return NULL; 187 return snd_seq_port_use_ptr(*cp, addr->port); 188 } 189 190 static void delete_and_unsubscribe_port(struct snd_seq_client *client, 191 struct snd_seq_client_port *port, 192 struct snd_seq_subscribers *subs, 193 bool is_src, bool ack); 194 195 static inline struct snd_seq_subscribers * 196 get_subscriber(struct list_head *p, bool is_src) 197 { 198 if (is_src) 199 return list_entry(p, struct snd_seq_subscribers, src_list); 200 else 201 return list_entry(p, struct snd_seq_subscribers, dest_list); 202 } 203 204 /* 205 * remove all subscribers on the list 206 * this is called from port_delete, for each src and dest list. 207 */ 208 static void clear_subscriber_list(struct snd_seq_client *client, 209 struct snd_seq_client_port *port, 210 struct snd_seq_port_subs_info *grp, 211 int is_src) 212 { 213 struct list_head *p, *n; 214 215 list_for_each_safe(p, n, &grp->list_head) { 216 struct snd_seq_subscribers *subs; 217 218 subs = get_subscriber(p, is_src); 219 struct snd_seq_client *c __free(snd_seq_client) = NULL; 220 struct snd_seq_client_port *aport __free(snd_seq_port) = 221 is_src ? 222 get_client_port(&subs->info.dest, &c) : 223 get_client_port(&subs->info.sender, &c); 224 delete_and_unsubscribe_port(client, port, subs, is_src, false); 225 226 if (!aport) { 227 /* looks like the connected port is being deleted. 228 * we decrease the counter, and when both ports are deleted 229 * remove the subscriber info 230 */ 231 if (atomic_dec_and_test(&subs->ref_count)) 232 kfree(subs); 233 continue; 234 } 235 236 /* ok we got the connected port */ 237 delete_and_unsubscribe_port(c, aport, subs, !is_src, true); 238 kfree(subs); 239 } 240 } 241 242 /* delete port data */ 243 static int port_delete(struct snd_seq_client *client, 244 struct snd_seq_client_port *port) 245 { 246 /* set closing flag and wait for all port access are gone */ 247 port->closing = 1; 248 snd_use_lock_sync(&port->use_lock); 249 250 /* clear subscribers info */ 251 clear_subscriber_list(client, port, &port->c_src, true); 252 clear_subscriber_list(client, port, &port->c_dest, false); 253 254 if (port->private_free) 255 port->private_free(port->private_data); 256 257 snd_BUG_ON(port->c_src.count != 0); 258 snd_BUG_ON(port->c_dest.count != 0); 259 260 kfree(port); 261 return 0; 262 } 263 264 265 /* delete a port with the given port id */ 266 int snd_seq_delete_port(struct snd_seq_client *client, int port) 267 { 268 struct snd_seq_client_port *found = NULL, *p; 269 270 scoped_guard(mutex, &client->ports_mutex) { 271 guard(write_lock_irq)(&client->ports_lock); 272 list_for_each_entry(p, &client->ports_list_head, list) { 273 if (p->addr.port == port) { 274 /* ok found. delete from the list at first */ 275 list_del(&p->list); 276 client->num_ports--; 277 found = p; 278 break; 279 } 280 } 281 } 282 if (found) 283 return port_delete(client, found); 284 else 285 return -ENOENT; 286 } 287 288 /* delete the all ports belonging to the given client */ 289 int snd_seq_delete_all_ports(struct snd_seq_client *client) 290 { 291 struct list_head deleted_list; 292 struct snd_seq_client_port *port, *tmp; 293 294 /* move the port list to deleted_list, and 295 * clear the port list in the client data. 296 */ 297 guard(mutex)(&client->ports_mutex); 298 scoped_guard(write_lock_irq, &client->ports_lock) { 299 if (!list_empty(&client->ports_list_head)) { 300 list_add(&deleted_list, &client->ports_list_head); 301 list_del_init(&client->ports_list_head); 302 } else { 303 INIT_LIST_HEAD(&deleted_list); 304 } 305 client->num_ports = 0; 306 } 307 308 /* remove each port in deleted_list */ 309 list_for_each_entry_safe(port, tmp, &deleted_list, list) { 310 list_del(&port->list); 311 snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port); 312 port_delete(client, port); 313 } 314 return 0; 315 } 316 317 /* set port info fields */ 318 int snd_seq_set_port_info(struct snd_seq_client_port * port, 319 struct snd_seq_port_info * info) 320 { 321 if (snd_BUG_ON(!port || !info)) 322 return -EINVAL; 323 324 /* set port name */ 325 if (info->name[0]) 326 strscpy(port->name, info->name, sizeof(port->name)); 327 328 /* set capabilities */ 329 port->capability = info->capability; 330 331 /* get port type */ 332 port->type = info->type; 333 334 /* information about supported channels/voices */ 335 port->midi_channels = info->midi_channels; 336 port->midi_voices = info->midi_voices; 337 port->synth_voices = info->synth_voices; 338 339 /* timestamping */ 340 port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0; 341 port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0; 342 port->time_queue = info->time_queue; 343 344 /* UMP direction and group */ 345 port->direction = info->direction; 346 port->ump_group = info->ump_group; 347 if (port->ump_group > SNDRV_UMP_MAX_GROUPS) 348 port->ump_group = 0; 349 350 /* fill default port direction */ 351 if (!port->direction) { 352 if (info->capability & SNDRV_SEQ_PORT_CAP_READ) 353 port->direction |= SNDRV_SEQ_PORT_DIR_INPUT; 354 if (info->capability & SNDRV_SEQ_PORT_CAP_WRITE) 355 port->direction |= SNDRV_SEQ_PORT_DIR_OUTPUT; 356 } 357 358 port->is_midi1 = !!(info->flags & SNDRV_SEQ_PORT_FLG_IS_MIDI1); 359 360 return 0; 361 } 362 363 /* get port info fields */ 364 int snd_seq_get_port_info(struct snd_seq_client_port * port, 365 struct snd_seq_port_info * info) 366 { 367 if (snd_BUG_ON(!port || !info)) 368 return -EINVAL; 369 370 /* get port name */ 371 strscpy(info->name, port->name, sizeof(info->name)); 372 373 /* get capabilities */ 374 info->capability = port->capability; 375 376 /* get port type */ 377 info->type = port->type; 378 379 /* information about supported channels/voices */ 380 info->midi_channels = port->midi_channels; 381 info->midi_voices = port->midi_voices; 382 info->synth_voices = port->synth_voices; 383 384 /* get subscriber counts */ 385 info->read_use = port->c_src.count; 386 info->write_use = port->c_dest.count; 387 388 /* timestamping */ 389 info->flags = 0; 390 if (port->timestamping) { 391 info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP; 392 if (port->time_real) 393 info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL; 394 info->time_queue = port->time_queue; 395 } 396 397 if (port->is_midi1) 398 info->flags |= SNDRV_SEQ_PORT_FLG_IS_MIDI1; 399 400 /* UMP direction and group */ 401 info->direction = port->direction; 402 info->ump_group = port->ump_group; 403 404 return 0; 405 } 406 407 408 409 /* 410 * call callback functions (if any): 411 * the callbacks are invoked only when the first (for connection) or 412 * the last subscription (for disconnection) is done. Second or later 413 * subscription results in increment of counter, but no callback is 414 * invoked. 415 * This feature is useful if these callbacks are associated with 416 * initialization or termination of devices (see seq_midi.c). 417 */ 418 419 static int subscribe_port(struct snd_seq_client *client, 420 struct snd_seq_client_port *port, 421 struct snd_seq_port_subs_info *grp, 422 struct snd_seq_port_subscribe *info, 423 int send_ack) 424 { 425 int err = 0; 426 427 if (!try_module_get(port->owner)) 428 return -EFAULT; 429 grp->count++; 430 if (grp->open && grp->count == 1) { 431 err = grp->open(port->private_data, info); 432 if (err < 0) { 433 module_put(port->owner); 434 grp->count--; 435 } 436 } 437 if (err >= 0 && send_ack && client->type == USER_CLIENT) 438 snd_seq_client_notify_subscription(port->addr.client, port->addr.port, 439 info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED); 440 441 return err; 442 } 443 444 static int unsubscribe_port(struct snd_seq_client *client, 445 struct snd_seq_client_port *port, 446 struct snd_seq_port_subs_info *grp, 447 struct snd_seq_port_subscribe *info, 448 int send_ack) 449 { 450 int err = 0; 451 452 if (! grp->count) 453 return -EINVAL; 454 grp->count--; 455 if (grp->close && grp->count == 0) 456 err = grp->close(port->private_data, info); 457 if (send_ack && client->type == USER_CLIENT) 458 snd_seq_client_notify_subscription(port->addr.client, port->addr.port, 459 info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED); 460 module_put(port->owner); 461 return err; 462 } 463 464 465 466 /* check if both addresses are identical */ 467 static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s) 468 { 469 return (r->client == s->client) && (r->port == s->port); 470 } 471 472 /* check the two subscribe info match */ 473 /* if flags is zero, checks only sender and destination addresses */ 474 static int match_subs_info(struct snd_seq_port_subscribe *r, 475 struct snd_seq_port_subscribe *s) 476 { 477 if (addr_match(&r->sender, &s->sender) && 478 addr_match(&r->dest, &s->dest)) { 479 if (r->flags && r->flags == s->flags) 480 return r->queue == s->queue; 481 else if (! r->flags) 482 return 1; 483 } 484 return 0; 485 } 486 487 static int check_and_subscribe_port(struct snd_seq_client *client, 488 struct snd_seq_client_port *port, 489 struct snd_seq_subscribers *subs, 490 bool is_src, bool exclusive, bool ack) 491 { 492 struct snd_seq_port_subs_info *grp; 493 struct list_head *p; 494 struct snd_seq_subscribers *s; 495 int err; 496 497 grp = is_src ? &port->c_src : &port->c_dest; 498 guard(rwsem_write)(&grp->list_mutex); 499 if (exclusive) { 500 if (!list_empty(&grp->list_head)) 501 return -EBUSY; 502 } else { 503 if (grp->exclusive) 504 return -EBUSY; 505 /* check whether already exists */ 506 list_for_each(p, &grp->list_head) { 507 s = get_subscriber(p, is_src); 508 if (match_subs_info(&subs->info, &s->info)) 509 return -EBUSY; 510 } 511 } 512 513 err = subscribe_port(client, port, grp, &subs->info, ack); 514 if (err < 0) { 515 grp->exclusive = 0; 516 return err; 517 } 518 519 /* add to list */ 520 guard(write_lock_irq)(&grp->list_lock); 521 if (is_src) 522 list_add_tail(&subs->src_list, &grp->list_head); 523 else 524 list_add_tail(&subs->dest_list, &grp->list_head); 525 grp->exclusive = exclusive; 526 atomic_inc(&subs->ref_count); 527 528 return 0; 529 } 530 531 /* called with grp->list_mutex held */ 532 static void __delete_and_unsubscribe_port(struct snd_seq_client *client, 533 struct snd_seq_client_port *port, 534 struct snd_seq_subscribers *subs, 535 bool is_src, bool ack) 536 { 537 struct snd_seq_port_subs_info *grp; 538 struct list_head *list; 539 bool empty; 540 541 grp = is_src ? &port->c_src : &port->c_dest; 542 list = is_src ? &subs->src_list : &subs->dest_list; 543 scoped_guard(write_lock_irq, &grp->list_lock) { 544 empty = list_empty(list); 545 if (!empty) 546 list_del_init(list); 547 grp->exclusive = 0; 548 } 549 550 if (!empty) 551 unsubscribe_port(client, port, grp, &subs->info, ack); 552 } 553 554 static void delete_and_unsubscribe_port(struct snd_seq_client *client, 555 struct snd_seq_client_port *port, 556 struct snd_seq_subscribers *subs, 557 bool is_src, bool ack) 558 { 559 struct snd_seq_port_subs_info *grp; 560 561 grp = is_src ? &port->c_src : &port->c_dest; 562 guard(rwsem_write)(&grp->list_mutex); 563 __delete_and_unsubscribe_port(client, port, subs, is_src, ack); 564 } 565 566 /* connect two ports */ 567 int snd_seq_port_connect(struct snd_seq_client *connector, 568 struct snd_seq_client *src_client, 569 struct snd_seq_client_port *src_port, 570 struct snd_seq_client *dest_client, 571 struct snd_seq_client_port *dest_port, 572 struct snd_seq_port_subscribe *info) 573 { 574 struct snd_seq_subscribers *subs; 575 bool exclusive; 576 int err; 577 578 subs = kzalloc_obj(*subs); 579 if (!subs) 580 return -ENOMEM; 581 582 subs->info = *info; 583 atomic_set(&subs->ref_count, 0); 584 INIT_LIST_HEAD(&subs->src_list); 585 INIT_LIST_HEAD(&subs->dest_list); 586 587 exclusive = !!(info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE); 588 589 err = check_and_subscribe_port(src_client, src_port, subs, true, 590 exclusive, 591 connector->number != src_client->number); 592 if (err < 0) 593 goto error; 594 err = check_and_subscribe_port(dest_client, dest_port, subs, false, 595 exclusive, 596 connector->number != dest_client->number); 597 if (err < 0) 598 goto error_dest; 599 600 return 0; 601 602 error_dest: 603 delete_and_unsubscribe_port(src_client, src_port, subs, true, 604 connector->number != src_client->number); 605 error: 606 kfree(subs); 607 return err; 608 } 609 610 /* remove the connection */ 611 int snd_seq_port_disconnect(struct snd_seq_client *connector, 612 struct snd_seq_client *src_client, 613 struct snd_seq_client_port *src_port, 614 struct snd_seq_client *dest_client, 615 struct snd_seq_client_port *dest_port, 616 struct snd_seq_port_subscribe *info) 617 { 618 struct snd_seq_port_subs_info *dest = &dest_port->c_dest; 619 struct snd_seq_subscribers *subs; 620 int err = -ENOENT; 621 622 /* always start from deleting the dest port for avoiding concurrent 623 * deletions 624 */ 625 scoped_guard(rwsem_write, &dest->list_mutex) { 626 /* look for the connection */ 627 list_for_each_entry(subs, &dest->list_head, dest_list) { 628 if (match_subs_info(info, &subs->info)) { 629 __delete_and_unsubscribe_port(dest_client, dest_port, 630 subs, false, 631 connector->number != dest_client->number); 632 err = 0; 633 break; 634 } 635 } 636 } 637 if (err < 0) 638 return err; 639 640 delete_and_unsubscribe_port(src_client, src_port, subs, true, 641 connector->number != src_client->number); 642 kfree(subs); 643 return 0; 644 } 645 646 647 /* get matched subscriber */ 648 int snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp, 649 struct snd_seq_addr *dest_addr, 650 struct snd_seq_port_subscribe *subs) 651 { 652 struct snd_seq_subscribers *s; 653 int err = -ENOENT; 654 655 guard(rwsem_read)(&src_grp->list_mutex); 656 list_for_each_entry(s, &src_grp->list_head, src_list) { 657 if (addr_match(dest_addr, &s->info.dest)) { 658 *subs = s->info; 659 err = 0; 660 break; 661 } 662 } 663 return err; 664 } 665 666 /* 667 * Attach a device driver that wants to receive events from the 668 * sequencer. Returns the new port number on success. 669 * A driver that wants to receive the events converted to midi, will 670 * use snd_seq_midisynth_register_port(). 671 */ 672 /* exported */ 673 int snd_seq_event_port_attach(int client, 674 struct snd_seq_port_callback *pcbp, 675 int cap, int type, int midi_channels, 676 int midi_voices, char *portname) 677 { 678 struct snd_seq_port_info portinfo; 679 int ret; 680 681 /* Set up the port */ 682 memset(&portinfo, 0, sizeof(portinfo)); 683 portinfo.addr.client = client; 684 strscpy(portinfo.name, portname ? portname : "Unnamed port", 685 sizeof(portinfo.name)); 686 687 portinfo.capability = cap; 688 portinfo.type = type; 689 portinfo.kernel = pcbp; 690 portinfo.midi_channels = midi_channels; 691 portinfo.midi_voices = midi_voices; 692 693 /* Create it */ 694 ret = snd_seq_kernel_client_ctl(client, 695 SNDRV_SEQ_IOCTL_CREATE_PORT, 696 &portinfo); 697 698 if (ret >= 0) 699 ret = portinfo.addr.port; 700 701 return ret; 702 } 703 EXPORT_SYMBOL(snd_seq_event_port_attach); 704 705 /* 706 * Detach the driver from a port. 707 */ 708 /* exported */ 709 int snd_seq_event_port_detach(int client, int port) 710 { 711 struct snd_seq_port_info portinfo; 712 int err; 713 714 memset(&portinfo, 0, sizeof(portinfo)); 715 portinfo.addr.client = client; 716 portinfo.addr.port = port; 717 err = snd_seq_kernel_client_ctl(client, 718 SNDRV_SEQ_IOCTL_DELETE_PORT, 719 &portinfo); 720 721 return err; 722 } 723 EXPORT_SYMBOL(snd_seq_event_port_detach); 724