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