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