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