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