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