seq_clientmgr.c 66.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *  ALSA sequencer Client Manager
 *  Copyright (c) 1998-2001 by Frank van de Pol <fvdpol@coil.demon.nl>
4
 *                             Jaroslav Kysela <perex@perex.cz>
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *                             Takashi Iwai <tiwai@suse.de>
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <linux/init.h>
25
#include <linux/export.h>
L
Linus Torvalds 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <linux/slab.h>
#include <sound/core.h>
#include <sound/minors.h>
#include <linux/kmod.h>

#include <sound/seq_kernel.h>
#include "seq_clientmgr.h"
#include "seq_memory.h"
#include "seq_queue.h"
#include "seq_timer.h"
#include "seq_info.h"
#include "seq_system.h"
#include <sound/seq_device.h>
#ifdef CONFIG_COMPAT
#include <linux/compat.h>
#endif

/* Client Manager

 * this module handles the connections of userland and kernel clients
 * 
 */

49 50 51 52 53 54 55 56 57 58 59 60 61 62
/*
 * There are four ranges of client numbers (last two shared):
 * 0..15: global clients
 * 16..127: statically allocated client numbers for cards 0..27
 * 128..191: dynamically allocated client numbers for cards 28..31
 * 128..191: dynamically allocated client numbers for applications
 */

/* number of kernel non-card clients */
#define SNDRV_SEQ_GLOBAL_CLIENTS	16
/* clients per cards, for static clients */
#define SNDRV_SEQ_CLIENTS_PER_CARD	4
/* dynamically allocated client numbers (both kernel drivers and user space) */
#define SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN	128
63

L
Linus Torvalds 已提交
64 65 66 67 68
#define SNDRV_SEQ_LFLG_INPUT	0x0001
#define SNDRV_SEQ_LFLG_OUTPUT	0x0002
#define SNDRV_SEQ_LFLG_OPEN	(SNDRV_SEQ_LFLG_INPUT|SNDRV_SEQ_LFLG_OUTPUT)

static DEFINE_SPINLOCK(clients_lock);
69
static DEFINE_MUTEX(register_mutex);
L
Linus Torvalds 已提交
70 71 72 73 74

/*
 * client table
 */
static char clienttablock[SNDRV_SEQ_MAX_CLIENTS];
75 76
static struct snd_seq_client *clienttab[SNDRV_SEQ_MAX_CLIENTS];
static struct snd_seq_usage client_usage;
L
Linus Torvalds 已提交
77 78 79 80

/*
 * prototypes
 */
81 82 83 84 85 86
static int bounce_error_event(struct snd_seq_client *client,
			      struct snd_seq_event *event,
			      int err, int atomic, int hop);
static int snd_seq_deliver_single_event(struct snd_seq_client *client,
					struct snd_seq_event *event,
					int filter, int atomic, int hop);
L
Linus Torvalds 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

/*
 */
 
static inline mm_segment_t snd_enter_user(void)
{
	mm_segment_t fs = get_fs();
	set_fs(get_ds());
	return fs;
}

static inline void snd_leave_user(mm_segment_t fs)
{
	set_fs(fs);
}

/*
 */
static inline unsigned short snd_seq_file_flags(struct file *file)
{
        switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
        case FMODE_WRITE:
                return SNDRV_SEQ_LFLG_OUTPUT;
        case FMODE_READ:
                return SNDRV_SEQ_LFLG_INPUT;
        default:
                return SNDRV_SEQ_LFLG_OPEN;
        }
}

117
static inline int snd_seq_write_pool_allocated(struct snd_seq_client *client)
L
Linus Torvalds 已提交
118 119 120 121 122
{
	return snd_seq_total_cells(client->pool) > 0;
}

/* return pointer to client structure for specified id */
123
static struct snd_seq_client *clientptr(int clientid)
L
Linus Torvalds 已提交
124 125
{
	if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) {
126
		pr_debug("ALSA: seq: oops. Trying to get pointer to client %d\n",
127
			   clientid);
L
Linus Torvalds 已提交
128 129 130 131 132
		return NULL;
	}
	return clienttab[clientid];
}

133
struct snd_seq_client *snd_seq_client_use_ptr(int clientid)
L
Linus Torvalds 已提交
134 135
{
	unsigned long flags;
136
	struct snd_seq_client *client;
L
Linus Torvalds 已提交
137 138

	if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) {
139
		pr_debug("ALSA: seq: oops. Trying to get pointer to client %d\n",
140
			   clientid);
L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148 149 150 151
		return NULL;
	}
	spin_lock_irqsave(&clients_lock, flags);
	client = clientptr(clientid);
	if (client)
		goto __lock;
	if (clienttablock[clientid]) {
		spin_unlock_irqrestore(&clients_lock, flags);
		return NULL;
	}
	spin_unlock_irqrestore(&clients_lock, flags);
152
#ifdef CONFIG_MODULES
J
Jan Blunck 已提交
153
	if (!in_interrupt()) {
154
		static char client_requested[SNDRV_SEQ_GLOBAL_CLIENTS];
L
Linus Torvalds 已提交
155
		static char card_requested[SNDRV_CARDS];
156
		if (clientid < SNDRV_SEQ_GLOBAL_CLIENTS) {
L
Linus Torvalds 已提交
157 158
			int idx;
			
J
Jan Blunck 已提交
159
			if (!client_requested[clientid]) {
L
Linus Torvalds 已提交
160
				client_requested[clientid] = 1;
161
				for (idx = 0; idx < 15; idx++) {
L
Linus Torvalds 已提交
162 163 164
					if (seq_client_load[idx] < 0)
						break;
					if (seq_client_load[idx] == clientid) {
165 166
						request_module("snd-seq-client-%i",
							       clientid);
L
Linus Torvalds 已提交
167 168 169 170
						break;
					}
				}
			}
171 172 173
		} else if (clientid < SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN) {
			int card = (clientid - SNDRV_SEQ_GLOBAL_CLIENTS) /
				SNDRV_SEQ_CLIENTS_PER_CARD;
L
Linus Torvalds 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
			if (card < snd_ecards_limit) {
				if (! card_requested[card]) {
					card_requested[card] = 1;
					snd_request_card(card);
				}
				snd_seq_device_load_drivers();
			}
		}
		spin_lock_irqsave(&clients_lock, flags);
		client = clientptr(clientid);
		if (client)
			goto __lock;
		spin_unlock_irqrestore(&clients_lock, flags);
	}
#endif
	return NULL;

      __lock:
	snd_use_lock_use(&client->use_lock);
	spin_unlock_irqrestore(&clients_lock, flags);
	return client;
}

197
static void usage_alloc(struct snd_seq_usage *res, int num)
L
Linus Torvalds 已提交
198 199 200 201 202 203
{
	res->cur += num;
	if (res->cur > res->peak)
		res->peak = res->cur;
}

204
static void usage_free(struct snd_seq_usage *res, int num)
L
Linus Torvalds 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218
{
	res->cur -= num;
}

/* initialise data structures */
int __init client_init_data(void)
{
	/* zap out the client table */
	memset(&clienttablock, 0, sizeof(clienttablock));
	memset(&clienttab, 0, sizeof(clienttab));
	return 0;
}


219
static struct snd_seq_client *seq_create_client1(int client_index, int poolsize)
L
Linus Torvalds 已提交
220 221 222
{
	unsigned long flags;
	int c;
223
	struct snd_seq_client *client;
L
Linus Torvalds 已提交
224 225

	/* init client data */
226
	client = kzalloc(sizeof(*client), GFP_KERNEL);
L
Linus Torvalds 已提交
227 228 229 230 231 232 233 234 235 236
	if (client == NULL)
		return NULL;
	client->pool = snd_seq_pool_new(poolsize);
	if (client->pool == NULL) {
		kfree(client);
		return NULL;
	}
	client->type = NO_CLIENT;
	snd_use_lock_init(&client->use_lock);
	rwlock_init(&client->ports_lock);
237
	mutex_init(&client->ports_mutex);
L
Linus Torvalds 已提交
238 239 240 241 242
	INIT_LIST_HEAD(&client->ports_list_head);

	/* find free slot in the client table */
	spin_lock_irqsave(&clients_lock, flags);
	if (client_index < 0) {
243 244 245
		for (c = SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN;
		     c < SNDRV_SEQ_MAX_CLIENTS;
		     c++) {
L
Linus Torvalds 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
			if (clienttab[c] || clienttablock[c])
				continue;
			clienttab[client->number = c] = client;
			spin_unlock_irqrestore(&clients_lock, flags);
			return client;
		}
	} else {
		if (clienttab[client_index] == NULL && !clienttablock[client_index]) {
			clienttab[client->number = client_index] = client;
			spin_unlock_irqrestore(&clients_lock, flags);
			return client;
		}
	}
	spin_unlock_irqrestore(&clients_lock, flags);
	snd_seq_pool_delete(&client->pool);
	kfree(client);
	return NULL;	/* no free slot found or busy, return failure code */
}


266
static int seq_free_client1(struct snd_seq_client *client)
L
Linus Torvalds 已提交
267 268 269
{
	unsigned long flags;

270 271
	if (!client)
		return 0;
L
Linus Torvalds 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
	snd_seq_delete_all_ports(client);
	snd_seq_queue_client_leave(client->number);
	spin_lock_irqsave(&clients_lock, flags);
	clienttablock[client->number] = 1;
	clienttab[client->number] = NULL;
	spin_unlock_irqrestore(&clients_lock, flags);
	snd_use_lock_sync(&client->use_lock);
	snd_seq_queue_client_termination(client->number);
	if (client->pool)
		snd_seq_pool_delete(&client->pool);
	spin_lock_irqsave(&clients_lock, flags);
	clienttablock[client->number] = 0;
	spin_unlock_irqrestore(&clients_lock, flags);
	return 0;
}


289
static void seq_free_client(struct snd_seq_client * client)
L
Linus Torvalds 已提交
290
{
291
	mutex_lock(&register_mutex);
L
Linus Torvalds 已提交
292 293
	switch (client->type) {
	case NO_CLIENT:
294 295
		pr_warn("ALSA: seq: Trying to free unused client %d\n",
			client->number);
L
Linus Torvalds 已提交
296 297 298 299 300 301 302 303
		break;
	case USER_CLIENT:
	case KERNEL_CLIENT:
		seq_free_client1(client);
		usage_free(&client_usage, 1);
		break;

	default:
304
		pr_err("ALSA: seq: Trying to free client %d with undefined type = %d\n",
305
			   client->number, client->type);
L
Linus Torvalds 已提交
306
	}
307
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
308 309 310 311 312 313 314 315 316 317 318 319

	snd_seq_system_client_ev_client_exit(client->number);
}



/* -------------------------------------------------------- */

/* create a user client */
static int snd_seq_open(struct inode *inode, struct file *file)
{
	int c, mode;			/* client id */
320 321
	struct snd_seq_client *client;
	struct snd_seq_user_client *user;
T
Takashi Iwai 已提交
322 323 324 325 326
	int err;

	err = nonseekable_open(inode, file);
	if (err < 0)
		return err;
L
Linus Torvalds 已提交
327

328
	if (mutex_lock_interruptible(&register_mutex))
L
Linus Torvalds 已提交
329
		return -ERESTARTSYS;
330
	client = seq_create_client1(-1, SNDRV_SEQ_DEFAULT_EVENTS);
L
Linus Torvalds 已提交
331
	if (client == NULL) {
332
		mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
		return -ENOMEM;	/* failure code */
	}

	mode = snd_seq_file_flags(file);
	if (mode & SNDRV_SEQ_LFLG_INPUT)
		client->accept_input = 1;
	if (mode & SNDRV_SEQ_LFLG_OUTPUT)
		client->accept_output = 1;

	user = &client->data.user;
	user->fifo = NULL;
	user->fifo_pool_size = 0;

	if (mode & SNDRV_SEQ_LFLG_INPUT) {
		user->fifo_pool_size = SNDRV_SEQ_DEFAULT_CLIENT_EVENTS;
		user->fifo = snd_seq_fifo_new(user->fifo_pool_size);
		if (user->fifo == NULL) {
			seq_free_client1(client);
			kfree(client);
352
			mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
353 354 355 356 357 358
			return -ENOMEM;
		}
	}

	usage_alloc(&client_usage, 1);
	client->type = USER_CLIENT;
359
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

	c = client->number;
	file->private_data = client;

	/* fill client data */
	user->file = file;
	sprintf(client->name, "Client-%d", c);

	/* make others aware this new client */
	snd_seq_system_client_ev_client_start(c);

	return 0;
}

/* delete a user client */
static int snd_seq_release(struct inode *inode, struct file *file)
{
377
	struct snd_seq_client *client = file->private_data;
L
Linus Torvalds 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396

	if (client) {
		seq_free_client(client);
		if (client->data.user.fifo)
			snd_seq_fifo_delete(&client->data.user.fifo);
		kfree(client);
	}

	return 0;
}


/* handle client read() */
/* possible error values:
 *	-ENXIO	invalid client or file open mode
 *	-ENOSPC	FIFO overflow (the flag is cleared after this error report)
 *	-EINVAL	no enough user-space buffer to write the whole event
 *	-EFAULT	seg. fault during copy to user space
 */
397 398
static ssize_t snd_seq_read(struct file *file, char __user *buf, size_t count,
			    loff_t *offset)
L
Linus Torvalds 已提交
399
{
400 401
	struct snd_seq_client *client = file->private_data;
	struct snd_seq_fifo *fifo;
L
Linus Torvalds 已提交
402 403
	int err;
	long result = 0;
404
	struct snd_seq_event_cell *cell;
L
Linus Torvalds 已提交
405 406 407 408 409 410 411 412

	if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT))
		return -ENXIO;

	if (!access_ok(VERIFY_WRITE, buf, count))
		return -EFAULT;

	/* check client structures are in place */
413 414
	if (snd_BUG_ON(!client))
		return -ENXIO;
L
Linus Torvalds 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

	if (!client->accept_input || (fifo = client->data.user.fifo) == NULL)
		return -ENXIO;

	if (atomic_read(&fifo->overflow) > 0) {
		/* buffer overflow is detected */
		snd_seq_fifo_clear(fifo);
		/* return error code */
		return -ENOSPC;
	}

	cell = NULL;
	err = 0;
	snd_seq_fifo_lock(fifo);

	/* while data available in queue */
431
	while (count >= sizeof(struct snd_seq_event)) {
L
Linus Torvalds 已提交
432 433 434 435 436 437 438
		int nonblock;

		nonblock = (file->f_flags & O_NONBLOCK) || result > 0;
		if ((err = snd_seq_fifo_cell_out(fifo, &cell, nonblock)) < 0) {
			break;
		}
		if (snd_seq_ev_is_variable(&cell->event)) {
439
			struct snd_seq_event tmpev;
L
Linus Torvalds 已提交
440 441
			tmpev = cell->event;
			tmpev.data.ext.len &= ~SNDRV_SEQ_EXT_MASK;
442
			if (copy_to_user(buf, &tmpev, sizeof(struct snd_seq_event))) {
L
Linus Torvalds 已提交
443 444 445
				err = -EFAULT;
				break;
			}
446 447
			count -= sizeof(struct snd_seq_event);
			buf += sizeof(struct snd_seq_event);
448 449
			err = snd_seq_expand_var_event(&cell->event, count,
						       (char __force *)buf, 0,
450
						       sizeof(struct snd_seq_event));
L
Linus Torvalds 已提交
451 452 453 454 455 456
			if (err < 0)
				break;
			result += err;
			count -= err;
			buf += err;
		} else {
457
			if (copy_to_user(buf, &cell->event, sizeof(struct snd_seq_event))) {
L
Linus Torvalds 已提交
458 459 460
				err = -EFAULT;
				break;
			}
461 462
			count -= sizeof(struct snd_seq_event);
			buf += sizeof(struct snd_seq_event);
L
Linus Torvalds 已提交
463 464 465
		}
		snd_seq_cell_free(cell);
		cell = NULL; /* to be sure */
466
		result += sizeof(struct snd_seq_event);
L
Linus Torvalds 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	}

	if (err < 0) {
		if (cell)
			snd_seq_fifo_cell_putback(fifo, cell);
		if (err == -EAGAIN && result > 0)
			err = 0;
	}
	snd_seq_fifo_unlock(fifo);

	return (err < 0) ? err : result;
}


/*
 * check access permission to the port
 */
484
static int check_port_perm(struct snd_seq_client_port *port, unsigned int flags)
L
Linus Torvalds 已提交
485 486 487 488 489 490 491 492 493 494
{
	if ((port->capability & flags) != flags)
		return 0;
	return flags;
}

/*
 * check if the destination client is available, and return the pointer
 * if filter is non-zero, client filter bitmap is tested.
 */
495 496
static struct snd_seq_client *get_event_dest_client(struct snd_seq_event *event,
						    int filter)
L
Linus Torvalds 已提交
497
{
498
	struct snd_seq_client *dest;
L
Linus Torvalds 已提交
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528

	dest = snd_seq_client_use_ptr(event->dest.client);
	if (dest == NULL)
		return NULL;
	if (! dest->accept_input)
		goto __not_avail;
	if ((dest->filter & SNDRV_SEQ_FILTER_USE_EVENT) &&
	    ! test_bit(event->type, dest->event_filter))
		goto __not_avail;
	if (filter && !(dest->filter & filter))
		goto __not_avail;

	return dest; /* ok - accessible */
__not_avail:
	snd_seq_client_unlock(dest);
	return NULL;
}


/*
 * Return the error event.
 *
 * If the receiver client is a user client, the original event is
 * encapsulated in SNDRV_SEQ_EVENT_BOUNCE as variable length event.  If
 * the original event is also variable length, the external data is
 * copied after the event record. 
 * If the receiver client is a kernel client, the original event is
 * quoted in SNDRV_SEQ_EVENT_KERNEL_ERROR, since this requires no extra
 * kmalloc.
 */
529 530
static int bounce_error_event(struct snd_seq_client *client,
			      struct snd_seq_event *event,
L
Linus Torvalds 已提交
531 532
			      int err, int atomic, int hop)
{
533
	struct snd_seq_event bounce_ev;
L
Linus Torvalds 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
	int result;

	if (client == NULL ||
	    ! (client->filter & SNDRV_SEQ_FILTER_BOUNCE) ||
	    ! client->accept_input)
		return 0; /* ignored */

	/* set up quoted error */
	memset(&bounce_ev, 0, sizeof(bounce_ev));
	bounce_ev.type = SNDRV_SEQ_EVENT_KERNEL_ERROR;
	bounce_ev.flags = SNDRV_SEQ_EVENT_LENGTH_FIXED;
	bounce_ev.queue = SNDRV_SEQ_QUEUE_DIRECT;
	bounce_ev.source.client = SNDRV_SEQ_CLIENT_SYSTEM;
	bounce_ev.source.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
	bounce_ev.dest.client = client->number;
	bounce_ev.dest.port = event->source.port;
	bounce_ev.data.quote.origin = event->dest;
	bounce_ev.data.quote.event = event;
	bounce_ev.data.quote.value = -err; /* use positive value */
	result = snd_seq_deliver_single_event(NULL, &bounce_ev, 0, atomic, hop + 1);
	if (result < 0) {
		client->event_lost++;
		return result;
	}

	return result;
}


/*
 * rewrite the time-stamp of the event record with the curren time
 * of the given queue.
 * return non-zero if updated.
 */
568 569
static int update_timestamp_of_queue(struct snd_seq_event *event,
				     int queue, int real_time)
L
Linus Torvalds 已提交
570
{
571
	struct snd_seq_queue *q;
L
Linus Torvalds 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596

	q = queueptr(queue);
	if (! q)
		return 0;
	event->queue = queue;
	event->flags &= ~SNDRV_SEQ_TIME_STAMP_MASK;
	if (real_time) {
		event->time.time = snd_seq_timer_get_cur_time(q->timer);
		event->flags |= SNDRV_SEQ_TIME_STAMP_REAL;
	} else {
		event->time.tick = snd_seq_timer_get_cur_tick(q->timer);
		event->flags |= SNDRV_SEQ_TIME_STAMP_TICK;
	}
	queuefree(q);
	return 1;
}


/*
 * deliver an event to the specified destination.
 * if filter is non-zero, client filter bitmap is tested.
 *
 *  RETURN VALUE: 0 : if succeeded
 *		 <0 : error
 */
597 598
static int snd_seq_deliver_single_event(struct snd_seq_client *client,
					struct snd_seq_event *event,
L
Linus Torvalds 已提交
599 600
					int filter, int atomic, int hop)
{
601 602
	struct snd_seq_client *dest = NULL;
	struct snd_seq_client_port *dest_port = NULL;
L
Linus Torvalds 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
	int result = -ENOENT;
	int direct;

	direct = snd_seq_ev_is_direct(event);

	dest = get_event_dest_client(event, filter);
	if (dest == NULL)
		goto __skip;
	dest_port = snd_seq_port_use_ptr(dest, event->dest.port);
	if (dest_port == NULL)
		goto __skip;

	/* check permission */
	if (! check_port_perm(dest_port, SNDRV_SEQ_PORT_CAP_WRITE)) {
		result = -EPERM;
		goto __skip;
	}
		
	if (dest_port->timestamping)
		update_timestamp_of_queue(event, dest_port->time_queue,
					  dest_port->time_real);

	switch (dest->type) {
	case USER_CLIENT:
		if (dest->data.user.fifo)
			result = snd_seq_fifo_event_in(dest->data.user.fifo, event);
		break;

	case KERNEL_CLIENT:
		if (dest_port->event_input == NULL)
			break;
634 635 636
		result = dest_port->event_input(event, direct,
						dest_port->private_data,
						atomic, hop);
L
Linus Torvalds 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
		break;
	default:
		break;
	}

  __skip:
	if (dest_port)
		snd_seq_port_unlock(dest_port);
	if (dest)
		snd_seq_client_unlock(dest);

	if (result < 0 && !direct) {
		result = bounce_error_event(client, event, result, atomic, hop);
	}
	return result;
}


/*
 * send the event to all subscribers:
 */
658 659
static int deliver_to_subscribers(struct snd_seq_client *client,
				  struct snd_seq_event *event,
L
Linus Torvalds 已提交
660 661
				  int atomic, int hop)
{
662
	struct snd_seq_subscribers *subs;
663
	int err, result = 0, num_ev = 0;
664 665 666
	struct snd_seq_event event_saved;
	struct snd_seq_client_port *src_port;
	struct snd_seq_port_subs_info *grp;
L
Linus Torvalds 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679

	src_port = snd_seq_port_use_ptr(client, event->source.port);
	if (src_port == NULL)
		return -EINVAL; /* invalid source port */
	/* save original event record */
	event_saved = *event;
	grp = &src_port->c_src;
	
	/* lock list */
	if (atomic)
		read_lock(&grp->list_lock);
	else
		down_read(&grp->list_mutex);
680
	list_for_each_entry(subs, &grp->list_head, src_list) {
L
Linus Torvalds 已提交
681 682 683 684 685 686 687
		event->dest = subs->info.dest;
		if (subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)
			/* convert time according to flag with subscription */
			update_timestamp_of_queue(event, subs->info.queue,
						  subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL);
		err = snd_seq_deliver_single_event(client, event,
						   0, atomic, hop);
688 689 690 691 692 693
		if (err < 0) {
			/* save first error that occurs and continue */
			if (!result)
				result = err;
			continue;
		}
L
Linus Torvalds 已提交
694 695 696 697 698 699 700 701 702 703
		num_ev++;
		/* restore original event record */
		*event = event_saved;
	}
	if (atomic)
		read_unlock(&grp->list_lock);
	else
		up_read(&grp->list_mutex);
	*event = event_saved; /* restore */
	snd_seq_port_unlock(src_port);
704
	return (result < 0) ? result : num_ev;
L
Linus Torvalds 已提交
705 706 707 708 709 710 711
}


#ifdef SUPPORT_BROADCAST 
/*
 * broadcast to all ports:
 */
712 713
static int port_broadcast_event(struct snd_seq_client *client,
				struct snd_seq_event *event,
L
Linus Torvalds 已提交
714 715
				int atomic, int hop)
{
716
	int num_ev = 0, err, result = 0;
717
	struct snd_seq_client *dest_client;
718
	struct snd_seq_client_port *port;
L
Linus Torvalds 已提交
719 720 721 722 723 724

	dest_client = get_event_dest_client(event, SNDRV_SEQ_FILTER_BROADCAST);
	if (dest_client == NULL)
		return 0; /* no matching destination */

	read_lock(&dest_client->ports_lock);
725
	list_for_each_entry(port, &dest_client->ports_list_head, list) {
L
Linus Torvalds 已提交
726 727 728 729 730
		event->dest.port = port->addr.port;
		/* pass NULL as source client to avoid error bounce */
		err = snd_seq_deliver_single_event(NULL, event,
						   SNDRV_SEQ_FILTER_BROADCAST,
						   atomic, hop);
731 732 733 734 735 736
		if (err < 0) {
			/* save first error that occurs and continue */
			if (!result)
				result = err;
			continue;
		}
L
Linus Torvalds 已提交
737 738 739 740 741
		num_ev++;
	}
	read_unlock(&dest_client->ports_lock);
	snd_seq_client_unlock(dest_client);
	event->dest.port = SNDRV_SEQ_ADDRESS_BROADCAST; /* restore */
742
	return (result < 0) ? result : num_ev;
L
Linus Torvalds 已提交
743 744 745 746 747 748
}

/*
 * send the event to all clients:
 * if destination port is also ADDRESS_BROADCAST, deliver to all ports.
 */
749 750
static int broadcast_event(struct snd_seq_client *client,
			   struct snd_seq_event *event, int atomic, int hop)
L
Linus Torvalds 已提交
751
{
752
	int err, result = 0, num_ev = 0;
L
Linus Torvalds 已提交
753
	int dest;
754
	struct snd_seq_addr addr;
L
Linus Torvalds 已提交
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770

	addr = event->dest; /* save */

	for (dest = 0; dest < SNDRV_SEQ_MAX_CLIENTS; dest++) {
		/* don't send to itself */
		if (dest == client->number)
			continue;
		event->dest.client = dest;
		event->dest.port = addr.port;
		if (addr.port == SNDRV_SEQ_ADDRESS_BROADCAST)
			err = port_broadcast_event(client, event, atomic, hop);
		else
			/* pass NULL as source client to avoid error bounce */
			err = snd_seq_deliver_single_event(NULL, event,
							   SNDRV_SEQ_FILTER_BROADCAST,
							   atomic, hop);
771 772 773 774 775 776
		if (err < 0) {
			/* save first error that occurs and continue */
			if (!result)
				result = err;
			continue;
		}
L
Linus Torvalds 已提交
777 778 779
		num_ev += err;
	}
	event->dest = addr; /* restore */
780
	return (result < 0) ? result : num_ev;
L
Linus Torvalds 已提交
781 782 783 784
}


/* multicast - not supported yet */
785
static int multicast_event(struct snd_seq_client *client, struct snd_seq_event *event,
L
Linus Torvalds 已提交
786 787
			   int atomic, int hop)
{
788
	pr_debug("ALSA: seq: multicast not supported yet.\n");
L
Linus Torvalds 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801
	return 0; /* ignored */
}
#endif /* SUPPORT_BROADCAST */


/* deliver an event to the destination port(s).
 * if the event is to subscribers or broadcast, the event is dispatched
 * to multiple targets.
 *
 * RETURN VALUE: n > 0  : the number of delivered events.
 *               n == 0 : the event was not passed to any client.
 *               n < 0  : error - event was not processed.
 */
802
static int snd_seq_deliver_event(struct snd_seq_client *client, struct snd_seq_event *event,
L
Linus Torvalds 已提交
803 804 805 806 807 808
				 int atomic, int hop)
{
	int result;

	hop++;
	if (hop >= SNDRV_SEQ_MAX_HOPS) {
809
		pr_debug("ALSA: seq: too long delivery path (%d:%d->%d:%d)\n",
L
Linus Torvalds 已提交
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
			   event->source.client, event->source.port,
			   event->dest.client, event->dest.port);
		return -EMLINK;
	}

	if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS ||
	    event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS)
		result = deliver_to_subscribers(client, event, atomic, hop);
#ifdef SUPPORT_BROADCAST
	else if (event->queue == SNDRV_SEQ_ADDRESS_BROADCAST ||
		 event->dest.client == SNDRV_SEQ_ADDRESS_BROADCAST)
		result = broadcast_event(client, event, atomic, hop);
	else if (event->dest.client >= SNDRV_SEQ_MAX_CLIENTS)
		result = multicast_event(client, event, atomic, hop);
	else if (event->dest.port == SNDRV_SEQ_ADDRESS_BROADCAST)
		result = port_broadcast_event(client, event, atomic, hop);
#endif
	else
		result = snd_seq_deliver_single_event(client, event, 0, atomic, hop);

	return result;
}

/*
 * dispatch an event cell:
 * This function is called only from queue check routines in timer
 * interrupts or after enqueued.
 * The event cell shall be released or re-queued in this function.
 *
 * RETURN VALUE: n > 0  : the number of delivered events.
 *		 n == 0 : the event was not passed to any client.
 *		 n < 0  : error - event was not processed.
 */
843
int snd_seq_dispatch_event(struct snd_seq_event_cell *cell, int atomic, int hop)
L
Linus Torvalds 已提交
844
{
845
	struct snd_seq_client *client;
L
Linus Torvalds 已提交
846 847
	int result;

848 849
	if (snd_BUG_ON(!cell))
		return -EINVAL;
L
Linus Torvalds 已提交
850 851 852 853 854 855 856 857 858 859 860 861

	client = snd_seq_client_use_ptr(cell->event.source.client);
	if (client == NULL) {
		snd_seq_cell_free(cell); /* release this cell */
		return -EINVAL;
	}

	if (cell->event.type == SNDRV_SEQ_EVENT_NOTE) {
		/* NOTE event:
		 * the event cell is re-used as a NOTE-OFF event and
		 * enqueued again.
		 */
862
		struct snd_seq_event tmpev, *ev;
L
Linus Torvalds 已提交
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914

		/* reserve this event to enqueue note-off later */
		tmpev = cell->event;
		tmpev.type = SNDRV_SEQ_EVENT_NOTEON;
		result = snd_seq_deliver_event(client, &tmpev, atomic, hop);

		/*
		 * This was originally a note event.  We now re-use the
		 * cell for the note-off event.
		 */

		ev = &cell->event;
		ev->type = SNDRV_SEQ_EVENT_NOTEOFF;
		ev->flags |= SNDRV_SEQ_PRIORITY_HIGH;

		/* add the duration time */
		switch (ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) {
		case SNDRV_SEQ_TIME_STAMP_TICK:
			ev->time.tick += ev->data.note.duration;
			break;
		case SNDRV_SEQ_TIME_STAMP_REAL:
			/* unit for duration is ms */
			ev->time.time.tv_nsec += 1000000 * (ev->data.note.duration % 1000);
			ev->time.time.tv_sec += ev->data.note.duration / 1000 +
						ev->time.time.tv_nsec / 1000000000;
			ev->time.time.tv_nsec %= 1000000000;
			break;
		}
		ev->data.note.velocity = ev->data.note.off_velocity;

		/* Now queue this cell as the note off event */
		if (snd_seq_enqueue_event(cell, atomic, hop) < 0)
			snd_seq_cell_free(cell); /* release this cell */

	} else {
		/* Normal events:
		 * event cell is freed after processing the event
		 */

		result = snd_seq_deliver_event(client, &cell->event, atomic, hop);
		snd_seq_cell_free(cell);
	}

	snd_seq_client_unlock(client);
	return result;
}


/* Allocate a cell from client pool and enqueue it to queue:
 * if pool is empty and blocking is TRUE, sleep until a new cell is
 * available.
 */
915 916
static int snd_seq_client_enqueue_event(struct snd_seq_client *client,
					struct snd_seq_event *event,
L
Linus Torvalds 已提交
917 918 919
					struct file *file, int blocking,
					int atomic, int hop)
{
920
	struct snd_seq_event_cell *cell;
L
Linus Torvalds 已提交
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
	int err;

	/* special queue values - force direct passing */
	if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) {
		event->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
		event->queue = SNDRV_SEQ_QUEUE_DIRECT;
	} else
#ifdef SUPPORT_BROADCAST
		if (event->queue == SNDRV_SEQ_ADDRESS_BROADCAST) {
			event->dest.client = SNDRV_SEQ_ADDRESS_BROADCAST;
			event->queue = SNDRV_SEQ_QUEUE_DIRECT;
		}
#endif
	if (event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) {
		/* check presence of source port */
936
		struct snd_seq_client_port *src_port = snd_seq_port_use_ptr(client, event->source.port);
L
Linus Torvalds 已提交
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
		if (src_port == NULL)
			return -EINVAL;
		snd_seq_port_unlock(src_port);
	}

	/* direct event processing without enqueued */
	if (snd_seq_ev_is_direct(event)) {
		if (event->type == SNDRV_SEQ_EVENT_NOTE)
			return -EINVAL; /* this event must be enqueued! */
		return snd_seq_deliver_event(client, event, atomic, hop);
	}

	/* Not direct, normal queuing */
	if (snd_seq_queue_is_used(event->queue, client->number) <= 0)
		return -EINVAL;  /* invalid queue */
	if (! snd_seq_write_pool_allocated(client))
		return -ENXIO; /* queue is not allocated */

	/* allocate an event cell */
	err = snd_seq_event_dup(client->pool, event, &cell, !blocking || atomic, file);
	if (err < 0)
		return err;

	/* we got a cell. enqueue it. */
	if ((err = snd_seq_enqueue_event(cell, atomic, hop)) < 0) {
		snd_seq_cell_free(cell);
		return err;
	}

	return 0;
}


/*
 * check validity of event type and data length.
 * return non-zero if invalid.
 */
974
static int check_event_type_and_length(struct snd_seq_event *ev)
L
Linus Torvalds 已提交
975 976 977 978 979 980 981 982 983 984 985 986
{
	switch (snd_seq_ev_length_type(ev)) {
	case SNDRV_SEQ_EVENT_LENGTH_FIXED:
		if (snd_seq_ev_is_variable_type(ev))
			return -EINVAL;
		break;
	case SNDRV_SEQ_EVENT_LENGTH_VARIABLE:
		if (! snd_seq_ev_is_variable_type(ev) ||
		    (ev->data.ext.len & ~SNDRV_SEQ_EXT_MASK) >= SNDRV_SEQ_MAX_EVENT_LEN)
			return -EINVAL;
		break;
	case SNDRV_SEQ_EVENT_LENGTH_VARUSR:
987
		if (! snd_seq_ev_is_direct(ev))
L
Linus Torvalds 已提交
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
			return -EINVAL;
		break;
	}
	return 0;
}


/* handle write() */
/* possible error values:
 *	-ENXIO	invalid client or file open mode
 *	-ENOMEM	malloc failed
 *	-EFAULT	seg. fault during copy from user space
 *	-EINVAL	invalid event
 *	-EAGAIN	no space in output pool
 *	-EINTR	interrupts while sleep
 *	-EMLINK	too many hops
 *	others	depends on return value from driver callback
 */
1006 1007
static ssize_t snd_seq_write(struct file *file, const char __user *buf,
			     size_t count, loff_t *offset)
L
Linus Torvalds 已提交
1008
{
1009
	struct snd_seq_client *client = file->private_data;
L
Linus Torvalds 已提交
1010 1011
	int written = 0, len;
	int err = -EINVAL;
1012
	struct snd_seq_event event;
L
Linus Torvalds 已提交
1013 1014 1015 1016 1017

	if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT))
		return -ENXIO;

	/* check client structures are in place */
1018 1019
	if (snd_BUG_ON(!client))
		return -ENXIO;
L
Linus Torvalds 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
		
	if (!client->accept_output || client->pool == NULL)
		return -ENXIO;

	/* allocate the pool now if the pool is not allocated yet */ 
	if (client->pool->size > 0 && !snd_seq_write_pool_allocated(client)) {
		if (snd_seq_pool_init(client->pool) < 0)
			return -ENOMEM;
	}

	/* only process whole events */
1031
	while (count >= sizeof(struct snd_seq_event)) {
L
Linus Torvalds 已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
		/* Read in the event header from the user */
		len = sizeof(event);
		if (copy_from_user(&event, buf, len)) {
			err = -EFAULT;
			break;
		}
		event.source.client = client->number;	/* fill in client number */
		/* Check for extension data length */
		if (check_event_type_and_length(&event)) {
			err = -EINVAL;
			break;
		}

		/* check for special events */
		if (event.type == SNDRV_SEQ_EVENT_NONE)
			goto __skip_event;
		else if (snd_seq_ev_is_reserved(&event)) {
			err = -EINVAL;
			break;
		}

		if (snd_seq_ev_is_variable(&event)) {
			int extlen = event.data.ext.len & ~SNDRV_SEQ_EXT_MASK;
			if ((size_t)(extlen + len) > count) {
				/* back out, will get an error this time or next */
				err = -EINVAL;
				break;
			}
			/* set user space pointer */
			event.data.ext.len = extlen | SNDRV_SEQ_EXT_USRPTR;
1062
			event.data.ext.ptr = (char __force *)buf
1063
						+ sizeof(struct snd_seq_event);
L
Linus Torvalds 已提交
1064 1065 1066 1067
			len += extlen; /* increment data length */
		} else {
#ifdef CONFIG_COMPAT
			if (client->convert32 && snd_seq_ev_is_varusr(&event)) {
C
Clemens Ladisch 已提交
1068
				void *ptr = (void __force *)compat_ptr(event.data.raw32.d[1]);
L
Linus Torvalds 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
				event.data.ext.ptr = ptr;
			}
#endif
		}

		/* ok, enqueue it */
		err = snd_seq_client_enqueue_event(client, &event, file,
						   !(file->f_flags & O_NONBLOCK),
						   0, 0);
		if (err < 0)
			break;

	__skip_event:
		/* Update pointers and counts */
		count -= len;
		buf += len;
		written += len;
	}

	return written ? written : err;
}


/*
 * handle polling
 */
static unsigned int snd_seq_poll(struct file *file, poll_table * wait)
{
1097
	struct snd_seq_client *client = file->private_data;
L
Linus Torvalds 已提交
1098 1099 1100
	unsigned int mask = 0;

	/* check client structures are in place */
1101 1102
	if (snd_BUG_ON(!client))
		return -ENXIO;
L
Linus Torvalds 已提交
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127

	if ((snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT) &&
	    client->data.user.fifo) {

		/* check if data is available in the outqueue */
		if (snd_seq_fifo_poll_wait(client->data.user.fifo, file, wait))
			mask |= POLLIN | POLLRDNORM;
	}

	if (snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT) {

		/* check if data is available in the pool */
		if (!snd_seq_write_pool_allocated(client) ||
		    snd_seq_pool_poll_wait(client->pool, file, wait))
			mask |= POLLOUT | POLLWRNORM;
	}

	return mask;
}


/*-----------------------------------------------------*/


/* SYSTEM_INFO ioctl() */
1128
static int snd_seq_ioctl_system_info(struct snd_seq_client *client, void __user *arg)
L
Linus Torvalds 已提交
1129
{
1130
	struct snd_seq_system_info info;
L
Linus Torvalds 已提交
1131 1132 1133 1134 1135

	memset(&info, 0, sizeof(info));
	/* fill the info fields */
	info.queues = SNDRV_SEQ_MAX_QUEUES;
	info.clients = SNDRV_SEQ_MAX_CLIENTS;
1136
	info.ports = SNDRV_SEQ_MAX_PORTS;
L
Linus Torvalds 已提交
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
	info.channels = 256;	/* fixed limit */
	info.cur_clients = client_usage.cur;
	info.cur_queues = snd_seq_queue_get_cur_queues();

	if (copy_to_user(arg, &info, sizeof(info)))
		return -EFAULT;
	return 0;
}


/* RUNNING_MODE ioctl() */
1148
static int snd_seq_ioctl_running_mode(struct snd_seq_client *client, void __user *arg)
L
Linus Torvalds 已提交
1149
{
1150 1151
	struct snd_seq_running_info info;
	struct snd_seq_client *cptr;
L
Linus Torvalds 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
	int err = 0;

	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;

	/* requested client number */
	cptr = snd_seq_client_use_ptr(info.client);
	if (cptr == NULL)
		return -ENOENT;		/* don't change !!! */

#ifdef SNDRV_BIG_ENDIAN
	if (! info.big_endian) {
		err = -EINVAL;
		goto __err;
	}
#else
	if (info.big_endian) {
		err = -EINVAL;
		goto __err;
	}

#endif
	if (info.cpu_mode > sizeof(long)) {
		err = -EINVAL;
		goto __err;
	}
	cptr->convert32 = (info.cpu_mode < sizeof(long));
 __err:
	snd_seq_client_unlock(cptr);
	return err;
}

/* CLIENT_INFO ioctl() */
1185 1186
static void get_client_info(struct snd_seq_client *cptr,
			    struct snd_seq_client_info *info)
L
Linus Torvalds 已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
{
	info->client = cptr->number;

	/* fill the info fields */
	info->type = cptr->type;
	strcpy(info->name, cptr->name);
	info->filter = cptr->filter;
	info->event_lost = cptr->event_lost;
	memcpy(info->event_filter, cptr->event_filter, 32);
	info->num_ports = cptr->num_ports;
	memset(info->reserved, 0, sizeof(info->reserved));
}

1200 1201
static int snd_seq_ioctl_get_client_info(struct snd_seq_client *client,
					 void __user *arg)
L
Linus Torvalds 已提交
1202
{
1203 1204
	struct snd_seq_client *cptr;
	struct snd_seq_client_info client_info;
L
Linus Torvalds 已提交
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223

	if (copy_from_user(&client_info, arg, sizeof(client_info)))
		return -EFAULT;

	/* requested client number */
	cptr = snd_seq_client_use_ptr(client_info.client);
	if (cptr == NULL)
		return -ENOENT;		/* don't change !!! */

	get_client_info(cptr, &client_info);
	snd_seq_client_unlock(cptr);

	if (copy_to_user(arg, &client_info, sizeof(client_info)))
		return -EFAULT;
	return 0;
}


/* CLIENT_INFO ioctl() */
1224 1225
static int snd_seq_ioctl_set_client_info(struct snd_seq_client *client,
					 void __user *arg)
L
Linus Torvalds 已提交
1226
{
1227
	struct snd_seq_client_info client_info;
L
Linus Torvalds 已提交
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253

	if (copy_from_user(&client_info, arg, sizeof(client_info)))
		return -EFAULT;

	/* it is not allowed to set the info fields for an another client */
	if (client->number != client_info.client)
		return -EPERM;
	/* also client type must be set now */
	if (client->type != client_info.type)
		return -EINVAL;

	/* fill the info fields */
	if (client_info.name[0])
		strlcpy(client->name, client_info.name, sizeof(client->name));

	client->filter = client_info.filter;
	client->event_lost = client_info.event_lost;
	memcpy(client->event_filter, client_info.event_filter, 32);

	return 0;
}


/* 
 * CREATE PORT ioctl() 
 */
1254 1255
static int snd_seq_ioctl_create_port(struct snd_seq_client *client,
				     void __user *arg)
L
Linus Torvalds 已提交
1256
{
1257 1258 1259
	struct snd_seq_client_port *port;
	struct snd_seq_port_info info;
	struct snd_seq_port_callback *callback;
L
Linus Torvalds 已提交
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303

	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;

	/* it is not allowed to create the port for an another client */
	if (info.addr.client != client->number)
		return -EPERM;

	port = snd_seq_create_port(client, (info.flags & SNDRV_SEQ_PORT_FLG_GIVEN_PORT) ? info.addr.port : -1);
	if (port == NULL)
		return -ENOMEM;

	if (client->type == USER_CLIENT && info.kernel) {
		snd_seq_delete_port(client, port->addr.port);
		return -EINVAL;
	}
	if (client->type == KERNEL_CLIENT) {
		if ((callback = info.kernel) != NULL) {
			if (callback->owner)
				port->owner = callback->owner;
			port->private_data = callback->private_data;
			port->private_free = callback->private_free;
			port->event_input = callback->event_input;
			port->c_src.open = callback->subscribe;
			port->c_src.close = callback->unsubscribe;
			port->c_dest.open = callback->use;
			port->c_dest.close = callback->unuse;
		}
	}

	info.addr = port->addr;

	snd_seq_set_port_info(port, &info);
	snd_seq_system_client_ev_port_start(port->addr.client, port->addr.port);

	if (copy_to_user(arg, &info, sizeof(info)))
		return -EFAULT;

	return 0;
}

/* 
 * DELETE PORT ioctl() 
 */
1304 1305
static int snd_seq_ioctl_delete_port(struct snd_seq_client *client,
				     void __user *arg)
L
Linus Torvalds 已提交
1306
{
1307
	struct snd_seq_port_info info;
L
Linus Torvalds 已提交
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
	int err;

	/* set passed parameters */
	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;
	
	/* it is not allowed to remove the port for an another client */
	if (info.addr.client != client->number)
		return -EPERM;

	err = snd_seq_delete_port(client, info.addr.port);
	if (err >= 0)
		snd_seq_system_client_ev_port_exit(client->number, info.addr.port);
	return err;
}


/* 
 * GET_PORT_INFO ioctl() (on any client) 
 */
1328 1329
static int snd_seq_ioctl_get_port_info(struct snd_seq_client *client,
				       void __user *arg)
L
Linus Torvalds 已提交
1330
{
1331 1332 1333
	struct snd_seq_client *cptr;
	struct snd_seq_client_port *port;
	struct snd_seq_port_info info;
L
Linus Torvalds 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360

	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;
	cptr = snd_seq_client_use_ptr(info.addr.client);
	if (cptr == NULL)
		return -ENXIO;

	port = snd_seq_port_use_ptr(cptr, info.addr.port);
	if (port == NULL) {
		snd_seq_client_unlock(cptr);
		return -ENOENT;			/* don't change */
	}

	/* get port info */
	snd_seq_get_port_info(port, &info);
	snd_seq_port_unlock(port);
	snd_seq_client_unlock(cptr);

	if (copy_to_user(arg, &info, sizeof(info)))
		return -EFAULT;
	return 0;
}


/* 
 * SET_PORT_INFO ioctl() (only ports on this/own client) 
 */
1361 1362
static int snd_seq_ioctl_set_port_info(struct snd_seq_client *client,
				       void __user *arg)
L
Linus Torvalds 已提交
1363
{
1364 1365
	struct snd_seq_client_port *port;
	struct snd_seq_port_info info;
L
Linus Torvalds 已提交
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386

	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;

	if (info.addr.client != client->number) /* only set our own ports ! */
		return -EPERM;
	port = snd_seq_port_use_ptr(client, info.addr.port);
	if (port) {
		snd_seq_set_port_info(port, &info);
		snd_seq_port_unlock(port);
	}
	return 0;
}


/*
 * port subscription (connection)
 */
#define PERM_RD		(SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ)
#define PERM_WR		(SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_SUBS_WRITE)

1387 1388 1389 1390
static int check_subscription_permission(struct snd_seq_client *client,
					 struct snd_seq_client_port *sport,
					 struct snd_seq_client_port *dport,
					 struct snd_seq_port_subscribe *subs)
L
Linus Torvalds 已提交
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
{
	if (client->number != subs->sender.client &&
	    client->number != subs->dest.client) {
		/* connection by third client - check export permission */
		if (check_port_perm(sport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))
			return -EPERM;
		if (check_port_perm(dport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))
			return -EPERM;
	}

	/* check read permission */
	/* if sender or receiver is the subscribing client itself,
	 * no permission check is necessary
	 */
	if (client->number != subs->sender.client) {
		if (! check_port_perm(sport, PERM_RD))
			return -EPERM;
	}
	/* check write permission */
	if (client->number != subs->dest.client) {
		if (! check_port_perm(dport, PERM_WR))
			return -EPERM;
	}
	return 0;
}

/*
 * send an subscription notify event to user client:
 * client must be user client.
 */
int snd_seq_client_notify_subscription(int client, int port,
1422 1423
				       struct snd_seq_port_subscribe *info,
				       int evtype)
L
Linus Torvalds 已提交
1424
{
1425
	struct snd_seq_event event;
L
Linus Torvalds 已提交
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438

	memset(&event, 0, sizeof(event));
	event.type = evtype;
	event.data.connect.dest = info->dest;
	event.data.connect.sender = info->sender;

	return snd_seq_system_notify(client, port, &event);  /* non-atomic */
}


/* 
 * add to port's subscription list IOCTL interface 
 */
1439 1440
static int snd_seq_ioctl_subscribe_port(struct snd_seq_client *client,
					void __user *arg)
L
Linus Torvalds 已提交
1441 1442
{
	int result = -EINVAL;
1443 1444 1445
	struct snd_seq_client *receiver = NULL, *sender = NULL;
	struct snd_seq_client_port *sport = NULL, *dport = NULL;
	struct snd_seq_port_subscribe subs;
L
Linus Torvalds 已提交
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483

	if (copy_from_user(&subs, arg, sizeof(subs)))
		return -EFAULT;

	if ((receiver = snd_seq_client_use_ptr(subs.dest.client)) == NULL)
		goto __end;
	if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)
		goto __end;
	if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)
		goto __end;
	if ((dport = snd_seq_port_use_ptr(receiver, subs.dest.port)) == NULL)
		goto __end;

	result = check_subscription_permission(client, sport, dport, &subs);
	if (result < 0)
		goto __end;

	/* connect them */
	result = snd_seq_port_connect(client, sender, sport, receiver, dport, &subs);
	if (! result) /* broadcast announce */
		snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,
						   &subs, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
      __end:
      	if (sport)
		snd_seq_port_unlock(sport);
	if (dport)
		snd_seq_port_unlock(dport);
	if (sender)
		snd_seq_client_unlock(sender);
	if (receiver)
		snd_seq_client_unlock(receiver);
	return result;
}


/* 
 * remove from port's subscription list 
 */
1484 1485
static int snd_seq_ioctl_unsubscribe_port(struct snd_seq_client *client,
					  void __user *arg)
L
Linus Torvalds 已提交
1486 1487
{
	int result = -ENXIO;
1488 1489 1490
	struct snd_seq_client *receiver = NULL, *sender = NULL;
	struct snd_seq_client_port *sport = NULL, *dport = NULL;
	struct snd_seq_port_subscribe subs;
L
Linus Torvalds 已提交
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525

	if (copy_from_user(&subs, arg, sizeof(subs)))
		return -EFAULT;

	if ((receiver = snd_seq_client_use_ptr(subs.dest.client)) == NULL)
		goto __end;
	if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)
		goto __end;
	if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)
		goto __end;
	if ((dport = snd_seq_port_use_ptr(receiver, subs.dest.port)) == NULL)
		goto __end;

	result = check_subscription_permission(client, sport, dport, &subs);
	if (result < 0)
		goto __end;

	result = snd_seq_port_disconnect(client, sender, sport, receiver, dport, &subs);
	if (! result) /* broadcast announce */
		snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,
						   &subs, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
      __end:
      	if (sport)
		snd_seq_port_unlock(sport);
	if (dport)
		snd_seq_port_unlock(dport);
	if (sender)
		snd_seq_client_unlock(sender);
	if (receiver)
		snd_seq_client_unlock(receiver);
	return result;
}


/* CREATE_QUEUE ioctl() */
1526 1527
static int snd_seq_ioctl_create_queue(struct snd_seq_client *client,
				      void __user *arg)
L
Linus Torvalds 已提交
1528
{
1529
	struct snd_seq_queue_info info;
L
Linus Torvalds 已提交
1530
	int result;
1531
	struct snd_seq_queue *q;
L
Linus Torvalds 已提交
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560

	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;

	result = snd_seq_queue_alloc(client->number, info.locked, info.flags);
	if (result < 0)
		return result;

	q = queueptr(result);
	if (q == NULL)
		return -EINVAL;

	info.queue = q->queue;
	info.locked = q->locked;
	info.owner = q->owner;

	/* set queue name */
	if (! info.name[0])
		snprintf(info.name, sizeof(info.name), "Queue-%d", q->queue);
	strlcpy(q->name, info.name, sizeof(q->name));
	queuefree(q);

	if (copy_to_user(arg, &info, sizeof(info)))
		return -EFAULT;

	return 0;
}

/* DELETE_QUEUE ioctl() */
1561 1562
static int snd_seq_ioctl_delete_queue(struct snd_seq_client *client,
				      void __user *arg)
L
Linus Torvalds 已提交
1563
{
1564
	struct snd_seq_queue_info info;
L
Linus Torvalds 已提交
1565 1566 1567 1568 1569 1570 1571 1572

	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;

	return snd_seq_queue_delete(client->number, info.queue);
}

/* GET_QUEUE_INFO ioctl() */
1573 1574
static int snd_seq_ioctl_get_queue_info(struct snd_seq_client *client,
					void __user *arg)
L
Linus Torvalds 已提交
1575
{
1576 1577
	struct snd_seq_queue_info info;
	struct snd_seq_queue *q;
L
Linus Torvalds 已提交
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599

	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;

	q = queueptr(info.queue);
	if (q == NULL)
		return -EINVAL;

	memset(&info, 0, sizeof(info));
	info.queue = q->queue;
	info.owner = q->owner;
	info.locked = q->locked;
	strlcpy(info.name, q->name, sizeof(info.name));
	queuefree(q);

	if (copy_to_user(arg, &info, sizeof(info)))
		return -EFAULT;

	return 0;
}

/* SET_QUEUE_INFO ioctl() */
1600 1601
static int snd_seq_ioctl_set_queue_info(struct snd_seq_client *client,
					void __user *arg)
L
Linus Torvalds 已提交
1602
{
1603 1604
	struct snd_seq_queue_info info;
	struct snd_seq_queue *q;
L
Linus Torvalds 已提交
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635

	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;

	if (info.owner != client->number)
		return -EINVAL;

	/* change owner/locked permission */
	if (snd_seq_queue_check_access(info.queue, client->number)) {
		if (snd_seq_queue_set_owner(info.queue, client->number, info.locked) < 0)
			return -EPERM;
		if (info.locked)
			snd_seq_queue_use(info.queue, client->number, 1);
	} else {
		return -EPERM;
	}	

	q = queueptr(info.queue);
	if (! q)
		return -EINVAL;
	if (q->owner != client->number) {
		queuefree(q);
		return -EPERM;
	}
	strlcpy(q->name, info.name, sizeof(q->name));
	queuefree(q);

	return 0;
}

/* GET_NAMED_QUEUE ioctl() */
1636
static int snd_seq_ioctl_get_named_queue(struct snd_seq_client *client, void __user *arg)
L
Linus Torvalds 已提交
1637
{
1638 1639
	struct snd_seq_queue_info info;
	struct snd_seq_queue *q;
L
Linus Torvalds 已提交
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658

	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;

	q = snd_seq_queue_find_name(info.name);
	if (q == NULL)
		return -EINVAL;
	info.queue = q->queue;
	info.owner = q->owner;
	info.locked = q->locked;
	queuefree(q);

	if (copy_to_user(arg, &info, sizeof(info)))
		return -EFAULT;

	return 0;
}

/* GET_QUEUE_STATUS ioctl() */
1659 1660
static int snd_seq_ioctl_get_queue_status(struct snd_seq_client *client,
					  void __user *arg)
L
Linus Torvalds 已提交
1661
{
1662 1663 1664
	struct snd_seq_queue_status status;
	struct snd_seq_queue *queue;
	struct snd_seq_timer *tmr;
L
Linus Torvalds 已提交
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692

	if (copy_from_user(&status, arg, sizeof(status)))
		return -EFAULT;

	queue = queueptr(status.queue);
	if (queue == NULL)
		return -EINVAL;
	memset(&status, 0, sizeof(status));
	status.queue = queue->queue;
	
	tmr = queue->timer;
	status.events = queue->tickq->cells + queue->timeq->cells;

	status.time = snd_seq_timer_get_cur_time(tmr);
	status.tick = snd_seq_timer_get_cur_tick(tmr);

	status.running = tmr->running;

	status.flags = queue->flags;
	queuefree(queue);

	if (copy_to_user(arg, &status, sizeof(status)))
		return -EFAULT;
	return 0;
}


/* GET_QUEUE_TEMPO ioctl() */
1693 1694
static int snd_seq_ioctl_get_queue_tempo(struct snd_seq_client *client,
					 void __user *arg)
L
Linus Torvalds 已提交
1695
{
1696 1697 1698
	struct snd_seq_queue_tempo tempo;
	struct snd_seq_queue *queue;
	struct snd_seq_timer *tmr;
L
Linus Torvalds 已提交
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723

	if (copy_from_user(&tempo, arg, sizeof(tempo)))
		return -EFAULT;

	queue = queueptr(tempo.queue);
	if (queue == NULL)
		return -EINVAL;
	memset(&tempo, 0, sizeof(tempo));
	tempo.queue = queue->queue;
	
	tmr = queue->timer;

	tempo.tempo = tmr->tempo;
	tempo.ppq = tmr->ppq;
	tempo.skew_value = tmr->skew;
	tempo.skew_base = tmr->skew_base;
	queuefree(queue);

	if (copy_to_user(arg, &tempo, sizeof(tempo)))
		return -EFAULT;
	return 0;
}


/* SET_QUEUE_TEMPO ioctl() */
1724
int snd_seq_set_queue_tempo(int client, struct snd_seq_queue_tempo *tempo)
L
Linus Torvalds 已提交
1725 1726 1727 1728 1729 1730
{
	if (!snd_seq_queue_check_access(tempo->queue, client))
		return -EPERM;
	return snd_seq_queue_timer_set_tempo(tempo->queue, client, tempo);
}

1731 1732
EXPORT_SYMBOL(snd_seq_set_queue_tempo);

1733 1734
static int snd_seq_ioctl_set_queue_tempo(struct snd_seq_client *client,
					 void __user *arg)
L
Linus Torvalds 已提交
1735 1736
{
	int result;
1737
	struct snd_seq_queue_tempo tempo;
L
Linus Torvalds 已提交
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747

	if (copy_from_user(&tempo, arg, sizeof(tempo)))
		return -EFAULT;

	result = snd_seq_set_queue_tempo(client->number, &tempo);
	return result < 0 ? result : 0;
}


/* GET_QUEUE_TIMER ioctl() */
1748 1749
static int snd_seq_ioctl_get_queue_timer(struct snd_seq_client *client,
					 void __user *arg)
L
Linus Torvalds 已提交
1750
{
1751 1752 1753
	struct snd_seq_queue_timer timer;
	struct snd_seq_queue *queue;
	struct snd_seq_timer *tmr;
L
Linus Torvalds 已提交
1754 1755 1756 1757 1758 1759 1760 1761

	if (copy_from_user(&timer, arg, sizeof(timer)))
		return -EFAULT;

	queue = queueptr(timer.queue);
	if (queue == NULL)
		return -EINVAL;

1762
	if (mutex_lock_interruptible(&queue->timer_mutex)) {
L
Linus Torvalds 已提交
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
		queuefree(queue);
		return -ERESTARTSYS;
	}
	tmr = queue->timer;
	memset(&timer, 0, sizeof(timer));
	timer.queue = queue->queue;

	timer.type = tmr->type;
	if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
		timer.u.alsa.id = tmr->alsa_id;
		timer.u.alsa.resolution = tmr->preferred_resolution;
	}
1775
	mutex_unlock(&queue->timer_mutex);
L
Linus Torvalds 已提交
1776 1777 1778 1779 1780 1781 1782 1783 1784
	queuefree(queue);
	
	if (copy_to_user(arg, &timer, sizeof(timer)))
		return -EFAULT;
	return 0;
}


/* SET_QUEUE_TIMER ioctl() */
1785 1786
static int snd_seq_ioctl_set_queue_timer(struct snd_seq_client *client,
					 void __user *arg)
L
Linus Torvalds 已提交
1787 1788
{
	int result = 0;
1789
	struct snd_seq_queue_timer timer;
L
Linus Torvalds 已提交
1790 1791 1792 1793 1794 1795 1796 1797

	if (copy_from_user(&timer, arg, sizeof(timer)))
		return -EFAULT;

	if (timer.type != SNDRV_SEQ_TIMER_ALSA)
		return -EINVAL;

	if (snd_seq_queue_check_access(timer.queue, client->number)) {
1798 1799
		struct snd_seq_queue *q;
		struct snd_seq_timer *tmr;
L
Linus Torvalds 已提交
1800 1801 1802 1803

		q = queueptr(timer.queue);
		if (q == NULL)
			return -ENXIO;
1804
		if (mutex_lock_interruptible(&q->timer_mutex)) {
L
Linus Torvalds 已提交
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
			queuefree(q);
			return -ERESTARTSYS;
		}
		tmr = q->timer;
		snd_seq_queue_timer_close(timer.queue);
		tmr->type = timer.type;
		if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
			tmr->alsa_id = timer.u.alsa.id;
			tmr->preferred_resolution = timer.u.alsa.resolution;
		}
		result = snd_seq_queue_timer_open(timer.queue);
1816
		mutex_unlock(&q->timer_mutex);
L
Linus Torvalds 已提交
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
		queuefree(q);
	} else {
		return -EPERM;
	}	

	return result;
}


/* GET_QUEUE_CLIENT ioctl() */
1827 1828
static int snd_seq_ioctl_get_queue_client(struct snd_seq_client *client,
					  void __user *arg)
L
Linus Torvalds 已提交
1829
{
1830
	struct snd_seq_queue_client info;
L
Linus Torvalds 已提交
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
	int used;

	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;

	used = snd_seq_queue_is_used(info.queue, client->number);
	if (used < 0)
		return -EINVAL;
	info.used = used;
	info.client = client->number;

	if (copy_to_user(arg, &info, sizeof(info)))
		return -EFAULT;
	return 0;
}


/* SET_QUEUE_CLIENT ioctl() */
1849 1850
static int snd_seq_ioctl_set_queue_client(struct snd_seq_client *client,
					  void __user *arg)
L
Linus Torvalds 已提交
1851 1852
{
	int err;
1853
	struct snd_seq_queue_client info;
L
Linus Torvalds 已提交
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868

	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;

	if (info.used >= 0) {
		err = snd_seq_queue_use(info.queue, client->number, info.used);
		if (err < 0)
			return err;
	}

	return snd_seq_ioctl_get_queue_client(client, arg);
}


/* GET_CLIENT_POOL ioctl() */
1869 1870
static int snd_seq_ioctl_get_client_pool(struct snd_seq_client *client,
					 void __user *arg)
L
Linus Torvalds 已提交
1871
{
1872 1873
	struct snd_seq_client_pool info;
	struct snd_seq_client *cptr;
L
Linus Torvalds 已提交
1874 1875 1876 1877 1878 1879 1880 1881

	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;

	cptr = snd_seq_client_use_ptr(info.client);
	if (cptr == NULL)
		return -ENOENT;
	memset(&info, 0, sizeof(info));
1882
	info.client = cptr->number;
L
Linus Torvalds 已提交
1883 1884 1885
	info.output_pool = cptr->pool->size;
	info.output_room = cptr->pool->room;
	info.output_free = info.output_pool;
1886
	info.output_free = snd_seq_unused_cells(cptr->pool);
L
Linus Torvalds 已提交
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
	if (cptr->type == USER_CLIENT) {
		info.input_pool = cptr->data.user.fifo_pool_size;
		info.input_free = info.input_pool;
		if (cptr->data.user.fifo)
			info.input_free = snd_seq_unused_cells(cptr->data.user.fifo->pool);
	} else {
		info.input_pool = 0;
		info.input_free = 0;
	}
	snd_seq_client_unlock(cptr);
	
	if (copy_to_user(arg, &info, sizeof(info)))
		return -EFAULT;
	return 0;
}

/* SET_CLIENT_POOL ioctl() */
1904 1905
static int snd_seq_ioctl_set_client_pool(struct snd_seq_client *client,
					 void __user *arg)
L
Linus Torvalds 已提交
1906
{
1907
	struct snd_seq_client_pool info;
L
Linus Torvalds 已提交
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
	int rc;

	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;

	if (client->number != info.client)
		return -EINVAL; /* can't change other clients */

	if (info.output_pool >= 1 && info.output_pool <= SNDRV_SEQ_MAX_EVENTS &&
	    (! snd_seq_write_pool_allocated(client) ||
	     info.output_pool != client->pool->size)) {
		if (snd_seq_write_pool_allocated(client)) {
			/* remove all existing cells */
			snd_seq_queue_client_leave_cells(client->number);
			snd_seq_pool_done(client->pool);
		}
		client->pool->size = info.output_pool;
		rc = snd_seq_pool_init(client->pool);
		if (rc < 0)
			return rc;
	}
	if (client->type == USER_CLIENT && client->data.user.fifo != NULL &&
	    info.input_pool >= 1 &&
	    info.input_pool <= SNDRV_SEQ_MAX_CLIENT_EVENTS &&
	    info.input_pool != client->data.user.fifo_pool_size) {
		/* change pool size */
		rc = snd_seq_fifo_resize(client->data.user.fifo, info.input_pool);
		if (rc < 0)
			return rc;
		client->data.user.fifo_pool_size = info.input_pool;
	}
	if (info.output_room >= 1 &&
	    info.output_room <= client->pool->size) {
		client->pool->room  = info.output_room;
	}

	return snd_seq_ioctl_get_client_pool(client, arg);
}


/* REMOVE_EVENTS ioctl() */
1949 1950
static int snd_seq_ioctl_remove_events(struct snd_seq_client *client,
				       void __user *arg)
L
Linus Torvalds 已提交
1951
{
1952
	struct snd_seq_remove_events info;
L
Linus Torvalds 已提交
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978

	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;

	/*
	 * Input mostly not implemented XXX.
	 */
	if (info.remove_mode & SNDRV_SEQ_REMOVE_INPUT) {
		/*
		 * No restrictions so for a user client we can clear
		 * the whole fifo
		 */
		if (client->type == USER_CLIENT)
			snd_seq_fifo_clear(client->data.user.fifo);
	}

	if (info.remove_mode & SNDRV_SEQ_REMOVE_OUTPUT)
		snd_seq_queue_remove_cells(client->number, &info);

	return 0;
}


/*
 * get subscription info
 */
1979 1980
static int snd_seq_ioctl_get_subscription(struct snd_seq_client *client,
					  void __user *arg)
L
Linus Torvalds 已提交
1981 1982
{
	int result;
1983 1984 1985 1986
	struct snd_seq_client *sender = NULL;
	struct snd_seq_client_port *sport = NULL;
	struct snd_seq_port_subscribe subs;
	struct snd_seq_subscribers *p;
L
Linus Torvalds 已提交
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018

	if (copy_from_user(&subs, arg, sizeof(subs)))
		return -EFAULT;

	result = -EINVAL;
	if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)
		goto __end;
	if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)
		goto __end;
	p = snd_seq_port_get_subscription(&sport->c_src, &subs.dest);
	if (p) {
		result = 0;
		subs = p->info;
	} else
		result = -ENOENT;

      __end:
      	if (sport)
		snd_seq_port_unlock(sport);
	if (sender)
		snd_seq_client_unlock(sender);
	if (result >= 0) {
		if (copy_to_user(arg, &subs, sizeof(subs)))
			return -EFAULT;
	}
	return result;
}


/*
 * get subscription info - check only its presence
 */
2019 2020
static int snd_seq_ioctl_query_subs(struct snd_seq_client *client,
				    void __user *arg)
L
Linus Torvalds 已提交
2021 2022
{
	int result = -ENXIO;
2023 2024 2025 2026
	struct snd_seq_client *cptr = NULL;
	struct snd_seq_client_port *port = NULL;
	struct snd_seq_query_subs subs;
	struct snd_seq_port_subs_info *group;
L
Linus Torvalds 已提交
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
	struct list_head *p;
	int i;

	if (copy_from_user(&subs, arg, sizeof(subs)))
		return -EFAULT;

	if ((cptr = snd_seq_client_use_ptr(subs.root.client)) == NULL)
		goto __end;
	if ((port = snd_seq_port_use_ptr(cptr, subs.root.port)) == NULL)
		goto __end;

	switch (subs.type) {
	case SNDRV_SEQ_QUERY_SUBS_READ:
		group = &port->c_src;
		break;
	case SNDRV_SEQ_QUERY_SUBS_WRITE:
		group = &port->c_dest;
		break;
	default:
		goto __end;
	}

	down_read(&group->list_mutex);
	/* search for the subscriber */
	subs.num_subs = group->count;
	i = 0;
	result = -ENOENT;
	list_for_each(p, &group->list_head) {
		if (i++ == subs.index) {
			/* found! */
2057
			struct snd_seq_subscribers *s;
L
Linus Torvalds 已提交
2058
			if (subs.type == SNDRV_SEQ_QUERY_SUBS_READ) {
2059
				s = list_entry(p, struct snd_seq_subscribers, src_list);
L
Linus Torvalds 已提交
2060 2061
				subs.addr = s->info.dest;
			} else {
2062
				s = list_entry(p, struct snd_seq_subscribers, dest_list);
L
Linus Torvalds 已提交
2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
				subs.addr = s->info.sender;
			}
			subs.flags = s->info.flags;
			subs.queue = s->info.queue;
			result = 0;
			break;
		}
	}
	up_read(&group->list_mutex);

      __end:
   	if (port)
		snd_seq_port_unlock(port);
	if (cptr)
		snd_seq_client_unlock(cptr);
	if (result >= 0) {
		if (copy_to_user(arg, &subs, sizeof(subs)))
			return -EFAULT;
	}
	return result;
}


/*
 * query next client
 */
2089 2090
static int snd_seq_ioctl_query_next_client(struct snd_seq_client *client,
					   void __user *arg)
L
Linus Torvalds 已提交
2091
{
2092 2093
	struct snd_seq_client *cptr = NULL;
	struct snd_seq_client_info info;
L
Linus Torvalds 已提交
2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120

	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;

	/* search for next client */
	info.client++;
	if (info.client < 0)
		info.client = 0;
	for (; info.client < SNDRV_SEQ_MAX_CLIENTS; info.client++) {
		cptr = snd_seq_client_use_ptr(info.client);
		if (cptr)
			break; /* found */
	}
	if (cptr == NULL)
		return -ENOENT;

	get_client_info(cptr, &info);
	snd_seq_client_unlock(cptr);

	if (copy_to_user(arg, &info, sizeof(info)))
		return -EFAULT;
	return 0;
}

/* 
 * query next port
 */
2121 2122
static int snd_seq_ioctl_query_next_port(struct snd_seq_client *client,
					 void __user *arg)
L
Linus Torvalds 已提交
2123
{
2124 2125 2126
	struct snd_seq_client *cptr;
	struct snd_seq_client_port *port = NULL;
	struct snd_seq_port_info info;
L
Linus Torvalds 已提交
2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156

	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;
	cptr = snd_seq_client_use_ptr(info.addr.client);
	if (cptr == NULL)
		return -ENXIO;

	/* search for next port */
	info.addr.port++;
	port = snd_seq_port_query_nearest(cptr, &info);
	if (port == NULL) {
		snd_seq_client_unlock(cptr);
		return -ENOENT;
	}

	/* get port info */
	info.addr = port->addr;
	snd_seq_get_port_info(port, &info);
	snd_seq_port_unlock(port);
	snd_seq_client_unlock(cptr);

	if (copy_to_user(arg, &info, sizeof(info)))
		return -EFAULT;
	return 0;
}

/* -------------------------------------------------------- */

static struct seq_ioctl_table {
	unsigned int cmd;
2157
	int (*func)(struct snd_seq_client *client, void __user * arg);
L
Linus Torvalds 已提交
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
} ioctl_tables[] = {
	{ SNDRV_SEQ_IOCTL_SYSTEM_INFO, snd_seq_ioctl_system_info },
	{ SNDRV_SEQ_IOCTL_RUNNING_MODE, snd_seq_ioctl_running_mode },
	{ SNDRV_SEQ_IOCTL_GET_CLIENT_INFO, snd_seq_ioctl_get_client_info },
	{ SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, snd_seq_ioctl_set_client_info },
	{ SNDRV_SEQ_IOCTL_CREATE_PORT, snd_seq_ioctl_create_port },
	{ SNDRV_SEQ_IOCTL_DELETE_PORT, snd_seq_ioctl_delete_port },
	{ SNDRV_SEQ_IOCTL_GET_PORT_INFO, snd_seq_ioctl_get_port_info },
	{ SNDRV_SEQ_IOCTL_SET_PORT_INFO, snd_seq_ioctl_set_port_info },
	{ SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, snd_seq_ioctl_subscribe_port },
	{ SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, snd_seq_ioctl_unsubscribe_port },
	{ SNDRV_SEQ_IOCTL_CREATE_QUEUE, snd_seq_ioctl_create_queue },
	{ SNDRV_SEQ_IOCTL_DELETE_QUEUE, snd_seq_ioctl_delete_queue },
	{ SNDRV_SEQ_IOCTL_GET_QUEUE_INFO, snd_seq_ioctl_get_queue_info },
	{ SNDRV_SEQ_IOCTL_SET_QUEUE_INFO, snd_seq_ioctl_set_queue_info },
	{ SNDRV_SEQ_IOCTL_GET_NAMED_QUEUE, snd_seq_ioctl_get_named_queue },
	{ SNDRV_SEQ_IOCTL_GET_QUEUE_STATUS, snd_seq_ioctl_get_queue_status },
	{ SNDRV_SEQ_IOCTL_GET_QUEUE_TEMPO, snd_seq_ioctl_get_queue_tempo },
	{ SNDRV_SEQ_IOCTL_SET_QUEUE_TEMPO, snd_seq_ioctl_set_queue_tempo },
	{ SNDRV_SEQ_IOCTL_GET_QUEUE_TIMER, snd_seq_ioctl_get_queue_timer },
	{ SNDRV_SEQ_IOCTL_SET_QUEUE_TIMER, snd_seq_ioctl_set_queue_timer },
	{ SNDRV_SEQ_IOCTL_GET_QUEUE_CLIENT, snd_seq_ioctl_get_queue_client },
	{ SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT, snd_seq_ioctl_set_queue_client },
	{ SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, snd_seq_ioctl_get_client_pool },
	{ SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, snd_seq_ioctl_set_client_pool },
	{ SNDRV_SEQ_IOCTL_GET_SUBSCRIPTION, snd_seq_ioctl_get_subscription },
	{ SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT, snd_seq_ioctl_query_next_client },
	{ SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, snd_seq_ioctl_query_next_port },
	{ SNDRV_SEQ_IOCTL_REMOVE_EVENTS, snd_seq_ioctl_remove_events },
	{ SNDRV_SEQ_IOCTL_QUERY_SUBS, snd_seq_ioctl_query_subs },
	{ 0, NULL },
};

2191 2192
static int snd_seq_do_ioctl(struct snd_seq_client *client, unsigned int cmd,
			    void __user *arg)
L
Linus Torvalds 已提交
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
{
	struct seq_ioctl_table *p;

	switch (cmd) {
	case SNDRV_SEQ_IOCTL_PVERSION:
		/* return sequencer version number */
		return put_user(SNDRV_SEQ_VERSION, (int __user *)arg) ? -EFAULT : 0;
	case SNDRV_SEQ_IOCTL_CLIENT_ID:
		/* return the id of this client */
		return put_user(client->number, (int __user *)arg) ? -EFAULT : 0;
	}

	if (! arg)
		return -EFAULT;
	for (p = ioctl_tables; p->cmd; p++) {
		if (p->cmd == cmd)
			return p->func(client, arg);
	}
2211
	pr_debug("ALSA: seq unknown ioctl() 0x%x (type='%c', number=0x%02x)\n",
L
Linus Torvalds 已提交
2212 2213 2214 2215 2216 2217 2218
		   cmd, _IOC_TYPE(cmd), _IOC_NR(cmd));
	return -ENOTTY;
}


static long snd_seq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
2219
	struct snd_seq_client *client = file->private_data;
L
Linus Torvalds 已提交
2220

2221 2222
	if (snd_BUG_ON(!client))
		return -ENXIO;
L
Linus Torvalds 已提交
2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
		
	return snd_seq_do_ioctl(client, cmd, (void __user *) arg);
}

#ifdef CONFIG_COMPAT
#include "seq_compat.c"
#else
#define snd_seq_ioctl_compat	NULL
#endif

/* -------------------------------------------------------- */


/* exported to kernel modules */
2237 2238
int snd_seq_create_kernel_client(struct snd_card *card, int client_index,
				 const char *name_fmt, ...)
L
Linus Torvalds 已提交
2239
{
2240
	struct snd_seq_client *client;
2241
	va_list args;
L
Linus Torvalds 已提交
2242

2243 2244
	if (snd_BUG_ON(in_interrupt()))
		return -EBUSY;
L
Linus Torvalds 已提交
2245

2246
	if (card && client_index >= SNDRV_SEQ_CLIENTS_PER_CARD)
L
Linus Torvalds 已提交
2247
		return -EINVAL;
2248
	if (card == NULL && client_index >= SNDRV_SEQ_GLOBAL_CLIENTS)
L
Linus Torvalds 已提交
2249 2250
		return -EINVAL;

2251
	if (mutex_lock_interruptible(&register_mutex))
L
Linus Torvalds 已提交
2252
		return -ERESTARTSYS;
2253 2254

	if (card) {
2255 2256 2257
		client_index += SNDRV_SEQ_GLOBAL_CLIENTS
			+ card->number * SNDRV_SEQ_CLIENTS_PER_CARD;
		if (client_index >= SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN)
2258 2259 2260
			client_index = -1;
	}

L
Linus Torvalds 已提交
2261
	/* empty write queue as default */
2262
	client = seq_create_client1(client_index, 0);
L
Linus Torvalds 已提交
2263
	if (client == NULL) {
2264
		mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
2265 2266 2267 2268
		return -EBUSY;	/* failure code */
	}
	usage_alloc(&client_usage, 1);

2269 2270
	client->accept_input = 1;
	client->accept_output = 1;
L
Linus Torvalds 已提交
2271
		
2272 2273 2274
	va_start(args, name_fmt);
	vsnprintf(client->name, sizeof(client->name), name_fmt, args);
	va_end(args);
L
Linus Torvalds 已提交
2275 2276

	client->type = KERNEL_CLIENT;
2277
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
2278 2279 2280 2281 2282 2283 2284 2285

	/* make others aware this new client */
	snd_seq_system_client_ev_client_start(client->number);
	
	/* return client number to caller */
	return client->number;
}

2286 2287
EXPORT_SYMBOL(snd_seq_create_kernel_client);

L
Linus Torvalds 已提交
2288 2289 2290
/* exported to kernel modules */
int snd_seq_delete_kernel_client(int client)
{
2291
	struct snd_seq_client *ptr;
L
Linus Torvalds 已提交
2292

2293 2294
	if (snd_BUG_ON(in_interrupt()))
		return -EBUSY;
L
Linus Torvalds 已提交
2295 2296 2297 2298 2299 2300 2301 2302 2303 2304

	ptr = clientptr(client);
	if (ptr == NULL)
		return -EINVAL;

	seq_free_client(ptr);
	kfree(ptr);
	return 0;
}

2305
EXPORT_SYMBOL(snd_seq_delete_kernel_client);
L
Linus Torvalds 已提交
2306 2307 2308 2309

/* skeleton to enqueue event, called from snd_seq_kernel_client_enqueue
 * and snd_seq_kernel_client_enqueue_blocking
 */
2310
static int kernel_client_enqueue(int client, struct snd_seq_event *ev,
L
Linus Torvalds 已提交
2311 2312 2313
				 struct file *file, int blocking,
				 int atomic, int hop)
{
2314
	struct snd_seq_client *cptr;
L
Linus Torvalds 已提交
2315 2316
	int result;

2317 2318
	if (snd_BUG_ON(!ev))
		return -EINVAL;
L
Linus Torvalds 已提交
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348

	if (ev->type == SNDRV_SEQ_EVENT_NONE)
		return 0; /* ignore this */
	if (ev->type == SNDRV_SEQ_EVENT_KERNEL_ERROR)
		return -EINVAL; /* quoted events can't be enqueued */

	/* fill in client number */
	ev->source.client = client;

	if (check_event_type_and_length(ev))
		return -EINVAL;

	cptr = snd_seq_client_use_ptr(client);
	if (cptr == NULL)
		return -EINVAL;
	
	if (! cptr->accept_output)
		result = -EPERM;
	else /* send it */
		result = snd_seq_client_enqueue_event(cptr, ev, file, blocking, atomic, hop);

	snd_seq_client_unlock(cptr);
	return result;
}

/*
 * exported, called by kernel clients to enqueue events (w/o blocking)
 *
 * RETURN VALUE: zero if succeed, negative if error
 */
2349
int snd_seq_kernel_client_enqueue(int client, struct snd_seq_event * ev,
L
Linus Torvalds 已提交
2350 2351 2352 2353 2354
				  int atomic, int hop)
{
	return kernel_client_enqueue(client, ev, NULL, 0, atomic, hop);
}

2355 2356
EXPORT_SYMBOL(snd_seq_kernel_client_enqueue);

L
Linus Torvalds 已提交
2357 2358 2359 2360 2361
/*
 * exported, called by kernel clients to enqueue events (with blocking)
 *
 * RETURN VALUE: zero if succeed, negative if error
 */
2362
int snd_seq_kernel_client_enqueue_blocking(int client, struct snd_seq_event * ev,
L
Linus Torvalds 已提交
2363 2364 2365 2366 2367 2368
					   struct file *file,
					   int atomic, int hop)
{
	return kernel_client_enqueue(client, ev, file, 1, atomic, hop);
}

2369
EXPORT_SYMBOL(snd_seq_kernel_client_enqueue_blocking);
L
Linus Torvalds 已提交
2370 2371 2372 2373 2374 2375 2376 2377

/* 
 * exported, called by kernel clients to dispatch events directly to other
 * clients, bypassing the queues.  Event time-stamp will be updated.
 *
 * RETURN VALUE: negative = delivery failed,
 *		 zero, or positive: the number of delivered events
 */
2378
int snd_seq_kernel_client_dispatch(int client, struct snd_seq_event * ev,
L
Linus Torvalds 已提交
2379 2380
				   int atomic, int hop)
{
2381
	struct snd_seq_client *cptr;
L
Linus Torvalds 已提交
2382 2383
	int result;

2384 2385
	if (snd_BUG_ON(!ev))
		return -EINVAL;
L
Linus Torvalds 已提交
2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406

	/* fill in client number */
	ev->queue = SNDRV_SEQ_QUEUE_DIRECT;
	ev->source.client = client;

	if (check_event_type_and_length(ev))
		return -EINVAL;

	cptr = snd_seq_client_use_ptr(client);
	if (cptr == NULL)
		return -EINVAL;

	if (!cptr->accept_output)
		result = -EPERM;
	else
		result = snd_seq_deliver_event(cptr, ev, atomic, hop);

	snd_seq_client_unlock(cptr);
	return result;
}

2407
EXPORT_SYMBOL(snd_seq_kernel_client_dispatch);
L
Linus Torvalds 已提交
2408 2409 2410 2411 2412 2413 2414

/*
 * exported, called by kernel clients to perform same functions as with
 * userland ioctl() 
 */
int snd_seq_kernel_client_ctl(int clientid, unsigned int cmd, void *arg)
{
2415
	struct snd_seq_client *client;
L
Linus Torvalds 已提交
2416 2417 2418 2419 2420 2421 2422
	mm_segment_t fs;
	int result;

	client = clientptr(clientid);
	if (client == NULL)
		return -ENXIO;
	fs = snd_enter_user();
C
Clemens Ladisch 已提交
2423
	result = snd_seq_do_ioctl(client, cmd, (void __force __user *)arg);
L
Linus Torvalds 已提交
2424 2425 2426 2427
	snd_leave_user(fs);
	return result;
}

2428
EXPORT_SYMBOL(snd_seq_kernel_client_ctl);
L
Linus Torvalds 已提交
2429 2430 2431 2432

/* exported (for OSS emulator) */
int snd_seq_kernel_client_write_poll(int clientid, struct file *file, poll_table *wait)
{
2433
	struct snd_seq_client *client;
L
Linus Torvalds 已提交
2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445

	client = clientptr(clientid);
	if (client == NULL)
		return -ENXIO;

	if (! snd_seq_write_pool_allocated(client))
		return 1;
	if (snd_seq_pool_poll_wait(client->pool, file, wait))
		return 1;
	return 0;
}

2446 2447
EXPORT_SYMBOL(snd_seq_kernel_client_write_poll);

L
Linus Torvalds 已提交
2448 2449
/*---------------------------------------------------------------------------*/

2450
#ifdef CONFIG_SND_PROC_FS
L
Linus Torvalds 已提交
2451 2452 2453
/*
 *  /proc interface
 */
2454 2455 2456
static void snd_seq_info_dump_subscribers(struct snd_info_buffer *buffer,
					  struct snd_seq_port_subs_info *group,
					  int is_src, char *msg)
L
Linus Torvalds 已提交
2457 2458
{
	struct list_head *p;
2459
	struct snd_seq_subscribers *s;
L
Linus Torvalds 已提交
2460 2461 2462 2463 2464 2465 2466 2467 2468 2469
	int count = 0;

	down_read(&group->list_mutex);
	if (list_empty(&group->list_head)) {
		up_read(&group->list_mutex);
		return;
	}
	snd_iprintf(buffer, msg);
	list_for_each(p, &group->list_head) {
		if (is_src)
2470
			s = list_entry(p, struct snd_seq_subscribers, src_list);
L
Linus Torvalds 已提交
2471
		else
2472
			s = list_entry(p, struct snd_seq_subscribers, dest_list);
L
Linus Torvalds 已提交
2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492
		if (count++)
			snd_iprintf(buffer, ", ");
		snd_iprintf(buffer, "%d:%d",
			    is_src ? s->info.dest.client : s->info.sender.client,
			    is_src ? s->info.dest.port : s->info.sender.port);
		if (s->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)
			snd_iprintf(buffer, "[%c:%d]", ((s->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL) ? 'r' : 't'), s->info.queue);
		if (group->exclusive)
			snd_iprintf(buffer, "[ex]");
	}
	up_read(&group->list_mutex);
	snd_iprintf(buffer, "\n");
}

#define FLAG_PERM_RD(perm) ((perm) & SNDRV_SEQ_PORT_CAP_READ ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_READ ? 'R' : 'r') : '-')
#define FLAG_PERM_WR(perm) ((perm) & SNDRV_SEQ_PORT_CAP_WRITE ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_WRITE ? 'W' : 'w') : '-')
#define FLAG_PERM_EX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_NO_EXPORT ? '-' : 'e')

#define FLAG_PERM_DUPLEX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_DUPLEX ? 'X' : '-')

2493 2494
static void snd_seq_info_dump_ports(struct snd_info_buffer *buffer,
				    struct snd_seq_client *client)
L
Linus Torvalds 已提交
2495
{
2496
	struct snd_seq_client_port *p;
L
Linus Torvalds 已提交
2497

2498
	mutex_lock(&client->ports_mutex);
2499
	list_for_each_entry(p, &client->ports_list_head, list) {
L
Linus Torvalds 已提交
2500 2501 2502 2503 2504 2505 2506 2507 2508
		snd_iprintf(buffer, "  Port %3d : \"%s\" (%c%c%c%c)\n",
			    p->addr.port, p->name,
			    FLAG_PERM_RD(p->capability),
			    FLAG_PERM_WR(p->capability),
			    FLAG_PERM_EX(p->capability),
			    FLAG_PERM_DUPLEX(p->capability));
		snd_seq_info_dump_subscribers(buffer, &p->c_src, 1, "    Connecting To: ");
		snd_seq_info_dump_subscribers(buffer, &p->c_dest, 0, "    Connected From: ");
	}
2509
	mutex_unlock(&client->ports_mutex);
L
Linus Torvalds 已提交
2510 2511 2512 2513
}


/* exported to seq_info.c */
2514 2515
void snd_seq_info_clients_read(struct snd_info_entry *entry, 
			       struct snd_info_buffer *buffer)
L
Linus Torvalds 已提交
2516 2517
{
	int c;
2518
	struct snd_seq_client *client;
L
Linus Torvalds 已提交
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551

	snd_iprintf(buffer, "Client info\n");
	snd_iprintf(buffer, "  cur  clients : %d\n", client_usage.cur);
	snd_iprintf(buffer, "  peak clients : %d\n", client_usage.peak);
	snd_iprintf(buffer, "  max  clients : %d\n", SNDRV_SEQ_MAX_CLIENTS);
	snd_iprintf(buffer, "\n");

	/* list the client table */
	for (c = 0; c < SNDRV_SEQ_MAX_CLIENTS; c++) {
		client = snd_seq_client_use_ptr(c);
		if (client == NULL)
			continue;
		if (client->type == NO_CLIENT) {
			snd_seq_client_unlock(client);
			continue;
		}

		snd_iprintf(buffer, "Client %3d : \"%s\" [%s]\n",
			    c, client->name,
			    client->type == USER_CLIENT ? "User" : "Kernel");
		snd_seq_info_dump_ports(buffer, client);
		if (snd_seq_write_pool_allocated(client)) {
			snd_iprintf(buffer, "  Output pool :\n");
			snd_seq_info_pool(buffer, client->pool, "    ");
		}
		if (client->type == USER_CLIENT && client->data.user.fifo &&
		    client->data.user.fifo->pool) {
			snd_iprintf(buffer, "  Input pool :\n");
			snd_seq_info_pool(buffer, client->data.user.fifo->pool, "    ");
		}
		snd_seq_client_unlock(client);
	}
}
2552
#endif /* CONFIG_SND_PROC_FS */
L
Linus Torvalds 已提交
2553 2554 2555 2556 2557 2558 2559 2560

/*---------------------------------------------------------------------------*/


/*
 *  REGISTRATION PART
 */

2561
static const struct file_operations snd_seq_f_ops =
L
Linus Torvalds 已提交
2562 2563 2564 2565 2566 2567
{
	.owner =	THIS_MODULE,
	.read =		snd_seq_read,
	.write =	snd_seq_write,
	.open =		snd_seq_open,
	.release =	snd_seq_release,
T
Takashi Iwai 已提交
2568
	.llseek =	no_llseek,
L
Linus Torvalds 已提交
2569 2570 2571 2572 2573
	.poll =		snd_seq_poll,
	.unlocked_ioctl =	snd_seq_ioctl,
	.compat_ioctl =	snd_seq_ioctl_compat,
};

2574 2575
static struct device seq_dev;

L
Linus Torvalds 已提交
2576 2577 2578 2579 2580 2581 2582
/* 
 * register sequencer device 
 */
int __init snd_sequencer_device_init(void)
{
	int err;

2583 2584 2585
	snd_device_initialize(&seq_dev, NULL);
	dev_set_name(&seq_dev, "seq");

2586
	if (mutex_lock_interruptible(&register_mutex))
L
Linus Torvalds 已提交
2587 2588
		return -ERESTARTSYS;

2589 2590
	err = snd_register_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0,
				  &snd_seq_f_ops, NULL, &seq_dev);
2591
	if (err < 0) {
2592
		mutex_unlock(&register_mutex);
2593
		put_device(&seq_dev);
L
Linus Torvalds 已提交
2594 2595 2596
		return err;
	}
	
2597
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608

	return 0;
}



/* 
 * unregister sequencer device 
 */
void __exit snd_sequencer_device_done(void)
{
2609
	snd_unregister_device(&seq_dev);
2610
	put_device(&seq_dev);
L
Linus Torvalds 已提交
2611
}