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(rcu)(); 52 list_for_each_entry_rcu(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(rcu)(); 75 list_for_each_entry_rcu(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_HLIST_HEAD(&grp->list_head); 102 grp->count = 0; 103 grp->exclusive = 0; 104 init_rwsem(&grp->list_mutex); 105 grp->open = NULL; 106 grp->close = NULL; 107 } 108 109 110 /* create a port, 0 on success or a negative error code is returned 111 * the caller needs to unref the port via snd_seq_port_unlock() appropriately 112 */ 113 int snd_seq_create_port(struct snd_seq_client *client, 114 struct snd_seq_client_port **port_ret) 115 { 116 struct snd_seq_client_port *new_port; 117 118 *port_ret = NULL; 119 120 /* sanity check */ 121 if (snd_BUG_ON(!client)) 122 return -EINVAL; 123 124 if (client->num_ports >= SNDRV_SEQ_MAX_PORTS) { 125 pr_warn("ALSA: seq: too many ports for client %d\n", client->number); 126 return -EINVAL; 127 } 128 129 /* create a new port */ 130 new_port = kzalloc_obj(*new_port); 131 if (!new_port) 132 return -ENOMEM; /* failure, out of memory */ 133 /* init port data */ 134 new_port->addr.client = client->number; 135 new_port->addr.port = -1; 136 new_port->owner = THIS_MODULE; 137 snd_use_lock_init(&new_port->use_lock); 138 port_subs_info_init(&new_port->c_src); 139 port_subs_info_init(&new_port->c_dest); 140 snd_use_lock_use(&new_port->use_lock); 141 142 *port_ret = new_port; 143 144 return 0; 145 } 146 147 /* insert the port; return the port address or a negative error code */ 148 int snd_seq_insert_port(struct snd_seq_client *client, int port, 149 struct snd_seq_client_port *new_port) 150 { 151 struct snd_seq_client_port *p; 152 int num; 153 154 num = max(port, 0); 155 guard(mutex)(&client->ports_mutex); 156 struct list_head *insert_before = &client->ports_list_head; 157 list_for_each_entry(p, &client->ports_list_head, list) { 158 if (p->addr.port == port) 159 return -EBUSY; 160 if (p->addr.port > num) { 161 insert_before = &p->list; 162 break; 163 } 164 if (port < 0) /* auto-probe mode */ 165 num = p->addr.port + 1; 166 } 167 /* finish initializing the port before publishing it to RCU readers */ 168 new_port->addr.port = num; /* store the port number in the port */ 169 if (!new_port->name[0]) 170 sprintf(new_port->name, "port-%d", num); 171 /* insert the new port */ 172 list_add_tail_rcu(&new_port->list, insert_before); 173 client->num_ports++; 174 175 return num; 176 } 177 178 /* */ 179 static int subscribe_port(struct snd_seq_client *client, 180 struct snd_seq_client_port *port, 181 struct snd_seq_port_subs_info *grp, 182 struct snd_seq_port_subscribe *info, int send_ack); 183 static int unsubscribe_port(struct snd_seq_client *client, 184 struct snd_seq_client_port *port, 185 struct snd_seq_port_subs_info *grp, 186 struct snd_seq_port_subscribe *info, int send_ack); 187 188 189 static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr, 190 struct snd_seq_client **cp) 191 { 192 *cp = snd_seq_client_use_ptr(addr->client); 193 if (!*cp) 194 return NULL; 195 return snd_seq_port_use_ptr(*cp, addr->port); 196 } 197 198 static void delete_and_unsubscribe_port(struct snd_seq_client *client, 199 struct snd_seq_client_port *port, 200 struct snd_seq_subscribers *subs, 201 bool is_src, bool ack); 202 203 static inline struct snd_seq_subscribers * 204 get_subscriber(struct hlist_node *p, bool is_src) 205 { 206 if (is_src) 207 return hlist_entry(p, struct snd_seq_subscribers, src_list); 208 else 209 return hlist_entry(p, struct snd_seq_subscribers, dest_list); 210 } 211 212 /* 213 * remove all subscribers on the list 214 * this is called from port_delete, for each src and dest list. 215 */ 216 static void clear_subscriber_list(struct snd_seq_client *client, 217 struct snd_seq_client_port *port, 218 struct snd_seq_port_subs_info *grp, 219 int is_src) 220 { 221 struct hlist_node *p, *n; 222 223 hlist_for_each_safe(p, n, &grp->list_head) { 224 struct snd_seq_subscribers *subs; 225 226 subs = get_subscriber(p, is_src); 227 struct snd_seq_client *c __free(snd_seq_client) = NULL; 228 struct snd_seq_client_port *aport __free(snd_seq_port) = 229 is_src ? 230 get_client_port(&subs->info.dest, &c) : 231 get_client_port(&subs->info.sender, &c); 232 delete_and_unsubscribe_port(client, port, subs, is_src, false); 233 234 if (!aport) { 235 /* looks like the connected port is being deleted. 236 * we decrease the counter, and when both ports are deleted 237 * remove the subscriber info 238 */ 239 if (atomic_dec_and_test(&subs->ref_count)) 240 kfree_rcu(subs, rcu); 241 continue; 242 } 243 244 /* ok we got the connected port */ 245 delete_and_unsubscribe_port(c, aport, subs, !is_src, true); 246 kfree_rcu(subs, rcu); 247 } 248 } 249 250 /* delete port data */ 251 static int port_delete(struct snd_seq_client *client, 252 struct snd_seq_client_port *port) 253 { 254 /* set closing flag and wait for all port access are gone */ 255 port->closing = 1; 256 /* the port has already been unlinked from the client's port list; 257 * wait for a grace period so that RCU readers still traversing the 258 * list can no longer take a new use_lock reference, then drain the 259 * outstanding references before freeing 260 */ 261 synchronize_rcu(); 262 snd_use_lock_sync(&port->use_lock); 263 264 /* clear subscribers info */ 265 clear_subscriber_list(client, port, &port->c_src, true); 266 clear_subscriber_list(client, port, &port->c_dest, false); 267 268 if (port->private_free) 269 port->private_free(port->private_data); 270 271 snd_BUG_ON(port->c_src.count != 0); 272 snd_BUG_ON(port->c_dest.count != 0); 273 274 kfree(port); 275 return 0; 276 } 277 278 279 /* delete a port with the given port id */ 280 int snd_seq_delete_port(struct snd_seq_client *client, int port) 281 { 282 struct snd_seq_client_port *found = NULL, *p; 283 284 scoped_guard(mutex, &client->ports_mutex) { 285 list_for_each_entry(p, &client->ports_list_head, list) { 286 if (p->addr.port == port) { 287 /* ok found. delete from the list at first */ 288 list_del_rcu(&p->list); 289 client->num_ports--; 290 found = p; 291 break; 292 } 293 } 294 } 295 if (found) 296 return port_delete(client, found); 297 else 298 return -ENOENT; 299 } 300 301 /* delete the all ports belonging to the given client */ 302 int snd_seq_delete_all_ports(struct snd_seq_client *client) 303 { 304 struct snd_seq_client_port *port, *tmp; 305 306 /* unlink and delete each port; port_delete() waits for an RCU grace 307 * period before draining the port, so concurrent lockless readers can 308 * no longer take a new use_lock reference on it 309 */ 310 guard(mutex)(&client->ports_mutex); 311 list_for_each_entry_safe(port, tmp, &client->ports_list_head, list) { 312 list_del_rcu(&port->list); 313 client->num_ports--; 314 snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port); 315 port_delete(client, port); 316 } 317 return 0; 318 } 319 320 /* set port info fields */ 321 int snd_seq_set_port_info(struct snd_seq_client_port * port, 322 struct snd_seq_port_info * info) 323 { 324 if (snd_BUG_ON(!port || !info)) 325 return -EINVAL; 326 327 /* set port name */ 328 if (info->name[0]) 329 strscpy(port->name, info->name, sizeof(port->name)); 330 331 /* set capabilities */ 332 port->capability = info->capability; 333 334 /* get port type */ 335 port->type = info->type; 336 337 /* information about supported channels/voices */ 338 port->midi_channels = info->midi_channels; 339 port->midi_voices = info->midi_voices; 340 port->synth_voices = info->synth_voices; 341 342 /* timestamping */ 343 port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0; 344 port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0; 345 port->time_queue = info->time_queue; 346 347 /* UMP direction and group */ 348 port->direction = info->direction; 349 port->ump_group = info->ump_group; 350 if (port->ump_group > SNDRV_UMP_MAX_GROUPS) 351 port->ump_group = 0; 352 353 /* fill default port direction */ 354 if (!port->direction) { 355 if (info->capability & SNDRV_SEQ_PORT_CAP_READ) 356 port->direction |= SNDRV_SEQ_PORT_DIR_INPUT; 357 if (info->capability & SNDRV_SEQ_PORT_CAP_WRITE) 358 port->direction |= SNDRV_SEQ_PORT_DIR_OUTPUT; 359 } 360 361 port->is_midi1 = !!(info->flags & SNDRV_SEQ_PORT_FLG_IS_MIDI1); 362 363 return 0; 364 } 365 366 /* get port info fields */ 367 int snd_seq_get_port_info(struct snd_seq_client_port * port, 368 struct snd_seq_port_info * info) 369 { 370 if (snd_BUG_ON(!port || !info)) 371 return -EINVAL; 372 373 /* get port name */ 374 strscpy(info->name, port->name, sizeof(info->name)); 375 376 /* get capabilities */ 377 info->capability = port->capability; 378 379 /* get port type */ 380 info->type = port->type; 381 382 /* information about supported channels/voices */ 383 info->midi_channels = port->midi_channels; 384 info->midi_voices = port->midi_voices; 385 info->synth_voices = port->synth_voices; 386 387 /* get subscriber counts */ 388 info->read_use = port->c_src.count; 389 info->write_use = port->c_dest.count; 390 391 /* timestamping */ 392 info->flags = 0; 393 if (port->timestamping) { 394 info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP; 395 if (port->time_real) 396 info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL; 397 info->time_queue = port->time_queue; 398 } 399 400 if (port->is_midi1) 401 info->flags |= SNDRV_SEQ_PORT_FLG_IS_MIDI1; 402 403 /* UMP direction and group */ 404 info->direction = port->direction; 405 info->ump_group = port->ump_group; 406 407 return 0; 408 } 409 410 411 412 /* 413 * call callback functions (if any): 414 * the callbacks are invoked only when the first (for connection) or 415 * the last subscription (for disconnection) is done. Second or later 416 * subscription results in increment of counter, but no callback is 417 * invoked. 418 * This feature is useful if these callbacks are associated with 419 * initialization or termination of devices (see seq_midi.c). 420 */ 421 422 static int subscribe_port(struct snd_seq_client *client, 423 struct snd_seq_client_port *port, 424 struct snd_seq_port_subs_info *grp, 425 struct snd_seq_port_subscribe *info, 426 int send_ack) 427 { 428 int err = 0; 429 430 if (!try_module_get(port->owner)) 431 return -EFAULT; 432 grp->count++; 433 if (grp->open && grp->count == 1) { 434 err = grp->open(port->private_data, info); 435 if (err < 0) { 436 module_put(port->owner); 437 grp->count--; 438 } 439 } 440 if (err >= 0 && send_ack && client->type == USER_CLIENT) 441 snd_seq_client_notify_subscription(port->addr.client, port->addr.port, 442 info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED); 443 444 return err; 445 } 446 447 static int unsubscribe_port(struct snd_seq_client *client, 448 struct snd_seq_client_port *port, 449 struct snd_seq_port_subs_info *grp, 450 struct snd_seq_port_subscribe *info, 451 int send_ack) 452 { 453 int err = 0; 454 455 if (! grp->count) 456 return -EINVAL; 457 grp->count--; 458 if (grp->close && grp->count == 0) 459 err = grp->close(port->private_data, info); 460 if (send_ack && client->type == USER_CLIENT) 461 snd_seq_client_notify_subscription(port->addr.client, port->addr.port, 462 info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED); 463 module_put(port->owner); 464 return err; 465 } 466 467 468 469 /* check if both addresses are identical */ 470 static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s) 471 { 472 return (r->client == s->client) && (r->port == s->port); 473 } 474 475 /* check the two subscribe info match */ 476 /* if flags is zero, checks only sender and destination addresses */ 477 static int match_subs_info(struct snd_seq_port_subscribe *r, 478 struct snd_seq_port_subscribe *s) 479 { 480 if (addr_match(&r->sender, &s->sender) && 481 addr_match(&r->dest, &s->dest)) { 482 if (r->flags && r->flags == s->flags) 483 return r->queue == s->queue; 484 else if (! r->flags) 485 return 1; 486 } 487 return 0; 488 } 489 490 static int check_and_subscribe_port(struct snd_seq_client *client, 491 struct snd_seq_client_port *port, 492 struct snd_seq_subscribers *subs, 493 bool is_src, bool exclusive, bool ack) 494 { 495 struct snd_seq_port_subs_info *grp; 496 struct hlist_node *p; 497 struct snd_seq_subscribers *s; 498 int err; 499 500 grp = is_src ? &port->c_src : &port->c_dest; 501 guard(rwsem_write)(&grp->list_mutex); 502 if (exclusive) { 503 if (!hlist_empty(&grp->list_head)) 504 return -EBUSY; 505 } else { 506 if (grp->exclusive) 507 return -EBUSY; 508 /* check whether already exists */ 509 hlist_for_each(p, &grp->list_head) { 510 s = get_subscriber(p, is_src); 511 if (match_subs_info(&subs->info, &s->info)) 512 return -EBUSY; 513 } 514 } 515 516 err = subscribe_port(client, port, grp, &subs->info, ack); 517 if (err < 0) { 518 grp->exclusive = 0; 519 return err; 520 } 521 522 /* add to list */ 523 if (is_src) 524 hlist_add_tail_rcu(&subs->src_list, &grp->list_head); 525 else 526 hlist_add_tail_rcu(&subs->dest_list, &grp->list_head); 527 grp->exclusive = exclusive; 528 atomic_inc(&subs->ref_count); 529 530 return 0; 531 } 532 533 /* called with grp->list_mutex held */ 534 static void __delete_and_unsubscribe_port(struct snd_seq_client *client, 535 struct snd_seq_client_port *port, 536 struct snd_seq_subscribers *subs, 537 bool is_src, bool ack) 538 { 539 struct snd_seq_port_subs_info *grp; 540 struct hlist_node *list; 541 bool empty; 542 543 grp = is_src ? &port->c_src : &port->c_dest; 544 list = is_src ? &subs->src_list : &subs->dest_list; 545 empty = hlist_unhashed(list); 546 if (!empty) 547 hlist_del_init_rcu(list); 548 grp->exclusive = 0; 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_HLIST_NODE(&subs->src_list); 585 INIT_HLIST_NODE(&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_rcu(subs, rcu); 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 hlist_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_rcu(subs, rcu); 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 hlist_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