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