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