blob: 47cfa5186e34e15f4775f6c15172b136f90a3999 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALSA sequencer Client Manager
3 * Copyright (c) 1998-2001 by Frank van de Pol <fvdpol@coil.demon.nl>
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/slab.h>
26#include <sound/core.h>
27#include <sound/minors.h>
28#include <linux/kmod.h>
29
30#include <sound/seq_kernel.h>
31#include "seq_clientmgr.h"
32#include "seq_memory.h"
33#include "seq_queue.h"
34#include "seq_timer.h"
35#include "seq_info.h"
36#include "seq_system.h"
37#include <sound/seq_device.h>
38#ifdef CONFIG_COMPAT
39#include <linux/compat.h>
40#endif
41
42/* Client Manager
43
44 * this module handles the connections of userland and kernel clients
45 *
46 */
47
Clemens Ladischaa1e77e2005-12-12 09:36:01 +010048/*
49 * There are four ranges of client numbers (last two shared):
50 * 0..15: global clients
51 * 16..127: statically allocated client numbers for cards 0..27
52 * 128..191: dynamically allocated client numbers for cards 28..31
53 * 128..191: dynamically allocated client numbers for applications
54 */
55
56/* number of kernel non-card clients */
57#define SNDRV_SEQ_GLOBAL_CLIENTS 16
58/* clients per cards, for static clients */
59#define SNDRV_SEQ_CLIENTS_PER_CARD 4
60/* dynamically allocated client numbers (both kernel drivers and user space) */
61#define SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN 128
Clemens Ladischd0015442005-11-20 14:09:05 +010062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define SNDRV_SEQ_LFLG_INPUT 0x0001
64#define SNDRV_SEQ_LFLG_OUTPUT 0x0002
65#define SNDRV_SEQ_LFLG_OPEN (SNDRV_SEQ_LFLG_INPUT|SNDRV_SEQ_LFLG_OUTPUT)
66
67static DEFINE_SPINLOCK(clients_lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010068static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 * client table
72 */
73static char clienttablock[SNDRV_SEQ_MAX_CLIENTS];
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010074static struct snd_seq_client *clienttab[SNDRV_SEQ_MAX_CLIENTS];
75static struct snd_seq_usage client_usage;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * prototypes
79 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010080static int bounce_error_event(struct snd_seq_client *client,
81 struct snd_seq_event *event,
82 int err, int atomic, int hop);
83static int snd_seq_deliver_single_event(struct snd_seq_client *client,
84 struct snd_seq_event *event,
85 int filter, int atomic, int hop);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 */
89
90static inline mm_segment_t snd_enter_user(void)
91{
92 mm_segment_t fs = get_fs();
93 set_fs(get_ds());
94 return fs;
95}
96
97static inline void snd_leave_user(mm_segment_t fs)
98{
99 set_fs(fs);
100}
101
102/*
103 */
104static inline unsigned short snd_seq_file_flags(struct file *file)
105{
106 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
107 case FMODE_WRITE:
108 return SNDRV_SEQ_LFLG_OUTPUT;
109 case FMODE_READ:
110 return SNDRV_SEQ_LFLG_INPUT;
111 default:
112 return SNDRV_SEQ_LFLG_OPEN;
113 }
114}
115
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100116static inline int snd_seq_write_pool_allocated(struct snd_seq_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 return snd_seq_total_cells(client->pool) > 0;
119}
120
121/* return pointer to client structure for specified id */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100122static struct snd_seq_client *clientptr(int clientid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100125 snd_printd("Seq: oops. Trying to get pointer to client %d\n",
126 clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return NULL;
128 }
129 return clienttab[clientid];
130}
131
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100132struct snd_seq_client *snd_seq_client_use_ptr(int clientid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 unsigned long flags;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100135 struct snd_seq_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100138 snd_printd("Seq: oops. Trying to get pointer to client %d\n",
139 clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return NULL;
141 }
142 spin_lock_irqsave(&clients_lock, flags);
143 client = clientptr(clientid);
144 if (client)
145 goto __lock;
146 if (clienttablock[clientid]) {
147 spin_unlock_irqrestore(&clients_lock, flags);
148 return NULL;
149 }
150 spin_unlock_irqrestore(&clients_lock, flags);
151#ifdef CONFIG_KMOD
Jan Blunck0d63e4f2008-02-14 19:34:28 -0800152 if (!in_interrupt()) {
Clemens Ladischaa1e77e2005-12-12 09:36:01 +0100153 static char client_requested[SNDRV_SEQ_GLOBAL_CLIENTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 static char card_requested[SNDRV_CARDS];
Clemens Ladischaa1e77e2005-12-12 09:36:01 +0100155 if (clientid < SNDRV_SEQ_GLOBAL_CLIENTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 int idx;
157
Jan Blunck0d63e4f2008-02-14 19:34:28 -0800158 if (!client_requested[clientid]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 client_requested[clientid] = 1;
Clemens Ladischaa1e77e2005-12-12 09:36:01 +0100160 for (idx = 0; idx < 15; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (seq_client_load[idx] < 0)
162 break;
163 if (seq_client_load[idx] == clientid) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100164 request_module("snd-seq-client-%i",
165 clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 break;
167 }
168 }
169 }
Clemens Ladischaa1e77e2005-12-12 09:36:01 +0100170 } else if (clientid < SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN) {
171 int card = (clientid - SNDRV_SEQ_GLOBAL_CLIENTS) /
172 SNDRV_SEQ_CLIENTS_PER_CARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (card < snd_ecards_limit) {
174 if (! card_requested[card]) {
175 card_requested[card] = 1;
176 snd_request_card(card);
177 }
178 snd_seq_device_load_drivers();
179 }
180 }
181 spin_lock_irqsave(&clients_lock, flags);
182 client = clientptr(clientid);
183 if (client)
184 goto __lock;
185 spin_unlock_irqrestore(&clients_lock, flags);
186 }
187#endif
188 return NULL;
189
190 __lock:
191 snd_use_lock_use(&client->use_lock);
192 spin_unlock_irqrestore(&clients_lock, flags);
193 return client;
194}
195
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100196static void usage_alloc(struct snd_seq_usage *res, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 res->cur += num;
199 if (res->cur > res->peak)
200 res->peak = res->cur;
201}
202
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100203static void usage_free(struct snd_seq_usage *res, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 res->cur -= num;
206}
207
208/* initialise data structures */
209int __init client_init_data(void)
210{
211 /* zap out the client table */
212 memset(&clienttablock, 0, sizeof(clienttablock));
213 memset(&clienttab, 0, sizeof(clienttab));
214 return 0;
215}
216
217
Clemens Ladischaa1e77e2005-12-12 09:36:01 +0100218static struct snd_seq_client *seq_create_client1(int client_index, int poolsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 unsigned long flags;
221 int c;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100222 struct snd_seq_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* init client data */
Takashi Iwaiecca82b2005-09-09 14:20:49 +0200225 client = kzalloc(sizeof(*client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (client == NULL)
227 return NULL;
228 client->pool = snd_seq_pool_new(poolsize);
229 if (client->pool == NULL) {
230 kfree(client);
231 return NULL;
232 }
233 client->type = NO_CLIENT;
234 snd_use_lock_init(&client->use_lock);
235 rwlock_init(&client->ports_lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100236 mutex_init(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 INIT_LIST_HEAD(&client->ports_list_head);
238
239 /* find free slot in the client table */
240 spin_lock_irqsave(&clients_lock, flags);
241 if (client_index < 0) {
Clemens Ladischaa1e77e2005-12-12 09:36:01 +0100242 for (c = SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN;
243 c < SNDRV_SEQ_MAX_CLIENTS;
244 c++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (clienttab[c] || clienttablock[c])
246 continue;
247 clienttab[client->number = c] = client;
248 spin_unlock_irqrestore(&clients_lock, flags);
249 return client;
250 }
251 } else {
252 if (clienttab[client_index] == NULL && !clienttablock[client_index]) {
253 clienttab[client->number = client_index] = client;
254 spin_unlock_irqrestore(&clients_lock, flags);
255 return client;
256 }
257 }
258 spin_unlock_irqrestore(&clients_lock, flags);
259 snd_seq_pool_delete(&client->pool);
260 kfree(client);
261 return NULL; /* no free slot found or busy, return failure code */
262}
263
264
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100265static int seq_free_client1(struct snd_seq_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 unsigned long flags;
268
269 snd_assert(client != NULL, return -EINVAL);
270 snd_seq_delete_all_ports(client);
271 snd_seq_queue_client_leave(client->number);
272 spin_lock_irqsave(&clients_lock, flags);
273 clienttablock[client->number] = 1;
274 clienttab[client->number] = NULL;
275 spin_unlock_irqrestore(&clients_lock, flags);
276 snd_use_lock_sync(&client->use_lock);
277 snd_seq_queue_client_termination(client->number);
278 if (client->pool)
279 snd_seq_pool_delete(&client->pool);
280 spin_lock_irqsave(&clients_lock, flags);
281 clienttablock[client->number] = 0;
282 spin_unlock_irqrestore(&clients_lock, flags);
283 return 0;
284}
285
286
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100287static void seq_free_client(struct snd_seq_client * client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100289 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 switch (client->type) {
291 case NO_CLIENT:
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100292 snd_printk(KERN_WARNING "Seq: Trying to free unused client %d\n",
293 client->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 break;
295 case USER_CLIENT:
296 case KERNEL_CLIENT:
297 seq_free_client1(client);
298 usage_free(&client_usage, 1);
299 break;
300
301 default:
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100302 snd_printk(KERN_ERR "Seq: Trying to free client %d with undefined type = %d\n",
303 client->number, client->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100305 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 snd_seq_system_client_ev_client_exit(client->number);
308}
309
310
311
312/* -------------------------------------------------------- */
313
314/* create a user client */
315static int snd_seq_open(struct inode *inode, struct file *file)
316{
317 int c, mode; /* client id */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100318 struct snd_seq_client *client;
319 struct snd_seq_user_client *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100321 if (mutex_lock_interruptible(&register_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return -ERESTARTSYS;
Clemens Ladischaa1e77e2005-12-12 09:36:01 +0100323 client = seq_create_client1(-1, SNDRV_SEQ_DEFAULT_EVENTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (client == NULL) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100325 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return -ENOMEM; /* failure code */
327 }
328
329 mode = snd_seq_file_flags(file);
330 if (mode & SNDRV_SEQ_LFLG_INPUT)
331 client->accept_input = 1;
332 if (mode & SNDRV_SEQ_LFLG_OUTPUT)
333 client->accept_output = 1;
334
335 user = &client->data.user;
336 user->fifo = NULL;
337 user->fifo_pool_size = 0;
338
339 if (mode & SNDRV_SEQ_LFLG_INPUT) {
340 user->fifo_pool_size = SNDRV_SEQ_DEFAULT_CLIENT_EVENTS;
341 user->fifo = snd_seq_fifo_new(user->fifo_pool_size);
342 if (user->fifo == NULL) {
343 seq_free_client1(client);
344 kfree(client);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100345 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return -ENOMEM;
347 }
348 }
349
350 usage_alloc(&client_usage, 1);
351 client->type = USER_CLIENT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100352 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 c = client->number;
355 file->private_data = client;
356
357 /* fill client data */
358 user->file = file;
359 sprintf(client->name, "Client-%d", c);
360
361 /* make others aware this new client */
362 snd_seq_system_client_ev_client_start(c);
363
364 return 0;
365}
366
367/* delete a user client */
368static int snd_seq_release(struct inode *inode, struct file *file)
369{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100370 struct snd_seq_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 if (client) {
373 seq_free_client(client);
374 if (client->data.user.fifo)
375 snd_seq_fifo_delete(&client->data.user.fifo);
376 kfree(client);
377 }
378
379 return 0;
380}
381
382
383/* handle client read() */
384/* possible error values:
385 * -ENXIO invalid client or file open mode
386 * -ENOSPC FIFO overflow (the flag is cleared after this error report)
387 * -EINVAL no enough user-space buffer to write the whole event
388 * -EFAULT seg. fault during copy to user space
389 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100390static ssize_t snd_seq_read(struct file *file, char __user *buf, size_t count,
391 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100393 struct snd_seq_client *client = file->private_data;
394 struct snd_seq_fifo *fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 int err;
396 long result = 0;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100397 struct snd_seq_event_cell *cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT))
400 return -ENXIO;
401
402 if (!access_ok(VERIFY_WRITE, buf, count))
403 return -EFAULT;
404
405 /* check client structures are in place */
406 snd_assert(client != NULL, return -ENXIO);
407
408 if (!client->accept_input || (fifo = client->data.user.fifo) == NULL)
409 return -ENXIO;
410
411 if (atomic_read(&fifo->overflow) > 0) {
412 /* buffer overflow is detected */
413 snd_seq_fifo_clear(fifo);
414 /* return error code */
415 return -ENOSPC;
416 }
417
418 cell = NULL;
419 err = 0;
420 snd_seq_fifo_lock(fifo);
421
422 /* while data available in queue */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100423 while (count >= sizeof(struct snd_seq_event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 int nonblock;
425
426 nonblock = (file->f_flags & O_NONBLOCK) || result > 0;
427 if ((err = snd_seq_fifo_cell_out(fifo, &cell, nonblock)) < 0) {
428 break;
429 }
430 if (snd_seq_ev_is_variable(&cell->event)) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100431 struct snd_seq_event tmpev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 tmpev = cell->event;
433 tmpev.data.ext.len &= ~SNDRV_SEQ_EXT_MASK;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100434 if (copy_to_user(buf, &tmpev, sizeof(struct snd_seq_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 err = -EFAULT;
436 break;
437 }
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100438 count -= sizeof(struct snd_seq_event);
439 buf += sizeof(struct snd_seq_event);
Clemens Ladisch4d233592005-09-05 10:35:20 +0200440 err = snd_seq_expand_var_event(&cell->event, count,
441 (char __force *)buf, 0,
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100442 sizeof(struct snd_seq_event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (err < 0)
444 break;
445 result += err;
446 count -= err;
447 buf += err;
448 } else {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100449 if (copy_to_user(buf, &cell->event, sizeof(struct snd_seq_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 err = -EFAULT;
451 break;
452 }
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100453 count -= sizeof(struct snd_seq_event);
454 buf += sizeof(struct snd_seq_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456 snd_seq_cell_free(cell);
457 cell = NULL; /* to be sure */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100458 result += sizeof(struct snd_seq_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
461 if (err < 0) {
462 if (cell)
463 snd_seq_fifo_cell_putback(fifo, cell);
464 if (err == -EAGAIN && result > 0)
465 err = 0;
466 }
467 snd_seq_fifo_unlock(fifo);
468
469 return (err < 0) ? err : result;
470}
471
472
473/*
474 * check access permission to the port
475 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100476static int check_port_perm(struct snd_seq_client_port *port, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 if ((port->capability & flags) != flags)
479 return 0;
480 return flags;
481}
482
483/*
484 * check if the destination client is available, and return the pointer
485 * if filter is non-zero, client filter bitmap is tested.
486 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100487static struct snd_seq_client *get_event_dest_client(struct snd_seq_event *event,
488 int filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100490 struct snd_seq_client *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 dest = snd_seq_client_use_ptr(event->dest.client);
493 if (dest == NULL)
494 return NULL;
495 if (! dest->accept_input)
496 goto __not_avail;
497 if ((dest->filter & SNDRV_SEQ_FILTER_USE_EVENT) &&
498 ! test_bit(event->type, dest->event_filter))
499 goto __not_avail;
500 if (filter && !(dest->filter & filter))
501 goto __not_avail;
502
503 return dest; /* ok - accessible */
504__not_avail:
505 snd_seq_client_unlock(dest);
506 return NULL;
507}
508
509
510/*
511 * Return the error event.
512 *
513 * If the receiver client is a user client, the original event is
514 * encapsulated in SNDRV_SEQ_EVENT_BOUNCE as variable length event. If
515 * the original event is also variable length, the external data is
516 * copied after the event record.
517 * If the receiver client is a kernel client, the original event is
518 * quoted in SNDRV_SEQ_EVENT_KERNEL_ERROR, since this requires no extra
519 * kmalloc.
520 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100521static int bounce_error_event(struct snd_seq_client *client,
522 struct snd_seq_event *event,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 int err, int atomic, int hop)
524{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100525 struct snd_seq_event bounce_ev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 int result;
527
528 if (client == NULL ||
529 ! (client->filter & SNDRV_SEQ_FILTER_BOUNCE) ||
530 ! client->accept_input)
531 return 0; /* ignored */
532
533 /* set up quoted error */
534 memset(&bounce_ev, 0, sizeof(bounce_ev));
535 bounce_ev.type = SNDRV_SEQ_EVENT_KERNEL_ERROR;
536 bounce_ev.flags = SNDRV_SEQ_EVENT_LENGTH_FIXED;
537 bounce_ev.queue = SNDRV_SEQ_QUEUE_DIRECT;
538 bounce_ev.source.client = SNDRV_SEQ_CLIENT_SYSTEM;
539 bounce_ev.source.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
540 bounce_ev.dest.client = client->number;
541 bounce_ev.dest.port = event->source.port;
542 bounce_ev.data.quote.origin = event->dest;
543 bounce_ev.data.quote.event = event;
544 bounce_ev.data.quote.value = -err; /* use positive value */
545 result = snd_seq_deliver_single_event(NULL, &bounce_ev, 0, atomic, hop + 1);
546 if (result < 0) {
547 client->event_lost++;
548 return result;
549 }
550
551 return result;
552}
553
554
555/*
556 * rewrite the time-stamp of the event record with the curren time
557 * of the given queue.
558 * return non-zero if updated.
559 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100560static int update_timestamp_of_queue(struct snd_seq_event *event,
561 int queue, int real_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100563 struct snd_seq_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 q = queueptr(queue);
566 if (! q)
567 return 0;
568 event->queue = queue;
569 event->flags &= ~SNDRV_SEQ_TIME_STAMP_MASK;
570 if (real_time) {
571 event->time.time = snd_seq_timer_get_cur_time(q->timer);
572 event->flags |= SNDRV_SEQ_TIME_STAMP_REAL;
573 } else {
574 event->time.tick = snd_seq_timer_get_cur_tick(q->timer);
575 event->flags |= SNDRV_SEQ_TIME_STAMP_TICK;
576 }
577 queuefree(q);
578 return 1;
579}
580
581
582/*
583 * deliver an event to the specified destination.
584 * if filter is non-zero, client filter bitmap is tested.
585 *
586 * RETURN VALUE: 0 : if succeeded
587 * <0 : error
588 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100589static int snd_seq_deliver_single_event(struct snd_seq_client *client,
590 struct snd_seq_event *event,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 int filter, int atomic, int hop)
592{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100593 struct snd_seq_client *dest = NULL;
594 struct snd_seq_client_port *dest_port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int result = -ENOENT;
596 int direct;
597
598 direct = snd_seq_ev_is_direct(event);
599
600 dest = get_event_dest_client(event, filter);
601 if (dest == NULL)
602 goto __skip;
603 dest_port = snd_seq_port_use_ptr(dest, event->dest.port);
604 if (dest_port == NULL)
605 goto __skip;
606
607 /* check permission */
608 if (! check_port_perm(dest_port, SNDRV_SEQ_PORT_CAP_WRITE)) {
609 result = -EPERM;
610 goto __skip;
611 }
612
613 if (dest_port->timestamping)
614 update_timestamp_of_queue(event, dest_port->time_queue,
615 dest_port->time_real);
616
617 switch (dest->type) {
618 case USER_CLIENT:
619 if (dest->data.user.fifo)
620 result = snd_seq_fifo_event_in(dest->data.user.fifo, event);
621 break;
622
623 case KERNEL_CLIENT:
624 if (dest_port->event_input == NULL)
625 break;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100626 result = dest_port->event_input(event, direct,
627 dest_port->private_data,
628 atomic, hop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 break;
630 default:
631 break;
632 }
633
634 __skip:
635 if (dest_port)
636 snd_seq_port_unlock(dest_port);
637 if (dest)
638 snd_seq_client_unlock(dest);
639
640 if (result < 0 && !direct) {
641 result = bounce_error_event(client, event, result, atomic, hop);
642 }
643 return result;
644}
645
646
647/*
648 * send the event to all subscribers:
649 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100650static int deliver_to_subscribers(struct snd_seq_client *client,
651 struct snd_seq_event *event,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 int atomic, int hop)
653{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100654 struct snd_seq_subscribers *subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 int err = 0, num_ev = 0;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100656 struct snd_seq_event event_saved;
657 struct snd_seq_client_port *src_port;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100658 struct snd_seq_port_subs_info *grp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 src_port = snd_seq_port_use_ptr(client, event->source.port);
661 if (src_port == NULL)
662 return -EINVAL; /* invalid source port */
663 /* save original event record */
664 event_saved = *event;
665 grp = &src_port->c_src;
666
667 /* lock list */
668 if (atomic)
669 read_lock(&grp->list_lock);
670 else
671 down_read(&grp->list_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200672 list_for_each_entry(subs, &grp->list_head, src_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 event->dest = subs->info.dest;
674 if (subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)
675 /* convert time according to flag with subscription */
676 update_timestamp_of_queue(event, subs->info.queue,
677 subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL);
678 err = snd_seq_deliver_single_event(client, event,
679 0, atomic, hop);
680 if (err < 0)
681 break;
682 num_ev++;
683 /* restore original event record */
684 *event = event_saved;
685 }
686 if (atomic)
687 read_unlock(&grp->list_lock);
688 else
689 up_read(&grp->list_mutex);
690 *event = event_saved; /* restore */
691 snd_seq_port_unlock(src_port);
692 return (err < 0) ? err : num_ev;
693}
694
695
696#ifdef SUPPORT_BROADCAST
697/*
698 * broadcast to all ports:
699 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100700static int port_broadcast_event(struct snd_seq_client *client,
701 struct snd_seq_event *event,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 int atomic, int hop)
703{
704 int num_ev = 0, err = 0;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100705 struct snd_seq_client *dest_client;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200706 struct snd_seq_client_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 dest_client = get_event_dest_client(event, SNDRV_SEQ_FILTER_BROADCAST);
709 if (dest_client == NULL)
710 return 0; /* no matching destination */
711
712 read_lock(&dest_client->ports_lock);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200713 list_for_each_entry(port, &dest_client->ports_list_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 event->dest.port = port->addr.port;
715 /* pass NULL as source client to avoid error bounce */
716 err = snd_seq_deliver_single_event(NULL, event,
717 SNDRV_SEQ_FILTER_BROADCAST,
718 atomic, hop);
719 if (err < 0)
720 break;
721 num_ev++;
722 }
723 read_unlock(&dest_client->ports_lock);
724 snd_seq_client_unlock(dest_client);
725 event->dest.port = SNDRV_SEQ_ADDRESS_BROADCAST; /* restore */
726 return (err < 0) ? err : num_ev;
727}
728
729/*
730 * send the event to all clients:
731 * if destination port is also ADDRESS_BROADCAST, deliver to all ports.
732 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100733static int broadcast_event(struct snd_seq_client *client,
734 struct snd_seq_event *event, int atomic, int hop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 int err = 0, num_ev = 0;
737 int dest;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100738 struct snd_seq_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 addr = event->dest; /* save */
741
742 for (dest = 0; dest < SNDRV_SEQ_MAX_CLIENTS; dest++) {
743 /* don't send to itself */
744 if (dest == client->number)
745 continue;
746 event->dest.client = dest;
747 event->dest.port = addr.port;
748 if (addr.port == SNDRV_SEQ_ADDRESS_BROADCAST)
749 err = port_broadcast_event(client, event, atomic, hop);
750 else
751 /* pass NULL as source client to avoid error bounce */
752 err = snd_seq_deliver_single_event(NULL, event,
753 SNDRV_SEQ_FILTER_BROADCAST,
754 atomic, hop);
755 if (err < 0)
756 break;
757 num_ev += err;
758 }
759 event->dest = addr; /* restore */
760 return (err < 0) ? err : num_ev;
761}
762
763
764/* multicast - not supported yet */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100765static int multicast_event(struct snd_seq_client *client, struct snd_seq_event *event,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 int atomic, int hop)
767{
768 snd_printd("seq: multicast not supported yet.\n");
769 return 0; /* ignored */
770}
771#endif /* SUPPORT_BROADCAST */
772
773
774/* deliver an event to the destination port(s).
775 * if the event is to subscribers or broadcast, the event is dispatched
776 * to multiple targets.
777 *
778 * RETURN VALUE: n > 0 : the number of delivered events.
779 * n == 0 : the event was not passed to any client.
780 * n < 0 : error - event was not processed.
781 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100782static int snd_seq_deliver_event(struct snd_seq_client *client, struct snd_seq_event *event,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 int atomic, int hop)
784{
785 int result;
786
787 hop++;
788 if (hop >= SNDRV_SEQ_MAX_HOPS) {
789 snd_printd("too long delivery path (%d:%d->%d:%d)\n",
790 event->source.client, event->source.port,
791 event->dest.client, event->dest.port);
792 return -EMLINK;
793 }
794
795 if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS ||
796 event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS)
797 result = deliver_to_subscribers(client, event, atomic, hop);
798#ifdef SUPPORT_BROADCAST
799 else if (event->queue == SNDRV_SEQ_ADDRESS_BROADCAST ||
800 event->dest.client == SNDRV_SEQ_ADDRESS_BROADCAST)
801 result = broadcast_event(client, event, atomic, hop);
802 else if (event->dest.client >= SNDRV_SEQ_MAX_CLIENTS)
803 result = multicast_event(client, event, atomic, hop);
804 else if (event->dest.port == SNDRV_SEQ_ADDRESS_BROADCAST)
805 result = port_broadcast_event(client, event, atomic, hop);
806#endif
807 else
808 result = snd_seq_deliver_single_event(client, event, 0, atomic, hop);
809
810 return result;
811}
812
813/*
814 * dispatch an event cell:
815 * This function is called only from queue check routines in timer
816 * interrupts or after enqueued.
817 * The event cell shall be released or re-queued in this function.
818 *
819 * RETURN VALUE: n > 0 : the number of delivered events.
820 * n == 0 : the event was not passed to any client.
821 * n < 0 : error - event was not processed.
822 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100823int snd_seq_dispatch_event(struct snd_seq_event_cell *cell, int atomic, int hop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100825 struct snd_seq_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 int result;
827
828 snd_assert(cell != NULL, return -EINVAL);
829
830 client = snd_seq_client_use_ptr(cell->event.source.client);
831 if (client == NULL) {
832 snd_seq_cell_free(cell); /* release this cell */
833 return -EINVAL;
834 }
835
836 if (cell->event.type == SNDRV_SEQ_EVENT_NOTE) {
837 /* NOTE event:
838 * the event cell is re-used as a NOTE-OFF event and
839 * enqueued again.
840 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100841 struct snd_seq_event tmpev, *ev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /* reserve this event to enqueue note-off later */
844 tmpev = cell->event;
845 tmpev.type = SNDRV_SEQ_EVENT_NOTEON;
846 result = snd_seq_deliver_event(client, &tmpev, atomic, hop);
847
848 /*
849 * This was originally a note event. We now re-use the
850 * cell for the note-off event.
851 */
852
853 ev = &cell->event;
854 ev->type = SNDRV_SEQ_EVENT_NOTEOFF;
855 ev->flags |= SNDRV_SEQ_PRIORITY_HIGH;
856
857 /* add the duration time */
858 switch (ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) {
859 case SNDRV_SEQ_TIME_STAMP_TICK:
860 ev->time.tick += ev->data.note.duration;
861 break;
862 case SNDRV_SEQ_TIME_STAMP_REAL:
863 /* unit for duration is ms */
864 ev->time.time.tv_nsec += 1000000 * (ev->data.note.duration % 1000);
865 ev->time.time.tv_sec += ev->data.note.duration / 1000 +
866 ev->time.time.tv_nsec / 1000000000;
867 ev->time.time.tv_nsec %= 1000000000;
868 break;
869 }
870 ev->data.note.velocity = ev->data.note.off_velocity;
871
872 /* Now queue this cell as the note off event */
873 if (snd_seq_enqueue_event(cell, atomic, hop) < 0)
874 snd_seq_cell_free(cell); /* release this cell */
875
876 } else {
877 /* Normal events:
878 * event cell is freed after processing the event
879 */
880
881 result = snd_seq_deliver_event(client, &cell->event, atomic, hop);
882 snd_seq_cell_free(cell);
883 }
884
885 snd_seq_client_unlock(client);
886 return result;
887}
888
889
890/* Allocate a cell from client pool and enqueue it to queue:
891 * if pool is empty and blocking is TRUE, sleep until a new cell is
892 * available.
893 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100894static int snd_seq_client_enqueue_event(struct snd_seq_client *client,
895 struct snd_seq_event *event,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 struct file *file, int blocking,
897 int atomic, int hop)
898{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100899 struct snd_seq_event_cell *cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 int err;
901
902 /* special queue values - force direct passing */
903 if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) {
904 event->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
905 event->queue = SNDRV_SEQ_QUEUE_DIRECT;
906 } else
907#ifdef SUPPORT_BROADCAST
908 if (event->queue == SNDRV_SEQ_ADDRESS_BROADCAST) {
909 event->dest.client = SNDRV_SEQ_ADDRESS_BROADCAST;
910 event->queue = SNDRV_SEQ_QUEUE_DIRECT;
911 }
912#endif
913 if (event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) {
914 /* check presence of source port */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100915 struct snd_seq_client_port *src_port = snd_seq_port_use_ptr(client, event->source.port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (src_port == NULL)
917 return -EINVAL;
918 snd_seq_port_unlock(src_port);
919 }
920
921 /* direct event processing without enqueued */
922 if (snd_seq_ev_is_direct(event)) {
923 if (event->type == SNDRV_SEQ_EVENT_NOTE)
924 return -EINVAL; /* this event must be enqueued! */
925 return snd_seq_deliver_event(client, event, atomic, hop);
926 }
927
928 /* Not direct, normal queuing */
929 if (snd_seq_queue_is_used(event->queue, client->number) <= 0)
930 return -EINVAL; /* invalid queue */
931 if (! snd_seq_write_pool_allocated(client))
932 return -ENXIO; /* queue is not allocated */
933
934 /* allocate an event cell */
935 err = snd_seq_event_dup(client->pool, event, &cell, !blocking || atomic, file);
936 if (err < 0)
937 return err;
938
939 /* we got a cell. enqueue it. */
940 if ((err = snd_seq_enqueue_event(cell, atomic, hop)) < 0) {
941 snd_seq_cell_free(cell);
942 return err;
943 }
944
945 return 0;
946}
947
948
949/*
950 * check validity of event type and data length.
951 * return non-zero if invalid.
952 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100953static int check_event_type_and_length(struct snd_seq_event *ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 switch (snd_seq_ev_length_type(ev)) {
956 case SNDRV_SEQ_EVENT_LENGTH_FIXED:
957 if (snd_seq_ev_is_variable_type(ev))
958 return -EINVAL;
959 break;
960 case SNDRV_SEQ_EVENT_LENGTH_VARIABLE:
961 if (! snd_seq_ev_is_variable_type(ev) ||
962 (ev->data.ext.len & ~SNDRV_SEQ_EXT_MASK) >= SNDRV_SEQ_MAX_EVENT_LEN)
963 return -EINVAL;
964 break;
965 case SNDRV_SEQ_EVENT_LENGTH_VARUSR:
Takashi Iwaie5723b42007-10-30 12:17:17 +0100966 if (! snd_seq_ev_is_direct(ev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return -EINVAL;
968 break;
969 }
970 return 0;
971}
972
973
974/* handle write() */
975/* possible error values:
976 * -ENXIO invalid client or file open mode
977 * -ENOMEM malloc failed
978 * -EFAULT seg. fault during copy from user space
979 * -EINVAL invalid event
980 * -EAGAIN no space in output pool
981 * -EINTR interrupts while sleep
982 * -EMLINK too many hops
983 * others depends on return value from driver callback
984 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100985static ssize_t snd_seq_write(struct file *file, const char __user *buf,
986 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100988 struct snd_seq_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 int written = 0, len;
990 int err = -EINVAL;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100991 struct snd_seq_event event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT))
994 return -ENXIO;
995
996 /* check client structures are in place */
997 snd_assert(client != NULL, return -ENXIO);
998
999 if (!client->accept_output || client->pool == NULL)
1000 return -ENXIO;
1001
1002 /* allocate the pool now if the pool is not allocated yet */
1003 if (client->pool->size > 0 && !snd_seq_write_pool_allocated(client)) {
1004 if (snd_seq_pool_init(client->pool) < 0)
1005 return -ENOMEM;
1006 }
1007
1008 /* only process whole events */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001009 while (count >= sizeof(struct snd_seq_event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /* Read in the event header from the user */
1011 len = sizeof(event);
1012 if (copy_from_user(&event, buf, len)) {
1013 err = -EFAULT;
1014 break;
1015 }
1016 event.source.client = client->number; /* fill in client number */
1017 /* Check for extension data length */
1018 if (check_event_type_and_length(&event)) {
1019 err = -EINVAL;
1020 break;
1021 }
1022
1023 /* check for special events */
1024 if (event.type == SNDRV_SEQ_EVENT_NONE)
1025 goto __skip_event;
1026 else if (snd_seq_ev_is_reserved(&event)) {
1027 err = -EINVAL;
1028 break;
1029 }
1030
1031 if (snd_seq_ev_is_variable(&event)) {
1032 int extlen = event.data.ext.len & ~SNDRV_SEQ_EXT_MASK;
1033 if ((size_t)(extlen + len) > count) {
1034 /* back out, will get an error this time or next */
1035 err = -EINVAL;
1036 break;
1037 }
1038 /* set user space pointer */
1039 event.data.ext.len = extlen | SNDRV_SEQ_EXT_USRPTR;
Clemens Ladisch4d233592005-09-05 10:35:20 +02001040 event.data.ext.ptr = (char __force *)buf
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001041 + sizeof(struct snd_seq_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 len += extlen; /* increment data length */
1043 } else {
1044#ifdef CONFIG_COMPAT
1045 if (client->convert32 && snd_seq_ev_is_varusr(&event)) {
1046 void *ptr = compat_ptr(event.data.raw32.d[1]);
1047 event.data.ext.ptr = ptr;
1048 }
1049#endif
1050 }
1051
1052 /* ok, enqueue it */
1053 err = snd_seq_client_enqueue_event(client, &event, file,
1054 !(file->f_flags & O_NONBLOCK),
1055 0, 0);
1056 if (err < 0)
1057 break;
1058
1059 __skip_event:
1060 /* Update pointers and counts */
1061 count -= len;
1062 buf += len;
1063 written += len;
1064 }
1065
1066 return written ? written : err;
1067}
1068
1069
1070/*
1071 * handle polling
1072 */
1073static unsigned int snd_seq_poll(struct file *file, poll_table * wait)
1074{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001075 struct snd_seq_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 unsigned int mask = 0;
1077
1078 /* check client structures are in place */
1079 snd_assert(client != NULL, return -ENXIO);
1080
1081 if ((snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT) &&
1082 client->data.user.fifo) {
1083
1084 /* check if data is available in the outqueue */
1085 if (snd_seq_fifo_poll_wait(client->data.user.fifo, file, wait))
1086 mask |= POLLIN | POLLRDNORM;
1087 }
1088
1089 if (snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT) {
1090
1091 /* check if data is available in the pool */
1092 if (!snd_seq_write_pool_allocated(client) ||
1093 snd_seq_pool_poll_wait(client->pool, file, wait))
1094 mask |= POLLOUT | POLLWRNORM;
1095 }
1096
1097 return mask;
1098}
1099
1100
1101/*-----------------------------------------------------*/
1102
1103
1104/* SYSTEM_INFO ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001105static int snd_seq_ioctl_system_info(struct snd_seq_client *client, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001107 struct snd_seq_system_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 memset(&info, 0, sizeof(info));
1110 /* fill the info fields */
1111 info.queues = SNDRV_SEQ_MAX_QUEUES;
1112 info.clients = SNDRV_SEQ_MAX_CLIENTS;
1113 info.ports = 256; /* fixed limit */
1114 info.channels = 256; /* fixed limit */
1115 info.cur_clients = client_usage.cur;
1116 info.cur_queues = snd_seq_queue_get_cur_queues();
1117
1118 if (copy_to_user(arg, &info, sizeof(info)))
1119 return -EFAULT;
1120 return 0;
1121}
1122
1123
1124/* RUNNING_MODE ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001125static int snd_seq_ioctl_running_mode(struct snd_seq_client *client, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001127 struct snd_seq_running_info info;
1128 struct snd_seq_client *cptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 int err = 0;
1130
1131 if (copy_from_user(&info, arg, sizeof(info)))
1132 return -EFAULT;
1133
1134 /* requested client number */
1135 cptr = snd_seq_client_use_ptr(info.client);
1136 if (cptr == NULL)
1137 return -ENOENT; /* don't change !!! */
1138
1139#ifdef SNDRV_BIG_ENDIAN
1140 if (! info.big_endian) {
1141 err = -EINVAL;
1142 goto __err;
1143 }
1144#else
1145 if (info.big_endian) {
1146 err = -EINVAL;
1147 goto __err;
1148 }
1149
1150#endif
1151 if (info.cpu_mode > sizeof(long)) {
1152 err = -EINVAL;
1153 goto __err;
1154 }
1155 cptr->convert32 = (info.cpu_mode < sizeof(long));
1156 __err:
1157 snd_seq_client_unlock(cptr);
1158 return err;
1159}
1160
1161/* CLIENT_INFO ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001162static void get_client_info(struct snd_seq_client *cptr,
1163 struct snd_seq_client_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 info->client = cptr->number;
1166
1167 /* fill the info fields */
1168 info->type = cptr->type;
1169 strcpy(info->name, cptr->name);
1170 info->filter = cptr->filter;
1171 info->event_lost = cptr->event_lost;
1172 memcpy(info->event_filter, cptr->event_filter, 32);
1173 info->num_ports = cptr->num_ports;
1174 memset(info->reserved, 0, sizeof(info->reserved));
1175}
1176
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001177static int snd_seq_ioctl_get_client_info(struct snd_seq_client *client,
1178 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001180 struct snd_seq_client *cptr;
1181 struct snd_seq_client_info client_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 if (copy_from_user(&client_info, arg, sizeof(client_info)))
1184 return -EFAULT;
1185
1186 /* requested client number */
1187 cptr = snd_seq_client_use_ptr(client_info.client);
1188 if (cptr == NULL)
1189 return -ENOENT; /* don't change !!! */
1190
1191 get_client_info(cptr, &client_info);
1192 snd_seq_client_unlock(cptr);
1193
1194 if (copy_to_user(arg, &client_info, sizeof(client_info)))
1195 return -EFAULT;
1196 return 0;
1197}
1198
1199
1200/* CLIENT_INFO ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001201static int snd_seq_ioctl_set_client_info(struct snd_seq_client *client,
1202 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001204 struct snd_seq_client_info client_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 if (copy_from_user(&client_info, arg, sizeof(client_info)))
1207 return -EFAULT;
1208
1209 /* it is not allowed to set the info fields for an another client */
1210 if (client->number != client_info.client)
1211 return -EPERM;
1212 /* also client type must be set now */
1213 if (client->type != client_info.type)
1214 return -EINVAL;
1215
1216 /* fill the info fields */
1217 if (client_info.name[0])
1218 strlcpy(client->name, client_info.name, sizeof(client->name));
1219
1220 client->filter = client_info.filter;
1221 client->event_lost = client_info.event_lost;
1222 memcpy(client->event_filter, client_info.event_filter, 32);
1223
1224 return 0;
1225}
1226
1227
1228/*
1229 * CREATE PORT ioctl()
1230 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001231static int snd_seq_ioctl_create_port(struct snd_seq_client *client,
1232 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001234 struct snd_seq_client_port *port;
1235 struct snd_seq_port_info info;
1236 struct snd_seq_port_callback *callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 if (copy_from_user(&info, arg, sizeof(info)))
1239 return -EFAULT;
1240
1241 /* it is not allowed to create the port for an another client */
1242 if (info.addr.client != client->number)
1243 return -EPERM;
1244
1245 port = snd_seq_create_port(client, (info.flags & SNDRV_SEQ_PORT_FLG_GIVEN_PORT) ? info.addr.port : -1);
1246 if (port == NULL)
1247 return -ENOMEM;
1248
1249 if (client->type == USER_CLIENT && info.kernel) {
1250 snd_seq_delete_port(client, port->addr.port);
1251 return -EINVAL;
1252 }
1253 if (client->type == KERNEL_CLIENT) {
1254 if ((callback = info.kernel) != NULL) {
1255 if (callback->owner)
1256 port->owner = callback->owner;
1257 port->private_data = callback->private_data;
1258 port->private_free = callback->private_free;
1259 port->callback_all = callback->callback_all;
1260 port->event_input = callback->event_input;
1261 port->c_src.open = callback->subscribe;
1262 port->c_src.close = callback->unsubscribe;
1263 port->c_dest.open = callback->use;
1264 port->c_dest.close = callback->unuse;
1265 }
1266 }
1267
1268 info.addr = port->addr;
1269
1270 snd_seq_set_port_info(port, &info);
1271 snd_seq_system_client_ev_port_start(port->addr.client, port->addr.port);
1272
1273 if (copy_to_user(arg, &info, sizeof(info)))
1274 return -EFAULT;
1275
1276 return 0;
1277}
1278
1279/*
1280 * DELETE PORT ioctl()
1281 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001282static int snd_seq_ioctl_delete_port(struct snd_seq_client *client,
1283 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001285 struct snd_seq_port_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int err;
1287
1288 /* set passed parameters */
1289 if (copy_from_user(&info, arg, sizeof(info)))
1290 return -EFAULT;
1291
1292 /* it is not allowed to remove the port for an another client */
1293 if (info.addr.client != client->number)
1294 return -EPERM;
1295
1296 err = snd_seq_delete_port(client, info.addr.port);
1297 if (err >= 0)
1298 snd_seq_system_client_ev_port_exit(client->number, info.addr.port);
1299 return err;
1300}
1301
1302
1303/*
1304 * GET_PORT_INFO ioctl() (on any client)
1305 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001306static int snd_seq_ioctl_get_port_info(struct snd_seq_client *client,
1307 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001309 struct snd_seq_client *cptr;
1310 struct snd_seq_client_port *port;
1311 struct snd_seq_port_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 if (copy_from_user(&info, arg, sizeof(info)))
1314 return -EFAULT;
1315 cptr = snd_seq_client_use_ptr(info.addr.client);
1316 if (cptr == NULL)
1317 return -ENXIO;
1318
1319 port = snd_seq_port_use_ptr(cptr, info.addr.port);
1320 if (port == NULL) {
1321 snd_seq_client_unlock(cptr);
1322 return -ENOENT; /* don't change */
1323 }
1324
1325 /* get port info */
1326 snd_seq_get_port_info(port, &info);
1327 snd_seq_port_unlock(port);
1328 snd_seq_client_unlock(cptr);
1329
1330 if (copy_to_user(arg, &info, sizeof(info)))
1331 return -EFAULT;
1332 return 0;
1333}
1334
1335
1336/*
1337 * SET_PORT_INFO ioctl() (only ports on this/own client)
1338 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001339static int snd_seq_ioctl_set_port_info(struct snd_seq_client *client,
1340 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001342 struct snd_seq_client_port *port;
1343 struct snd_seq_port_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 if (copy_from_user(&info, arg, sizeof(info)))
1346 return -EFAULT;
1347
1348 if (info.addr.client != client->number) /* only set our own ports ! */
1349 return -EPERM;
1350 port = snd_seq_port_use_ptr(client, info.addr.port);
1351 if (port) {
1352 snd_seq_set_port_info(port, &info);
1353 snd_seq_port_unlock(port);
1354 }
1355 return 0;
1356}
1357
1358
1359/*
1360 * port subscription (connection)
1361 */
1362#define PERM_RD (SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ)
1363#define PERM_WR (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_SUBS_WRITE)
1364
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001365static int check_subscription_permission(struct snd_seq_client *client,
1366 struct snd_seq_client_port *sport,
1367 struct snd_seq_client_port *dport,
1368 struct snd_seq_port_subscribe *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 if (client->number != subs->sender.client &&
1371 client->number != subs->dest.client) {
1372 /* connection by third client - check export permission */
1373 if (check_port_perm(sport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))
1374 return -EPERM;
1375 if (check_port_perm(dport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))
1376 return -EPERM;
1377 }
1378
1379 /* check read permission */
1380 /* if sender or receiver is the subscribing client itself,
1381 * no permission check is necessary
1382 */
1383 if (client->number != subs->sender.client) {
1384 if (! check_port_perm(sport, PERM_RD))
1385 return -EPERM;
1386 }
1387 /* check write permission */
1388 if (client->number != subs->dest.client) {
1389 if (! check_port_perm(dport, PERM_WR))
1390 return -EPERM;
1391 }
1392 return 0;
1393}
1394
1395/*
1396 * send an subscription notify event to user client:
1397 * client must be user client.
1398 */
1399int snd_seq_client_notify_subscription(int client, int port,
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001400 struct snd_seq_port_subscribe *info,
1401 int evtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001403 struct snd_seq_event event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 memset(&event, 0, sizeof(event));
1406 event.type = evtype;
1407 event.data.connect.dest = info->dest;
1408 event.data.connect.sender = info->sender;
1409
1410 return snd_seq_system_notify(client, port, &event); /* non-atomic */
1411}
1412
1413
1414/*
1415 * add to port's subscription list IOCTL interface
1416 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001417static int snd_seq_ioctl_subscribe_port(struct snd_seq_client *client,
1418 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 int result = -EINVAL;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001421 struct snd_seq_client *receiver = NULL, *sender = NULL;
1422 struct snd_seq_client_port *sport = NULL, *dport = NULL;
1423 struct snd_seq_port_subscribe subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 if (copy_from_user(&subs, arg, sizeof(subs)))
1426 return -EFAULT;
1427
1428 if ((receiver = snd_seq_client_use_ptr(subs.dest.client)) == NULL)
1429 goto __end;
1430 if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)
1431 goto __end;
1432 if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)
1433 goto __end;
1434 if ((dport = snd_seq_port_use_ptr(receiver, subs.dest.port)) == NULL)
1435 goto __end;
1436
1437 result = check_subscription_permission(client, sport, dport, &subs);
1438 if (result < 0)
1439 goto __end;
1440
1441 /* connect them */
1442 result = snd_seq_port_connect(client, sender, sport, receiver, dport, &subs);
1443 if (! result) /* broadcast announce */
1444 snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,
1445 &subs, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
1446 __end:
1447 if (sport)
1448 snd_seq_port_unlock(sport);
1449 if (dport)
1450 snd_seq_port_unlock(dport);
1451 if (sender)
1452 snd_seq_client_unlock(sender);
1453 if (receiver)
1454 snd_seq_client_unlock(receiver);
1455 return result;
1456}
1457
1458
1459/*
1460 * remove from port's subscription list
1461 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001462static int snd_seq_ioctl_unsubscribe_port(struct snd_seq_client *client,
1463 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 int result = -ENXIO;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001466 struct snd_seq_client *receiver = NULL, *sender = NULL;
1467 struct snd_seq_client_port *sport = NULL, *dport = NULL;
1468 struct snd_seq_port_subscribe subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 if (copy_from_user(&subs, arg, sizeof(subs)))
1471 return -EFAULT;
1472
1473 if ((receiver = snd_seq_client_use_ptr(subs.dest.client)) == NULL)
1474 goto __end;
1475 if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)
1476 goto __end;
1477 if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)
1478 goto __end;
1479 if ((dport = snd_seq_port_use_ptr(receiver, subs.dest.port)) == NULL)
1480 goto __end;
1481
1482 result = check_subscription_permission(client, sport, dport, &subs);
1483 if (result < 0)
1484 goto __end;
1485
1486 result = snd_seq_port_disconnect(client, sender, sport, receiver, dport, &subs);
1487 if (! result) /* broadcast announce */
1488 snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,
1489 &subs, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
1490 __end:
1491 if (sport)
1492 snd_seq_port_unlock(sport);
1493 if (dport)
1494 snd_seq_port_unlock(dport);
1495 if (sender)
1496 snd_seq_client_unlock(sender);
1497 if (receiver)
1498 snd_seq_client_unlock(receiver);
1499 return result;
1500}
1501
1502
1503/* CREATE_QUEUE ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001504static int snd_seq_ioctl_create_queue(struct snd_seq_client *client,
1505 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001507 struct snd_seq_queue_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 int result;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001509 struct snd_seq_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 if (copy_from_user(&info, arg, sizeof(info)))
1512 return -EFAULT;
1513
1514 result = snd_seq_queue_alloc(client->number, info.locked, info.flags);
1515 if (result < 0)
1516 return result;
1517
1518 q = queueptr(result);
1519 if (q == NULL)
1520 return -EINVAL;
1521
1522 info.queue = q->queue;
1523 info.locked = q->locked;
1524 info.owner = q->owner;
1525
1526 /* set queue name */
1527 if (! info.name[0])
1528 snprintf(info.name, sizeof(info.name), "Queue-%d", q->queue);
1529 strlcpy(q->name, info.name, sizeof(q->name));
1530 queuefree(q);
1531
1532 if (copy_to_user(arg, &info, sizeof(info)))
1533 return -EFAULT;
1534
1535 return 0;
1536}
1537
1538/* DELETE_QUEUE ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001539static int snd_seq_ioctl_delete_queue(struct snd_seq_client *client,
1540 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001542 struct snd_seq_queue_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 if (copy_from_user(&info, arg, sizeof(info)))
1545 return -EFAULT;
1546
1547 return snd_seq_queue_delete(client->number, info.queue);
1548}
1549
1550/* GET_QUEUE_INFO ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001551static int snd_seq_ioctl_get_queue_info(struct snd_seq_client *client,
1552 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001554 struct snd_seq_queue_info info;
1555 struct snd_seq_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 if (copy_from_user(&info, arg, sizeof(info)))
1558 return -EFAULT;
1559
1560 q = queueptr(info.queue);
1561 if (q == NULL)
1562 return -EINVAL;
1563
1564 memset(&info, 0, sizeof(info));
1565 info.queue = q->queue;
1566 info.owner = q->owner;
1567 info.locked = q->locked;
1568 strlcpy(info.name, q->name, sizeof(info.name));
1569 queuefree(q);
1570
1571 if (copy_to_user(arg, &info, sizeof(info)))
1572 return -EFAULT;
1573
1574 return 0;
1575}
1576
1577/* SET_QUEUE_INFO ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001578static int snd_seq_ioctl_set_queue_info(struct snd_seq_client *client,
1579 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001581 struct snd_seq_queue_info info;
1582 struct snd_seq_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 if (copy_from_user(&info, arg, sizeof(info)))
1585 return -EFAULT;
1586
1587 if (info.owner != client->number)
1588 return -EINVAL;
1589
1590 /* change owner/locked permission */
1591 if (snd_seq_queue_check_access(info.queue, client->number)) {
1592 if (snd_seq_queue_set_owner(info.queue, client->number, info.locked) < 0)
1593 return -EPERM;
1594 if (info.locked)
1595 snd_seq_queue_use(info.queue, client->number, 1);
1596 } else {
1597 return -EPERM;
1598 }
1599
1600 q = queueptr(info.queue);
1601 if (! q)
1602 return -EINVAL;
1603 if (q->owner != client->number) {
1604 queuefree(q);
1605 return -EPERM;
1606 }
1607 strlcpy(q->name, info.name, sizeof(q->name));
1608 queuefree(q);
1609
1610 return 0;
1611}
1612
1613/* GET_NAMED_QUEUE ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001614static int snd_seq_ioctl_get_named_queue(struct snd_seq_client *client, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001616 struct snd_seq_queue_info info;
1617 struct snd_seq_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 if (copy_from_user(&info, arg, sizeof(info)))
1620 return -EFAULT;
1621
1622 q = snd_seq_queue_find_name(info.name);
1623 if (q == NULL)
1624 return -EINVAL;
1625 info.queue = q->queue;
1626 info.owner = q->owner;
1627 info.locked = q->locked;
1628 queuefree(q);
1629
1630 if (copy_to_user(arg, &info, sizeof(info)))
1631 return -EFAULT;
1632
1633 return 0;
1634}
1635
1636/* GET_QUEUE_STATUS ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001637static int snd_seq_ioctl_get_queue_status(struct snd_seq_client *client,
1638 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001640 struct snd_seq_queue_status status;
1641 struct snd_seq_queue *queue;
1642 struct snd_seq_timer *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 if (copy_from_user(&status, arg, sizeof(status)))
1645 return -EFAULT;
1646
1647 queue = queueptr(status.queue);
1648 if (queue == NULL)
1649 return -EINVAL;
1650 memset(&status, 0, sizeof(status));
1651 status.queue = queue->queue;
1652
1653 tmr = queue->timer;
1654 status.events = queue->tickq->cells + queue->timeq->cells;
1655
1656 status.time = snd_seq_timer_get_cur_time(tmr);
1657 status.tick = snd_seq_timer_get_cur_tick(tmr);
1658
1659 status.running = tmr->running;
1660
1661 status.flags = queue->flags;
1662 queuefree(queue);
1663
1664 if (copy_to_user(arg, &status, sizeof(status)))
1665 return -EFAULT;
1666 return 0;
1667}
1668
1669
1670/* GET_QUEUE_TEMPO ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001671static int snd_seq_ioctl_get_queue_tempo(struct snd_seq_client *client,
1672 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001674 struct snd_seq_queue_tempo tempo;
1675 struct snd_seq_queue *queue;
1676 struct snd_seq_timer *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 if (copy_from_user(&tempo, arg, sizeof(tempo)))
1679 return -EFAULT;
1680
1681 queue = queueptr(tempo.queue);
1682 if (queue == NULL)
1683 return -EINVAL;
1684 memset(&tempo, 0, sizeof(tempo));
1685 tempo.queue = queue->queue;
1686
1687 tmr = queue->timer;
1688
1689 tempo.tempo = tmr->tempo;
1690 tempo.ppq = tmr->ppq;
1691 tempo.skew_value = tmr->skew;
1692 tempo.skew_base = tmr->skew_base;
1693 queuefree(queue);
1694
1695 if (copy_to_user(arg, &tempo, sizeof(tempo)))
1696 return -EFAULT;
1697 return 0;
1698}
1699
1700
1701/* SET_QUEUE_TEMPO ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001702int snd_seq_set_queue_tempo(int client, struct snd_seq_queue_tempo *tempo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
1704 if (!snd_seq_queue_check_access(tempo->queue, client))
1705 return -EPERM;
1706 return snd_seq_queue_timer_set_tempo(tempo->queue, client, tempo);
1707}
1708
Takashi Iwai91715ed2006-04-28 15:13:39 +02001709EXPORT_SYMBOL(snd_seq_set_queue_tempo);
1710
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001711static int snd_seq_ioctl_set_queue_tempo(struct snd_seq_client *client,
1712 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 int result;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001715 struct snd_seq_queue_tempo tempo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 if (copy_from_user(&tempo, arg, sizeof(tempo)))
1718 return -EFAULT;
1719
1720 result = snd_seq_set_queue_tempo(client->number, &tempo);
1721 return result < 0 ? result : 0;
1722}
1723
1724
1725/* GET_QUEUE_TIMER ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001726static int snd_seq_ioctl_get_queue_timer(struct snd_seq_client *client,
1727 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001729 struct snd_seq_queue_timer timer;
1730 struct snd_seq_queue *queue;
1731 struct snd_seq_timer *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 if (copy_from_user(&timer, arg, sizeof(timer)))
1734 return -EFAULT;
1735
1736 queue = queueptr(timer.queue);
1737 if (queue == NULL)
1738 return -EINVAL;
1739
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001740 if (mutex_lock_interruptible(&queue->timer_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 queuefree(queue);
1742 return -ERESTARTSYS;
1743 }
1744 tmr = queue->timer;
1745 memset(&timer, 0, sizeof(timer));
1746 timer.queue = queue->queue;
1747
1748 timer.type = tmr->type;
1749 if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
1750 timer.u.alsa.id = tmr->alsa_id;
1751 timer.u.alsa.resolution = tmr->preferred_resolution;
1752 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001753 mutex_unlock(&queue->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 queuefree(queue);
1755
1756 if (copy_to_user(arg, &timer, sizeof(timer)))
1757 return -EFAULT;
1758 return 0;
1759}
1760
1761
1762/* SET_QUEUE_TIMER ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001763static int snd_seq_ioctl_set_queue_timer(struct snd_seq_client *client,
1764 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
1766 int result = 0;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001767 struct snd_seq_queue_timer timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 if (copy_from_user(&timer, arg, sizeof(timer)))
1770 return -EFAULT;
1771
1772 if (timer.type != SNDRV_SEQ_TIMER_ALSA)
1773 return -EINVAL;
1774
1775 if (snd_seq_queue_check_access(timer.queue, client->number)) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001776 struct snd_seq_queue *q;
1777 struct snd_seq_timer *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 q = queueptr(timer.queue);
1780 if (q == NULL)
1781 return -ENXIO;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001782 if (mutex_lock_interruptible(&q->timer_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 queuefree(q);
1784 return -ERESTARTSYS;
1785 }
1786 tmr = q->timer;
1787 snd_seq_queue_timer_close(timer.queue);
1788 tmr->type = timer.type;
1789 if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
1790 tmr->alsa_id = timer.u.alsa.id;
1791 tmr->preferred_resolution = timer.u.alsa.resolution;
1792 }
1793 result = snd_seq_queue_timer_open(timer.queue);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001794 mutex_unlock(&q->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 queuefree(q);
1796 } else {
1797 return -EPERM;
1798 }
1799
1800 return result;
1801}
1802
1803
1804/* GET_QUEUE_CLIENT ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001805static int snd_seq_ioctl_get_queue_client(struct snd_seq_client *client,
1806 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001808 struct snd_seq_queue_client info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 int used;
1810
1811 if (copy_from_user(&info, arg, sizeof(info)))
1812 return -EFAULT;
1813
1814 used = snd_seq_queue_is_used(info.queue, client->number);
1815 if (used < 0)
1816 return -EINVAL;
1817 info.used = used;
1818 info.client = client->number;
1819
1820 if (copy_to_user(arg, &info, sizeof(info)))
1821 return -EFAULT;
1822 return 0;
1823}
1824
1825
1826/* SET_QUEUE_CLIENT ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001827static int snd_seq_ioctl_set_queue_client(struct snd_seq_client *client,
1828 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830 int err;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001831 struct snd_seq_queue_client info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 if (copy_from_user(&info, arg, sizeof(info)))
1834 return -EFAULT;
1835
1836 if (info.used >= 0) {
1837 err = snd_seq_queue_use(info.queue, client->number, info.used);
1838 if (err < 0)
1839 return err;
1840 }
1841
1842 return snd_seq_ioctl_get_queue_client(client, arg);
1843}
1844
1845
1846/* GET_CLIENT_POOL ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001847static int snd_seq_ioctl_get_client_pool(struct snd_seq_client *client,
1848 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001850 struct snd_seq_client_pool info;
1851 struct snd_seq_client *cptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 if (copy_from_user(&info, arg, sizeof(info)))
1854 return -EFAULT;
1855
1856 cptr = snd_seq_client_use_ptr(info.client);
1857 if (cptr == NULL)
1858 return -ENOENT;
1859 memset(&info, 0, sizeof(info));
1860 info.output_pool = cptr->pool->size;
1861 info.output_room = cptr->pool->room;
1862 info.output_free = info.output_pool;
Eugene Teoe64d2e32006-03-17 16:32:17 +01001863 info.output_free = snd_seq_unused_cells(cptr->pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (cptr->type == USER_CLIENT) {
1865 info.input_pool = cptr->data.user.fifo_pool_size;
1866 info.input_free = info.input_pool;
1867 if (cptr->data.user.fifo)
1868 info.input_free = snd_seq_unused_cells(cptr->data.user.fifo->pool);
1869 } else {
1870 info.input_pool = 0;
1871 info.input_free = 0;
1872 }
1873 snd_seq_client_unlock(cptr);
1874
1875 if (copy_to_user(arg, &info, sizeof(info)))
1876 return -EFAULT;
1877 return 0;
1878}
1879
1880/* SET_CLIENT_POOL ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001881static int snd_seq_ioctl_set_client_pool(struct snd_seq_client *client,
1882 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001884 struct snd_seq_client_pool info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 int rc;
1886
1887 if (copy_from_user(&info, arg, sizeof(info)))
1888 return -EFAULT;
1889
1890 if (client->number != info.client)
1891 return -EINVAL; /* can't change other clients */
1892
1893 if (info.output_pool >= 1 && info.output_pool <= SNDRV_SEQ_MAX_EVENTS &&
1894 (! snd_seq_write_pool_allocated(client) ||
1895 info.output_pool != client->pool->size)) {
1896 if (snd_seq_write_pool_allocated(client)) {
1897 /* remove all existing cells */
1898 snd_seq_queue_client_leave_cells(client->number);
1899 snd_seq_pool_done(client->pool);
1900 }
1901 client->pool->size = info.output_pool;
1902 rc = snd_seq_pool_init(client->pool);
1903 if (rc < 0)
1904 return rc;
1905 }
1906 if (client->type == USER_CLIENT && client->data.user.fifo != NULL &&
1907 info.input_pool >= 1 &&
1908 info.input_pool <= SNDRV_SEQ_MAX_CLIENT_EVENTS &&
1909 info.input_pool != client->data.user.fifo_pool_size) {
1910 /* change pool size */
1911 rc = snd_seq_fifo_resize(client->data.user.fifo, info.input_pool);
1912 if (rc < 0)
1913 return rc;
1914 client->data.user.fifo_pool_size = info.input_pool;
1915 }
1916 if (info.output_room >= 1 &&
1917 info.output_room <= client->pool->size) {
1918 client->pool->room = info.output_room;
1919 }
1920
1921 return snd_seq_ioctl_get_client_pool(client, arg);
1922}
1923
1924
1925/* REMOVE_EVENTS ioctl() */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001926static int snd_seq_ioctl_remove_events(struct snd_seq_client *client,
1927 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001929 struct snd_seq_remove_events info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 if (copy_from_user(&info, arg, sizeof(info)))
1932 return -EFAULT;
1933
1934 /*
1935 * Input mostly not implemented XXX.
1936 */
1937 if (info.remove_mode & SNDRV_SEQ_REMOVE_INPUT) {
1938 /*
1939 * No restrictions so for a user client we can clear
1940 * the whole fifo
1941 */
1942 if (client->type == USER_CLIENT)
1943 snd_seq_fifo_clear(client->data.user.fifo);
1944 }
1945
1946 if (info.remove_mode & SNDRV_SEQ_REMOVE_OUTPUT)
1947 snd_seq_queue_remove_cells(client->number, &info);
1948
1949 return 0;
1950}
1951
1952
1953/*
1954 * get subscription info
1955 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001956static int snd_seq_ioctl_get_subscription(struct snd_seq_client *client,
1957 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959 int result;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001960 struct snd_seq_client *sender = NULL;
1961 struct snd_seq_client_port *sport = NULL;
1962 struct snd_seq_port_subscribe subs;
1963 struct snd_seq_subscribers *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 if (copy_from_user(&subs, arg, sizeof(subs)))
1966 return -EFAULT;
1967
1968 result = -EINVAL;
1969 if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)
1970 goto __end;
1971 if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)
1972 goto __end;
1973 p = snd_seq_port_get_subscription(&sport->c_src, &subs.dest);
1974 if (p) {
1975 result = 0;
1976 subs = p->info;
1977 } else
1978 result = -ENOENT;
1979
1980 __end:
1981 if (sport)
1982 snd_seq_port_unlock(sport);
1983 if (sender)
1984 snd_seq_client_unlock(sender);
1985 if (result >= 0) {
1986 if (copy_to_user(arg, &subs, sizeof(subs)))
1987 return -EFAULT;
1988 }
1989 return result;
1990}
1991
1992
1993/*
1994 * get subscription info - check only its presence
1995 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01001996static int snd_seq_ioctl_query_subs(struct snd_seq_client *client,
1997 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
1999 int result = -ENXIO;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002000 struct snd_seq_client *cptr = NULL;
2001 struct snd_seq_client_port *port = NULL;
2002 struct snd_seq_query_subs subs;
2003 struct snd_seq_port_subs_info *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 struct list_head *p;
2005 int i;
2006
2007 if (copy_from_user(&subs, arg, sizeof(subs)))
2008 return -EFAULT;
2009
2010 if ((cptr = snd_seq_client_use_ptr(subs.root.client)) == NULL)
2011 goto __end;
2012 if ((port = snd_seq_port_use_ptr(cptr, subs.root.port)) == NULL)
2013 goto __end;
2014
2015 switch (subs.type) {
2016 case SNDRV_SEQ_QUERY_SUBS_READ:
2017 group = &port->c_src;
2018 break;
2019 case SNDRV_SEQ_QUERY_SUBS_WRITE:
2020 group = &port->c_dest;
2021 break;
2022 default:
2023 goto __end;
2024 }
2025
2026 down_read(&group->list_mutex);
2027 /* search for the subscriber */
2028 subs.num_subs = group->count;
2029 i = 0;
2030 result = -ENOENT;
2031 list_for_each(p, &group->list_head) {
2032 if (i++ == subs.index) {
2033 /* found! */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002034 struct snd_seq_subscribers *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (subs.type == SNDRV_SEQ_QUERY_SUBS_READ) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002036 s = list_entry(p, struct snd_seq_subscribers, src_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 subs.addr = s->info.dest;
2038 } else {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002039 s = list_entry(p, struct snd_seq_subscribers, dest_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 subs.addr = s->info.sender;
2041 }
2042 subs.flags = s->info.flags;
2043 subs.queue = s->info.queue;
2044 result = 0;
2045 break;
2046 }
2047 }
2048 up_read(&group->list_mutex);
2049
2050 __end:
2051 if (port)
2052 snd_seq_port_unlock(port);
2053 if (cptr)
2054 snd_seq_client_unlock(cptr);
2055 if (result >= 0) {
2056 if (copy_to_user(arg, &subs, sizeof(subs)))
2057 return -EFAULT;
2058 }
2059 return result;
2060}
2061
2062
2063/*
2064 * query next client
2065 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002066static int snd_seq_ioctl_query_next_client(struct snd_seq_client *client,
2067 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002069 struct snd_seq_client *cptr = NULL;
2070 struct snd_seq_client_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 if (copy_from_user(&info, arg, sizeof(info)))
2073 return -EFAULT;
2074
2075 /* search for next client */
2076 info.client++;
2077 if (info.client < 0)
2078 info.client = 0;
2079 for (; info.client < SNDRV_SEQ_MAX_CLIENTS; info.client++) {
2080 cptr = snd_seq_client_use_ptr(info.client);
2081 if (cptr)
2082 break; /* found */
2083 }
2084 if (cptr == NULL)
2085 return -ENOENT;
2086
2087 get_client_info(cptr, &info);
2088 snd_seq_client_unlock(cptr);
2089
2090 if (copy_to_user(arg, &info, sizeof(info)))
2091 return -EFAULT;
2092 return 0;
2093}
2094
2095/*
2096 * query next port
2097 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002098static int snd_seq_ioctl_query_next_port(struct snd_seq_client *client,
2099 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002101 struct snd_seq_client *cptr;
2102 struct snd_seq_client_port *port = NULL;
2103 struct snd_seq_port_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 if (copy_from_user(&info, arg, sizeof(info)))
2106 return -EFAULT;
2107 cptr = snd_seq_client_use_ptr(info.addr.client);
2108 if (cptr == NULL)
2109 return -ENXIO;
2110
2111 /* search for next port */
2112 info.addr.port++;
2113 port = snd_seq_port_query_nearest(cptr, &info);
2114 if (port == NULL) {
2115 snd_seq_client_unlock(cptr);
2116 return -ENOENT;
2117 }
2118
2119 /* get port info */
2120 info.addr = port->addr;
2121 snd_seq_get_port_info(port, &info);
2122 snd_seq_port_unlock(port);
2123 snd_seq_client_unlock(cptr);
2124
2125 if (copy_to_user(arg, &info, sizeof(info)))
2126 return -EFAULT;
2127 return 0;
2128}
2129
2130/* -------------------------------------------------------- */
2131
2132static struct seq_ioctl_table {
2133 unsigned int cmd;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002134 int (*func)(struct snd_seq_client *client, void __user * arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135} ioctl_tables[] = {
2136 { SNDRV_SEQ_IOCTL_SYSTEM_INFO, snd_seq_ioctl_system_info },
2137 { SNDRV_SEQ_IOCTL_RUNNING_MODE, snd_seq_ioctl_running_mode },
2138 { SNDRV_SEQ_IOCTL_GET_CLIENT_INFO, snd_seq_ioctl_get_client_info },
2139 { SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, snd_seq_ioctl_set_client_info },
2140 { SNDRV_SEQ_IOCTL_CREATE_PORT, snd_seq_ioctl_create_port },
2141 { SNDRV_SEQ_IOCTL_DELETE_PORT, snd_seq_ioctl_delete_port },
2142 { SNDRV_SEQ_IOCTL_GET_PORT_INFO, snd_seq_ioctl_get_port_info },
2143 { SNDRV_SEQ_IOCTL_SET_PORT_INFO, snd_seq_ioctl_set_port_info },
2144 { SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, snd_seq_ioctl_subscribe_port },
2145 { SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, snd_seq_ioctl_unsubscribe_port },
2146 { SNDRV_SEQ_IOCTL_CREATE_QUEUE, snd_seq_ioctl_create_queue },
2147 { SNDRV_SEQ_IOCTL_DELETE_QUEUE, snd_seq_ioctl_delete_queue },
2148 { SNDRV_SEQ_IOCTL_GET_QUEUE_INFO, snd_seq_ioctl_get_queue_info },
2149 { SNDRV_SEQ_IOCTL_SET_QUEUE_INFO, snd_seq_ioctl_set_queue_info },
2150 { SNDRV_SEQ_IOCTL_GET_NAMED_QUEUE, snd_seq_ioctl_get_named_queue },
2151 { SNDRV_SEQ_IOCTL_GET_QUEUE_STATUS, snd_seq_ioctl_get_queue_status },
2152 { SNDRV_SEQ_IOCTL_GET_QUEUE_TEMPO, snd_seq_ioctl_get_queue_tempo },
2153 { SNDRV_SEQ_IOCTL_SET_QUEUE_TEMPO, snd_seq_ioctl_set_queue_tempo },
2154 { SNDRV_SEQ_IOCTL_GET_QUEUE_TIMER, snd_seq_ioctl_get_queue_timer },
2155 { SNDRV_SEQ_IOCTL_SET_QUEUE_TIMER, snd_seq_ioctl_set_queue_timer },
2156 { SNDRV_SEQ_IOCTL_GET_QUEUE_CLIENT, snd_seq_ioctl_get_queue_client },
2157 { SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT, snd_seq_ioctl_set_queue_client },
2158 { SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, snd_seq_ioctl_get_client_pool },
2159 { SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, snd_seq_ioctl_set_client_pool },
2160 { SNDRV_SEQ_IOCTL_GET_SUBSCRIPTION, snd_seq_ioctl_get_subscription },
2161 { SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT, snd_seq_ioctl_query_next_client },
2162 { SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, snd_seq_ioctl_query_next_port },
2163 { SNDRV_SEQ_IOCTL_REMOVE_EVENTS, snd_seq_ioctl_remove_events },
2164 { SNDRV_SEQ_IOCTL_QUERY_SUBS, snd_seq_ioctl_query_subs },
2165 { 0, NULL },
2166};
2167
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002168static int snd_seq_do_ioctl(struct snd_seq_client *client, unsigned int cmd,
2169 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
2171 struct seq_ioctl_table *p;
2172
2173 switch (cmd) {
2174 case SNDRV_SEQ_IOCTL_PVERSION:
2175 /* return sequencer version number */
2176 return put_user(SNDRV_SEQ_VERSION, (int __user *)arg) ? -EFAULT : 0;
2177 case SNDRV_SEQ_IOCTL_CLIENT_ID:
2178 /* return the id of this client */
2179 return put_user(client->number, (int __user *)arg) ? -EFAULT : 0;
2180 }
2181
2182 if (! arg)
2183 return -EFAULT;
2184 for (p = ioctl_tables; p->cmd; p++) {
2185 if (p->cmd == cmd)
2186 return p->func(client, arg);
2187 }
2188 snd_printd("seq unknown ioctl() 0x%x (type='%c', number=0x%2x)\n",
2189 cmd, _IOC_TYPE(cmd), _IOC_NR(cmd));
2190 return -ENOTTY;
2191}
2192
2193
2194static long snd_seq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2195{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002196 struct snd_seq_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 snd_assert(client != NULL, return -ENXIO);
2199
2200 return snd_seq_do_ioctl(client, cmd, (void __user *) arg);
2201}
2202
2203#ifdef CONFIG_COMPAT
2204#include "seq_compat.c"
2205#else
2206#define snd_seq_ioctl_compat NULL
2207#endif
2208
2209/* -------------------------------------------------------- */
2210
2211
2212/* exported to kernel modules */
Clemens Ladisch7b6d9242005-12-12 09:33:37 +01002213int snd_seq_create_kernel_client(struct snd_card *card, int client_index,
2214 const char *name_fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002216 struct snd_seq_client *client;
Clemens Ladisch7b6d9242005-12-12 09:33:37 +01002217 va_list args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 snd_assert(! in_interrupt(), return -EBUSY);
2220
Clemens Ladischaa1e77e2005-12-12 09:36:01 +01002221 if (card && client_index >= SNDRV_SEQ_CLIENTS_PER_CARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 return -EINVAL;
Clemens Ladischaa1e77e2005-12-12 09:36:01 +01002223 if (card == NULL && client_index >= SNDRV_SEQ_GLOBAL_CLIENTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002226 if (mutex_lock_interruptible(&register_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return -ERESTARTSYS;
Clemens Ladischd0015442005-11-20 14:09:05 +01002228
2229 if (card) {
Clemens Ladischaa1e77e2005-12-12 09:36:01 +01002230 client_index += SNDRV_SEQ_GLOBAL_CLIENTS
2231 + card->number * SNDRV_SEQ_CLIENTS_PER_CARD;
2232 if (client_index >= SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN)
Clemens Ladischd0015442005-11-20 14:09:05 +01002233 client_index = -1;
2234 }
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 /* empty write queue as default */
Clemens Ladischaa1e77e2005-12-12 09:36:01 +01002237 client = seq_create_client1(client_index, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (client == NULL) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002239 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return -EBUSY; /* failure code */
2241 }
2242 usage_alloc(&client_usage, 1);
2243
Clemens Ladisch83e8ad62005-12-12 09:30:43 +01002244 client->accept_input = 1;
2245 client->accept_output = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Clemens Ladisch7b6d9242005-12-12 09:33:37 +01002247 va_start(args, name_fmt);
2248 vsnprintf(client->name, sizeof(client->name), name_fmt, args);
2249 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 client->type = KERNEL_CLIENT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002252 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 /* make others aware this new client */
2255 snd_seq_system_client_ev_client_start(client->number);
2256
2257 /* return client number to caller */
2258 return client->number;
2259}
2260
Takashi Iwai91715ed2006-04-28 15:13:39 +02002261EXPORT_SYMBOL(snd_seq_create_kernel_client);
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263/* exported to kernel modules */
2264int snd_seq_delete_kernel_client(int client)
2265{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002266 struct snd_seq_client *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
2268 snd_assert(! in_interrupt(), return -EBUSY);
2269
2270 ptr = clientptr(client);
2271 if (ptr == NULL)
2272 return -EINVAL;
2273
2274 seq_free_client(ptr);
2275 kfree(ptr);
2276 return 0;
2277}
2278
Takashi Iwai91715ed2006-04-28 15:13:39 +02002279EXPORT_SYMBOL(snd_seq_delete_kernel_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281/* skeleton to enqueue event, called from snd_seq_kernel_client_enqueue
2282 * and snd_seq_kernel_client_enqueue_blocking
2283 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002284static int kernel_client_enqueue(int client, struct snd_seq_event *ev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 struct file *file, int blocking,
2286 int atomic, int hop)
2287{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002288 struct snd_seq_client *cptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 int result;
2290
2291 snd_assert(ev != NULL, return -EINVAL);
2292
2293 if (ev->type == SNDRV_SEQ_EVENT_NONE)
2294 return 0; /* ignore this */
2295 if (ev->type == SNDRV_SEQ_EVENT_KERNEL_ERROR)
2296 return -EINVAL; /* quoted events can't be enqueued */
2297
2298 /* fill in client number */
2299 ev->source.client = client;
2300
2301 if (check_event_type_and_length(ev))
2302 return -EINVAL;
2303
2304 cptr = snd_seq_client_use_ptr(client);
2305 if (cptr == NULL)
2306 return -EINVAL;
2307
2308 if (! cptr->accept_output)
2309 result = -EPERM;
2310 else /* send it */
2311 result = snd_seq_client_enqueue_event(cptr, ev, file, blocking, atomic, hop);
2312
2313 snd_seq_client_unlock(cptr);
2314 return result;
2315}
2316
2317/*
2318 * exported, called by kernel clients to enqueue events (w/o blocking)
2319 *
2320 * RETURN VALUE: zero if succeed, negative if error
2321 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002322int snd_seq_kernel_client_enqueue(int client, struct snd_seq_event * ev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 int atomic, int hop)
2324{
2325 return kernel_client_enqueue(client, ev, NULL, 0, atomic, hop);
2326}
2327
Takashi Iwai91715ed2006-04-28 15:13:39 +02002328EXPORT_SYMBOL(snd_seq_kernel_client_enqueue);
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330/*
2331 * exported, called by kernel clients to enqueue events (with blocking)
2332 *
2333 * RETURN VALUE: zero if succeed, negative if error
2334 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002335int snd_seq_kernel_client_enqueue_blocking(int client, struct snd_seq_event * ev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 struct file *file,
2337 int atomic, int hop)
2338{
2339 return kernel_client_enqueue(client, ev, file, 1, atomic, hop);
2340}
2341
Takashi Iwai91715ed2006-04-28 15:13:39 +02002342EXPORT_SYMBOL(snd_seq_kernel_client_enqueue_blocking);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344/*
2345 * exported, called by kernel clients to dispatch events directly to other
2346 * clients, bypassing the queues. Event time-stamp will be updated.
2347 *
2348 * RETURN VALUE: negative = delivery failed,
2349 * zero, or positive: the number of delivered events
2350 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002351int snd_seq_kernel_client_dispatch(int client, struct snd_seq_event * ev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 int atomic, int hop)
2353{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002354 struct snd_seq_client *cptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 int result;
2356
2357 snd_assert(ev != NULL, return -EINVAL);
2358
2359 /* fill in client number */
2360 ev->queue = SNDRV_SEQ_QUEUE_DIRECT;
2361 ev->source.client = client;
2362
2363 if (check_event_type_and_length(ev))
2364 return -EINVAL;
2365
2366 cptr = snd_seq_client_use_ptr(client);
2367 if (cptr == NULL)
2368 return -EINVAL;
2369
2370 if (!cptr->accept_output)
2371 result = -EPERM;
2372 else
2373 result = snd_seq_deliver_event(cptr, ev, atomic, hop);
2374
2375 snd_seq_client_unlock(cptr);
2376 return result;
2377}
2378
Takashi Iwai91715ed2006-04-28 15:13:39 +02002379EXPORT_SYMBOL(snd_seq_kernel_client_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381/*
2382 * exported, called by kernel clients to perform same functions as with
2383 * userland ioctl()
2384 */
2385int snd_seq_kernel_client_ctl(int clientid, unsigned int cmd, void *arg)
2386{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002387 struct snd_seq_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 mm_segment_t fs;
2389 int result;
2390
2391 client = clientptr(clientid);
2392 if (client == NULL)
2393 return -ENXIO;
2394 fs = snd_enter_user();
2395 result = snd_seq_do_ioctl(client, cmd, (void __user *)arg);
2396 snd_leave_user(fs);
2397 return result;
2398}
2399
Takashi Iwai91715ed2006-04-28 15:13:39 +02002400EXPORT_SYMBOL(snd_seq_kernel_client_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402/* exported (for OSS emulator) */
2403int snd_seq_kernel_client_write_poll(int clientid, struct file *file, poll_table *wait)
2404{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002405 struct snd_seq_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 client = clientptr(clientid);
2408 if (client == NULL)
2409 return -ENXIO;
2410
2411 if (! snd_seq_write_pool_allocated(client))
2412 return 1;
2413 if (snd_seq_pool_poll_wait(client->pool, file, wait))
2414 return 1;
2415 return 0;
2416}
2417
Takashi Iwai91715ed2006-04-28 15:13:39 +02002418EXPORT_SYMBOL(snd_seq_kernel_client_write_poll);
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420/*---------------------------------------------------------------------------*/
2421
Takashi Iwai04f141a2005-12-01 10:43:51 +01002422#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423/*
2424 * /proc interface
2425 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002426static void snd_seq_info_dump_subscribers(struct snd_info_buffer *buffer,
2427 struct snd_seq_port_subs_info *group,
2428 int is_src, char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
2430 struct list_head *p;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002431 struct snd_seq_subscribers *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 int count = 0;
2433
2434 down_read(&group->list_mutex);
2435 if (list_empty(&group->list_head)) {
2436 up_read(&group->list_mutex);
2437 return;
2438 }
2439 snd_iprintf(buffer, msg);
2440 list_for_each(p, &group->list_head) {
2441 if (is_src)
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002442 s = list_entry(p, struct snd_seq_subscribers, src_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 else
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002444 s = list_entry(p, struct snd_seq_subscribers, dest_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 if (count++)
2446 snd_iprintf(buffer, ", ");
2447 snd_iprintf(buffer, "%d:%d",
2448 is_src ? s->info.dest.client : s->info.sender.client,
2449 is_src ? s->info.dest.port : s->info.sender.port);
2450 if (s->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)
2451 snd_iprintf(buffer, "[%c:%d]", ((s->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL) ? 'r' : 't'), s->info.queue);
2452 if (group->exclusive)
2453 snd_iprintf(buffer, "[ex]");
2454 }
2455 up_read(&group->list_mutex);
2456 snd_iprintf(buffer, "\n");
2457}
2458
2459#define FLAG_PERM_RD(perm) ((perm) & SNDRV_SEQ_PORT_CAP_READ ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_READ ? 'R' : 'r') : '-')
2460#define FLAG_PERM_WR(perm) ((perm) & SNDRV_SEQ_PORT_CAP_WRITE ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_WRITE ? 'W' : 'w') : '-')
2461#define FLAG_PERM_EX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_NO_EXPORT ? '-' : 'e')
2462
2463#define FLAG_PERM_DUPLEX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_DUPLEX ? 'X' : '-')
2464
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002465static void snd_seq_info_dump_ports(struct snd_info_buffer *buffer,
2466 struct snd_seq_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
Johannes Berg9244b2c2006-10-05 16:02:22 +02002468 struct snd_seq_client_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002470 mutex_lock(&client->ports_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02002471 list_for_each_entry(p, &client->ports_list_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 snd_iprintf(buffer, " Port %3d : \"%s\" (%c%c%c%c)\n",
2473 p->addr.port, p->name,
2474 FLAG_PERM_RD(p->capability),
2475 FLAG_PERM_WR(p->capability),
2476 FLAG_PERM_EX(p->capability),
2477 FLAG_PERM_DUPLEX(p->capability));
2478 snd_seq_info_dump_subscribers(buffer, &p->c_src, 1, " Connecting To: ");
2479 snd_seq_info_dump_subscribers(buffer, &p->c_dest, 0, " Connected From: ");
2480 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002481 mutex_unlock(&client->ports_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
2484
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002485void snd_seq_info_pool(struct snd_info_buffer *buffer,
2486 struct snd_seq_pool *pool, char *space);
2487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488/* exported to seq_info.c */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002489void snd_seq_info_clients_read(struct snd_info_entry *entry,
2490 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 int c;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +01002493 struct snd_seq_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
2495 snd_iprintf(buffer, "Client info\n");
2496 snd_iprintf(buffer, " cur clients : %d\n", client_usage.cur);
2497 snd_iprintf(buffer, " peak clients : %d\n", client_usage.peak);
2498 snd_iprintf(buffer, " max clients : %d\n", SNDRV_SEQ_MAX_CLIENTS);
2499 snd_iprintf(buffer, "\n");
2500
2501 /* list the client table */
2502 for (c = 0; c < SNDRV_SEQ_MAX_CLIENTS; c++) {
2503 client = snd_seq_client_use_ptr(c);
2504 if (client == NULL)
2505 continue;
2506 if (client->type == NO_CLIENT) {
2507 snd_seq_client_unlock(client);
2508 continue;
2509 }
2510
2511 snd_iprintf(buffer, "Client %3d : \"%s\" [%s]\n",
2512 c, client->name,
2513 client->type == USER_CLIENT ? "User" : "Kernel");
2514 snd_seq_info_dump_ports(buffer, client);
2515 if (snd_seq_write_pool_allocated(client)) {
2516 snd_iprintf(buffer, " Output pool :\n");
2517 snd_seq_info_pool(buffer, client->pool, " ");
2518 }
2519 if (client->type == USER_CLIENT && client->data.user.fifo &&
2520 client->data.user.fifo->pool) {
2521 snd_iprintf(buffer, " Input pool :\n");
2522 snd_seq_info_pool(buffer, client->data.user.fifo->pool, " ");
2523 }
2524 snd_seq_client_unlock(client);
2525 }
2526}
Takashi Iwai04f141a2005-12-01 10:43:51 +01002527#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529/*---------------------------------------------------------------------------*/
2530
2531
2532/*
2533 * REGISTRATION PART
2534 */
2535
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08002536static const struct file_operations snd_seq_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
2538 .owner = THIS_MODULE,
2539 .read = snd_seq_read,
2540 .write = snd_seq_write,
2541 .open = snd_seq_open,
2542 .release = snd_seq_release,
2543 .poll = snd_seq_poll,
2544 .unlocked_ioctl = snd_seq_ioctl,
2545 .compat_ioctl = snd_seq_ioctl_compat,
2546};
2547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548/*
2549 * register sequencer device
2550 */
2551int __init snd_sequencer_device_init(void)
2552{
2553 int err;
2554
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002555 if (mutex_lock_interruptible(&register_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 return -ERESTARTSYS;
2557
Clemens Ladisch2af677f2005-11-20 14:03:48 +01002558 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0,
Clemens Ladischf87135f2005-11-20 14:06:59 +01002559 &snd_seq_f_ops, NULL, "seq")) < 0) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002560 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return err;
2562 }
2563
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002564 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
2566 return 0;
2567}
2568
2569
2570
2571/*
2572 * unregister sequencer device
2573 */
2574void __exit snd_sequencer_device_done(void)
2575{
2576 snd_unregister_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0);
2577}