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