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