~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/core/seq/seq_clientmgr.c

Version: ~ [ linux-5.1.2 ] ~ [ linux-5.0.16 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.43 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.119 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.176 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.179 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.139 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.67 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp