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, 0 on success 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, 115 struct snd_seq_client_port **port_ret) 116 { 117 struct snd_seq_client_port *new_port; 118 119 *port_ret = NULL; 120 121 /* sanity check */ 122 if (snd_BUG_ON(!client)) 123 return -EINVAL; 124 125 if (client->num_ports >= SNDRV_SEQ_MAX_PORTS) { 126 pr_warn("ALSA: seq: too many ports for client %d\n", client->number); 127 return -EINVAL; 128 } 129 130 /* create a new port */ 131 new_port = kzalloc_obj(*new_port); 132 if (!new_port) 133 return -ENOMEM; /* failure, out of memory */ 134 /* init port data */ 135 new_port->addr.client = client->number; 136 new_port->addr.port = -1; 137 new_port->owner = THIS_MODULE; 138 snd_use_lock_init(&new_port->use_lock); 139 port_subs_info_init(&new_port->c_src); 140 port_subs_info_init(&new_port->c_dest); 141 snd_use_lock_use(&new_port->use_lock); 142 143 *port_ret = new_port; 144 145 return 0; 146 } 147 148 /* insert the port; return the port address or a negative error code */ 149 int snd_seq_insert_port(struct snd_seq_client *client, int port, 150 struct snd_seq_client_port *new_port) 151 { 152 struct snd_seq_client_port *p; 153 int num; 154 155 num = max(port, 0); 156 guard(mutex)(&client->ports_mutex); 157 guard(write_lock_irq)(&client->ports_lock); 158 struct list_head *insert_before = &client->ports_list_head; 159 list_for_each_entry(p, &client->ports_list_head, list) { 160 if (p->addr.port == port) 161 return -EBUSY; 162 if (p->addr.port > num) { 163 insert_before = &p->list; 164 break; 165 } 166 if (port < 0) /* auto-probe mode */ 167 num = p->addr.port + 1; 168 } 169 /* insert the new port */ 170 list_add_tail(&new_port->list, insert_before); 171 client->num_ports++; 172 new_port->addr.port = num; /* store the port number in the port */ 173 if (!new_port->name[0]) 174 sprintf(new_port->name, "port-%d", num); 175 176 return num; 177 } 178 179 /* */ 180 static int subscribe_port(struct snd_seq_client *client, 181 struct snd_seq_client_port *port, 182 struct snd_seq_port_subs_info *grp, 183 struct snd_seq_port_subscribe *info, int send_ack); 184 static int unsubscribe_port(struct snd_seq_client *client, 185 struct snd_seq_client_port *port, 186 struct snd_seq_port_subs_info *grp, 187 struct snd_seq_port_subscribe *info, int send_ack); 188 189 190 static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr, 191 struct snd_seq_client **cp) 192 { 193 *cp = snd_seq_client_use_ptr(addr->client); 194 if (!*cp) 195 return NULL; 196 return snd_seq_port_use_ptr(*cp, addr->port); 197 } 198 199 static void delete_and_unsubscribe_port(struct snd_seq_client *client, 200 struct snd_seq_client_port *port, 201 struct snd_seq_subscribers *subs, 202 bool is_src, bool ack); 203 204 static inline struct snd_seq_subscribers * 205 get_subscriber(struct list_head *p, bool is_src) 206 { 207 if (is_src) 208 return list_entry(p, struct snd_seq_subscribers, src_list); 209 else 210 return list_entry(p, struct snd_seq_subscribers, dest_list); 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 is_src) 221 { 222 struct list_head *p, *n; 223 224 list_for_each_safe(p, n, &grp->list_head) { 225 struct snd_seq_subscribers *subs; 226 227 subs = get_subscriber(p, is_src); 228 struct snd_seq_client *c __free(snd_seq_client) = NULL; 229 struct snd_seq_client_port *aport __free(snd_seq_port) = 230 is_src ? 231 get_client_port(&subs->info.dest, &c) : 232 get_client_port(&subs->info.sender, &c); 233 delete_and_unsubscribe_port(client, port, subs, is_src, false); 234 235 if (!aport) { 236 /* looks like the connected port is being deleted. 237 * we decrease the counter, and when both ports are deleted 238 * remove the subscriber info 239 */ 240 if (atomic_dec_and_test(&subs->ref_count)) 241 kfree(subs); 242 continue; 243 } 244 245 /* ok we got the connected port */ 246 delete_and_unsubscribe_port(c, aport, subs, !is_src, true); 247 kfree(subs); 248 } 249 } 250 251 /* delete port data */ 252 static int port_delete(struct snd_seq_client *client, 253 struct snd_seq_client_port *port) 254 { 255 /* set closing flag and wait for all port access are gone */ 256 port->closing = 1; 257 snd_use_lock_sync(&port->use_lock); 258 259 /* clear subscribers info */ 260 clear_subscriber_list(client, port, &port->c_src, true); 261 clear_subscriber_list(client, port, &port->c_dest, false); 262 263 if (port->private_free) 264 port->private_free(port->private_data); 265 266 snd_BUG_ON(port->c_src.count != 0); 267 snd_BUG_ON(port->c_dest.count != 0); 268 269 kfree(port); 270 return 0; 271 } 272 273 274 /* delete a port with the given port id */ 275 int snd_seq_delete_port(struct snd_seq_client *client, int port) 276 { 277 struct snd_seq_client_port *found = NULL, *p; 278 279 scoped_guard(mutex, &client->ports_mutex) { 280 guard(write_lock_irq)(&client->ports_lock); 281 list_for_each_entry(p, &client->ports_list_head, list) { 282 if (p->addr.port == port) { 283 /* ok found. delete from the list at first */ 284 list_del(&p->list); 285 client->num_ports--; 286 found = p; 287 break; 288 } 289 } 290 } 291 if (found) 292 return port_delete(client, found); 293 else 294 return -ENOENT; 295 } 296 297 /* delete the all ports belonging to the given client */ 298 int snd_seq_delete_all_ports(struct snd_seq_client *client) 299 { 300 struct list_head deleted_list; 301 struct snd_seq_client_port *port, *tmp; 302 303 /* move the port list to deleted_list, and 304 * clear the port list in the client data. 305 */ 306 guard(mutex)(&client->ports_mutex); 307 scoped_guard(write_lock_irq, &client->ports_lock) { 308 if (!list_empty(&client->ports_list_head)) { 309 list_add(&deleted_list, &client->ports_list_head); 310 list_del_init(&client->ports_list_head); 311 } else { 312 INIT_LIST_HEAD(&deleted_list); 313 } 314 client->num_ports = 0; 315 } 316 317 /* remove each port in deleted_list */ 318 list_for_each_entry_safe(port, tmp, &deleted_list, list) { 319 list_del(&port->list); 320 snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port); 321 port_delete(client, port); 322 } 323 return 0; 324 } 325 326 /* set port info fields */ 327 int snd_seq_set_port_info(struct snd_seq_client_port * port, 328 struct snd_seq_port_info * info) 329 { 330 if (snd_BUG_ON(!port || !info)) 331 return -EINVAL; 332 333 /* set port name */ 334 if (info->name[0]) 335 strscpy(port->name, info->name, sizeof(port->name)); 336 337 /* set capabilities */ 338 port->capability = info->capability; 339 340 /* get port type */ 341 port->type = info->type; 342 343 /* information about supported channels/voices */ 344 port->midi_channels = info->midi_channels; 345 port->midi_voices = info->midi_voices; 346 port->synth_voices = info->synth_voices; 347 348 /* timestamping */ 349 port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0; 350 port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0; 351 port->time_queue = info->time_queue; 352 353 /* UMP direction and group */ 354 port->direction = info->direction; 355 port->ump_group = info->ump_group; 356 if (port->ump_group > SNDRV_UMP_MAX_GROUPS) 357 port->ump_group = 0; 358 359 /* fill default port direction */ 360 if (!port->direction) { 361 if (info->capability & SNDRV_SEQ_PORT_CAP_READ) 362 port->direction |= SNDRV_SEQ_PORT_DIR_INPUT; 363 if (info->capability & SNDRV_SEQ_PORT_CAP_WRITE) 364 port->direction |= SNDRV_SEQ_PORT_DIR_OUTPUT; 365 } 366 367 port->is_midi1 = !!(info->flags & SNDRV_SEQ_PORT_FLG_IS_MIDI1); 368 369 return 0; 370 } 371 372 /* get port info fields */ 373 int snd_seq_get_port_info(struct snd_seq_client_port * port, 374 struct snd_seq_port_info * info) 375 { 376 if (snd_BUG_ON(!port || !info)) 377 return -EINVAL; 378 379 /* get port name */ 380 strscpy(info->name, port->name, sizeof(info->name)); 381 382 /* get capabilities */ 383 info->capability = port->capability; 384 385 /* get port type */ 386 info->type = port->type; 387 388 /* information about supported channels/voices */ 389 info->midi_channels = port->midi_channels; 390 info->midi_voices = port->midi_voices; 391 info->synth_voices = port->synth_voices; 392 393 /* get subscriber counts */ 394 info->read_use = port->c_src.count; 395 info->write_use = port->c_dest.count; 396 397 /* timestamping */ 398 info->flags = 0; 399 if (port->timestamping) { 400 info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP; 401 if (port->time_real) 402 info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL; 403 info->time_queue = port->time_queue; 404 } 405 406 if (port->is_midi1) 407 info->flags |= SNDRV_SEQ_PORT_FLG_IS_MIDI1; 408 409 /* UMP direction and group */ 410 info->direction = port->direction; 411 info->ump_group = port->ump_group; 412 413 return 0; 414 } 415 416 417 418 /* 419 * call callback functions (if any): 420 * the callbacks are invoked only when the first (for connection) or 421 * the last subscription (for disconnection) is done. Second or later 422 * subscription results in increment of counter, but no callback is 423 * invoked. 424 * This feature is useful if these callbacks are associated with 425 * initialization or termination of devices (see seq_midi.c). 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 && 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 && 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 static int check_and_subscribe_port(struct snd_seq_client *client, 497 struct snd_seq_client_port *port, 498 struct snd_seq_subscribers *subs, 499 bool is_src, bool exclusive, bool ack) 500 { 501 struct snd_seq_port_subs_info *grp; 502 struct list_head *p; 503 struct snd_seq_subscribers *s; 504 int err; 505 506 grp = is_src ? &port->c_src : &port->c_dest; 507 guard(rwsem_write)(&grp->list_mutex); 508 if (exclusive) { 509 if (!list_empty(&grp->list_head)) 510 return -EBUSY; 511 } else { 512 if (grp->exclusive) 513 return -EBUSY; 514 /* check whether already exists */ 515 list_for_each(p, &grp->list_head) { 516 s = get_subscriber(p, is_src); 517 if (match_subs_info(&subs->info, &s->info)) 518 return -EBUSY; 519 } 520 } 521 522 err = subscribe_port(client, port, grp, &subs->info, ack); 523 if (err < 0) { 524 grp->exclusive = 0; 525 return err; 526 } 527 528 /* add to list */ 529 guard(write_lock_irq)(&grp->list_lock); 530 if (is_src) 531 list_add_tail(&subs->src_list, &grp->list_head); 532 else 533 list_add_tail(&subs->dest_list, &grp->list_head); 534 grp->exclusive = exclusive; 535 atomic_inc(&subs->ref_count); 536 537 return 0; 538 } 539 540 /* called with grp->list_mutex held */ 541 static void __delete_and_unsubscribe_port(struct snd_seq_client *client, 542 struct snd_seq_client_port *port, 543 struct snd_seq_subscribers *subs, 544 bool is_src, bool ack) 545 { 546 struct snd_seq_port_subs_info *grp; 547 struct list_head *list; 548 bool empty; 549 550 grp = is_src ? &port->c_src : &port->c_dest; 551 list = is_src ? &subs->src_list : &subs->dest_list; 552 scoped_guard(write_lock_irq, &grp->list_lock) { 553 empty = list_empty(list); 554 if (!empty) 555 list_del_init(list); 556 grp->exclusive = 0; 557 } 558 559 if (!empty) 560 unsubscribe_port(client, port, grp, &subs->info, ack); 561 } 562 563 static void delete_and_unsubscribe_port(struct snd_seq_client *client, 564 struct snd_seq_client_port *port, 565 struct snd_seq_subscribers *subs, 566 bool is_src, bool ack) 567 { 568 struct snd_seq_port_subs_info *grp; 569 570 grp = is_src ? &port->c_src : &port->c_dest; 571 guard(rwsem_write)(&grp->list_mutex); 572 __delete_and_unsubscribe_port(client, port, subs, is_src, ack); 573 } 574 575 /* connect two ports */ 576 int snd_seq_port_connect(struct snd_seq_client *connector, 577 struct snd_seq_client *src_client, 578 struct snd_seq_client_port *src_port, 579 struct snd_seq_client *dest_client, 580 struct snd_seq_client_port *dest_port, 581 struct snd_seq_port_subscribe *info) 582 { 583 struct snd_seq_subscribers *subs; 584 bool exclusive; 585 int err; 586 587 subs = kzalloc_obj(*subs); 588 if (!subs) 589 return -ENOMEM; 590 591 subs->info = *info; 592 atomic_set(&subs->ref_count, 0); 593 INIT_LIST_HEAD(&subs->src_list); 594 INIT_LIST_HEAD(&subs->dest_list); 595 596 exclusive = !!(info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE); 597 598 err = check_and_subscribe_port(src_client, src_port, subs, true, 599 exclusive, 600 connector->number != src_client->number); 601 if (err < 0) 602 goto error; 603 err = check_and_subscribe_port(dest_client, dest_port, subs, false, 604 exclusive, 605 connector->number != dest_client->number); 606 if (err < 0) 607 goto error_dest; 608 609 return 0; 610 611 error_dest: 612 delete_and_unsubscribe_port(src_client, src_port, subs, true, 613 connector->number != src_client->number); 614 error: 615 kfree(subs); 616 return err; 617 } 618 619 /* remove the connection */ 620 int snd_seq_port_disconnect(struct snd_seq_client *connector, 621 struct snd_seq_client *src_client, 622 struct snd_seq_client_port *src_port, 623 struct snd_seq_client *dest_client, 624 struct snd_seq_client_port *dest_port, 625 struct snd_seq_port_subscribe *info) 626 { 627 struct snd_seq_port_subs_info *dest = &dest_port->c_dest; 628 struct snd_seq_subscribers *subs; 629 int err = -ENOENT; 630 631 /* always start from deleting the dest port for avoiding concurrent 632 * deletions 633 */ 634 scoped_guard(rwsem_write, &dest->list_mutex) { 635 /* look for the connection */ 636 list_for_each_entry(subs, &dest->list_head, dest_list) { 637 if (match_subs_info(info, &subs->info)) { 638 __delete_and_unsubscribe_port(dest_client, dest_port, 639 subs, false, 640 connector->number != dest_client->number); 641 err = 0; 642 break; 643 } 644 } 645 } 646 if (err < 0) 647 return err; 648 649 delete_and_unsubscribe_port(src_client, src_port, subs, true, 650 connector->number != src_client->number); 651 kfree(subs); 652 return 0; 653 } 654 655 656 /* get matched subscriber */ 657 int snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp, 658 struct snd_seq_addr *dest_addr, 659 struct snd_seq_port_subscribe *subs) 660 { 661 struct snd_seq_subscribers *s; 662 int err = -ENOENT; 663 664 guard(rwsem_read)(&src_grp->list_mutex); 665 list_for_each_entry(s, &src_grp->list_head, src_list) { 666 if (addr_match(dest_addr, &s->info.dest)) { 667 *subs = s->info; 668 err = 0; 669 break; 670 } 671 } 672 return err; 673 } 674 675 /* 676 * Attach a device driver that wants to receive events from the 677 * sequencer. Returns the new port number on success. 678 * A driver that wants to receive the events converted to midi, will 679 * use snd_seq_midisynth_register_port(). 680 */ 681 /* exported */ 682 int snd_seq_event_port_attach(int client, 683 struct snd_seq_port_callback *pcbp, 684 int cap, int type, int midi_channels, 685 int midi_voices, char *portname) 686 { 687 struct snd_seq_port_info portinfo; 688 int ret; 689 690 /* Set up the port */ 691 memset(&portinfo, 0, sizeof(portinfo)); 692 portinfo.addr.client = client; 693 strscpy(portinfo.name, portname ? portname : "Unnamed port", 694 sizeof(portinfo.name)); 695 696 portinfo.capability = cap; 697 portinfo.type = type; 698 portinfo.kernel = pcbp; 699 portinfo.midi_channels = midi_channels; 700 portinfo.midi_voices = midi_voices; 701 702 /* Create it */ 703 ret = snd_seq_kernel_client_ctl(client, 704 SNDRV_SEQ_IOCTL_CREATE_PORT, 705 &portinfo); 706 707 if (ret >= 0) 708 ret = portinfo.addr.port; 709 710 return ret; 711 } 712 EXPORT_SYMBOL(snd_seq_event_port_attach); 713 714 /* 715 * Detach the driver from a port. 716 */ 717 /* exported */ 718 int snd_seq_event_port_detach(int client, int port) 719 { 720 struct snd_seq_port_info portinfo; 721 int err; 722 723 memset(&portinfo, 0, sizeof(portinfo)); 724 portinfo.addr.client = client; 725 portinfo.addr.port = port; 726 err = snd_seq_kernel_client_ctl(client, 727 SNDRV_SEQ_IOCTL_DELETE_PORT, 728 &portinfo); 729 730 return err; 731 } 732 EXPORT_SYMBOL(snd_seq_event_port_detach); 733