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