1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * ALSA sequencer Client Manager 4 * Copyright (c) 1998-2001 by Frank van de Pol <fvdpol@coil.demon.nl> 5 * Jaroslav Kysela <perex@perex.cz> 6 * Takashi Iwai <tiwai@suse.de> 7 */ 8 9 #include <linux/init.h> 10 #include <linux/export.h> 11 #include <linux/slab.h> 12 #include <sound/core.h> 13 #include <sound/minors.h> 14 #include <linux/kmod.h> 15 16 #include <sound/seq_kernel.h> 17 #include <sound/ump.h> 18 #include "seq_clientmgr.h" 19 #include "seq_memory.h" 20 #include "seq_queue.h" 21 #include "seq_timer.h" 22 #include "seq_info.h" 23 #include "seq_system.h" 24 #include "seq_ump_convert.h" 25 #include <sound/seq_device.h> 26 #ifdef CONFIG_COMPAT 27 #include <linux/compat.h> 28 #endif 29 30 /* Client Manager 31 32 * this module handles the connections of userland and kernel clients 33 * 34 */ 35 36 /* 37 * There are four ranges of client numbers (last two shared): 38 * 0..15: global clients 39 * 16..127: statically allocated client numbers for cards 0..27 40 * 128..191: dynamically allocated client numbers for cards 28..31 41 * 128..191: dynamically allocated client numbers for applications 42 */ 43 44 /* number of kernel non-card clients */ 45 #define SNDRV_SEQ_GLOBAL_CLIENTS 16 46 /* clients per cards, for static clients */ 47 #define SNDRV_SEQ_CLIENTS_PER_CARD 4 48 /* dynamically allocated client numbers (both kernel drivers and user space) */ 49 #define SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN 128 50 51 #define SNDRV_SEQ_LFLG_INPUT 0x0001 52 #define SNDRV_SEQ_LFLG_OUTPUT 0x0002 53 #define SNDRV_SEQ_LFLG_OPEN (SNDRV_SEQ_LFLG_INPUT|SNDRV_SEQ_LFLG_OUTPUT) 54 55 static DEFINE_SPINLOCK(clients_lock); 56 static DEFINE_MUTEX(register_mutex); 57 58 /* 59 * client table 60 */ 61 static char clienttablock[SNDRV_SEQ_MAX_CLIENTS]; 62 static struct snd_seq_client __rcu *clienttab[SNDRV_SEQ_MAX_CLIENTS]; 63 static struct snd_seq_usage client_usage; 64 65 /* 66 * prototypes 67 */ 68 static int bounce_error_event(struct snd_seq_client *client, 69 struct snd_seq_event *event, 70 int err, int atomic, int hop); 71 static int snd_seq_deliver_single_event(struct snd_seq_client *client, 72 struct snd_seq_event *event, 73 int atomic, int hop); 74 75 #if IS_ENABLED(CONFIG_SND_SEQ_UMP) 76 static void free_ump_info(struct snd_seq_client *client); 77 #endif 78 79 /* 80 */ 81 static inline unsigned short snd_seq_file_flags(struct file *file) 82 { 83 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) { 84 case FMODE_WRITE: 85 return SNDRV_SEQ_LFLG_OUTPUT; 86 case FMODE_READ: 87 return SNDRV_SEQ_LFLG_INPUT; 88 default: 89 return SNDRV_SEQ_LFLG_OPEN; 90 } 91 } 92 93 static inline int snd_seq_write_pool_allocated(struct snd_seq_client *client) 94 { 95 return snd_seq_total_cells(client->pool) > 0; 96 } 97 98 /* return pointer to client structure for specified id; 99 * the caller must guarantee the client's lifetime by itself, as neither RCU 100 * nor a use_lock reference is taken here. 101 */ 102 static struct snd_seq_client *clientptr(int clientid) 103 { 104 if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) { 105 pr_debug("ALSA: seq: oops. Trying to get pointer to client %d\n", 106 clientid); 107 return NULL; 108 } 109 return rcu_dereference_protected(clienttab[clientid], true); 110 } 111 112 static struct snd_seq_client *client_use_ptr(int clientid, bool load_module) 113 { 114 struct snd_seq_client *client; 115 116 if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) { 117 pr_debug("ALSA: seq: oops. Trying to get pointer to client %d\n", 118 clientid); 119 return NULL; 120 } 121 scoped_guard(rcu) { 122 client = rcu_dereference(clienttab[clientid]); 123 if (client) 124 return snd_seq_client_ref(client); 125 if (clienttablock[clientid]) 126 return NULL; 127 } 128 #ifdef CONFIG_MODULES 129 if (load_module) { 130 static DECLARE_BITMAP(client_requested, SNDRV_SEQ_GLOBAL_CLIENTS); 131 static DECLARE_BITMAP(card_requested, SNDRV_CARDS); 132 133 if (clientid < SNDRV_SEQ_GLOBAL_CLIENTS) { 134 int idx; 135 136 if (!test_and_set_bit(clientid, client_requested)) { 137 for (idx = 0; idx < 15; idx++) { 138 if (seq_client_load[idx] < 0) 139 break; 140 if (seq_client_load[idx] == clientid) { 141 request_module("snd-seq-client-%i", 142 clientid); 143 break; 144 } 145 } 146 } 147 } else if (clientid < SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN) { 148 int card = (clientid - SNDRV_SEQ_GLOBAL_CLIENTS) / 149 SNDRV_SEQ_CLIENTS_PER_CARD; 150 if (card < snd_ecards_limit) { 151 if (!test_and_set_bit(card, card_requested)) 152 snd_request_card(card); 153 snd_seq_device_load_drivers(); 154 } 155 } 156 scoped_guard(rcu) { 157 client = rcu_dereference(clienttab[clientid]); 158 if (client) 159 return snd_seq_client_ref(client); 160 } 161 } 162 #endif 163 return NULL; 164 } 165 166 /* get snd_seq_client object for the given id quickly */ 167 struct snd_seq_client *snd_seq_client_use_ptr(int clientid) 168 { 169 return client_use_ptr(clientid, false); 170 } 171 172 /* get snd_seq_client object for the given id; 173 * if not found, retry after loading the modules 174 */ 175 static struct snd_seq_client *client_load_and_use_ptr(int clientid) 176 { 177 return client_use_ptr(clientid, IS_ENABLED(CONFIG_MODULES)); 178 } 179 180 static void usage_alloc(struct snd_seq_usage *res, int num) 181 { 182 res->cur += num; 183 if (res->cur > res->peak) 184 res->peak = res->cur; 185 } 186 187 static void usage_free(struct snd_seq_usage *res, int num) 188 { 189 res->cur -= num; 190 } 191 192 /* initialise data structures */ 193 int __init client_init_data(void) 194 { 195 /* zap out the client table */ 196 memset(&clienttablock, 0, sizeof(clienttablock)); 197 memset(&clienttab, 0, sizeof(clienttab)); 198 return 0; 199 } 200 201 202 static struct snd_seq_client *seq_create_client1(int client_index, int poolsize) 203 { 204 int c; 205 struct snd_seq_client *client; 206 207 /* init client data */ 208 client = kzalloc_obj(*client); 209 if (client == NULL) 210 return NULL; 211 client->pool = snd_seq_pool_new(poolsize); 212 if (client->pool == NULL) { 213 kfree(client); 214 return NULL; 215 } 216 client->type = NO_CLIENT; 217 snd_use_lock_init(&client->use_lock); 218 mutex_init(&client->ports_mutex); 219 INIT_LIST_HEAD(&client->ports_list_head); 220 mutex_init(&client->ioctl_mutex); 221 client->ump_endpoint_port = -1; 222 223 /* find free slot in the client table */ 224 scoped_guard(spinlock_irq, &clients_lock) { 225 if (client_index < 0) { 226 for (c = SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN; 227 c < SNDRV_SEQ_MAX_CLIENTS; 228 c++) { 229 if (rcu_access_pointer(clienttab[c]) || clienttablock[c]) 230 continue; 231 client->number = c; 232 rcu_assign_pointer(clienttab[c], client); 233 return client; 234 } 235 } else { 236 if (rcu_access_pointer(clienttab[client_index]) == NULL && 237 !clienttablock[client_index]) { 238 client->number = client_index; 239 rcu_assign_pointer(clienttab[client_index], client); 240 return client; 241 } 242 } 243 } 244 245 snd_seq_pool_delete(&client->pool); 246 kfree(client); 247 return NULL; /* no free slot found or busy, return failure code */ 248 } 249 250 251 static int seq_free_client1(struct snd_seq_client *client) 252 { 253 if (!client) 254 return 0; 255 scoped_guard(spinlock_irq, &clients_lock) { 256 clienttablock[client->number] = 1; 257 rcu_assign_pointer(clienttab[client->number], NULL); 258 } 259 snd_seq_delete_all_ports(client); 260 snd_seq_queue_client_leave(client->number); 261 /* the client has been unpublished from the table; wait for a grace 262 * period so that lockless readers (snd_seq_client_use_ptr()) that 263 * observed the old pointer can no longer take a new use_lock 264 * reference, then drain the outstanding references before freeing 265 */ 266 synchronize_rcu(); 267 snd_use_lock_sync(&client->use_lock); 268 if (client->pool) 269 snd_seq_pool_delete(&client->pool); 270 scoped_guard(spinlock_irq, &clients_lock) { 271 clienttablock[client->number] = 0; 272 } 273 return 0; 274 } 275 276 277 static void seq_free_client(struct snd_seq_client * client) 278 { 279 scoped_guard(mutex, ®ister_mutex) { 280 switch (client->type) { 281 case NO_CLIENT: 282 pr_warn("ALSA: seq: Trying to free unused client %d\n", 283 client->number); 284 break; 285 case USER_CLIENT: 286 case KERNEL_CLIENT: 287 seq_free_client1(client); 288 usage_free(&client_usage, 1); 289 break; 290 291 default: 292 pr_err("ALSA: seq: Trying to free client %d with undefined type = %d\n", 293 client->number, client->type); 294 } 295 } 296 297 snd_seq_system_client_ev_client_exit(client->number); 298 } 299 300 301 302 /* -------------------------------------------------------- */ 303 304 /* create a user client */ 305 static int snd_seq_open(struct inode *inode, struct file *file) 306 { 307 int c, mode; /* client id */ 308 struct snd_seq_client *client; 309 struct snd_seq_user_client *user; 310 311 stream_open(inode, file); 312 313 scoped_guard(mutex, ®ister_mutex) { 314 client = seq_create_client1(-1, SNDRV_SEQ_DEFAULT_EVENTS); 315 if (!client) 316 return -ENOMEM; /* failure code */ 317 318 mode = snd_seq_file_flags(file); 319 if (mode & SNDRV_SEQ_LFLG_INPUT) 320 client->accept_input = 1; 321 if (mode & SNDRV_SEQ_LFLG_OUTPUT) 322 client->accept_output = 1; 323 324 user = &client->data.user; 325 user->fifo = NULL; 326 user->fifo_pool_size = 0; 327 328 if (mode & SNDRV_SEQ_LFLG_INPUT) { 329 user->fifo_pool_size = SNDRV_SEQ_DEFAULT_CLIENT_EVENTS; 330 user->fifo = snd_seq_fifo_new(user->fifo_pool_size); 331 if (user->fifo == NULL) { 332 seq_free_client1(client); 333 kfree(client); 334 return -ENOMEM; 335 } 336 } 337 338 usage_alloc(&client_usage, 1); 339 client->type = USER_CLIENT; 340 } 341 342 c = client->number; 343 file->private_data = client; 344 345 /* fill client data */ 346 user->file = file; 347 sprintf(client->name, "Client-%d", c); 348 client->data.user.owner = get_pid(task_pid(current)); 349 350 /* make others aware this new client */ 351 snd_seq_system_client_ev_client_start(c); 352 353 return 0; 354 } 355 356 /* delete a user client */ 357 static int snd_seq_release(struct inode *inode, struct file *file) 358 { 359 struct snd_seq_client *client = file->private_data; 360 361 if (client) { 362 seq_free_client(client); 363 if (client->data.user.fifo) 364 snd_seq_fifo_delete(&client->data.user.fifo); 365 #if IS_ENABLED(CONFIG_SND_SEQ_UMP) 366 free_ump_info(client); 367 #endif 368 put_pid(client->data.user.owner); 369 kfree(client); 370 } 371 372 return 0; 373 } 374 375 static bool event_is_compatible(const struct snd_seq_client *client, 376 const struct snd_seq_event *ev) 377 { 378 if (snd_seq_ev_is_ump(ev) && !client->midi_version) 379 return false; 380 if (snd_seq_ev_is_ump(ev) && snd_seq_ev_is_variable(ev)) 381 return false; 382 return true; 383 } 384 385 /* handle client read() */ 386 /* possible error values: 387 * -ENXIO invalid client or file open mode 388 * -ENOSPC FIFO overflow (the flag is cleared after this error report) 389 * -EINVAL no enough user-space buffer to write the whole event 390 * -EFAULT seg. fault during copy to user space 391 */ 392 static ssize_t snd_seq_read(struct file *file, char __user *buf, size_t count, 393 loff_t *offset) 394 { 395 struct snd_seq_client *client = file->private_data; 396 struct snd_seq_fifo *fifo; 397 size_t aligned_size; 398 int err; 399 long result = 0; 400 struct snd_seq_event_cell *cell; 401 402 if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT)) 403 return -ENXIO; 404 405 if (!access_ok(buf, count)) 406 return -EFAULT; 407 408 /* check client structures are in place */ 409 if (snd_BUG_ON(!client)) 410 return -ENXIO; 411 412 if (!client->accept_input) 413 return -ENXIO; 414 fifo = client->data.user.fifo; 415 if (!fifo) 416 return -ENXIO; 417 418 if (atomic_read(&fifo->overflow) > 0) { 419 /* buffer overflow is detected */ 420 snd_seq_fifo_clear(fifo); 421 /* return error code */ 422 return -ENOSPC; 423 } 424 425 cell = NULL; 426 err = 0; 427 guard(snd_seq_fifo)(fifo); 428 429 if (IS_ENABLED(CONFIG_SND_SEQ_UMP) && client->midi_version > 0) 430 aligned_size = sizeof(struct snd_seq_ump_event); 431 else 432 aligned_size = sizeof(struct snd_seq_event); 433 434 /* while data available in queue */ 435 while (count >= aligned_size) { 436 int nonblock; 437 438 nonblock = (file->f_flags & O_NONBLOCK) || result > 0; 439 err = snd_seq_fifo_cell_out(fifo, &cell, nonblock); 440 if (err < 0) 441 break; 442 if (!event_is_compatible(client, &cell->event)) { 443 snd_seq_cell_free(cell); 444 cell = NULL; 445 continue; 446 } 447 if (snd_seq_ev_is_variable(&cell->event)) { 448 struct snd_seq_ump_event tmpev; 449 450 memcpy(&tmpev, &cell->event, aligned_size); 451 tmpev.data.ext.len &= ~SNDRV_SEQ_EXT_MASK; 452 tmpev.data.ext.ptr = NULL; 453 if (copy_to_user(buf, &tmpev, aligned_size)) { 454 err = -EFAULT; 455 break; 456 } 457 count -= aligned_size; 458 buf += aligned_size; 459 err = snd_seq_expand_var_event(&cell->event, count, 460 (char __force *)buf, 0, 461 aligned_size); 462 if (err < 0) 463 break; 464 result += err; 465 count -= err; 466 buf += err; 467 } else { 468 if (copy_to_user(buf, &cell->event, aligned_size)) { 469 err = -EFAULT; 470 break; 471 } 472 count -= aligned_size; 473 buf += aligned_size; 474 } 475 snd_seq_cell_free(cell); 476 cell = NULL; /* to be sure */ 477 result += aligned_size; 478 } 479 480 if (err < 0) { 481 if (cell) 482 snd_seq_fifo_cell_putback(fifo, cell); 483 } 484 485 if (result > 0) 486 return result; 487 return err < 0 ? err : 0; 488 } 489 490 491 /* 492 * check access permission to the port 493 */ 494 static int check_port_perm(struct snd_seq_client_port *port, unsigned int flags) 495 { 496 if ((port->capability & flags) != flags) 497 return 0; 498 return flags; 499 } 500 501 /* 502 * check if the destination client is available, and return the pointer 503 */ 504 static struct snd_seq_client *get_event_dest_client(struct snd_seq_event *event) 505 { 506 struct snd_seq_client *dest __free(snd_seq_client) = 507 snd_seq_client_use_ptr(event->dest.client); 508 509 if (dest == NULL) 510 return NULL; 511 if (! dest->accept_input) 512 return NULL; 513 if (snd_seq_ev_is_ump(event)) 514 return no_free_ptr(dest); /* ok - no filter checks */ 515 516 if ((dest->filter & SNDRV_SEQ_FILTER_USE_EVENT) && 517 ! test_bit(event->type, dest->event_filter)) 518 return NULL; 519 520 return no_free_ptr(dest); /* ok - accessible */ 521 } 522 523 524 /* 525 * Return the error event. 526 * 527 * If the receiver client is a user client, the original event is 528 * encapsulated in SNDRV_SEQ_EVENT_BOUNCE as variable length event. The 529 * external data of a variable length event is not copied along. 530 * If the receiver client is a kernel client, the original event is 531 * quoted in SNDRV_SEQ_EVENT_KERNEL_ERROR, since this requires no extra 532 * kmalloc. 533 */ 534 static int bounce_error_event(struct snd_seq_client *client, 535 struct snd_seq_event *event, 536 int err, int atomic, int hop) 537 { 538 struct snd_seq_event bounce_ev, quoted; 539 int result; 540 541 if (client == NULL || 542 ! (client->filter & SNDRV_SEQ_FILTER_BOUNCE) || 543 ! client->accept_input) 544 return 0; /* ignored */ 545 546 if (event->type == SNDRV_SEQ_EVENT_BOUNCE || 547 event->type == SNDRV_SEQ_EVENT_KERNEL_ERROR) 548 return err; /* avoid re-bouncing */ 549 550 /* set up quoted error */ 551 memset(&bounce_ev, 0, sizeof(bounce_ev)); 552 553 if (client->type == USER_CLIENT) { 554 /* 555 * For user clients, send SNDRV_SEQ_EVENT_BOUNCE with the 556 * original event embedded as variable-length data. This 557 * avoids exposing data.quote.event (a kernel pointer) to 558 * userspace. Sanitise the embedded copy too - a queued 559 * variable-length event carries the address of its own 560 * extension cell, and the payload goes out verbatim. 561 */ 562 quoted = *event; 563 if (snd_seq_ev_is_variable("ed)) { 564 quoted.data.ext.len &= ~SNDRV_SEQ_EXT_MASK; 565 quoted.data.ext.ptr = NULL; 566 } 567 bounce_ev.type = SNDRV_SEQ_EVENT_BOUNCE; 568 bounce_ev.flags = SNDRV_SEQ_EVENT_LENGTH_VARIABLE; 569 bounce_ev.data.ext.len = sizeof(struct snd_seq_event); 570 bounce_ev.data.ext.ptr = (char *)"ed; 571 } else { 572 /* 573 * For kernel clients, quote the event pointer directly. 574 * Kernel consumers can safely dereference the pointer. 575 */ 576 bounce_ev.type = SNDRV_SEQ_EVENT_KERNEL_ERROR; 577 bounce_ev.flags = SNDRV_SEQ_EVENT_LENGTH_FIXED; 578 bounce_ev.data.quote.origin = event->dest; 579 bounce_ev.data.quote.event = event; 580 bounce_ev.data.quote.value = -err; /* use positive value */ 581 } 582 583 bounce_ev.queue = SNDRV_SEQ_QUEUE_DIRECT; 584 bounce_ev.source.client = SNDRV_SEQ_CLIENT_SYSTEM; 585 bounce_ev.source.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE; 586 bounce_ev.dest.client = client->number; 587 bounce_ev.dest.port = event->source.port; 588 result = snd_seq_deliver_single_event(NULL, &bounce_ev, atomic, hop + 1); 589 if (result < 0) { 590 client->event_lost++; 591 return result; 592 } 593 594 return result; 595 } 596 597 598 /* 599 * rewrite the time-stamp of the event record with the curren time 600 * of the given queue. 601 * return non-zero if updated. 602 */ 603 static int update_timestamp_of_queue(struct snd_seq_event *event, 604 int queue, int real_time) 605 { 606 struct snd_seq_queue *q __free(snd_seq_queue) = 607 queueptr(queue); 608 609 if (! q) 610 return 0; 611 event->queue = queue; 612 event->flags &= ~SNDRV_SEQ_TIME_STAMP_MASK; 613 if (real_time) { 614 event->time.time = snd_seq_timer_get_cur_time(q->timer, true); 615 event->flags |= SNDRV_SEQ_TIME_STAMP_REAL; 616 } else { 617 event->time.tick = snd_seq_timer_get_cur_tick(q->timer); 618 event->flags |= SNDRV_SEQ_TIME_STAMP_TICK; 619 } 620 return 1; 621 } 622 623 /* deliver a single event; called from below and UMP converter */ 624 int __snd_seq_deliver_single_event(struct snd_seq_client *dest, 625 struct snd_seq_client_port *dest_port, 626 struct snd_seq_event *event, 627 int atomic, int hop) 628 { 629 switch (dest->type) { 630 case USER_CLIENT: 631 if (!dest->data.user.fifo) 632 return 0; 633 return snd_seq_fifo_event_in(dest->data.user.fifo, event); 634 case KERNEL_CLIENT: 635 if (!dest_port->event_input) 636 return 0; 637 return dest_port->event_input(event, 638 snd_seq_ev_is_direct(event), 639 dest_port->private_data, 640 atomic, hop); 641 } 642 return 0; 643 } 644 645 /* deliver a single event; called from snd_seq_deliver_single_event() */ 646 static int _snd_seq_deliver_single_event(struct snd_seq_client *client, 647 struct snd_seq_event *event, 648 int atomic, int hop) 649 { 650 struct snd_seq_client *dest __free(snd_seq_client) = 651 get_event_dest_client(event); 652 if (dest == NULL) 653 return -ENOENT; 654 655 struct snd_seq_client_port *dest_port __free(snd_seq_port) = 656 snd_seq_port_use_ptr(dest, event->dest.port); 657 if (dest_port == NULL) 658 return -ENOENT; 659 660 /* check permission */ 661 if (!check_port_perm(dest_port, SNDRV_SEQ_PORT_CAP_WRITE)) 662 return -EPERM; 663 664 if (dest_port->timestamping) 665 update_timestamp_of_queue(event, dest_port->time_queue, 666 dest_port->time_real); 667 668 #if IS_ENABLED(CONFIG_SND_SEQ_UMP) 669 if (snd_seq_ev_is_ump(event)) { 670 if (!(dest->filter & SNDRV_SEQ_FILTER_NO_CONVERT)) 671 return snd_seq_deliver_from_ump(client, dest, dest_port, 672 event, atomic, hop); 673 else if (dest->type == USER_CLIENT && 674 !snd_seq_client_is_ump(dest)) 675 return 0; // drop the event 676 } else if (snd_seq_client_is_ump(dest)) { 677 if (!(dest->filter & SNDRV_SEQ_FILTER_NO_CONVERT)) 678 return snd_seq_deliver_to_ump(client, dest, dest_port, 679 event, atomic, hop); 680 } 681 #endif /* CONFIG_SND_SEQ_UMP */ 682 683 return __snd_seq_deliver_single_event(dest, dest_port, event, 684 atomic, hop); 685 } 686 687 /* 688 * deliver an event to the specified destination. 689 * if filter is non-zero, client filter bitmap is tested. 690 * 691 * RETURN VALUE: 0 : if succeeded 692 * <0 : error 693 */ 694 static int snd_seq_deliver_single_event(struct snd_seq_client *client, 695 struct snd_seq_event *event, 696 int atomic, int hop) 697 { 698 int result = _snd_seq_deliver_single_event(client, event, atomic, hop); 699 700 if (result < 0 && !snd_seq_ev_is_direct(event)) 701 return bounce_error_event(client, event, result, atomic, hop); 702 return result; 703 } 704 705 706 /* 707 * send the event to all subscribers: 708 */ 709 static int __deliver_to_subscribers(struct snd_seq_client *client, 710 struct snd_seq_event *event, 711 int port, int atomic, int hop) 712 { 713 struct snd_seq_subscribers *subs; 714 int err, result = 0, num_ev = 0; 715 union __snd_seq_event event_saved; 716 size_t saved_size; 717 struct snd_seq_port_subs_info *grp; 718 719 if (port < 0) 720 return 0; 721 722 struct snd_seq_client_port *src_port __free(snd_seq_port) = 723 snd_seq_port_use_ptr(client, port); 724 if (!src_port) 725 return 0; 726 727 /* save original event record */ 728 saved_size = snd_seq_event_packet_size(event); 729 memcpy(&event_saved, event, saved_size); 730 grp = &src_port->c_src; 731 732 /* lock list */ 733 if (atomic) 734 rcu_read_lock(); 735 else 736 down_read_nested(&grp->list_mutex, hop); 737 hlist_for_each_entry_rcu(subs, &grp->list_head, src_list, 738 lockdep_is_held(&grp->list_mutex)) { 739 /* both ports ready? */ 740 if (atomic_read(&subs->ref_count) != 2) 741 continue; 742 event->dest = subs->info.dest; 743 if (subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP) 744 /* convert time according to flag with subscription */ 745 update_timestamp_of_queue(event, subs->info.queue, 746 subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL); 747 err = snd_seq_deliver_single_event(client, event, atomic, hop); 748 if (err < 0) { 749 /* save first error that occurs and continue */ 750 if (!result) 751 result = err; 752 continue; 753 } 754 num_ev++; 755 /* restore original event record */ 756 memcpy(event, &event_saved, saved_size); 757 } 758 if (atomic) 759 rcu_read_unlock(); 760 else 761 up_read(&grp->list_mutex); 762 memcpy(event, &event_saved, saved_size); 763 return (result < 0) ? result : num_ev; 764 } 765 766 static int deliver_to_subscribers(struct snd_seq_client *client, 767 struct snd_seq_event *event, 768 int atomic, int hop) 769 { 770 int ret; 771 #if IS_ENABLED(CONFIG_SND_SEQ_UMP) 772 int ret2; 773 #endif 774 775 ret = __deliver_to_subscribers(client, event, 776 event->source.port, atomic, hop); 777 #if IS_ENABLED(CONFIG_SND_SEQ_UMP) 778 if (!snd_seq_client_is_ump(client) || client->ump_endpoint_port < 0) 779 return ret; 780 /* If it's an event from EP port (and with a UMP group), 781 * deliver to subscribers of the corresponding UMP group port, too. 782 * Or, if it's from non-EP port, deliver to subscribers of EP port, too. 783 */ 784 if (event->source.port == client->ump_endpoint_port) 785 ret2 = __deliver_to_subscribers(client, event, 786 snd_seq_ump_group_port(event), 787 atomic, hop); 788 else 789 ret2 = __deliver_to_subscribers(client, event, 790 client->ump_endpoint_port, 791 atomic, hop); 792 if (ret2 < 0) 793 return ret2; 794 #endif 795 return ret; 796 } 797 798 /* deliver an event to the destination port(s). 799 * if the event is to subscribers or broadcast, the event is dispatched 800 * to multiple targets. 801 * 802 * RETURN VALUE: n > 0 : the number of delivered events. 803 * n == 0 : the event was not passed to any client. 804 * n < 0 : error - event was not processed. 805 */ 806 static int snd_seq_deliver_event(struct snd_seq_client *client, struct snd_seq_event *event, 807 int atomic, int hop) 808 { 809 int result; 810 811 hop++; 812 if (hop >= SNDRV_SEQ_MAX_HOPS) { 813 pr_debug("ALSA: seq: too long delivery path (%d:%d->%d:%d)\n", 814 event->source.client, event->source.port, 815 event->dest.client, event->dest.port); 816 return -EMLINK; 817 } 818 819 if (snd_seq_ev_is_variable(event) && 820 snd_BUG_ON(atomic && (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR))) 821 return -EINVAL; 822 823 if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS || 824 event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) 825 result = deliver_to_subscribers(client, event, atomic, hop); 826 else 827 result = snd_seq_deliver_single_event(client, event, atomic, hop); 828 829 return result; 830 } 831 832 /* 833 * dispatch an event cell: 834 * This function is called only from queue check routines in timer 835 * interrupts or after enqueued. 836 * The event cell shall be released or re-queued in this function. 837 * 838 * RETURN VALUE: n > 0 : the number of delivered events. 839 * n == 0 : the event was not passed to any client. 840 * n < 0 : error - event was not processed. 841 */ 842 int snd_seq_dispatch_event(struct snd_seq_event_cell *cell, int atomic, int hop) 843 { 844 int result; 845 846 if (snd_BUG_ON(!cell)) 847 return -EINVAL; 848 849 struct snd_seq_client *client __free(snd_seq_client) = 850 snd_seq_client_use_ptr(cell->event.source.client); 851 if (client == NULL) { 852 snd_seq_cell_free(cell); /* release this cell */ 853 return -EINVAL; 854 } 855 856 if (!snd_seq_ev_is_ump(&cell->event) && 857 cell->event.type == SNDRV_SEQ_EVENT_NOTE) { 858 /* NOTE event: 859 * the event cell is re-used as a NOTE-OFF event and 860 * enqueued again. 861 */ 862 struct snd_seq_event tmpev, *ev; 863 864 /* reserve this event to enqueue note-off later */ 865 tmpev = cell->event; 866 tmpev.type = SNDRV_SEQ_EVENT_NOTEON; 867 result = snd_seq_deliver_event(client, &tmpev, atomic, hop); 868 869 /* 870 * This was originally a note event. We now re-use the 871 * cell for the note-off event. 872 */ 873 874 ev = &cell->event; 875 ev->type = SNDRV_SEQ_EVENT_NOTEOFF; 876 ev->flags |= SNDRV_SEQ_PRIORITY_HIGH; 877 878 /* add the duration time */ 879 switch (ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) { 880 case SNDRV_SEQ_TIME_STAMP_TICK: 881 cell->event.time.tick += ev->data.note.duration; 882 break; 883 case SNDRV_SEQ_TIME_STAMP_REAL: 884 /* unit for duration is ms */ 885 ev->time.time.tv_nsec += 1000000 * (ev->data.note.duration % 1000); 886 ev->time.time.tv_sec += ev->data.note.duration / 1000 + 887 ev->time.time.tv_nsec / 1000000000; 888 ev->time.time.tv_nsec %= 1000000000; 889 break; 890 } 891 ev->data.note.velocity = ev->data.note.off_velocity; 892 893 /* Now queue this cell as the note off event */ 894 if (snd_seq_enqueue_event(cell, atomic, hop) < 0) 895 snd_seq_cell_free(cell); /* release this cell */ 896 897 } else { 898 /* Normal events: 899 * event cell is freed after processing the event 900 */ 901 902 result = snd_seq_deliver_event(client, &cell->event, atomic, hop); 903 snd_seq_cell_free(cell); 904 } 905 906 return result; 907 } 908 909 910 /* Allocate a cell from client pool and enqueue it to queue: 911 * if pool is empty and blocking is TRUE, sleep until a new cell is 912 * available. 913 */ 914 static int snd_seq_client_enqueue_event(struct snd_seq_client *client, 915 struct snd_seq_event *event, 916 struct file *file, int blocking, 917 int atomic, int hop, 918 struct mutex *mutexp) 919 { 920 struct snd_seq_event_cell *cell; 921 int err; 922 923 /* special queue values - force direct passing */ 924 if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) { 925 event->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS; 926 event->queue = SNDRV_SEQ_QUEUE_DIRECT; 927 } else if (event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) { 928 /* check presence of source port */ 929 struct snd_seq_client_port *src_port __free(snd_seq_port) = 930 snd_seq_port_use_ptr(client, event->source.port); 931 if (!src_port) 932 return -EINVAL; 933 } 934 935 /* direct event processing without enqueued */ 936 if (snd_seq_ev_is_direct(event)) { 937 if (!snd_seq_ev_is_ump(event) && 938 event->type == SNDRV_SEQ_EVENT_NOTE) 939 return -EINVAL; /* this event must be enqueued! */ 940 return snd_seq_deliver_event(client, event, atomic, hop); 941 } 942 943 /* Not direct, normal queuing */ 944 if (snd_seq_queue_is_used(event->queue, client->number) <= 0) 945 return -EINVAL; /* invalid queue */ 946 if (! snd_seq_write_pool_allocated(client)) 947 return -ENXIO; /* queue is not allocated */ 948 949 /* allocate an event cell */ 950 err = snd_seq_event_dup(client->pool, event, &cell, !blocking || atomic, 951 file, mutexp); 952 if (err < 0) 953 return err; 954 955 /* we got a cell. enqueue it. */ 956 err = snd_seq_enqueue_event(cell, atomic, hop); 957 if (err < 0) { 958 snd_seq_cell_free(cell); 959 return err; 960 } 961 962 return 0; 963 } 964 965 966 /* 967 * check validity of event type and data length. 968 * return non-zero if invalid. 969 */ 970 static int check_event_type_and_length(struct snd_seq_event *ev) 971 { 972 switch (snd_seq_ev_length_type(ev)) { 973 case SNDRV_SEQ_EVENT_LENGTH_FIXED: 974 if (snd_seq_ev_is_variable_type(ev)) 975 return -EINVAL; 976 break; 977 case SNDRV_SEQ_EVENT_LENGTH_VARIABLE: 978 if (! snd_seq_ev_is_variable_type(ev) || 979 (ev->data.ext.len & ~SNDRV_SEQ_EXT_MASK) >= SNDRV_SEQ_MAX_EVENT_LEN) 980 return -EINVAL; 981 break; 982 case SNDRV_SEQ_EVENT_LENGTH_VARUSR: 983 if (! snd_seq_ev_is_direct(ev)) 984 return -EINVAL; 985 break; 986 } 987 return 0; 988 } 989 990 991 /* handle write() */ 992 /* possible error values: 993 * -ENXIO invalid client or file open mode 994 * -ENOMEM malloc failed 995 * -EFAULT seg. fault during copy from user space 996 * -EINVAL invalid event 997 * -EAGAIN no space in output pool 998 * -EINTR interrupts while sleep 999 * -EMLINK too many hops 1000 * others depends on return value from driver callback 1001 */ 1002 static ssize_t snd_seq_write(struct file *file, const char __user *buf, 1003 size_t count, loff_t *offset) 1004 { 1005 struct snd_seq_client *client = file->private_data; 1006 int written = 0, len; 1007 int err, handled; 1008 union __snd_seq_event __event; 1009 struct snd_seq_event *ev = &__event.legacy; 1010 1011 if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT)) 1012 return -ENXIO; 1013 1014 /* check client structures are in place */ 1015 if (snd_BUG_ON(!client)) 1016 return -ENXIO; 1017 1018 if (!client->accept_output || client->pool == NULL) 1019 return -ENXIO; 1020 1021 repeat: 1022 handled = 0; 1023 /* allocate the pool now if the pool is not allocated yet */ 1024 mutex_lock(&client->ioctl_mutex); 1025 if (client->pool->size > 0 && !snd_seq_write_pool_allocated(client)) { 1026 err = snd_seq_pool_init(client->pool); 1027 if (err < 0) 1028 goto out; 1029 } 1030 1031 /* only process whole events */ 1032 err = -EINVAL; 1033 while (count >= sizeof(struct snd_seq_event)) { 1034 /* Read in the event header from the user */ 1035 len = sizeof(struct snd_seq_event); 1036 if (copy_from_user(ev, buf, len)) { 1037 err = -EFAULT; 1038 break; 1039 } 1040 /* read in the rest bytes for UMP events */ 1041 if (snd_seq_ev_is_ump(ev)) { 1042 if (count < sizeof(struct snd_seq_ump_event)) 1043 break; 1044 if (copy_from_user((char *)ev + len, buf + len, 1045 sizeof(struct snd_seq_ump_event) - len)) { 1046 err = -EFAULT; 1047 break; 1048 } 1049 len = sizeof(struct snd_seq_ump_event); 1050 } 1051 1052 ev->source.client = client->number; /* fill in client number */ 1053 /* Check for extension data length */ 1054 if (check_event_type_and_length(ev)) { 1055 err = -EINVAL; 1056 break; 1057 } 1058 1059 if (!event_is_compatible(client, ev)) { 1060 err = -EINVAL; 1061 break; 1062 } 1063 1064 /* check for special events */ 1065 if (!snd_seq_ev_is_ump(ev)) { 1066 if (ev->type == SNDRV_SEQ_EVENT_NONE) 1067 goto __skip_event; 1068 else if (snd_seq_ev_is_reserved(ev)) { 1069 err = -EINVAL; 1070 break; 1071 } 1072 } 1073 1074 if (snd_seq_ev_is_variable(ev)) { 1075 int extlen = ev->data.ext.len & ~SNDRV_SEQ_EXT_MASK; 1076 if ((size_t)(extlen + len) > count) { 1077 /* back out, will get an error this time or next */ 1078 err = -EINVAL; 1079 break; 1080 } 1081 /* set user space pointer */ 1082 ev->data.ext.len = extlen | SNDRV_SEQ_EXT_USRPTR; 1083 ev->data.ext.ptr = (char __force *)buf + len; 1084 len += extlen; /* increment data length */ 1085 } else { 1086 #ifdef CONFIG_COMPAT 1087 if (client->convert32 && snd_seq_ev_is_varusr(ev)) 1088 ev->data.ext.ptr = 1089 (void __force *)compat_ptr(ev->data.raw32.d[1]); 1090 #endif 1091 } 1092 1093 /* ok, enqueue it */ 1094 err = snd_seq_client_enqueue_event(client, ev, file, 1095 !(file->f_flags & O_NONBLOCK), 1096 0, 0, &client->ioctl_mutex); 1097 if (err < 0) 1098 break; 1099 handled++; 1100 1101 __skip_event: 1102 /* Update pointers and counts */ 1103 count -= len; 1104 buf += len; 1105 written += len; 1106 1107 /* let's have a coffee break if too many events are queued */ 1108 if (++handled >= 200) { 1109 mutex_unlock(&client->ioctl_mutex); 1110 goto repeat; 1111 } 1112 } 1113 1114 out: 1115 mutex_unlock(&client->ioctl_mutex); 1116 return written ? written : err; 1117 } 1118 1119 1120 /* 1121 * handle polling 1122 */ 1123 static __poll_t snd_seq_poll(struct file *file, poll_table * wait) 1124 { 1125 struct snd_seq_client *client = file->private_data; 1126 __poll_t mask = 0; 1127 1128 /* check client structures are in place */ 1129 if (snd_BUG_ON(!client)) 1130 return EPOLLERR; 1131 1132 if ((snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT) && 1133 client->data.user.fifo) { 1134 1135 /* check if data is available in the outqueue */ 1136 if (snd_seq_fifo_poll_wait(client->data.user.fifo, file, wait)) 1137 mask |= EPOLLIN | EPOLLRDNORM; 1138 } 1139 1140 if (snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT) { 1141 1142 /* check if data is available in the pool */ 1143 if (snd_seq_pool_poll_wait(client->pool, file, wait)) 1144 mask |= EPOLLOUT | EPOLLWRNORM; 1145 } 1146 1147 return mask; 1148 } 1149 1150 1151 /*-----------------------------------------------------*/ 1152 1153 static int snd_seq_ioctl_pversion(struct snd_seq_client *client, void *arg) 1154 { 1155 int *pversion = arg; 1156 1157 *pversion = SNDRV_SEQ_VERSION; 1158 return 0; 1159 } 1160 1161 static int snd_seq_ioctl_user_pversion(struct snd_seq_client *client, void *arg) 1162 { 1163 client->user_pversion = *(unsigned int *)arg; 1164 return 0; 1165 } 1166 1167 static int snd_seq_ioctl_client_id(struct snd_seq_client *client, void *arg) 1168 { 1169 int *client_id = arg; 1170 1171 *client_id = client->number; 1172 return 0; 1173 } 1174 1175 /* SYSTEM_INFO ioctl() */ 1176 static int snd_seq_ioctl_system_info(struct snd_seq_client *client, void *arg) 1177 { 1178 struct snd_seq_system_info *info = arg; 1179 1180 memset(info, 0, sizeof(*info)); 1181 /* fill the info fields */ 1182 info->queues = SNDRV_SEQ_MAX_QUEUES; 1183 info->clients = SNDRV_SEQ_MAX_CLIENTS; 1184 info->ports = SNDRV_SEQ_MAX_PORTS; 1185 info->channels = 256; /* fixed limit */ 1186 info->cur_clients = client_usage.cur; 1187 info->cur_queues = snd_seq_queue_get_cur_queues(); 1188 1189 return 0; 1190 } 1191 1192 1193 /* RUNNING_MODE ioctl() */ 1194 static int snd_seq_ioctl_running_mode(struct snd_seq_client *client, void *arg) 1195 { 1196 struct snd_seq_running_info *info = arg; 1197 /* requested client number */ 1198 struct snd_seq_client *cptr __free(snd_seq_client) = 1199 client_load_and_use_ptr(info->client); 1200 1201 if (cptr == NULL) 1202 return -ENOENT; /* don't change !!! */ 1203 1204 #ifdef SNDRV_BIG_ENDIAN 1205 if (!info->big_endian) 1206 return -EINVAL; 1207 #else 1208 if (info->big_endian) 1209 return -EINVAL; 1210 #endif 1211 if (info->cpu_mode > sizeof(long)) 1212 return -EINVAL; 1213 cptr->convert32 = (info->cpu_mode < sizeof(long)); 1214 return 0; 1215 } 1216 1217 /* CLIENT_INFO ioctl() */ 1218 static void get_client_info(struct snd_seq_client *cptr, 1219 struct snd_seq_client_info *info) 1220 { 1221 info->client = cptr->number; 1222 1223 /* fill the info fields */ 1224 info->type = cptr->type; 1225 strscpy(info->name, cptr->name); 1226 info->filter = cptr->filter; 1227 info->event_lost = cptr->event_lost; 1228 memcpy(info->event_filter, cptr->event_filter, 32); 1229 info->group_filter = cptr->group_filter; 1230 info->num_ports = cptr->num_ports; 1231 1232 if (cptr->type == USER_CLIENT) 1233 info->pid = pid_vnr(cptr->data.user.owner); 1234 else 1235 info->pid = -1; 1236 1237 if (cptr->type == KERNEL_CLIENT) 1238 info->card = cptr->data.kernel.card ? cptr->data.kernel.card->number : -1; 1239 else 1240 info->card = -1; 1241 1242 info->midi_version = cptr->midi_version; 1243 memset(info->reserved, 0, sizeof(info->reserved)); 1244 } 1245 1246 static int snd_seq_ioctl_get_client_info(struct snd_seq_client *client, 1247 void *arg) 1248 { 1249 struct snd_seq_client_info *client_info = arg; 1250 /* requested client number */ 1251 struct snd_seq_client *cptr __free(snd_seq_client) = 1252 client_load_and_use_ptr(client_info->client); 1253 1254 if (cptr == NULL) 1255 return -ENOENT; /* don't change !!! */ 1256 1257 get_client_info(cptr, client_info); 1258 return 0; 1259 } 1260 1261 1262 /* CLIENT_INFO ioctl() */ 1263 static int snd_seq_ioctl_set_client_info(struct snd_seq_client *client, 1264 void *arg) 1265 { 1266 struct snd_seq_client_info *client_info = arg; 1267 1268 /* it is not allowed to set the info fields for an another client */ 1269 if (client->number != client_info->client) 1270 return -EPERM; 1271 /* also client type must be set now */ 1272 if (client->type != client_info->type) 1273 return -EINVAL; 1274 1275 if (client->user_pversion >= SNDRV_PROTOCOL_VERSION(1, 0, 3)) { 1276 /* check validity of midi_version field */ 1277 if (client_info->midi_version > SNDRV_SEQ_CLIENT_UMP_MIDI_2_0) 1278 return -EINVAL; 1279 1280 /* check if UMP is supported in kernel */ 1281 if (!IS_ENABLED(CONFIG_SND_SEQ_UMP) && 1282 client_info->midi_version > 0) 1283 return -EINVAL; 1284 } 1285 1286 /* fill the info fields */ 1287 if (client_info->name[0]) 1288 strscpy(client->name, client_info->name, sizeof(client->name)); 1289 1290 client->filter = client_info->filter; 1291 client->event_lost = client_info->event_lost; 1292 if (client->user_pversion >= SNDRV_PROTOCOL_VERSION(1, 0, 3)) 1293 client->midi_version = client_info->midi_version; 1294 memcpy(client->event_filter, client_info->event_filter, 32); 1295 client->group_filter = client_info->group_filter & SND_SEQ_GROUP_FILTER_MASK; 1296 1297 /* notify the change */ 1298 snd_seq_system_client_ev_client_change(client->number); 1299 1300 return 0; 1301 } 1302 1303 1304 /* 1305 * CREATE PORT ioctl() 1306 */ 1307 static int snd_seq_ioctl_create_port(struct snd_seq_client *client, void *arg) 1308 { 1309 struct snd_seq_port_info *info = arg; 1310 struct snd_seq_client_port *port; 1311 struct snd_seq_port_callback *callback; 1312 int port_idx, err; 1313 1314 /* it is not allowed to create the port for an another client */ 1315 if (info->addr.client != client->number) 1316 return -EPERM; 1317 if (client->type == USER_CLIENT && info->kernel) 1318 return -EINVAL; 1319 if ((info->capability & SNDRV_SEQ_PORT_CAP_UMP_ENDPOINT) && 1320 client->ump_endpoint_port >= 0) 1321 return -EBUSY; 1322 1323 if (info->flags & SNDRV_SEQ_PORT_FLG_GIVEN_PORT) 1324 port_idx = info->addr.port; 1325 else 1326 port_idx = -1; 1327 if (port_idx >= SNDRV_SEQ_ADDRESS_UNKNOWN) 1328 return -EINVAL; 1329 err = snd_seq_create_port(client, &port); 1330 if (err < 0) 1331 return err; 1332 1333 if (client->type == KERNEL_CLIENT) { 1334 callback = info->kernel; 1335 if (callback) { 1336 if (callback->owner) 1337 port->owner = callback->owner; 1338 port->private_data = callback->private_data; 1339 port->private_free = callback->private_free; 1340 port->event_input = callback->event_input; 1341 port->c_src.open = callback->subscribe; 1342 port->c_src.close = callback->unsubscribe; 1343 port->c_dest.open = callback->use; 1344 port->c_dest.close = callback->unuse; 1345 } 1346 } 1347 1348 snd_seq_set_port_info(port, info); 1349 err = snd_seq_insert_port(client, port_idx, port); 1350 if (err < 0) { 1351 kfree(port); 1352 return err; 1353 } 1354 info->addr = port->addr; 1355 if (info->capability & SNDRV_SEQ_PORT_CAP_UMP_ENDPOINT) 1356 client->ump_endpoint_port = port->addr.port; 1357 snd_seq_system_client_ev_port_start(port->addr.client, port->addr.port); 1358 snd_seq_port_unlock(port); 1359 1360 return 0; 1361 } 1362 1363 /* 1364 * DELETE PORT ioctl() 1365 */ 1366 static int snd_seq_ioctl_delete_port(struct snd_seq_client *client, void *arg) 1367 { 1368 struct snd_seq_port_info *info = arg; 1369 int err; 1370 1371 /* it is not allowed to remove the port for an another client */ 1372 if (info->addr.client != client->number) 1373 return -EPERM; 1374 1375 err = snd_seq_delete_port(client, info->addr.port); 1376 if (err >= 0) { 1377 if (client->ump_endpoint_port == info->addr.port) 1378 client->ump_endpoint_port = -1; 1379 snd_seq_system_client_ev_port_exit(client->number, info->addr.port); 1380 } 1381 return err; 1382 } 1383 1384 1385 /* 1386 * GET_PORT_INFO ioctl() (on any client) 1387 */ 1388 static int snd_seq_ioctl_get_port_info(struct snd_seq_client *client, void *arg) 1389 { 1390 struct snd_seq_port_info *info = arg; 1391 1392 struct snd_seq_client *cptr __free(snd_seq_client) = 1393 client_load_and_use_ptr(info->addr.client); 1394 if (cptr == NULL) 1395 return -ENXIO; 1396 1397 struct snd_seq_client_port *port __free(snd_seq_port) = 1398 snd_seq_port_use_ptr(cptr, info->addr.port); 1399 if (port == NULL) 1400 return -ENOENT; /* don't change */ 1401 1402 /* get port info */ 1403 snd_seq_get_port_info(port, info); 1404 return 0; 1405 } 1406 1407 1408 /* 1409 * SET_PORT_INFO ioctl() (only ports on this/own client) 1410 */ 1411 static int snd_seq_ioctl_set_port_info(struct snd_seq_client *client, void *arg) 1412 { 1413 struct snd_seq_port_info *info = arg; 1414 1415 if (info->addr.client != client->number) /* only set our own ports ! */ 1416 return -EPERM; 1417 1418 struct snd_seq_client_port *port __free(snd_seq_port) = 1419 snd_seq_port_use_ptr(client, info->addr.port); 1420 if (port) { 1421 snd_seq_set_port_info(port, info); 1422 /* notify the change */ 1423 snd_seq_system_client_ev_port_change(info->addr.client, 1424 info->addr.port); 1425 } 1426 return 0; 1427 } 1428 1429 1430 /* 1431 * port subscription (connection) 1432 */ 1433 #define PERM_RD (SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ) 1434 #define PERM_WR (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_SUBS_WRITE) 1435 1436 static int check_subscription_permission(struct snd_seq_client *client, 1437 struct snd_seq_client_port *sport, 1438 struct snd_seq_client_port *dport, 1439 struct snd_seq_port_subscribe *subs) 1440 { 1441 if (client->number != subs->sender.client && 1442 client->number != subs->dest.client) { 1443 /* connection by third client - check export permission */ 1444 if (check_port_perm(sport, SNDRV_SEQ_PORT_CAP_NO_EXPORT)) 1445 return -EPERM; 1446 if (check_port_perm(dport, SNDRV_SEQ_PORT_CAP_NO_EXPORT)) 1447 return -EPERM; 1448 } 1449 1450 /* check read permission */ 1451 /* if sender or receiver is the subscribing client itself, 1452 * no permission check is necessary 1453 */ 1454 if (client->number != subs->sender.client) { 1455 if (! check_port_perm(sport, PERM_RD)) 1456 return -EPERM; 1457 } 1458 /* check write permission */ 1459 if (client->number != subs->dest.client) { 1460 if (! check_port_perm(dport, PERM_WR)) 1461 return -EPERM; 1462 } 1463 return 0; 1464 } 1465 1466 /* 1467 * send an subscription notify event to user client: 1468 * client must be user client. 1469 */ 1470 int snd_seq_client_notify_subscription(int client, int port, 1471 struct snd_seq_port_subscribe *info, 1472 int evtype) 1473 { 1474 struct snd_seq_event event; 1475 1476 memset(&event, 0, sizeof(event)); 1477 event.type = evtype; 1478 event.data.connect.dest = info->dest; 1479 event.data.connect.sender = info->sender; 1480 1481 return snd_seq_system_notify(client, port, &event, false); /* non-atomic */ 1482 } 1483 1484 1485 /* 1486 * add to port's subscription list IOCTL interface 1487 */ 1488 static int snd_seq_ioctl_subscribe_port(struct snd_seq_client *client, 1489 void *arg) 1490 { 1491 struct snd_seq_port_subscribe *subs = arg; 1492 int result; 1493 1494 struct snd_seq_client *receiver __free(snd_seq_client) = 1495 client_load_and_use_ptr(subs->dest.client); 1496 if (!receiver) 1497 return -EINVAL; 1498 struct snd_seq_client *sender __free(snd_seq_client) = 1499 client_load_and_use_ptr(subs->sender.client); 1500 if (!sender) 1501 return -EINVAL; 1502 struct snd_seq_client_port *sport __free(snd_seq_port) = 1503 snd_seq_port_use_ptr(sender, subs->sender.port); 1504 if (!sport) 1505 return -EINVAL; 1506 struct snd_seq_client_port *dport __free(snd_seq_port) = 1507 snd_seq_port_use_ptr(receiver, subs->dest.port); 1508 if (!dport) 1509 return -EINVAL; 1510 1511 result = check_subscription_permission(client, sport, dport, subs); 1512 if (result < 0) 1513 return result; 1514 1515 /* connect them */ 1516 result = snd_seq_port_connect(client, sender, sport, receiver, dport, subs); 1517 if (! result) /* broadcast announce */ 1518 snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0, 1519 subs, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED); 1520 return result; 1521 } 1522 1523 1524 /* 1525 * remove from port's subscription list 1526 */ 1527 static int snd_seq_ioctl_unsubscribe_port(struct snd_seq_client *client, 1528 void *arg) 1529 { 1530 struct snd_seq_port_subscribe *subs = arg; 1531 int result; 1532 1533 struct snd_seq_client *receiver __free(snd_seq_client) = 1534 snd_seq_client_use_ptr(subs->dest.client); 1535 if (!receiver) 1536 return -ENXIO; 1537 struct snd_seq_client *sender __free(snd_seq_client) = 1538 snd_seq_client_use_ptr(subs->sender.client); 1539 if (!sender) 1540 return -ENXIO; 1541 struct snd_seq_client_port *sport __free(snd_seq_port) = 1542 snd_seq_port_use_ptr(sender, subs->sender.port); 1543 if (!sport) 1544 return -ENXIO; 1545 struct snd_seq_client_port *dport __free(snd_seq_port) = 1546 snd_seq_port_use_ptr(receiver, subs->dest.port); 1547 if (!dport) 1548 return -ENXIO; 1549 1550 result = check_subscription_permission(client, sport, dport, subs); 1551 if (result < 0) 1552 return result; 1553 1554 result = snd_seq_port_disconnect(client, sender, sport, receiver, dport, subs); 1555 if (! result) /* broadcast announce */ 1556 snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0, 1557 subs, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED); 1558 return result; 1559 } 1560 1561 1562 /* CREATE_QUEUE ioctl() */ 1563 static int snd_seq_ioctl_create_queue(struct snd_seq_client *client, void *arg) 1564 { 1565 struct snd_seq_queue_info *info = arg; 1566 struct snd_seq_queue *q __free(snd_seq_queue) = 1567 snd_seq_queue_alloc(client->number, info->locked, info->flags); 1568 1569 if (IS_ERR(q)) 1570 return PTR_ERR(q); 1571 1572 info->queue = q->queue; 1573 info->locked = q->locked; 1574 info->owner = q->owner; 1575 1576 /* set queue name */ 1577 if (!info->name[0]) 1578 snprintf(info->name, sizeof(info->name), "Queue-%d", q->queue); 1579 strscpy(q->name, info->name, sizeof(q->name)); 1580 1581 return 0; 1582 } 1583 1584 /* DELETE_QUEUE ioctl() */ 1585 static int snd_seq_ioctl_delete_queue(struct snd_seq_client *client, void *arg) 1586 { 1587 struct snd_seq_queue_info *info = arg; 1588 1589 return snd_seq_queue_delete(client->number, info->queue); 1590 } 1591 1592 /* GET_QUEUE_INFO ioctl() */ 1593 static int snd_seq_ioctl_get_queue_info(struct snd_seq_client *client, 1594 void *arg) 1595 { 1596 struct snd_seq_queue_info *info = arg; 1597 struct snd_seq_queue *q __free(snd_seq_queue) = 1598 queueptr(info->queue); 1599 1600 if (q == NULL) 1601 return -EINVAL; 1602 1603 memset(info, 0, sizeof(*info)); 1604 info->queue = q->queue; 1605 info->owner = q->owner; 1606 info->locked = q->locked; 1607 strscpy(info->name, q->name, sizeof(info->name)); 1608 1609 return 0; 1610 } 1611 1612 /* SET_QUEUE_INFO ioctl() */ 1613 static int snd_seq_ioctl_set_queue_info(struct snd_seq_client *client, 1614 void *arg) 1615 { 1616 struct snd_seq_queue_info *info = arg; 1617 1618 if (info->owner != client->number) 1619 return -EINVAL; 1620 1621 /* change owner/locked permission */ 1622 if (snd_seq_queue_check_access(info->queue, client->number)) { 1623 if (snd_seq_queue_set_owner(info->queue, client->number, info->locked) < 0) 1624 return -EPERM; 1625 if (info->locked) 1626 snd_seq_queue_use(info->queue, client->number, 1); 1627 } else { 1628 return -EPERM; 1629 } 1630 1631 struct snd_seq_queue *q __free(snd_seq_queue) = 1632 queueptr(info->queue); 1633 if (! q) 1634 return -EINVAL; 1635 if (q->owner != client->number) 1636 return -EPERM; 1637 strscpy(q->name, info->name, sizeof(q->name)); 1638 1639 return 0; 1640 } 1641 1642 /* GET_NAMED_QUEUE ioctl() */ 1643 static int snd_seq_ioctl_get_named_queue(struct snd_seq_client *client, 1644 void *arg) 1645 { 1646 struct snd_seq_queue_info *info = arg; 1647 struct snd_seq_queue *q __free(snd_seq_queue) = 1648 snd_seq_queue_find_name(info->name); 1649 1650 if (q == NULL) 1651 return -EINVAL; 1652 info->queue = q->queue; 1653 info->owner = q->owner; 1654 info->locked = q->locked; 1655 1656 return 0; 1657 } 1658 1659 /* GET_QUEUE_STATUS ioctl() */ 1660 static int snd_seq_ioctl_get_queue_status(struct snd_seq_client *client, 1661 void *arg) 1662 { 1663 struct snd_seq_queue_status *status = arg; 1664 struct snd_seq_timer *tmr; 1665 struct snd_seq_queue *queue __free(snd_seq_queue) = 1666 queueptr(status->queue); 1667 1668 if (queue == NULL) 1669 return -EINVAL; 1670 memset(status, 0, sizeof(*status)); 1671 status->queue = queue->queue; 1672 1673 tmr = queue->timer; 1674 status->events = queue->tickq->cells + queue->timeq->cells; 1675 1676 status->time = snd_seq_timer_get_cur_time(tmr, true); 1677 status->tick = snd_seq_timer_get_cur_tick(tmr); 1678 1679 status->running = tmr->running; 1680 1681 status->flags = queue->flags; 1682 1683 return 0; 1684 } 1685 1686 1687 /* GET_QUEUE_TEMPO ioctl() */ 1688 static int snd_seq_ioctl_get_queue_tempo(struct snd_seq_client *client, 1689 void *arg) 1690 { 1691 struct snd_seq_queue_tempo *tempo = arg; 1692 struct snd_seq_timer *tmr; 1693 struct snd_seq_queue *queue __free(snd_seq_queue) = 1694 queueptr(tempo->queue); 1695 1696 if (queue == NULL) 1697 return -EINVAL; 1698 memset(tempo, 0, sizeof(*tempo)); 1699 tempo->queue = queue->queue; 1700 1701 tmr = queue->timer; 1702 1703 tempo->tempo = tmr->tempo; 1704 tempo->ppq = tmr->ppq; 1705 tempo->skew_value = tmr->skew; 1706 tempo->skew_base = tmr->skew_base; 1707 if (client->user_pversion >= SNDRV_PROTOCOL_VERSION(1, 0, 4)) 1708 tempo->tempo_base = tmr->tempo_base; 1709 1710 return 0; 1711 } 1712 1713 1714 /* SET_QUEUE_TEMPO ioctl() */ 1715 int snd_seq_set_queue_tempo(int client, struct snd_seq_queue_tempo *tempo) 1716 { 1717 if (!snd_seq_queue_check_access(tempo->queue, client)) 1718 return -EPERM; 1719 return snd_seq_queue_timer_set_tempo(tempo->queue, client, tempo); 1720 } 1721 EXPORT_SYMBOL(snd_seq_set_queue_tempo); 1722 1723 static int snd_seq_ioctl_set_queue_tempo(struct snd_seq_client *client, 1724 void *arg) 1725 { 1726 struct snd_seq_queue_tempo *tempo = arg; 1727 int result; 1728 1729 if (client->user_pversion < SNDRV_PROTOCOL_VERSION(1, 0, 4)) 1730 tempo->tempo_base = 0; 1731 result = snd_seq_set_queue_tempo(client->number, tempo); 1732 return result < 0 ? result : 0; 1733 } 1734 1735 1736 /* GET_QUEUE_TIMER ioctl() */ 1737 static int snd_seq_ioctl_get_queue_timer(struct snd_seq_client *client, 1738 void *arg) 1739 { 1740 struct snd_seq_queue_timer *timer = arg; 1741 struct snd_seq_timer *tmr; 1742 struct snd_seq_queue *queue __free(snd_seq_queue) = 1743 queueptr(timer->queue); 1744 1745 if (queue == NULL) 1746 return -EINVAL; 1747 1748 guard(mutex)(&queue->timer_mutex); 1749 tmr = queue->timer; 1750 memset(timer, 0, sizeof(*timer)); 1751 timer->queue = queue->queue; 1752 1753 timer->type = tmr->type; 1754 if (tmr->type == SNDRV_SEQ_TIMER_ALSA) { 1755 timer->u.alsa.id = tmr->alsa_id; 1756 timer->u.alsa.resolution = tmr->preferred_resolution; 1757 } 1758 1759 return 0; 1760 } 1761 1762 1763 /* SET_QUEUE_TIMER ioctl() */ 1764 static int snd_seq_ioctl_set_queue_timer(struct snd_seq_client *client, 1765 void *arg) 1766 { 1767 struct snd_seq_queue_timer *timer = arg; 1768 int result = 0; 1769 1770 if (timer->type != SNDRV_SEQ_TIMER_ALSA) 1771 return -EINVAL; 1772 1773 if (snd_seq_queue_check_access(timer->queue, client->number)) { 1774 struct snd_seq_timer *tmr; 1775 struct snd_seq_queue *q __free(snd_seq_queue) = 1776 queueptr(timer->queue); 1777 1778 if (q == NULL) 1779 return -ENXIO; 1780 guard(mutex)(&q->timer_mutex); 1781 tmr = q->timer; 1782 snd_seq_queue_timer_close(timer->queue); 1783 tmr->type = timer->type; 1784 if (tmr->type == SNDRV_SEQ_TIMER_ALSA) { 1785 tmr->alsa_id = timer->u.alsa.id; 1786 tmr->preferred_resolution = timer->u.alsa.resolution; 1787 } 1788 result = snd_seq_queue_timer_open(timer->queue); 1789 } else { 1790 return -EPERM; 1791 } 1792 1793 return result; 1794 } 1795 1796 1797 /* GET_QUEUE_CLIENT ioctl() */ 1798 static int snd_seq_ioctl_get_queue_client(struct snd_seq_client *client, 1799 void *arg) 1800 { 1801 struct snd_seq_queue_client *info = arg; 1802 int used; 1803 1804 used = snd_seq_queue_is_used(info->queue, client->number); 1805 if (used < 0) 1806 return -EINVAL; 1807 info->used = used; 1808 info->client = client->number; 1809 1810 return 0; 1811 } 1812 1813 1814 /* SET_QUEUE_CLIENT ioctl() */ 1815 static int snd_seq_ioctl_set_queue_client(struct snd_seq_client *client, 1816 void *arg) 1817 { 1818 struct snd_seq_queue_client *info = arg; 1819 int err; 1820 1821 if (info->used >= 0) { 1822 err = snd_seq_queue_use(info->queue, client->number, info->used); 1823 if (err < 0) 1824 return err; 1825 } 1826 1827 return snd_seq_ioctl_get_queue_client(client, arg); 1828 } 1829 1830 1831 /* GET_CLIENT_POOL ioctl() */ 1832 static int snd_seq_ioctl_get_client_pool(struct snd_seq_client *client, 1833 void *arg) 1834 { 1835 struct snd_seq_client_pool *info = arg; 1836 struct snd_seq_client *cptr __free(snd_seq_client) = 1837 client_load_and_use_ptr(info->client); 1838 1839 if (cptr == NULL) 1840 return -ENOENT; 1841 memset(info, 0, sizeof(*info)); 1842 info->client = cptr->number; 1843 info->output_pool = cptr->pool->size; 1844 info->output_room = cptr->pool->room; 1845 info->output_free = info->output_pool; 1846 info->output_free = snd_seq_unused_cells(cptr->pool); 1847 if (cptr->type == USER_CLIENT) { 1848 info->input_pool = cptr->data.user.fifo_pool_size; 1849 info->input_free = info->input_pool; 1850 info->input_free = snd_seq_fifo_unused_cells(cptr->data.user.fifo); 1851 } else { 1852 info->input_pool = 0; 1853 info->input_free = 0; 1854 } 1855 1856 return 0; 1857 } 1858 1859 /* SET_CLIENT_POOL ioctl() */ 1860 static int snd_seq_ioctl_set_client_pool(struct snd_seq_client *client, 1861 void *arg) 1862 { 1863 struct snd_seq_client_pool *info = arg; 1864 int rc; 1865 1866 if (client->number != info->client) 1867 return -EINVAL; /* can't change other clients */ 1868 1869 if (info->output_pool >= 1 && info->output_pool <= SNDRV_SEQ_MAX_EVENTS && 1870 (! snd_seq_write_pool_allocated(client) || 1871 info->output_pool != client->pool->size)) { 1872 if (snd_seq_write_pool_allocated(client)) { 1873 /* is the pool in use? */ 1874 if (atomic_read(&client->pool->counter)) 1875 return -EBUSY; 1876 /* remove all existing cells */ 1877 snd_seq_pool_mark_closing(client->pool); 1878 snd_seq_pool_done(client->pool); 1879 } 1880 client->pool->size = info->output_pool; 1881 rc = snd_seq_pool_init(client->pool); 1882 if (rc < 0) 1883 return rc; 1884 } 1885 if (client->type == USER_CLIENT && client->data.user.fifo != NULL && 1886 info->input_pool >= 1 && 1887 info->input_pool <= SNDRV_SEQ_MAX_CLIENT_EVENTS && 1888 info->input_pool != client->data.user.fifo_pool_size) { 1889 /* change pool size */ 1890 rc = snd_seq_fifo_resize(client->data.user.fifo, info->input_pool); 1891 if (rc < 0) 1892 return rc; 1893 client->data.user.fifo_pool_size = info->input_pool; 1894 } 1895 if (info->output_room >= 1 && 1896 info->output_room <= client->pool->size) { 1897 client->pool->room = info->output_room; 1898 } 1899 1900 return snd_seq_ioctl_get_client_pool(client, arg); 1901 } 1902 1903 1904 /* REMOVE_EVENTS ioctl() */ 1905 static int snd_seq_ioctl_remove_events(struct snd_seq_client *client, 1906 void *arg) 1907 { 1908 struct snd_seq_remove_events *info = arg; 1909 1910 /* 1911 * Input mostly not implemented XXX. 1912 */ 1913 if (info->remove_mode & SNDRV_SEQ_REMOVE_INPUT) { 1914 /* 1915 * No restrictions so for a user client we can clear 1916 * the whole fifo 1917 */ 1918 if (client->type == USER_CLIENT && client->data.user.fifo) 1919 snd_seq_fifo_clear(client->data.user.fifo); 1920 } 1921 1922 if (info->remove_mode & SNDRV_SEQ_REMOVE_OUTPUT) 1923 snd_seq_queue_remove_cells(client->number, info); 1924 1925 return 0; 1926 } 1927 1928 1929 /* 1930 * get subscription info 1931 */ 1932 static int snd_seq_ioctl_get_subscription(struct snd_seq_client *client, 1933 void *arg) 1934 { 1935 struct snd_seq_port_subscribe *subs = arg; 1936 1937 struct snd_seq_client *sender __free(snd_seq_client) = 1938 client_load_and_use_ptr(subs->sender.client); 1939 if (!sender) 1940 return -EINVAL; 1941 struct snd_seq_client_port *sport __free(snd_seq_port) = 1942 snd_seq_port_use_ptr(sender, subs->sender.port); 1943 if (!sport) 1944 return -EINVAL; 1945 return snd_seq_port_get_subscription(&sport->c_src, &subs->dest, subs); 1946 } 1947 1948 1949 /* 1950 * get subscription info - check only its presence 1951 */ 1952 static int snd_seq_ioctl_query_subs(struct snd_seq_client *client, void *arg) 1953 { 1954 struct snd_seq_query_subs *subs = arg; 1955 struct snd_seq_port_subs_info *group; 1956 struct hlist_node *p; 1957 int i; 1958 1959 struct snd_seq_client *cptr __free(snd_seq_client) = 1960 client_load_and_use_ptr(subs->root.client); 1961 if (!cptr) 1962 return -ENXIO; 1963 struct snd_seq_client_port *port __free(snd_seq_port) = 1964 snd_seq_port_use_ptr(cptr, subs->root.port); 1965 if (!port) 1966 return -ENXIO; 1967 1968 switch (subs->type) { 1969 case SNDRV_SEQ_QUERY_SUBS_READ: 1970 group = &port->c_src; 1971 break; 1972 case SNDRV_SEQ_QUERY_SUBS_WRITE: 1973 group = &port->c_dest; 1974 break; 1975 default: 1976 return -ENXIO; 1977 } 1978 1979 guard(rwsem_read)(&group->list_mutex); 1980 /* search for the subscriber */ 1981 subs->num_subs = group->count; 1982 i = 0; 1983 hlist_for_each(p, &group->list_head) { 1984 if (i++ == subs->index) { 1985 /* found! */ 1986 struct snd_seq_subscribers *s; 1987 if (subs->type == SNDRV_SEQ_QUERY_SUBS_READ) { 1988 s = hlist_entry(p, struct snd_seq_subscribers, src_list); 1989 subs->addr = s->info.dest; 1990 } else { 1991 s = hlist_entry(p, struct snd_seq_subscribers, dest_list); 1992 subs->addr = s->info.sender; 1993 } 1994 subs->flags = s->info.flags; 1995 subs->queue = s->info.queue; 1996 return 0; 1997 } 1998 } 1999 2000 return -ENOENT; 2001 } 2002 2003 2004 /* 2005 * query next client 2006 */ 2007 static int snd_seq_ioctl_query_next_client(struct snd_seq_client *client, 2008 void *arg) 2009 { 2010 struct snd_seq_client_info *info = arg; 2011 2012 /* search for next client */ 2013 if (info->client < INT_MAX) 2014 info->client++; 2015 if (info->client < 0) 2016 info->client = 0; 2017 for (; info->client < SNDRV_SEQ_MAX_CLIENTS; info->client++) { 2018 struct snd_seq_client *cptr __free(snd_seq_client) = 2019 client_load_and_use_ptr(info->client); 2020 if (cptr) { 2021 get_client_info(cptr, info); 2022 return 0; /* found */ 2023 } 2024 } 2025 return -ENOENT; 2026 } 2027 2028 /* 2029 * query next port 2030 */ 2031 static int snd_seq_ioctl_query_next_port(struct snd_seq_client *client, 2032 void *arg) 2033 { 2034 struct snd_seq_port_info *info = arg; 2035 2036 struct snd_seq_client *cptr __free(snd_seq_client) = 2037 client_load_and_use_ptr(info->addr.client); 2038 if (cptr == NULL) 2039 return -ENXIO; 2040 2041 /* search for next port */ 2042 info->addr.port++; 2043 struct snd_seq_client_port *port __free(snd_seq_port) = 2044 snd_seq_port_query_nearest(cptr, info); 2045 if (port == NULL) 2046 return -ENOENT; 2047 2048 /* get port info */ 2049 info->addr = port->addr; 2050 snd_seq_get_port_info(port, info); 2051 2052 return 0; 2053 } 2054 2055 #if IS_ENABLED(CONFIG_SND_SEQ_UMP) 2056 #define NUM_UMP_INFOS (SNDRV_UMP_MAX_BLOCKS + 1) 2057 2058 static void free_ump_info(struct snd_seq_client *client) 2059 { 2060 int i; 2061 2062 if (!client->ump_info) 2063 return; 2064 for (i = 0; i < NUM_UMP_INFOS; i++) 2065 kfree(client->ump_info[i]); 2066 kfree(client->ump_info); 2067 client->ump_info = NULL; 2068 } 2069 2070 static void terminate_ump_info_strings(void *p, int type) 2071 { 2072 if (type == SNDRV_SEQ_CLIENT_UMP_INFO_ENDPOINT) { 2073 struct snd_ump_endpoint_info *ep = p; 2074 ep->name[sizeof(ep->name) - 1] = 0; 2075 } else { 2076 struct snd_ump_block_info *bp = p; 2077 bp->name[sizeof(bp->name) - 1] = 0; 2078 } 2079 } 2080 2081 #ifdef CONFIG_SND_PROC_FS 2082 static void dump_ump_info(struct snd_info_buffer *buffer, 2083 struct snd_seq_client *client) 2084 { 2085 struct snd_ump_endpoint_info *ep; 2086 struct snd_ump_block_info *bp; 2087 int i; 2088 2089 if (!client->ump_info) 2090 return; 2091 ep = client->ump_info[SNDRV_SEQ_CLIENT_UMP_INFO_ENDPOINT]; 2092 if (ep && *ep->name) 2093 snd_iprintf(buffer, " UMP Endpoint: \"%s\"\n", ep->name); 2094 for (i = 0; i < SNDRV_UMP_MAX_BLOCKS; i++) { 2095 bp = client->ump_info[i + 1]; 2096 if (bp && *bp->name) { 2097 snd_iprintf(buffer, " UMP Block %d: \"%s\" [%s]\n", 2098 i, bp->name, 2099 bp->active ? "Active" : "Inactive"); 2100 snd_iprintf(buffer, " Groups: %d-%d\n", 2101 bp->first_group + 1, 2102 bp->first_group + bp->num_groups); 2103 } 2104 } 2105 } 2106 #endif 2107 2108 /* UMP-specific ioctls -- called directly without data copy */ 2109 static int snd_seq_ioctl_client_ump_info(struct snd_seq_client *caller, 2110 unsigned int cmd, 2111 unsigned long arg) 2112 { 2113 struct snd_seq_client_ump_info __user *argp = 2114 (struct snd_seq_client_ump_info __user *)arg; 2115 int client, type, err = 0; 2116 size_t size; 2117 void *p; 2118 2119 if (get_user(client, &argp->client) || get_user(type, &argp->type)) 2120 return -EFAULT; 2121 if (cmd == SNDRV_SEQ_IOCTL_SET_CLIENT_UMP_INFO && 2122 caller->number != client) 2123 return -EPERM; 2124 if (type < 0 || type >= NUM_UMP_INFOS) 2125 return -EINVAL; 2126 if (type == SNDRV_SEQ_CLIENT_UMP_INFO_ENDPOINT) 2127 size = sizeof(struct snd_ump_endpoint_info); 2128 else 2129 size = sizeof(struct snd_ump_block_info); 2130 2131 struct snd_seq_client *cptr __free(snd_seq_client) = 2132 client_load_and_use_ptr(client); 2133 if (!cptr) 2134 return -ENOENT; 2135 2136 scoped_guard(mutex, &cptr->ioctl_mutex) { 2137 if (!cptr->midi_version) { 2138 err = -EBADFD; 2139 break; 2140 } 2141 2142 if (cmd == SNDRV_SEQ_IOCTL_GET_CLIENT_UMP_INFO) { 2143 if (!cptr->ump_info) 2144 p = NULL; 2145 else 2146 p = cptr->ump_info[type]; 2147 if (!p) { 2148 err = -ENODEV; 2149 break; 2150 } 2151 if (copy_to_user(argp->info, p, size)) { 2152 err = -EFAULT; 2153 break; 2154 } 2155 } else { 2156 if (cptr->type != USER_CLIENT) { 2157 err = -EBADFD; 2158 break; 2159 } 2160 if (!cptr->ump_info) { 2161 cptr->ump_info = kcalloc(NUM_UMP_INFOS, 2162 sizeof(void *), GFP_KERNEL); 2163 if (!cptr->ump_info) { 2164 err = -ENOMEM; 2165 break; 2166 } 2167 } 2168 p = memdup_user(argp->info, size); 2169 if (IS_ERR(p)) { 2170 err = PTR_ERR(p); 2171 break; 2172 } 2173 kfree(cptr->ump_info[type]); 2174 terminate_ump_info_strings(p, type); 2175 cptr->ump_info[type] = p; 2176 } 2177 2178 } 2179 if (!err && cmd == SNDRV_SEQ_IOCTL_SET_CLIENT_UMP_INFO) { 2180 if (type == SNDRV_SEQ_CLIENT_UMP_INFO_ENDPOINT) 2181 snd_seq_system_ump_notify(client, 0, 2182 SNDRV_SEQ_EVENT_UMP_EP_CHANGE, 2183 false); 2184 else 2185 snd_seq_system_ump_notify(client, type - 1, 2186 SNDRV_SEQ_EVENT_UMP_BLOCK_CHANGE, 2187 false); 2188 } 2189 return err; 2190 } 2191 #endif 2192 2193 /* -------------------------------------------------------- */ 2194 2195 static const struct ioctl_handler { 2196 unsigned int cmd; 2197 int (*func)(struct snd_seq_client *client, void *arg); 2198 } ioctl_handlers[] = { 2199 { SNDRV_SEQ_IOCTL_PVERSION, snd_seq_ioctl_pversion }, 2200 { SNDRV_SEQ_IOCTL_USER_PVERSION, snd_seq_ioctl_user_pversion }, 2201 { SNDRV_SEQ_IOCTL_CLIENT_ID, snd_seq_ioctl_client_id }, 2202 { SNDRV_SEQ_IOCTL_SYSTEM_INFO, snd_seq_ioctl_system_info }, 2203 { SNDRV_SEQ_IOCTL_RUNNING_MODE, snd_seq_ioctl_running_mode }, 2204 { SNDRV_SEQ_IOCTL_GET_CLIENT_INFO, snd_seq_ioctl_get_client_info }, 2205 { SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, snd_seq_ioctl_set_client_info }, 2206 { SNDRV_SEQ_IOCTL_CREATE_PORT, snd_seq_ioctl_create_port }, 2207 { SNDRV_SEQ_IOCTL_DELETE_PORT, snd_seq_ioctl_delete_port }, 2208 { SNDRV_SEQ_IOCTL_GET_PORT_INFO, snd_seq_ioctl_get_port_info }, 2209 { SNDRV_SEQ_IOCTL_SET_PORT_INFO, snd_seq_ioctl_set_port_info }, 2210 { SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, snd_seq_ioctl_subscribe_port }, 2211 { SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, snd_seq_ioctl_unsubscribe_port }, 2212 { SNDRV_SEQ_IOCTL_CREATE_QUEUE, snd_seq_ioctl_create_queue }, 2213 { SNDRV_SEQ_IOCTL_DELETE_QUEUE, snd_seq_ioctl_delete_queue }, 2214 { SNDRV_SEQ_IOCTL_GET_QUEUE_INFO, snd_seq_ioctl_get_queue_info }, 2215 { SNDRV_SEQ_IOCTL_SET_QUEUE_INFO, snd_seq_ioctl_set_queue_info }, 2216 { SNDRV_SEQ_IOCTL_GET_NAMED_QUEUE, snd_seq_ioctl_get_named_queue }, 2217 { SNDRV_SEQ_IOCTL_GET_QUEUE_STATUS, snd_seq_ioctl_get_queue_status }, 2218 { SNDRV_SEQ_IOCTL_GET_QUEUE_TEMPO, snd_seq_ioctl_get_queue_tempo }, 2219 { SNDRV_SEQ_IOCTL_SET_QUEUE_TEMPO, snd_seq_ioctl_set_queue_tempo }, 2220 { SNDRV_SEQ_IOCTL_GET_QUEUE_TIMER, snd_seq_ioctl_get_queue_timer }, 2221 { SNDRV_SEQ_IOCTL_SET_QUEUE_TIMER, snd_seq_ioctl_set_queue_timer }, 2222 { SNDRV_SEQ_IOCTL_GET_QUEUE_CLIENT, snd_seq_ioctl_get_queue_client }, 2223 { SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT, snd_seq_ioctl_set_queue_client }, 2224 { SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, snd_seq_ioctl_get_client_pool }, 2225 { SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, snd_seq_ioctl_set_client_pool }, 2226 { SNDRV_SEQ_IOCTL_GET_SUBSCRIPTION, snd_seq_ioctl_get_subscription }, 2227 { SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT, snd_seq_ioctl_query_next_client }, 2228 { SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, snd_seq_ioctl_query_next_port }, 2229 { SNDRV_SEQ_IOCTL_REMOVE_EVENTS, snd_seq_ioctl_remove_events }, 2230 { SNDRV_SEQ_IOCTL_QUERY_SUBS, snd_seq_ioctl_query_subs }, 2231 { 0, NULL }, 2232 }; 2233 2234 static long snd_seq_ioctl(struct file *file, unsigned int cmd, 2235 unsigned long arg) 2236 { 2237 struct snd_seq_client *client = file->private_data; 2238 /* To use kernel stack for ioctl data. */ 2239 union { 2240 int pversion; 2241 int client_id; 2242 struct snd_seq_system_info system_info; 2243 struct snd_seq_running_info running_info; 2244 struct snd_seq_client_info client_info; 2245 struct snd_seq_port_info port_info; 2246 struct snd_seq_port_subscribe port_subscribe; 2247 struct snd_seq_queue_info queue_info; 2248 struct snd_seq_queue_status queue_status; 2249 struct snd_seq_queue_tempo tempo; 2250 struct snd_seq_queue_timer queue_timer; 2251 struct snd_seq_queue_client queue_client; 2252 struct snd_seq_client_pool client_pool; 2253 struct snd_seq_remove_events remove_events; 2254 struct snd_seq_query_subs query_subs; 2255 } buf; 2256 const struct ioctl_handler *handler; 2257 unsigned long size; 2258 int err; 2259 2260 if (snd_BUG_ON(!client)) 2261 return -ENXIO; 2262 2263 #if IS_ENABLED(CONFIG_SND_SEQ_UMP) 2264 /* exception - handling large data */ 2265 switch (cmd) { 2266 case SNDRV_SEQ_IOCTL_GET_CLIENT_UMP_INFO: 2267 case SNDRV_SEQ_IOCTL_SET_CLIENT_UMP_INFO: 2268 return snd_seq_ioctl_client_ump_info(client, cmd, arg); 2269 } 2270 #endif 2271 2272 for (handler = ioctl_handlers; handler->cmd > 0; ++handler) { 2273 if (handler->cmd == cmd) 2274 break; 2275 } 2276 if (handler->cmd == 0) 2277 return -ENOTTY; 2278 2279 memset(&buf, 0, sizeof(buf)); 2280 2281 /* 2282 * All of ioctl commands for ALSA sequencer get an argument of size 2283 * within 13 bits. We can safely pick up the size from the command. 2284 */ 2285 size = _IOC_SIZE(handler->cmd); 2286 if (handler->cmd & IOC_IN) { 2287 if (copy_from_user(&buf, (const void __user *)arg, size)) 2288 return -EFAULT; 2289 } 2290 2291 scoped_guard(mutex, &client->ioctl_mutex) { 2292 err = handler->func(client, &buf); 2293 } 2294 if (err >= 0) { 2295 /* Some commands includes a bug in 'dir' field. */ 2296 if (handler->cmd == SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT || 2297 handler->cmd == SNDRV_SEQ_IOCTL_SET_CLIENT_POOL || 2298 (handler->cmd & IOC_OUT)) 2299 if (copy_to_user((void __user *)arg, &buf, size)) 2300 return -EFAULT; 2301 } 2302 2303 return err; 2304 } 2305 2306 #ifdef CONFIG_COMPAT 2307 #include "seq_compat.c" 2308 #else 2309 #define snd_seq_ioctl_compat NULL 2310 #endif 2311 2312 /* -------------------------------------------------------- */ 2313 2314 2315 /* exported to kernel modules */ 2316 int snd_seq_create_kernel_client(struct snd_card *card, int client_index, 2317 const char *name_fmt, ...) 2318 { 2319 struct snd_seq_client *client; 2320 va_list args; 2321 2322 if (snd_BUG_ON(in_interrupt())) 2323 return -EBUSY; 2324 2325 if (card && client_index >= SNDRV_SEQ_CLIENTS_PER_CARD) 2326 return -EINVAL; 2327 if (card == NULL && client_index >= SNDRV_SEQ_GLOBAL_CLIENTS) 2328 return -EINVAL; 2329 2330 scoped_guard(mutex, ®ister_mutex) { 2331 2332 if (card) { 2333 client_index += SNDRV_SEQ_GLOBAL_CLIENTS 2334 + card->number * SNDRV_SEQ_CLIENTS_PER_CARD; 2335 if (client_index >= SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN) 2336 client_index = -1; 2337 } 2338 2339 /* empty write queue as default */ 2340 client = seq_create_client1(client_index, 0); 2341 if (client == NULL) 2342 return -EBUSY; /* failure code */ 2343 usage_alloc(&client_usage, 1); 2344 2345 client->accept_input = 1; 2346 client->accept_output = 1; 2347 client->data.kernel.card = card; 2348 client->user_pversion = SNDRV_SEQ_VERSION; 2349 2350 va_start(args, name_fmt); 2351 vsnprintf(client->name, sizeof(client->name), name_fmt, args); 2352 va_end(args); 2353 2354 client->type = KERNEL_CLIENT; 2355 } 2356 2357 /* make others aware this new client */ 2358 snd_seq_system_client_ev_client_start(client->number); 2359 2360 /* return client number to caller */ 2361 return client->number; 2362 } 2363 EXPORT_SYMBOL(snd_seq_create_kernel_client); 2364 2365 /* exported to kernel modules */ 2366 int snd_seq_delete_kernel_client(int client) 2367 { 2368 struct snd_seq_client *ptr; 2369 2370 if (snd_BUG_ON(in_interrupt())) 2371 return -EBUSY; 2372 2373 ptr = clientptr(client); 2374 if (ptr == NULL) 2375 return -EINVAL; 2376 2377 seq_free_client(ptr); 2378 kfree(ptr); 2379 return 0; 2380 } 2381 EXPORT_SYMBOL(snd_seq_delete_kernel_client); 2382 2383 /* 2384 * exported, called by kernel clients to enqueue events (w/o blocking) 2385 * 2386 * RETURN VALUE: zero if succeed, negative if error 2387 */ 2388 int snd_seq_kernel_client_enqueue(int client, struct snd_seq_event *ev, 2389 struct file *file, bool blocking) 2390 { 2391 if (snd_BUG_ON(!ev)) 2392 return -EINVAL; 2393 2394 if (!snd_seq_ev_is_ump(ev)) { 2395 if (ev->type == SNDRV_SEQ_EVENT_NONE) 2396 return 0; /* ignore this */ 2397 if (ev->type == SNDRV_SEQ_EVENT_KERNEL_ERROR) 2398 return -EINVAL; /* quoted events can't be enqueued */ 2399 } 2400 2401 /* fill in client number */ 2402 ev->source.client = client; 2403 2404 if (check_event_type_and_length(ev)) 2405 return -EINVAL; 2406 2407 struct snd_seq_client *cptr __free(snd_seq_client) = 2408 client_load_and_use_ptr(client); 2409 if (cptr == NULL) 2410 return -EINVAL; 2411 2412 if (!cptr->accept_output) { 2413 return -EPERM; 2414 } else { /* send it */ 2415 guard(mutex)(&cptr->ioctl_mutex); 2416 return snd_seq_client_enqueue_event(cptr, ev, file, blocking, 2417 false, 0, 2418 &cptr->ioctl_mutex); 2419 } 2420 } 2421 EXPORT_SYMBOL(snd_seq_kernel_client_enqueue); 2422 2423 /* 2424 * exported, called by kernel clients to dispatch events directly to other 2425 * clients, bypassing the queues. Event time-stamp will be updated. 2426 * 2427 * RETURN VALUE: negative = delivery failed, 2428 * zero, or positive: the number of delivered events 2429 */ 2430 int snd_seq_kernel_client_dispatch(int client, struct snd_seq_event * ev, 2431 int atomic, int hop) 2432 { 2433 if (snd_BUG_ON(!ev)) 2434 return -EINVAL; 2435 2436 /* fill in client number */ 2437 ev->queue = SNDRV_SEQ_QUEUE_DIRECT; 2438 ev->source.client = client; 2439 2440 if (check_event_type_and_length(ev)) 2441 return -EINVAL; 2442 2443 struct snd_seq_client *cptr __free(snd_seq_client) = 2444 snd_seq_client_use_ptr(client); 2445 if (cptr == NULL) 2446 return -EINVAL; 2447 2448 if (!cptr->accept_output) 2449 return -EPERM; 2450 else 2451 return snd_seq_deliver_event(cptr, ev, atomic, hop); 2452 } 2453 EXPORT_SYMBOL(snd_seq_kernel_client_dispatch); 2454 2455 static int call_seq_client_ctl(struct snd_seq_client *client, 2456 unsigned int cmd, void *arg) 2457 { 2458 const struct ioctl_handler *handler; 2459 2460 for (handler = ioctl_handlers; handler->cmd > 0; ++handler) { 2461 if (handler->cmd == cmd) 2462 return handler->func(client, arg); 2463 } 2464 2465 pr_debug("ALSA: seq unknown ioctl() 0x%x (type='%c', number=0x%02x)\n", 2466 cmd, _IOC_TYPE(cmd), _IOC_NR(cmd)); 2467 return -ENOTTY; 2468 } 2469 2470 /** 2471 * snd_seq_kernel_client_ctl - operate a command for a client with data in 2472 * kernel space. 2473 * @clientid: A numerical ID for a client. 2474 * @cmd: An ioctl(2) command for ALSA sequencer operation. 2475 * @arg: A pointer to data in kernel space. 2476 * 2477 * Against its name, both kernel/application client can be handled by this 2478 * kernel API. A pointer of 'arg' argument should be in kernel space. 2479 * 2480 * Return: 0 at success. Negative error code at failure. 2481 */ 2482 int snd_seq_kernel_client_ctl(int clientid, unsigned int cmd, void *arg) 2483 { 2484 struct snd_seq_client *client; 2485 2486 client = clientptr(clientid); 2487 if (client == NULL) 2488 return -ENXIO; 2489 2490 return call_seq_client_ctl(client, cmd, arg); 2491 } 2492 EXPORT_SYMBOL(snd_seq_kernel_client_ctl); 2493 2494 /* a similar like above but taking locks; used only from OSS sequencer layer */ 2495 int snd_seq_kernel_client_ioctl(int clientid, unsigned int cmd, void *arg) 2496 { 2497 struct snd_seq_client *client __free(snd_seq_client) = 2498 client_load_and_use_ptr(clientid); 2499 2500 if (!client) 2501 return -ENXIO; 2502 guard(mutex)(&client->ioctl_mutex); 2503 return call_seq_client_ctl(client, cmd, arg); 2504 } 2505 EXPORT_SYMBOL_GPL(snd_seq_kernel_client_ioctl); 2506 2507 /* exported (for OSS emulator) */ 2508 int snd_seq_kernel_client_write_poll(int clientid, struct file *file, poll_table *wait) 2509 { 2510 struct snd_seq_client *client; 2511 2512 client = clientptr(clientid); 2513 if (client == NULL) 2514 return -ENXIO; 2515 2516 if (snd_seq_pool_poll_wait(client->pool, file, wait)) 2517 return 1; 2518 return 0; 2519 } 2520 EXPORT_SYMBOL(snd_seq_kernel_client_write_poll); 2521 2522 /* get a sequencer client object; for internal use from a kernel client */ 2523 struct snd_seq_client *snd_seq_kernel_client_get(int id) 2524 { 2525 return snd_seq_client_use_ptr(id); 2526 } 2527 EXPORT_SYMBOL_GPL(snd_seq_kernel_client_get); 2528 2529 /* put a sequencer client object; for internal use from a kernel client */ 2530 void snd_seq_kernel_client_put(struct snd_seq_client *cptr) 2531 { 2532 if (cptr) 2533 snd_seq_client_unref(cptr); 2534 } 2535 EXPORT_SYMBOL_GPL(snd_seq_kernel_client_put); 2536 2537 /*---------------------------------------------------------------------------*/ 2538 2539 #ifdef CONFIG_SND_PROC_FS 2540 /* 2541 * /proc interface 2542 */ 2543 static void snd_seq_info_dump_subscribers(struct snd_info_buffer *buffer, 2544 struct snd_seq_port_subs_info *group, 2545 int is_src, char *msg) 2546 { 2547 struct hlist_node *p; 2548 struct snd_seq_subscribers *s; 2549 int count = 0; 2550 2551 guard(rwsem_read)(&group->list_mutex); 2552 if (hlist_empty(&group->list_head)) 2553 return; 2554 snd_iprintf(buffer, msg); 2555 hlist_for_each(p, &group->list_head) { 2556 if (is_src) 2557 s = hlist_entry(p, struct snd_seq_subscribers, src_list); 2558 else 2559 s = hlist_entry(p, struct snd_seq_subscribers, dest_list); 2560 if (count++) 2561 snd_iprintf(buffer, ", "); 2562 snd_iprintf(buffer, "%d:%d", 2563 is_src ? s->info.dest.client : s->info.sender.client, 2564 is_src ? s->info.dest.port : s->info.sender.port); 2565 if (s->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP) 2566 snd_iprintf(buffer, "[%c:%d]", ((s->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL) ? 'r' : 't'), s->info.queue); 2567 if (group->exclusive) 2568 snd_iprintf(buffer, "[ex]"); 2569 } 2570 snd_iprintf(buffer, "\n"); 2571 } 2572 2573 #define FLAG_PERM_RD(perm) ((perm) & SNDRV_SEQ_PORT_CAP_READ ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_READ ? 'R' : 'r') : '-') 2574 #define FLAG_PERM_WR(perm) ((perm) & SNDRV_SEQ_PORT_CAP_WRITE ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_WRITE ? 'W' : 'w') : '-') 2575 #define FLAG_PERM_EX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_NO_EXPORT ? '-' : 'e') 2576 2577 #define FLAG_PERM_DUPLEX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_DUPLEX ? 'X' : '-') 2578 2579 static const char *port_direction_name(unsigned char dir) 2580 { 2581 static const char *names[4] = { 2582 "-", "In", "Out", "In/Out" 2583 }; 2584 2585 if (dir > SNDRV_SEQ_PORT_DIR_BIDIRECTION) 2586 return "Invalid"; 2587 return names[dir]; 2588 } 2589 2590 static void snd_seq_info_dump_ports(struct snd_info_buffer *buffer, 2591 struct snd_seq_client *client) 2592 { 2593 struct snd_seq_client_port *p; 2594 2595 guard(mutex)(&client->ports_mutex); 2596 list_for_each_entry(p, &client->ports_list_head, list) { 2597 if (p->capability & SNDRV_SEQ_PORT_CAP_INACTIVE) 2598 continue; 2599 snd_iprintf(buffer, " Port %3d : \"%s\" (%c%c%c%c) [%s]", 2600 p->addr.port, p->name, 2601 FLAG_PERM_RD(p->capability), 2602 FLAG_PERM_WR(p->capability), 2603 FLAG_PERM_EX(p->capability), 2604 FLAG_PERM_DUPLEX(p->capability), 2605 port_direction_name(p->direction)); 2606 #if IS_ENABLED(CONFIG_SND_SEQ_UMP) 2607 if (snd_seq_client_is_midi2(client) && p->is_midi1) 2608 snd_iprintf(buffer, " [MIDI1]"); 2609 #endif 2610 snd_iprintf(buffer, "\n"); 2611 snd_seq_info_dump_subscribers(buffer, &p->c_src, 1, " Connecting To: "); 2612 snd_seq_info_dump_subscribers(buffer, &p->c_dest, 0, " Connected From: "); 2613 } 2614 } 2615 2616 static const char *midi_version_string(unsigned int version) 2617 { 2618 switch (version) { 2619 case SNDRV_SEQ_CLIENT_LEGACY_MIDI: 2620 return "Legacy"; 2621 case SNDRV_SEQ_CLIENT_UMP_MIDI_1_0: 2622 return "UMP MIDI1"; 2623 case SNDRV_SEQ_CLIENT_UMP_MIDI_2_0: 2624 return "UMP MIDI2"; 2625 default: 2626 return "Unknown"; 2627 } 2628 } 2629 2630 /* exported to seq_info.c */ 2631 void snd_seq_info_clients_read(struct snd_info_entry *entry, 2632 struct snd_info_buffer *buffer) 2633 { 2634 int c; 2635 2636 snd_iprintf(buffer, "Client info\n"); 2637 snd_iprintf(buffer, " cur clients : %d\n", client_usage.cur); 2638 snd_iprintf(buffer, " peak clients : %d\n", client_usage.peak); 2639 snd_iprintf(buffer, " max clients : %d\n", SNDRV_SEQ_MAX_CLIENTS); 2640 snd_iprintf(buffer, "\n"); 2641 2642 /* list the client table */ 2643 for (c = 0; c < SNDRV_SEQ_MAX_CLIENTS; c++) { 2644 struct snd_seq_client *client __free(snd_seq_client) = 2645 client_load_and_use_ptr(c); 2646 2647 if (client == NULL) 2648 continue; 2649 if (client->type == NO_CLIENT) 2650 continue; 2651 2652 guard(mutex)(&client->ioctl_mutex); 2653 snd_iprintf(buffer, "Client %3d : \"%s\" [%s %s]\n", 2654 c, client->name, 2655 client->type == USER_CLIENT ? "User" : "Kernel", 2656 midi_version_string(client->midi_version)); 2657 #if IS_ENABLED(CONFIG_SND_SEQ_UMP) 2658 dump_ump_info(buffer, client); 2659 #endif 2660 snd_seq_info_dump_ports(buffer, client); 2661 if (snd_seq_write_pool_allocated(client)) { 2662 snd_iprintf(buffer, " Output pool :\n"); 2663 snd_seq_info_pool(buffer, client->pool, " "); 2664 } 2665 if (client->type == USER_CLIENT && client->data.user.fifo && 2666 client->data.user.fifo->pool) { 2667 snd_iprintf(buffer, " Input pool :\n"); 2668 snd_seq_info_pool(buffer, client->data.user.fifo->pool, " "); 2669 } 2670 } 2671 } 2672 #endif /* CONFIG_SND_PROC_FS */ 2673 2674 /*---------------------------------------------------------------------------*/ 2675 2676 2677 /* 2678 * REGISTRATION PART 2679 */ 2680 2681 static const struct file_operations snd_seq_f_ops = { 2682 .owner = THIS_MODULE, 2683 .read = snd_seq_read, 2684 .write = snd_seq_write, 2685 .open = snd_seq_open, 2686 .release = snd_seq_release, 2687 .poll = snd_seq_poll, 2688 .unlocked_ioctl = snd_seq_ioctl, 2689 .compat_ioctl = snd_seq_ioctl_compat, 2690 }; 2691 2692 static struct device *seq_dev; 2693 2694 /* 2695 * register sequencer device 2696 */ 2697 int __init snd_sequencer_device_init(void) 2698 { 2699 int err; 2700 2701 err = snd_device_alloc(&seq_dev, NULL); 2702 if (err < 0) 2703 return err; 2704 dev_set_name(seq_dev, "seq"); 2705 2706 scoped_guard(mutex, ®ister_mutex) { 2707 err = snd_register_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0, 2708 &snd_seq_f_ops, NULL, seq_dev); 2709 } 2710 if (err < 0) { 2711 put_device(seq_dev); 2712 return err; 2713 } 2714 2715 return 0; 2716 } 2717 2718 2719 2720 /* 2721 * unregister sequencer device 2722 */ 2723 void snd_sequencer_device_done(void) 2724 { 2725 snd_unregister_device(seq_dev); 2726 put_device(seq_dev); 2727 } 2728