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