[PATCH] fix semaphore handling in __unregister_chrdev_region
[linux/fpc-iii.git] / sound / core / seq / seq_clientmgr.c
blobd8f76afd284bc19efec4b1c577280508c99a69db
1 /*
2 * ALSA sequencer Client Manager
3 * Copyright (c) 1998-2001 by Frank van de Pol <fvdpol@coil.demon.nl>
4 * Jaroslav Kysela <perex@suse.cz>
5 * Takashi Iwai <tiwai@suse.de>
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.
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.
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
24 #include <sound/driver.h>
25 #include <linux/init.h>
26 #include <linux/smp_lock.h>
27 #include <linux/slab.h>
28 #include <sound/core.h>
29 #include <sound/minors.h>
30 #include <linux/kmod.h>
32 #include <sound/seq_kernel.h>
33 #include "seq_clientmgr.h"
34 #include "seq_memory.h"
35 #include "seq_queue.h"
36 #include "seq_timer.h"
37 #include "seq_info.h"
38 #include "seq_system.h"
39 #include <sound/seq_device.h>
40 #ifdef CONFIG_COMPAT
41 #include <linux/compat.h>
42 #endif
44 /* Client Manager
46 * this module handles the connections of userland and kernel clients
50 #define SNDRV_SEQ_LFLG_INPUT 0x0001
51 #define SNDRV_SEQ_LFLG_OUTPUT 0x0002
52 #define SNDRV_SEQ_LFLG_OPEN (SNDRV_SEQ_LFLG_INPUT|SNDRV_SEQ_LFLG_OUTPUT)
54 static DEFINE_SPINLOCK(clients_lock);
55 static DECLARE_MUTEX(register_mutex);
58 * client table
60 static char clienttablock[SNDRV_SEQ_MAX_CLIENTS];
61 static client_t *clienttab[SNDRV_SEQ_MAX_CLIENTS];
62 static usage_t client_usage;
65 * prototypes
67 static int bounce_error_event(client_t *client, snd_seq_event_t *event, int err, int atomic, int hop);
68 static int snd_seq_deliver_single_event(client_t *client, snd_seq_event_t *event, int filter, int atomic, int hop);
73 static inline mm_segment_t snd_enter_user(void)
75 mm_segment_t fs = get_fs();
76 set_fs(get_ds());
77 return fs;
80 static inline void snd_leave_user(mm_segment_t fs)
82 set_fs(fs);
87 static inline unsigned short snd_seq_file_flags(struct file *file)
89 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
90 case FMODE_WRITE:
91 return SNDRV_SEQ_LFLG_OUTPUT;
92 case FMODE_READ:
93 return SNDRV_SEQ_LFLG_INPUT;
94 default:
95 return SNDRV_SEQ_LFLG_OPEN;
99 static inline int snd_seq_write_pool_allocated(client_t *client)
101 return snd_seq_total_cells(client->pool) > 0;
104 /* return pointer to client structure for specified id */
105 static client_t *clientptr(int clientid)
107 if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) {
108 snd_printd("Seq: oops. Trying to get pointer to client %d\n", clientid);
109 return NULL;
111 return clienttab[clientid];
114 extern int seq_client_load[];
116 client_t *snd_seq_client_use_ptr(int clientid)
118 unsigned long flags;
119 client_t *client;
121 if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) {
122 snd_printd("Seq: oops. Trying to get pointer to client %d\n", clientid);
123 return NULL;
125 spin_lock_irqsave(&clients_lock, flags);
126 client = clientptr(clientid);
127 if (client)
128 goto __lock;
129 if (clienttablock[clientid]) {
130 spin_unlock_irqrestore(&clients_lock, flags);
131 return NULL;
133 spin_unlock_irqrestore(&clients_lock, flags);
134 #ifdef CONFIG_KMOD
135 if (!in_interrupt() && current->fs->root) {
136 static char client_requested[64];
137 static char card_requested[SNDRV_CARDS];
138 if (clientid < 64) {
139 int idx;
141 if (! client_requested[clientid] && current->fs->root) {
142 client_requested[clientid] = 1;
143 for (idx = 0; idx < 64; idx++) {
144 if (seq_client_load[idx] < 0)
145 break;
146 if (seq_client_load[idx] == clientid) {
147 request_module("snd-seq-client-%i", clientid);
148 break;
152 } else if (clientid >= 64 && clientid < 128) {
153 int card = (clientid - 64) / 8;
154 if (card < snd_ecards_limit) {
155 if (! card_requested[card]) {
156 card_requested[card] = 1;
157 snd_request_card(card);
159 snd_seq_device_load_drivers();
162 spin_lock_irqsave(&clients_lock, flags);
163 client = clientptr(clientid);
164 if (client)
165 goto __lock;
166 spin_unlock_irqrestore(&clients_lock, flags);
168 #endif
169 return NULL;
171 __lock:
172 snd_use_lock_use(&client->use_lock);
173 spin_unlock_irqrestore(&clients_lock, flags);
174 return client;
177 static void usage_alloc(usage_t * res, int num)
179 res->cur += num;
180 if (res->cur > res->peak)
181 res->peak = res->cur;
184 static void usage_free(usage_t * res, int num)
186 res->cur -= num;
189 /* initialise data structures */
190 int __init client_init_data(void)
192 /* zap out the client table */
193 memset(&clienttablock, 0, sizeof(clienttablock));
194 memset(&clienttab, 0, sizeof(clienttab));
195 return 0;
199 static client_t *seq_create_client1(int client_index, int poolsize)
201 unsigned long flags;
202 int c;
203 client_t *client;
205 /* init client data */
206 client = kcalloc(1, sizeof(*client), GFP_KERNEL);
207 if (client == NULL)
208 return NULL;
209 client->pool = snd_seq_pool_new(poolsize);
210 if (client->pool == NULL) {
211 kfree(client);
212 return NULL;
214 client->type = NO_CLIENT;
215 snd_use_lock_init(&client->use_lock);
216 rwlock_init(&client->ports_lock);
217 init_MUTEX(&client->ports_mutex);
218 INIT_LIST_HEAD(&client->ports_list_head);
220 /* find free slot in the client table */
221 spin_lock_irqsave(&clients_lock, flags);
222 if (client_index < 0) {
223 for (c = 128; c < SNDRV_SEQ_MAX_CLIENTS; c++) {
224 if (clienttab[c] || clienttablock[c])
225 continue;
226 clienttab[client->number = c] = client;
227 spin_unlock_irqrestore(&clients_lock, flags);
228 return client;
230 } else {
231 if (clienttab[client_index] == NULL && !clienttablock[client_index]) {
232 clienttab[client->number = client_index] = client;
233 spin_unlock_irqrestore(&clients_lock, flags);
234 return client;
237 spin_unlock_irqrestore(&clients_lock, flags);
238 snd_seq_pool_delete(&client->pool);
239 kfree(client);
240 return NULL; /* no free slot found or busy, return failure code */
244 static int seq_free_client1(client_t *client)
246 unsigned long flags;
248 snd_assert(client != NULL, return -EINVAL);
249 snd_seq_delete_all_ports(client);
250 snd_seq_queue_client_leave(client->number);
251 spin_lock_irqsave(&clients_lock, flags);
252 clienttablock[client->number] = 1;
253 clienttab[client->number] = NULL;
254 spin_unlock_irqrestore(&clients_lock, flags);
255 snd_use_lock_sync(&client->use_lock);
256 snd_seq_queue_client_termination(client->number);
257 if (client->pool)
258 snd_seq_pool_delete(&client->pool);
259 spin_lock_irqsave(&clients_lock, flags);
260 clienttablock[client->number] = 0;
261 spin_unlock_irqrestore(&clients_lock, flags);
262 return 0;
266 static void seq_free_client(client_t * client)
268 down(&register_mutex);
269 switch (client->type) {
270 case NO_CLIENT:
271 snd_printk(KERN_WARNING "Seq: Trying to free unused client %d\n", client->number);
272 break;
273 case USER_CLIENT:
274 case KERNEL_CLIENT:
275 seq_free_client1(client);
276 usage_free(&client_usage, 1);
277 break;
279 default:
280 snd_printk(KERN_ERR "Seq: Trying to free client %d with undefined type = %d\n", client->number, client->type);
282 up(&register_mutex);
284 snd_seq_system_client_ev_client_exit(client->number);
289 /* -------------------------------------------------------- */
291 /* create a user client */
292 static int snd_seq_open(struct inode *inode, struct file *file)
294 int c, mode; /* client id */
295 client_t *client;
296 user_client_t *user;
298 if (down_interruptible(&register_mutex))
299 return -ERESTARTSYS;
300 client = seq_create_client1(-1, SNDRV_SEQ_DEFAULT_EVENTS);
301 if (client == NULL) {
302 up(&register_mutex);
303 return -ENOMEM; /* failure code */
306 mode = snd_seq_file_flags(file);
307 if (mode & SNDRV_SEQ_LFLG_INPUT)
308 client->accept_input = 1;
309 if (mode & SNDRV_SEQ_LFLG_OUTPUT)
310 client->accept_output = 1;
312 user = &client->data.user;
313 user->fifo = NULL;
314 user->fifo_pool_size = 0;
316 if (mode & SNDRV_SEQ_LFLG_INPUT) {
317 user->fifo_pool_size = SNDRV_SEQ_DEFAULT_CLIENT_EVENTS;
318 user->fifo = snd_seq_fifo_new(user->fifo_pool_size);
319 if (user->fifo == NULL) {
320 seq_free_client1(client);
321 kfree(client);
322 up(&register_mutex);
323 return -ENOMEM;
327 usage_alloc(&client_usage, 1);
328 client->type = USER_CLIENT;
329 up(&register_mutex);
331 c = client->number;
332 file->private_data = client;
334 /* fill client data */
335 user->file = file;
336 sprintf(client->name, "Client-%d", c);
338 /* make others aware this new client */
339 snd_seq_system_client_ev_client_start(c);
341 return 0;
344 /* delete a user client */
345 static int snd_seq_release(struct inode *inode, struct file *file)
347 client_t *client = (client_t *) file->private_data;
349 if (client) {
350 seq_free_client(client);
351 if (client->data.user.fifo)
352 snd_seq_fifo_delete(&client->data.user.fifo);
353 kfree(client);
356 return 0;
360 /* handle client read() */
361 /* possible error values:
362 * -ENXIO invalid client or file open mode
363 * -ENOSPC FIFO overflow (the flag is cleared after this error report)
364 * -EINVAL no enough user-space buffer to write the whole event
365 * -EFAULT seg. fault during copy to user space
367 static ssize_t snd_seq_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
369 client_t *client = (client_t *) file->private_data;
370 fifo_t *fifo;
371 int err;
372 long result = 0;
373 snd_seq_event_cell_t *cell;
375 if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT))
376 return -ENXIO;
378 if (!access_ok(VERIFY_WRITE, buf, count))
379 return -EFAULT;
381 /* check client structures are in place */
382 snd_assert(client != NULL, return -ENXIO);
384 if (!client->accept_input || (fifo = client->data.user.fifo) == NULL)
385 return -ENXIO;
387 if (atomic_read(&fifo->overflow) > 0) {
388 /* buffer overflow is detected */
389 snd_seq_fifo_clear(fifo);
390 /* return error code */
391 return -ENOSPC;
394 cell = NULL;
395 err = 0;
396 snd_seq_fifo_lock(fifo);
398 /* while data available in queue */
399 while (count >= sizeof(snd_seq_event_t)) {
400 int nonblock;
402 nonblock = (file->f_flags & O_NONBLOCK) || result > 0;
403 if ((err = snd_seq_fifo_cell_out(fifo, &cell, nonblock)) < 0) {
404 break;
406 if (snd_seq_ev_is_variable(&cell->event)) {
407 snd_seq_event_t tmpev;
408 tmpev = cell->event;
409 tmpev.data.ext.len &= ~SNDRV_SEQ_EXT_MASK;
410 if (copy_to_user(buf, &tmpev, sizeof(snd_seq_event_t))) {
411 err = -EFAULT;
412 break;
414 count -= sizeof(snd_seq_event_t);
415 buf += sizeof(snd_seq_event_t);
416 err = snd_seq_expand_var_event(&cell->event, count, (char *)buf, 0, sizeof(snd_seq_event_t));
417 if (err < 0)
418 break;
419 result += err;
420 count -= err;
421 buf += err;
422 } else {
423 if (copy_to_user(buf, &cell->event, sizeof(snd_seq_event_t))) {
424 err = -EFAULT;
425 break;
427 count -= sizeof(snd_seq_event_t);
428 buf += sizeof(snd_seq_event_t);
430 snd_seq_cell_free(cell);
431 cell = NULL; /* to be sure */
432 result += sizeof(snd_seq_event_t);
435 if (err < 0) {
436 if (cell)
437 snd_seq_fifo_cell_putback(fifo, cell);
438 if (err == -EAGAIN && result > 0)
439 err = 0;
441 snd_seq_fifo_unlock(fifo);
443 return (err < 0) ? err : result;
448 * check access permission to the port
450 static int check_port_perm(client_port_t *port, unsigned int flags)
452 if ((port->capability & flags) != flags)
453 return 0;
454 return flags;
458 * check if the destination client is available, and return the pointer
459 * if filter is non-zero, client filter bitmap is tested.
461 static client_t *get_event_dest_client(snd_seq_event_t *event, int filter)
463 client_t *dest;
465 dest = snd_seq_client_use_ptr(event->dest.client);
466 if (dest == NULL)
467 return NULL;
468 if (! dest->accept_input)
469 goto __not_avail;
470 if ((dest->filter & SNDRV_SEQ_FILTER_USE_EVENT) &&
471 ! test_bit(event->type, dest->event_filter))
472 goto __not_avail;
473 if (filter && !(dest->filter & filter))
474 goto __not_avail;
476 return dest; /* ok - accessible */
477 __not_avail:
478 snd_seq_client_unlock(dest);
479 return NULL;
484 * Return the error event.
486 * If the receiver client is a user client, the original event is
487 * encapsulated in SNDRV_SEQ_EVENT_BOUNCE as variable length event. If
488 * the original event is also variable length, the external data is
489 * copied after the event record.
490 * If the receiver client is a kernel client, the original event is
491 * quoted in SNDRV_SEQ_EVENT_KERNEL_ERROR, since this requires no extra
492 * kmalloc.
494 static int bounce_error_event(client_t *client, snd_seq_event_t *event,
495 int err, int atomic, int hop)
497 snd_seq_event_t bounce_ev;
498 int result;
500 if (client == NULL ||
501 ! (client->filter & SNDRV_SEQ_FILTER_BOUNCE) ||
502 ! client->accept_input)
503 return 0; /* ignored */
505 /* set up quoted error */
506 memset(&bounce_ev, 0, sizeof(bounce_ev));
507 bounce_ev.type = SNDRV_SEQ_EVENT_KERNEL_ERROR;
508 bounce_ev.flags = SNDRV_SEQ_EVENT_LENGTH_FIXED;
509 bounce_ev.queue = SNDRV_SEQ_QUEUE_DIRECT;
510 bounce_ev.source.client = SNDRV_SEQ_CLIENT_SYSTEM;
511 bounce_ev.source.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
512 bounce_ev.dest.client = client->number;
513 bounce_ev.dest.port = event->source.port;
514 bounce_ev.data.quote.origin = event->dest;
515 bounce_ev.data.quote.event = event;
516 bounce_ev.data.quote.value = -err; /* use positive value */
517 result = snd_seq_deliver_single_event(NULL, &bounce_ev, 0, atomic, hop + 1);
518 if (result < 0) {
519 client->event_lost++;
520 return result;
523 return result;
528 * rewrite the time-stamp of the event record with the curren time
529 * of the given queue.
530 * return non-zero if updated.
532 static int update_timestamp_of_queue(snd_seq_event_t *event, int queue, int real_time)
534 queue_t *q;
536 q = queueptr(queue);
537 if (! q)
538 return 0;
539 event->queue = queue;
540 event->flags &= ~SNDRV_SEQ_TIME_STAMP_MASK;
541 if (real_time) {
542 event->time.time = snd_seq_timer_get_cur_time(q->timer);
543 event->flags |= SNDRV_SEQ_TIME_STAMP_REAL;
544 } else {
545 event->time.tick = snd_seq_timer_get_cur_tick(q->timer);
546 event->flags |= SNDRV_SEQ_TIME_STAMP_TICK;
548 queuefree(q);
549 return 1;
554 * deliver an event to the specified destination.
555 * if filter is non-zero, client filter bitmap is tested.
557 * RETURN VALUE: 0 : if succeeded
558 * <0 : error
560 static int snd_seq_deliver_single_event(client_t *client,
561 snd_seq_event_t *event,
562 int filter, int atomic, int hop)
564 client_t *dest = NULL;
565 client_port_t *dest_port = NULL;
566 int result = -ENOENT;
567 int direct;
569 direct = snd_seq_ev_is_direct(event);
571 dest = get_event_dest_client(event, filter);
572 if (dest == NULL)
573 goto __skip;
574 dest_port = snd_seq_port_use_ptr(dest, event->dest.port);
575 if (dest_port == NULL)
576 goto __skip;
578 /* check permission */
579 if (! check_port_perm(dest_port, SNDRV_SEQ_PORT_CAP_WRITE)) {
580 result = -EPERM;
581 goto __skip;
584 if (dest_port->timestamping)
585 update_timestamp_of_queue(event, dest_port->time_queue,
586 dest_port->time_real);
588 switch (dest->type) {
589 case USER_CLIENT:
590 if (dest->data.user.fifo)
591 result = snd_seq_fifo_event_in(dest->data.user.fifo, event);
592 break;
594 case KERNEL_CLIENT:
595 if (dest_port->event_input == NULL)
596 break;
597 result = dest_port->event_input(event, direct, dest_port->private_data, atomic, hop);
598 break;
599 default:
600 break;
603 __skip:
604 if (dest_port)
605 snd_seq_port_unlock(dest_port);
606 if (dest)
607 snd_seq_client_unlock(dest);
609 if (result < 0 && !direct) {
610 result = bounce_error_event(client, event, result, atomic, hop);
612 return result;
617 * send the event to all subscribers:
619 static int deliver_to_subscribers(client_t *client,
620 snd_seq_event_t *event,
621 int atomic, int hop)
623 subscribers_t *subs;
624 int err = 0, num_ev = 0;
625 snd_seq_event_t event_saved;
626 client_port_t *src_port;
627 struct list_head *p;
628 port_subs_info_t *grp;
630 src_port = snd_seq_port_use_ptr(client, event->source.port);
631 if (src_port == NULL)
632 return -EINVAL; /* invalid source port */
633 /* save original event record */
634 event_saved = *event;
635 grp = &src_port->c_src;
637 /* lock list */
638 if (atomic)
639 read_lock(&grp->list_lock);
640 else
641 down_read(&grp->list_mutex);
642 list_for_each(p, &grp->list_head) {
643 subs = list_entry(p, subscribers_t, src_list);
644 event->dest = subs->info.dest;
645 if (subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)
646 /* convert time according to flag with subscription */
647 update_timestamp_of_queue(event, subs->info.queue,
648 subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL);
649 err = snd_seq_deliver_single_event(client, event,
650 0, atomic, hop);
651 if (err < 0)
652 break;
653 num_ev++;
654 /* restore original event record */
655 *event = event_saved;
657 if (atomic)
658 read_unlock(&grp->list_lock);
659 else
660 up_read(&grp->list_mutex);
661 *event = event_saved; /* restore */
662 snd_seq_port_unlock(src_port);
663 return (err < 0) ? err : num_ev;
667 #ifdef SUPPORT_BROADCAST
669 * broadcast to all ports:
671 static int port_broadcast_event(client_t *client,
672 snd_seq_event_t *event,
673 int atomic, int hop)
675 int num_ev = 0, err = 0;
676 client_t *dest_client;
677 struct list_head *p;
679 dest_client = get_event_dest_client(event, SNDRV_SEQ_FILTER_BROADCAST);
680 if (dest_client == NULL)
681 return 0; /* no matching destination */
683 read_lock(&dest_client->ports_lock);
684 list_for_each(p, &dest_client->ports_list_head) {
685 client_port_t *port = list_entry(p, client_port_t, list);
686 event->dest.port = port->addr.port;
687 /* pass NULL as source client to avoid error bounce */
688 err = snd_seq_deliver_single_event(NULL, event,
689 SNDRV_SEQ_FILTER_BROADCAST,
690 atomic, hop);
691 if (err < 0)
692 break;
693 num_ev++;
695 read_unlock(&dest_client->ports_lock);
696 snd_seq_client_unlock(dest_client);
697 event->dest.port = SNDRV_SEQ_ADDRESS_BROADCAST; /* restore */
698 return (err < 0) ? err : num_ev;
702 * send the event to all clients:
703 * if destination port is also ADDRESS_BROADCAST, deliver to all ports.
705 static int broadcast_event(client_t *client,
706 snd_seq_event_t *event, int atomic, int hop)
708 int err = 0, num_ev = 0;
709 int dest;
710 snd_seq_addr_t addr;
712 addr = event->dest; /* save */
714 for (dest = 0; dest < SNDRV_SEQ_MAX_CLIENTS; dest++) {
715 /* don't send to itself */
716 if (dest == client->number)
717 continue;
718 event->dest.client = dest;
719 event->dest.port = addr.port;
720 if (addr.port == SNDRV_SEQ_ADDRESS_BROADCAST)
721 err = port_broadcast_event(client, event, atomic, hop);
722 else
723 /* pass NULL as source client to avoid error bounce */
724 err = snd_seq_deliver_single_event(NULL, event,
725 SNDRV_SEQ_FILTER_BROADCAST,
726 atomic, hop);
727 if (err < 0)
728 break;
729 num_ev += err;
731 event->dest = addr; /* restore */
732 return (err < 0) ? err : num_ev;
736 /* multicast - not supported yet */
737 static int multicast_event(client_t *client, snd_seq_event_t *event,
738 int atomic, int hop)
740 snd_printd("seq: multicast not supported yet.\n");
741 return 0; /* ignored */
743 #endif /* SUPPORT_BROADCAST */
746 /* deliver an event to the destination port(s).
747 * if the event is to subscribers or broadcast, the event is dispatched
748 * to multiple targets.
750 * RETURN VALUE: n > 0 : the number of delivered events.
751 * n == 0 : the event was not passed to any client.
752 * n < 0 : error - event was not processed.
754 static int snd_seq_deliver_event(client_t *client, snd_seq_event_t *event,
755 int atomic, int hop)
757 int result;
759 hop++;
760 if (hop >= SNDRV_SEQ_MAX_HOPS) {
761 snd_printd("too long delivery path (%d:%d->%d:%d)\n",
762 event->source.client, event->source.port,
763 event->dest.client, event->dest.port);
764 return -EMLINK;
767 if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS ||
768 event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS)
769 result = deliver_to_subscribers(client, event, atomic, hop);
770 #ifdef SUPPORT_BROADCAST
771 else if (event->queue == SNDRV_SEQ_ADDRESS_BROADCAST ||
772 event->dest.client == SNDRV_SEQ_ADDRESS_BROADCAST)
773 result = broadcast_event(client, event, atomic, hop);
774 else if (event->dest.client >= SNDRV_SEQ_MAX_CLIENTS)
775 result = multicast_event(client, event, atomic, hop);
776 else if (event->dest.port == SNDRV_SEQ_ADDRESS_BROADCAST)
777 result = port_broadcast_event(client, event, atomic, hop);
778 #endif
779 else
780 result = snd_seq_deliver_single_event(client, event, 0, atomic, hop);
782 return result;
786 * dispatch an event cell:
787 * This function is called only from queue check routines in timer
788 * interrupts or after enqueued.
789 * The event cell shall be released or re-queued in this function.
791 * RETURN VALUE: n > 0 : the number of delivered events.
792 * n == 0 : the event was not passed to any client.
793 * n < 0 : error - event was not processed.
795 int snd_seq_dispatch_event(snd_seq_event_cell_t *cell, int atomic, int hop)
797 client_t *client;
798 int result;
800 snd_assert(cell != NULL, return -EINVAL);
802 client = snd_seq_client_use_ptr(cell->event.source.client);
803 if (client == NULL) {
804 snd_seq_cell_free(cell); /* release this cell */
805 return -EINVAL;
808 if (cell->event.type == SNDRV_SEQ_EVENT_NOTE) {
809 /* NOTE event:
810 * the event cell is re-used as a NOTE-OFF event and
811 * enqueued again.
813 snd_seq_event_t tmpev, *ev;
815 /* reserve this event to enqueue note-off later */
816 tmpev = cell->event;
817 tmpev.type = SNDRV_SEQ_EVENT_NOTEON;
818 result = snd_seq_deliver_event(client, &tmpev, atomic, hop);
821 * This was originally a note event. We now re-use the
822 * cell for the note-off event.
825 ev = &cell->event;
826 ev->type = SNDRV_SEQ_EVENT_NOTEOFF;
827 ev->flags |= SNDRV_SEQ_PRIORITY_HIGH;
829 /* add the duration time */
830 switch (ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) {
831 case SNDRV_SEQ_TIME_STAMP_TICK:
832 ev->time.tick += ev->data.note.duration;
833 break;
834 case SNDRV_SEQ_TIME_STAMP_REAL:
835 /* unit for duration is ms */
836 ev->time.time.tv_nsec += 1000000 * (ev->data.note.duration % 1000);
837 ev->time.time.tv_sec += ev->data.note.duration / 1000 +
838 ev->time.time.tv_nsec / 1000000000;
839 ev->time.time.tv_nsec %= 1000000000;
840 break;
842 ev->data.note.velocity = ev->data.note.off_velocity;
844 /* Now queue this cell as the note off event */
845 if (snd_seq_enqueue_event(cell, atomic, hop) < 0)
846 snd_seq_cell_free(cell); /* release this cell */
848 } else {
849 /* Normal events:
850 * event cell is freed after processing the event
853 result = snd_seq_deliver_event(client, &cell->event, atomic, hop);
854 snd_seq_cell_free(cell);
857 snd_seq_client_unlock(client);
858 return result;
862 /* Allocate a cell from client pool and enqueue it to queue:
863 * if pool is empty and blocking is TRUE, sleep until a new cell is
864 * available.
866 static int snd_seq_client_enqueue_event(client_t *client,
867 snd_seq_event_t *event,
868 struct file *file, int blocking,
869 int atomic, int hop)
871 snd_seq_event_cell_t *cell;
872 int err;
874 /* special queue values - force direct passing */
875 if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) {
876 event->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
877 event->queue = SNDRV_SEQ_QUEUE_DIRECT;
878 } else
879 #ifdef SUPPORT_BROADCAST
880 if (event->queue == SNDRV_SEQ_ADDRESS_BROADCAST) {
881 event->dest.client = SNDRV_SEQ_ADDRESS_BROADCAST;
882 event->queue = SNDRV_SEQ_QUEUE_DIRECT;
884 #endif
885 if (event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) {
886 /* check presence of source port */
887 client_port_t *src_port = snd_seq_port_use_ptr(client, event->source.port);
888 if (src_port == NULL)
889 return -EINVAL;
890 snd_seq_port_unlock(src_port);
893 /* direct event processing without enqueued */
894 if (snd_seq_ev_is_direct(event)) {
895 if (event->type == SNDRV_SEQ_EVENT_NOTE)
896 return -EINVAL; /* this event must be enqueued! */
897 return snd_seq_deliver_event(client, event, atomic, hop);
900 /* Not direct, normal queuing */
901 if (snd_seq_queue_is_used(event->queue, client->number) <= 0)
902 return -EINVAL; /* invalid queue */
903 if (! snd_seq_write_pool_allocated(client))
904 return -ENXIO; /* queue is not allocated */
906 /* allocate an event cell */
907 err = snd_seq_event_dup(client->pool, event, &cell, !blocking || atomic, file);
908 if (err < 0)
909 return err;
911 /* we got a cell. enqueue it. */
912 if ((err = snd_seq_enqueue_event(cell, atomic, hop)) < 0) {
913 snd_seq_cell_free(cell);
914 return err;
917 return 0;
922 * check validity of event type and data length.
923 * return non-zero if invalid.
925 static int check_event_type_and_length(snd_seq_event_t *ev)
927 switch (snd_seq_ev_length_type(ev)) {
928 case SNDRV_SEQ_EVENT_LENGTH_FIXED:
929 if (snd_seq_ev_is_variable_type(ev))
930 return -EINVAL;
931 break;
932 case SNDRV_SEQ_EVENT_LENGTH_VARIABLE:
933 if (! snd_seq_ev_is_variable_type(ev) ||
934 (ev->data.ext.len & ~SNDRV_SEQ_EXT_MASK) >= SNDRV_SEQ_MAX_EVENT_LEN)
935 return -EINVAL;
936 break;
937 case SNDRV_SEQ_EVENT_LENGTH_VARUSR:
938 if (! snd_seq_ev_is_instr_type(ev) ||
939 ! snd_seq_ev_is_direct(ev))
940 return -EINVAL;
941 break;
943 return 0;
947 /* handle write() */
948 /* possible error values:
949 * -ENXIO invalid client or file open mode
950 * -ENOMEM malloc failed
951 * -EFAULT seg. fault during copy from user space
952 * -EINVAL invalid event
953 * -EAGAIN no space in output pool
954 * -EINTR interrupts while sleep
955 * -EMLINK too many hops
956 * others depends on return value from driver callback
958 static ssize_t snd_seq_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
960 client_t *client = (client_t *) file->private_data;
961 int written = 0, len;
962 int err = -EINVAL;
963 snd_seq_event_t event;
965 if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT))
966 return -ENXIO;
968 /* check client structures are in place */
969 snd_assert(client != NULL, return -ENXIO);
971 if (!client->accept_output || client->pool == NULL)
972 return -ENXIO;
974 /* allocate the pool now if the pool is not allocated yet */
975 if (client->pool->size > 0 && !snd_seq_write_pool_allocated(client)) {
976 if (snd_seq_pool_init(client->pool) < 0)
977 return -ENOMEM;
980 /* only process whole events */
981 while (count >= sizeof(snd_seq_event_t)) {
982 /* Read in the event header from the user */
983 len = sizeof(event);
984 if (copy_from_user(&event, buf, len)) {
985 err = -EFAULT;
986 break;
988 event.source.client = client->number; /* fill in client number */
989 /* Check for extension data length */
990 if (check_event_type_and_length(&event)) {
991 err = -EINVAL;
992 break;
995 /* check for special events */
996 if (event.type == SNDRV_SEQ_EVENT_NONE)
997 goto __skip_event;
998 else if (snd_seq_ev_is_reserved(&event)) {
999 err = -EINVAL;
1000 break;
1003 if (snd_seq_ev_is_variable(&event)) {
1004 int extlen = event.data.ext.len & ~SNDRV_SEQ_EXT_MASK;
1005 if ((size_t)(extlen + len) > count) {
1006 /* back out, will get an error this time or next */
1007 err = -EINVAL;
1008 break;
1010 /* set user space pointer */
1011 event.data.ext.len = extlen | SNDRV_SEQ_EXT_USRPTR;
1012 event.data.ext.ptr = (char*)buf + sizeof(snd_seq_event_t);
1013 len += extlen; /* increment data length */
1014 } else {
1015 #ifdef CONFIG_COMPAT
1016 if (client->convert32 && snd_seq_ev_is_varusr(&event)) {
1017 void *ptr = compat_ptr(event.data.raw32.d[1]);
1018 event.data.ext.ptr = ptr;
1020 #endif
1023 /* ok, enqueue it */
1024 err = snd_seq_client_enqueue_event(client, &event, file,
1025 !(file->f_flags & O_NONBLOCK),
1026 0, 0);
1027 if (err < 0)
1028 break;
1030 __skip_event:
1031 /* Update pointers and counts */
1032 count -= len;
1033 buf += len;
1034 written += len;
1037 return written ? written : err;
1042 * handle polling
1044 static unsigned int snd_seq_poll(struct file *file, poll_table * wait)
1046 client_t *client = (client_t *) file->private_data;
1047 unsigned int mask = 0;
1049 /* check client structures are in place */
1050 snd_assert(client != NULL, return -ENXIO);
1052 if ((snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT) &&
1053 client->data.user.fifo) {
1055 /* check if data is available in the outqueue */
1056 if (snd_seq_fifo_poll_wait(client->data.user.fifo, file, wait))
1057 mask |= POLLIN | POLLRDNORM;
1060 if (snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT) {
1062 /* check if data is available in the pool */
1063 if (!snd_seq_write_pool_allocated(client) ||
1064 snd_seq_pool_poll_wait(client->pool, file, wait))
1065 mask |= POLLOUT | POLLWRNORM;
1068 return mask;
1072 /*-----------------------------------------------------*/
1075 /* SYSTEM_INFO ioctl() */
1076 static int snd_seq_ioctl_system_info(client_t *client, void __user *arg)
1078 snd_seq_system_info_t info;
1080 memset(&info, 0, sizeof(info));
1081 /* fill the info fields */
1082 info.queues = SNDRV_SEQ_MAX_QUEUES;
1083 info.clients = SNDRV_SEQ_MAX_CLIENTS;
1084 info.ports = 256; /* fixed limit */
1085 info.channels = 256; /* fixed limit */
1086 info.cur_clients = client_usage.cur;
1087 info.cur_queues = snd_seq_queue_get_cur_queues();
1089 if (copy_to_user(arg, &info, sizeof(info)))
1090 return -EFAULT;
1091 return 0;
1095 /* RUNNING_MODE ioctl() */
1096 static int snd_seq_ioctl_running_mode(client_t *client, void __user *arg)
1098 struct sndrv_seq_running_info info;
1099 client_t *cptr;
1100 int err = 0;
1102 if (copy_from_user(&info, arg, sizeof(info)))
1103 return -EFAULT;
1105 /* requested client number */
1106 cptr = snd_seq_client_use_ptr(info.client);
1107 if (cptr == NULL)
1108 return -ENOENT; /* don't change !!! */
1110 #ifdef SNDRV_BIG_ENDIAN
1111 if (! info.big_endian) {
1112 err = -EINVAL;
1113 goto __err;
1115 #else
1116 if (info.big_endian) {
1117 err = -EINVAL;
1118 goto __err;
1121 #endif
1122 if (info.cpu_mode > sizeof(long)) {
1123 err = -EINVAL;
1124 goto __err;
1126 cptr->convert32 = (info.cpu_mode < sizeof(long));
1127 __err:
1128 snd_seq_client_unlock(cptr);
1129 return err;
1132 /* CLIENT_INFO ioctl() */
1133 static void get_client_info(client_t *cptr, snd_seq_client_info_t *info)
1135 info->client = cptr->number;
1137 /* fill the info fields */
1138 info->type = cptr->type;
1139 strcpy(info->name, cptr->name);
1140 info->filter = cptr->filter;
1141 info->event_lost = cptr->event_lost;
1142 memcpy(info->event_filter, cptr->event_filter, 32);
1143 info->num_ports = cptr->num_ports;
1144 memset(info->reserved, 0, sizeof(info->reserved));
1147 static int snd_seq_ioctl_get_client_info(client_t * client, void __user *arg)
1149 client_t *cptr;
1150 snd_seq_client_info_t client_info;
1152 if (copy_from_user(&client_info, arg, sizeof(client_info)))
1153 return -EFAULT;
1155 /* requested client number */
1156 cptr = snd_seq_client_use_ptr(client_info.client);
1157 if (cptr == NULL)
1158 return -ENOENT; /* don't change !!! */
1160 get_client_info(cptr, &client_info);
1161 snd_seq_client_unlock(cptr);
1163 if (copy_to_user(arg, &client_info, sizeof(client_info)))
1164 return -EFAULT;
1165 return 0;
1169 /* CLIENT_INFO ioctl() */
1170 static int snd_seq_ioctl_set_client_info(client_t * client, void __user *arg)
1172 snd_seq_client_info_t client_info;
1174 if (copy_from_user(&client_info, arg, sizeof(client_info)))
1175 return -EFAULT;
1177 /* it is not allowed to set the info fields for an another client */
1178 if (client->number != client_info.client)
1179 return -EPERM;
1180 /* also client type must be set now */
1181 if (client->type != client_info.type)
1182 return -EINVAL;
1184 /* fill the info fields */
1185 if (client_info.name[0])
1186 strlcpy(client->name, client_info.name, sizeof(client->name));
1188 client->filter = client_info.filter;
1189 client->event_lost = client_info.event_lost;
1190 memcpy(client->event_filter, client_info.event_filter, 32);
1192 return 0;
1197 * CREATE PORT ioctl()
1199 static int snd_seq_ioctl_create_port(client_t * client, void __user *arg)
1201 client_port_t *port;
1202 snd_seq_port_info_t info;
1203 snd_seq_port_callback_t *callback;
1205 if (copy_from_user(&info, arg, sizeof(info)))
1206 return -EFAULT;
1208 /* it is not allowed to create the port for an another client */
1209 if (info.addr.client != client->number)
1210 return -EPERM;
1212 port = snd_seq_create_port(client, (info.flags & SNDRV_SEQ_PORT_FLG_GIVEN_PORT) ? info.addr.port : -1);
1213 if (port == NULL)
1214 return -ENOMEM;
1216 if (client->type == USER_CLIENT && info.kernel) {
1217 snd_seq_delete_port(client, port->addr.port);
1218 return -EINVAL;
1220 if (client->type == KERNEL_CLIENT) {
1221 if ((callback = info.kernel) != NULL) {
1222 if (callback->owner)
1223 port->owner = callback->owner;
1224 port->private_data = callback->private_data;
1225 port->private_free = callback->private_free;
1226 port->callback_all = callback->callback_all;
1227 port->event_input = callback->event_input;
1228 port->c_src.open = callback->subscribe;
1229 port->c_src.close = callback->unsubscribe;
1230 port->c_dest.open = callback->use;
1231 port->c_dest.close = callback->unuse;
1235 info.addr = port->addr;
1237 snd_seq_set_port_info(port, &info);
1238 snd_seq_system_client_ev_port_start(port->addr.client, port->addr.port);
1240 if (copy_to_user(arg, &info, sizeof(info)))
1241 return -EFAULT;
1243 return 0;
1247 * DELETE PORT ioctl()
1249 static int snd_seq_ioctl_delete_port(client_t * client, void __user *arg)
1251 snd_seq_port_info_t info;
1252 int err;
1254 /* set passed parameters */
1255 if (copy_from_user(&info, arg, sizeof(info)))
1256 return -EFAULT;
1258 /* it is not allowed to remove the port for an another client */
1259 if (info.addr.client != client->number)
1260 return -EPERM;
1262 err = snd_seq_delete_port(client, info.addr.port);
1263 if (err >= 0)
1264 snd_seq_system_client_ev_port_exit(client->number, info.addr.port);
1265 return err;
1270 * GET_PORT_INFO ioctl() (on any client)
1272 static int snd_seq_ioctl_get_port_info(client_t *client, void __user *arg)
1274 client_t *cptr;
1275 client_port_t *port;
1276 snd_seq_port_info_t info;
1278 if (copy_from_user(&info, arg, sizeof(info)))
1279 return -EFAULT;
1280 cptr = snd_seq_client_use_ptr(info.addr.client);
1281 if (cptr == NULL)
1282 return -ENXIO;
1284 port = snd_seq_port_use_ptr(cptr, info.addr.port);
1285 if (port == NULL) {
1286 snd_seq_client_unlock(cptr);
1287 return -ENOENT; /* don't change */
1290 /* get port info */
1291 snd_seq_get_port_info(port, &info);
1292 snd_seq_port_unlock(port);
1293 snd_seq_client_unlock(cptr);
1295 if (copy_to_user(arg, &info, sizeof(info)))
1296 return -EFAULT;
1297 return 0;
1302 * SET_PORT_INFO ioctl() (only ports on this/own client)
1304 static int snd_seq_ioctl_set_port_info(client_t * client, void __user *arg)
1306 client_port_t *port;
1307 snd_seq_port_info_t info;
1309 if (copy_from_user(&info, arg, sizeof(info)))
1310 return -EFAULT;
1312 if (info.addr.client != client->number) /* only set our own ports ! */
1313 return -EPERM;
1314 port = snd_seq_port_use_ptr(client, info.addr.port);
1315 if (port) {
1316 snd_seq_set_port_info(port, &info);
1317 snd_seq_port_unlock(port);
1319 return 0;
1324 * port subscription (connection)
1326 #define PERM_RD (SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ)
1327 #define PERM_WR (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_SUBS_WRITE)
1329 static int check_subscription_permission(client_t *client, client_port_t *sport,
1330 client_port_t *dport,
1331 snd_seq_port_subscribe_t *subs)
1333 if (client->number != subs->sender.client &&
1334 client->number != subs->dest.client) {
1335 /* connection by third client - check export permission */
1336 if (check_port_perm(sport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))
1337 return -EPERM;
1338 if (check_port_perm(dport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))
1339 return -EPERM;
1342 /* check read permission */
1343 /* if sender or receiver is the subscribing client itself,
1344 * no permission check is necessary
1346 if (client->number != subs->sender.client) {
1347 if (! check_port_perm(sport, PERM_RD))
1348 return -EPERM;
1350 /* check write permission */
1351 if (client->number != subs->dest.client) {
1352 if (! check_port_perm(dport, PERM_WR))
1353 return -EPERM;
1355 return 0;
1359 * send an subscription notify event to user client:
1360 * client must be user client.
1362 int snd_seq_client_notify_subscription(int client, int port,
1363 snd_seq_port_subscribe_t *info, int evtype)
1365 snd_seq_event_t event;
1367 memset(&event, 0, sizeof(event));
1368 event.type = evtype;
1369 event.data.connect.dest = info->dest;
1370 event.data.connect.sender = info->sender;
1372 return snd_seq_system_notify(client, port, &event); /* non-atomic */
1377 * add to port's subscription list IOCTL interface
1379 static int snd_seq_ioctl_subscribe_port(client_t * client, void __user *arg)
1381 int result = -EINVAL;
1382 client_t *receiver = NULL, *sender = NULL;
1383 client_port_t *sport = NULL, *dport = NULL;
1384 snd_seq_port_subscribe_t subs;
1386 if (copy_from_user(&subs, arg, sizeof(subs)))
1387 return -EFAULT;
1389 if ((receiver = snd_seq_client_use_ptr(subs.dest.client)) == NULL)
1390 goto __end;
1391 if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)
1392 goto __end;
1393 if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)
1394 goto __end;
1395 if ((dport = snd_seq_port_use_ptr(receiver, subs.dest.port)) == NULL)
1396 goto __end;
1398 result = check_subscription_permission(client, sport, dport, &subs);
1399 if (result < 0)
1400 goto __end;
1402 /* connect them */
1403 result = snd_seq_port_connect(client, sender, sport, receiver, dport, &subs);
1404 if (! result) /* broadcast announce */
1405 snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,
1406 &subs, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
1407 __end:
1408 if (sport)
1409 snd_seq_port_unlock(sport);
1410 if (dport)
1411 snd_seq_port_unlock(dport);
1412 if (sender)
1413 snd_seq_client_unlock(sender);
1414 if (receiver)
1415 snd_seq_client_unlock(receiver);
1416 return result;
1421 * remove from port's subscription list
1423 static int snd_seq_ioctl_unsubscribe_port(client_t * client, void __user *arg)
1425 int result = -ENXIO;
1426 client_t *receiver = NULL, *sender = NULL;
1427 client_port_t *sport = NULL, *dport = NULL;
1428 snd_seq_port_subscribe_t subs;
1430 if (copy_from_user(&subs, arg, sizeof(subs)))
1431 return -EFAULT;
1433 if ((receiver = snd_seq_client_use_ptr(subs.dest.client)) == NULL)
1434 goto __end;
1435 if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)
1436 goto __end;
1437 if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)
1438 goto __end;
1439 if ((dport = snd_seq_port_use_ptr(receiver, subs.dest.port)) == NULL)
1440 goto __end;
1442 result = check_subscription_permission(client, sport, dport, &subs);
1443 if (result < 0)
1444 goto __end;
1446 result = snd_seq_port_disconnect(client, sender, sport, receiver, dport, &subs);
1447 if (! result) /* broadcast announce */
1448 snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,
1449 &subs, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
1450 __end:
1451 if (sport)
1452 snd_seq_port_unlock(sport);
1453 if (dport)
1454 snd_seq_port_unlock(dport);
1455 if (sender)
1456 snd_seq_client_unlock(sender);
1457 if (receiver)
1458 snd_seq_client_unlock(receiver);
1459 return result;
1463 /* CREATE_QUEUE ioctl() */
1464 static int snd_seq_ioctl_create_queue(client_t *client, void __user *arg)
1466 snd_seq_queue_info_t info;
1467 int result;
1468 queue_t *q;
1470 if (copy_from_user(&info, arg, sizeof(info)))
1471 return -EFAULT;
1473 result = snd_seq_queue_alloc(client->number, info.locked, info.flags);
1474 if (result < 0)
1475 return result;
1477 q = queueptr(result);
1478 if (q == NULL)
1479 return -EINVAL;
1481 info.queue = q->queue;
1482 info.locked = q->locked;
1483 info.owner = q->owner;
1485 /* set queue name */
1486 if (! info.name[0])
1487 snprintf(info.name, sizeof(info.name), "Queue-%d", q->queue);
1488 strlcpy(q->name, info.name, sizeof(q->name));
1489 queuefree(q);
1491 if (copy_to_user(arg, &info, sizeof(info)))
1492 return -EFAULT;
1494 return 0;
1497 /* DELETE_QUEUE ioctl() */
1498 static int snd_seq_ioctl_delete_queue(client_t *client, void __user *arg)
1500 snd_seq_queue_info_t info;
1502 if (copy_from_user(&info, arg, sizeof(info)))
1503 return -EFAULT;
1505 return snd_seq_queue_delete(client->number, info.queue);
1508 /* GET_QUEUE_INFO ioctl() */
1509 static int snd_seq_ioctl_get_queue_info(client_t *client, void __user *arg)
1511 snd_seq_queue_info_t info;
1512 queue_t *q;
1514 if (copy_from_user(&info, arg, sizeof(info)))
1515 return -EFAULT;
1517 q = queueptr(info.queue);
1518 if (q == NULL)
1519 return -EINVAL;
1521 memset(&info, 0, sizeof(info));
1522 info.queue = q->queue;
1523 info.owner = q->owner;
1524 info.locked = q->locked;
1525 strlcpy(info.name, q->name, sizeof(info.name));
1526 queuefree(q);
1528 if (copy_to_user(arg, &info, sizeof(info)))
1529 return -EFAULT;
1531 return 0;
1534 /* SET_QUEUE_INFO ioctl() */
1535 static int snd_seq_ioctl_set_queue_info(client_t *client, void __user *arg)
1537 snd_seq_queue_info_t info;
1538 queue_t *q;
1540 if (copy_from_user(&info, arg, sizeof(info)))
1541 return -EFAULT;
1543 if (info.owner != client->number)
1544 return -EINVAL;
1546 /* change owner/locked permission */
1547 if (snd_seq_queue_check_access(info.queue, client->number)) {
1548 if (snd_seq_queue_set_owner(info.queue, client->number, info.locked) < 0)
1549 return -EPERM;
1550 if (info.locked)
1551 snd_seq_queue_use(info.queue, client->number, 1);
1552 } else {
1553 return -EPERM;
1556 q = queueptr(info.queue);
1557 if (! q)
1558 return -EINVAL;
1559 if (q->owner != client->number) {
1560 queuefree(q);
1561 return -EPERM;
1563 strlcpy(q->name, info.name, sizeof(q->name));
1564 queuefree(q);
1566 return 0;
1569 /* GET_NAMED_QUEUE ioctl() */
1570 static int snd_seq_ioctl_get_named_queue(client_t *client, void __user *arg)
1572 snd_seq_queue_info_t info;
1573 queue_t *q;
1575 if (copy_from_user(&info, arg, sizeof(info)))
1576 return -EFAULT;
1578 q = snd_seq_queue_find_name(info.name);
1579 if (q == NULL)
1580 return -EINVAL;
1581 info.queue = q->queue;
1582 info.owner = q->owner;
1583 info.locked = q->locked;
1584 queuefree(q);
1586 if (copy_to_user(arg, &info, sizeof(info)))
1587 return -EFAULT;
1589 return 0;
1592 /* GET_QUEUE_STATUS ioctl() */
1593 static int snd_seq_ioctl_get_queue_status(client_t * client, void __user *arg)
1595 snd_seq_queue_status_t status;
1596 queue_t *queue;
1597 seq_timer_t *tmr;
1599 if (copy_from_user(&status, arg, sizeof(status)))
1600 return -EFAULT;
1602 queue = queueptr(status.queue);
1603 if (queue == NULL)
1604 return -EINVAL;
1605 memset(&status, 0, sizeof(status));
1606 status.queue = queue->queue;
1608 tmr = queue->timer;
1609 status.events = queue->tickq->cells + queue->timeq->cells;
1611 status.time = snd_seq_timer_get_cur_time(tmr);
1612 status.tick = snd_seq_timer_get_cur_tick(tmr);
1614 status.running = tmr->running;
1616 status.flags = queue->flags;
1617 queuefree(queue);
1619 if (copy_to_user(arg, &status, sizeof(status)))
1620 return -EFAULT;
1621 return 0;
1625 /* GET_QUEUE_TEMPO ioctl() */
1626 static int snd_seq_ioctl_get_queue_tempo(client_t * client, void __user *arg)
1628 snd_seq_queue_tempo_t tempo;
1629 queue_t *queue;
1630 seq_timer_t *tmr;
1632 if (copy_from_user(&tempo, arg, sizeof(tempo)))
1633 return -EFAULT;
1635 queue = queueptr(tempo.queue);
1636 if (queue == NULL)
1637 return -EINVAL;
1638 memset(&tempo, 0, sizeof(tempo));
1639 tempo.queue = queue->queue;
1641 tmr = queue->timer;
1643 tempo.tempo = tmr->tempo;
1644 tempo.ppq = tmr->ppq;
1645 tempo.skew_value = tmr->skew;
1646 tempo.skew_base = tmr->skew_base;
1647 queuefree(queue);
1649 if (copy_to_user(arg, &tempo, sizeof(tempo)))
1650 return -EFAULT;
1651 return 0;
1655 /* SET_QUEUE_TEMPO ioctl() */
1656 int snd_seq_set_queue_tempo(int client, snd_seq_queue_tempo_t *tempo)
1658 if (!snd_seq_queue_check_access(tempo->queue, client))
1659 return -EPERM;
1660 return snd_seq_queue_timer_set_tempo(tempo->queue, client, tempo);
1663 static int snd_seq_ioctl_set_queue_tempo(client_t * client, void __user *arg)
1665 int result;
1666 snd_seq_queue_tempo_t tempo;
1668 if (copy_from_user(&tempo, arg, sizeof(tempo)))
1669 return -EFAULT;
1671 result = snd_seq_set_queue_tempo(client->number, &tempo);
1672 return result < 0 ? result : 0;
1676 /* GET_QUEUE_TIMER ioctl() */
1677 static int snd_seq_ioctl_get_queue_timer(client_t * client, void __user *arg)
1679 snd_seq_queue_timer_t timer;
1680 queue_t *queue;
1681 seq_timer_t *tmr;
1683 if (copy_from_user(&timer, arg, sizeof(timer)))
1684 return -EFAULT;
1686 queue = queueptr(timer.queue);
1687 if (queue == NULL)
1688 return -EINVAL;
1690 if (down_interruptible(&queue->timer_mutex)) {
1691 queuefree(queue);
1692 return -ERESTARTSYS;
1694 tmr = queue->timer;
1695 memset(&timer, 0, sizeof(timer));
1696 timer.queue = queue->queue;
1698 timer.type = tmr->type;
1699 if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
1700 timer.u.alsa.id = tmr->alsa_id;
1701 timer.u.alsa.resolution = tmr->preferred_resolution;
1703 up(&queue->timer_mutex);
1704 queuefree(queue);
1706 if (copy_to_user(arg, &timer, sizeof(timer)))
1707 return -EFAULT;
1708 return 0;
1712 /* SET_QUEUE_TIMER ioctl() */
1713 static int snd_seq_ioctl_set_queue_timer(client_t * client, void __user *arg)
1715 int result = 0;
1716 snd_seq_queue_timer_t timer;
1718 if (copy_from_user(&timer, arg, sizeof(timer)))
1719 return -EFAULT;
1721 if (timer.type != SNDRV_SEQ_TIMER_ALSA)
1722 return -EINVAL;
1724 if (snd_seq_queue_check_access(timer.queue, client->number)) {
1725 queue_t *q;
1726 seq_timer_t *tmr;
1728 q = queueptr(timer.queue);
1729 if (q == NULL)
1730 return -ENXIO;
1731 if (down_interruptible(&q->timer_mutex)) {
1732 queuefree(q);
1733 return -ERESTARTSYS;
1735 tmr = q->timer;
1736 snd_seq_queue_timer_close(timer.queue);
1737 tmr->type = timer.type;
1738 if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
1739 tmr->alsa_id = timer.u.alsa.id;
1740 tmr->preferred_resolution = timer.u.alsa.resolution;
1742 result = snd_seq_queue_timer_open(timer.queue);
1743 up(&q->timer_mutex);
1744 queuefree(q);
1745 } else {
1746 return -EPERM;
1749 return result;
1753 /* GET_QUEUE_CLIENT ioctl() */
1754 static int snd_seq_ioctl_get_queue_client(client_t * client, void __user *arg)
1756 snd_seq_queue_client_t info;
1757 int used;
1759 if (copy_from_user(&info, arg, sizeof(info)))
1760 return -EFAULT;
1762 used = snd_seq_queue_is_used(info.queue, client->number);
1763 if (used < 0)
1764 return -EINVAL;
1765 info.used = used;
1766 info.client = client->number;
1768 if (copy_to_user(arg, &info, sizeof(info)))
1769 return -EFAULT;
1770 return 0;
1774 /* SET_QUEUE_CLIENT ioctl() */
1775 static int snd_seq_ioctl_set_queue_client(client_t * client, void __user *arg)
1777 int err;
1778 snd_seq_queue_client_t info;
1780 if (copy_from_user(&info, arg, sizeof(info)))
1781 return -EFAULT;
1783 if (info.used >= 0) {
1784 err = snd_seq_queue_use(info.queue, client->number, info.used);
1785 if (err < 0)
1786 return err;
1789 return snd_seq_ioctl_get_queue_client(client, arg);
1793 /* GET_CLIENT_POOL ioctl() */
1794 static int snd_seq_ioctl_get_client_pool(client_t * client, void __user *arg)
1796 snd_seq_client_pool_t info;
1797 client_t *cptr;
1799 if (copy_from_user(&info, arg, sizeof(info)))
1800 return -EFAULT;
1802 cptr = snd_seq_client_use_ptr(info.client);
1803 if (cptr == NULL)
1804 return -ENOENT;
1805 memset(&info, 0, sizeof(info));
1806 info.output_pool = cptr->pool->size;
1807 info.output_room = cptr->pool->room;
1808 info.output_free = info.output_pool;
1809 if (cptr->pool)
1810 info.output_free = snd_seq_unused_cells(cptr->pool);
1811 if (cptr->type == USER_CLIENT) {
1812 info.input_pool = cptr->data.user.fifo_pool_size;
1813 info.input_free = info.input_pool;
1814 if (cptr->data.user.fifo)
1815 info.input_free = snd_seq_unused_cells(cptr->data.user.fifo->pool);
1816 } else {
1817 info.input_pool = 0;
1818 info.input_free = 0;
1820 snd_seq_client_unlock(cptr);
1822 if (copy_to_user(arg, &info, sizeof(info)))
1823 return -EFAULT;
1824 return 0;
1827 /* SET_CLIENT_POOL ioctl() */
1828 static int snd_seq_ioctl_set_client_pool(client_t * client, void __user *arg)
1830 snd_seq_client_pool_t info;
1831 int rc;
1833 if (copy_from_user(&info, arg, sizeof(info)))
1834 return -EFAULT;
1836 if (client->number != info.client)
1837 return -EINVAL; /* can't change other clients */
1839 if (info.output_pool >= 1 && info.output_pool <= SNDRV_SEQ_MAX_EVENTS &&
1840 (! snd_seq_write_pool_allocated(client) ||
1841 info.output_pool != client->pool->size)) {
1842 if (snd_seq_write_pool_allocated(client)) {
1843 /* remove all existing cells */
1844 snd_seq_queue_client_leave_cells(client->number);
1845 snd_seq_pool_done(client->pool);
1847 client->pool->size = info.output_pool;
1848 rc = snd_seq_pool_init(client->pool);
1849 if (rc < 0)
1850 return rc;
1852 if (client->type == USER_CLIENT && client->data.user.fifo != NULL &&
1853 info.input_pool >= 1 &&
1854 info.input_pool <= SNDRV_SEQ_MAX_CLIENT_EVENTS &&
1855 info.input_pool != client->data.user.fifo_pool_size) {
1856 /* change pool size */
1857 rc = snd_seq_fifo_resize(client->data.user.fifo, info.input_pool);
1858 if (rc < 0)
1859 return rc;
1860 client->data.user.fifo_pool_size = info.input_pool;
1862 if (info.output_room >= 1 &&
1863 info.output_room <= client->pool->size) {
1864 client->pool->room = info.output_room;
1867 return snd_seq_ioctl_get_client_pool(client, arg);
1871 /* REMOVE_EVENTS ioctl() */
1872 static int snd_seq_ioctl_remove_events(client_t * client, void __user *arg)
1874 snd_seq_remove_events_t info;
1876 if (copy_from_user(&info, arg, sizeof(info)))
1877 return -EFAULT;
1880 * Input mostly not implemented XXX.
1882 if (info.remove_mode & SNDRV_SEQ_REMOVE_INPUT) {
1884 * No restrictions so for a user client we can clear
1885 * the whole fifo
1887 if (client->type == USER_CLIENT)
1888 snd_seq_fifo_clear(client->data.user.fifo);
1891 if (info.remove_mode & SNDRV_SEQ_REMOVE_OUTPUT)
1892 snd_seq_queue_remove_cells(client->number, &info);
1894 return 0;
1899 * get subscription info
1901 static int snd_seq_ioctl_get_subscription(client_t *client, void __user *arg)
1903 int result;
1904 client_t *sender = NULL;
1905 client_port_t *sport = NULL;
1906 snd_seq_port_subscribe_t subs;
1907 subscribers_t *p;
1909 if (copy_from_user(&subs, arg, sizeof(subs)))
1910 return -EFAULT;
1912 result = -EINVAL;
1913 if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)
1914 goto __end;
1915 if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)
1916 goto __end;
1917 p = snd_seq_port_get_subscription(&sport->c_src, &subs.dest);
1918 if (p) {
1919 result = 0;
1920 subs = p->info;
1921 } else
1922 result = -ENOENT;
1924 __end:
1925 if (sport)
1926 snd_seq_port_unlock(sport);
1927 if (sender)
1928 snd_seq_client_unlock(sender);
1929 if (result >= 0) {
1930 if (copy_to_user(arg, &subs, sizeof(subs)))
1931 return -EFAULT;
1933 return result;
1938 * get subscription info - check only its presence
1940 static int snd_seq_ioctl_query_subs(client_t *client, void __user *arg)
1942 int result = -ENXIO;
1943 client_t *cptr = NULL;
1944 client_port_t *port = NULL;
1945 snd_seq_query_subs_t subs;
1946 port_subs_info_t *group;
1947 struct list_head *p;
1948 int i;
1950 if (copy_from_user(&subs, arg, sizeof(subs)))
1951 return -EFAULT;
1953 if ((cptr = snd_seq_client_use_ptr(subs.root.client)) == NULL)
1954 goto __end;
1955 if ((port = snd_seq_port_use_ptr(cptr, subs.root.port)) == NULL)
1956 goto __end;
1958 switch (subs.type) {
1959 case SNDRV_SEQ_QUERY_SUBS_READ:
1960 group = &port->c_src;
1961 break;
1962 case SNDRV_SEQ_QUERY_SUBS_WRITE:
1963 group = &port->c_dest;
1964 break;
1965 default:
1966 goto __end;
1969 down_read(&group->list_mutex);
1970 /* search for the subscriber */
1971 subs.num_subs = group->count;
1972 i = 0;
1973 result = -ENOENT;
1974 list_for_each(p, &group->list_head) {
1975 if (i++ == subs.index) {
1976 /* found! */
1977 subscribers_t *s;
1978 if (subs.type == SNDRV_SEQ_QUERY_SUBS_READ) {
1979 s = list_entry(p, subscribers_t, src_list);
1980 subs.addr = s->info.dest;
1981 } else {
1982 s = list_entry(p, subscribers_t, dest_list);
1983 subs.addr = s->info.sender;
1985 subs.flags = s->info.flags;
1986 subs.queue = s->info.queue;
1987 result = 0;
1988 break;
1991 up_read(&group->list_mutex);
1993 __end:
1994 if (port)
1995 snd_seq_port_unlock(port);
1996 if (cptr)
1997 snd_seq_client_unlock(cptr);
1998 if (result >= 0) {
1999 if (copy_to_user(arg, &subs, sizeof(subs)))
2000 return -EFAULT;
2002 return result;
2007 * query next client
2009 static int snd_seq_ioctl_query_next_client(client_t *client, void __user *arg)
2011 client_t *cptr = NULL;
2012 snd_seq_client_info_t info;
2014 if (copy_from_user(&info, arg, sizeof(info)))
2015 return -EFAULT;
2017 /* search for next client */
2018 info.client++;
2019 if (info.client < 0)
2020 info.client = 0;
2021 for (; info.client < SNDRV_SEQ_MAX_CLIENTS; info.client++) {
2022 cptr = snd_seq_client_use_ptr(info.client);
2023 if (cptr)
2024 break; /* found */
2026 if (cptr == NULL)
2027 return -ENOENT;
2029 get_client_info(cptr, &info);
2030 snd_seq_client_unlock(cptr);
2032 if (copy_to_user(arg, &info, sizeof(info)))
2033 return -EFAULT;
2034 return 0;
2038 * query next port
2040 static int snd_seq_ioctl_query_next_port(client_t *client, void __user *arg)
2042 client_t *cptr;
2043 client_port_t *port = NULL;
2044 snd_seq_port_info_t info;
2046 if (copy_from_user(&info, arg, sizeof(info)))
2047 return -EFAULT;
2048 cptr = snd_seq_client_use_ptr(info.addr.client);
2049 if (cptr == NULL)
2050 return -ENXIO;
2052 /* search for next port */
2053 info.addr.port++;
2054 port = snd_seq_port_query_nearest(cptr, &info);
2055 if (port == NULL) {
2056 snd_seq_client_unlock(cptr);
2057 return -ENOENT;
2060 /* get port info */
2061 info.addr = port->addr;
2062 snd_seq_get_port_info(port, &info);
2063 snd_seq_port_unlock(port);
2064 snd_seq_client_unlock(cptr);
2066 if (copy_to_user(arg, &info, sizeof(info)))
2067 return -EFAULT;
2068 return 0;
2071 /* -------------------------------------------------------- */
2073 static struct seq_ioctl_table {
2074 unsigned int cmd;
2075 int (*func)(client_t *client, void __user * arg);
2076 } ioctl_tables[] = {
2077 { SNDRV_SEQ_IOCTL_SYSTEM_INFO, snd_seq_ioctl_system_info },
2078 { SNDRV_SEQ_IOCTL_RUNNING_MODE, snd_seq_ioctl_running_mode },
2079 { SNDRV_SEQ_IOCTL_GET_CLIENT_INFO, snd_seq_ioctl_get_client_info },
2080 { SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, snd_seq_ioctl_set_client_info },
2081 { SNDRV_SEQ_IOCTL_CREATE_PORT, snd_seq_ioctl_create_port },
2082 { SNDRV_SEQ_IOCTL_DELETE_PORT, snd_seq_ioctl_delete_port },
2083 { SNDRV_SEQ_IOCTL_GET_PORT_INFO, snd_seq_ioctl_get_port_info },
2084 { SNDRV_SEQ_IOCTL_SET_PORT_INFO, snd_seq_ioctl_set_port_info },
2085 { SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, snd_seq_ioctl_subscribe_port },
2086 { SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, snd_seq_ioctl_unsubscribe_port },
2087 { SNDRV_SEQ_IOCTL_CREATE_QUEUE, snd_seq_ioctl_create_queue },
2088 { SNDRV_SEQ_IOCTL_DELETE_QUEUE, snd_seq_ioctl_delete_queue },
2089 { SNDRV_SEQ_IOCTL_GET_QUEUE_INFO, snd_seq_ioctl_get_queue_info },
2090 { SNDRV_SEQ_IOCTL_SET_QUEUE_INFO, snd_seq_ioctl_set_queue_info },
2091 { SNDRV_SEQ_IOCTL_GET_NAMED_QUEUE, snd_seq_ioctl_get_named_queue },
2092 { SNDRV_SEQ_IOCTL_GET_QUEUE_STATUS, snd_seq_ioctl_get_queue_status },
2093 { SNDRV_SEQ_IOCTL_GET_QUEUE_TEMPO, snd_seq_ioctl_get_queue_tempo },
2094 { SNDRV_SEQ_IOCTL_SET_QUEUE_TEMPO, snd_seq_ioctl_set_queue_tempo },
2095 { SNDRV_SEQ_IOCTL_GET_QUEUE_TIMER, snd_seq_ioctl_get_queue_timer },
2096 { SNDRV_SEQ_IOCTL_SET_QUEUE_TIMER, snd_seq_ioctl_set_queue_timer },
2097 { SNDRV_SEQ_IOCTL_GET_QUEUE_CLIENT, snd_seq_ioctl_get_queue_client },
2098 { SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT, snd_seq_ioctl_set_queue_client },
2099 { SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, snd_seq_ioctl_get_client_pool },
2100 { SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, snd_seq_ioctl_set_client_pool },
2101 { SNDRV_SEQ_IOCTL_GET_SUBSCRIPTION, snd_seq_ioctl_get_subscription },
2102 { SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT, snd_seq_ioctl_query_next_client },
2103 { SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, snd_seq_ioctl_query_next_port },
2104 { SNDRV_SEQ_IOCTL_REMOVE_EVENTS, snd_seq_ioctl_remove_events },
2105 { SNDRV_SEQ_IOCTL_QUERY_SUBS, snd_seq_ioctl_query_subs },
2106 { 0, NULL },
2109 static int snd_seq_do_ioctl(client_t *client, unsigned int cmd, void __user *arg)
2111 struct seq_ioctl_table *p;
2113 switch (cmd) {
2114 case SNDRV_SEQ_IOCTL_PVERSION:
2115 /* return sequencer version number */
2116 return put_user(SNDRV_SEQ_VERSION, (int __user *)arg) ? -EFAULT : 0;
2117 case SNDRV_SEQ_IOCTL_CLIENT_ID:
2118 /* return the id of this client */
2119 return put_user(client->number, (int __user *)arg) ? -EFAULT : 0;
2122 if (! arg)
2123 return -EFAULT;
2124 for (p = ioctl_tables; p->cmd; p++) {
2125 if (p->cmd == cmd)
2126 return p->func(client, arg);
2128 snd_printd("seq unknown ioctl() 0x%x (type='%c', number=0x%2x)\n",
2129 cmd, _IOC_TYPE(cmd), _IOC_NR(cmd));
2130 return -ENOTTY;
2134 static long snd_seq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2136 client_t *client = (client_t *) file->private_data;
2138 snd_assert(client != NULL, return -ENXIO);
2140 return snd_seq_do_ioctl(client, cmd, (void __user *) arg);
2143 #ifdef CONFIG_COMPAT
2144 #include "seq_compat.c"
2145 #else
2146 #define snd_seq_ioctl_compat NULL
2147 #endif
2149 /* -------------------------------------------------------- */
2152 /* exported to kernel modules */
2153 int snd_seq_create_kernel_client(snd_card_t *card, int client_index, snd_seq_client_callback_t * callback)
2155 client_t *client;
2157 snd_assert(! in_interrupt(), return -EBUSY);
2159 if (callback == NULL)
2160 return -EINVAL;
2161 if (card && client_index > 7)
2162 return -EINVAL;
2163 if (card == NULL && client_index > 63)
2164 return -EINVAL;
2165 if (card)
2166 client_index += 64 + (card->number << 3);
2168 if (down_interruptible(&register_mutex))
2169 return -ERESTARTSYS;
2170 /* empty write queue as default */
2171 client = seq_create_client1(client_index, 0);
2172 if (client == NULL) {
2173 up(&register_mutex);
2174 return -EBUSY; /* failure code */
2176 usage_alloc(&client_usage, 1);
2178 client->accept_input = callback->allow_output;
2179 client->accept_output = callback->allow_input;
2181 /* fill client data */
2182 client->data.kernel.card = card;
2183 client->data.kernel.private_data = callback->private_data;
2184 sprintf(client->name, "Client-%d", client->number);
2186 client->type = KERNEL_CLIENT;
2187 up(&register_mutex);
2189 /* make others aware this new client */
2190 snd_seq_system_client_ev_client_start(client->number);
2192 /* return client number to caller */
2193 return client->number;
2196 /* exported to kernel modules */
2197 int snd_seq_delete_kernel_client(int client)
2199 client_t *ptr;
2201 snd_assert(! in_interrupt(), return -EBUSY);
2203 ptr = clientptr(client);
2204 if (ptr == NULL)
2205 return -EINVAL;
2207 seq_free_client(ptr);
2208 kfree(ptr);
2209 return 0;
2213 /* skeleton to enqueue event, called from snd_seq_kernel_client_enqueue
2214 * and snd_seq_kernel_client_enqueue_blocking
2216 static int kernel_client_enqueue(int client, snd_seq_event_t *ev,
2217 struct file *file, int blocking,
2218 int atomic, int hop)
2220 client_t *cptr;
2221 int result;
2223 snd_assert(ev != NULL, return -EINVAL);
2225 if (ev->type == SNDRV_SEQ_EVENT_NONE)
2226 return 0; /* ignore this */
2227 if (ev->type == SNDRV_SEQ_EVENT_KERNEL_ERROR)
2228 return -EINVAL; /* quoted events can't be enqueued */
2230 /* fill in client number */
2231 ev->source.client = client;
2233 if (check_event_type_and_length(ev))
2234 return -EINVAL;
2236 cptr = snd_seq_client_use_ptr(client);
2237 if (cptr == NULL)
2238 return -EINVAL;
2240 if (! cptr->accept_output)
2241 result = -EPERM;
2242 else /* send it */
2243 result = snd_seq_client_enqueue_event(cptr, ev, file, blocking, atomic, hop);
2245 snd_seq_client_unlock(cptr);
2246 return result;
2250 * exported, called by kernel clients to enqueue events (w/o blocking)
2252 * RETURN VALUE: zero if succeed, negative if error
2254 int snd_seq_kernel_client_enqueue(int client, snd_seq_event_t * ev,
2255 int atomic, int hop)
2257 return kernel_client_enqueue(client, ev, NULL, 0, atomic, hop);
2261 * exported, called by kernel clients to enqueue events (with blocking)
2263 * RETURN VALUE: zero if succeed, negative if error
2265 int snd_seq_kernel_client_enqueue_blocking(int client, snd_seq_event_t * ev,
2266 struct file *file,
2267 int atomic, int hop)
2269 return kernel_client_enqueue(client, ev, file, 1, atomic, hop);
2274 * exported, called by kernel clients to dispatch events directly to other
2275 * clients, bypassing the queues. Event time-stamp will be updated.
2277 * RETURN VALUE: negative = delivery failed,
2278 * zero, or positive: the number of delivered events
2280 int snd_seq_kernel_client_dispatch(int client, snd_seq_event_t * ev,
2281 int atomic, int hop)
2283 client_t *cptr;
2284 int result;
2286 snd_assert(ev != NULL, return -EINVAL);
2288 /* fill in client number */
2289 ev->queue = SNDRV_SEQ_QUEUE_DIRECT;
2290 ev->source.client = client;
2292 if (check_event_type_and_length(ev))
2293 return -EINVAL;
2295 cptr = snd_seq_client_use_ptr(client);
2296 if (cptr == NULL)
2297 return -EINVAL;
2299 if (!cptr->accept_output)
2300 result = -EPERM;
2301 else
2302 result = snd_seq_deliver_event(cptr, ev, atomic, hop);
2304 snd_seq_client_unlock(cptr);
2305 return result;
2310 * exported, called by kernel clients to perform same functions as with
2311 * userland ioctl()
2313 int snd_seq_kernel_client_ctl(int clientid, unsigned int cmd, void *arg)
2315 client_t *client;
2316 mm_segment_t fs;
2317 int result;
2319 client = clientptr(clientid);
2320 if (client == NULL)
2321 return -ENXIO;
2322 fs = snd_enter_user();
2323 result = snd_seq_do_ioctl(client, cmd, (void __user *)arg);
2324 snd_leave_user(fs);
2325 return result;
2329 /* exported (for OSS emulator) */
2330 int snd_seq_kernel_client_write_poll(int clientid, struct file *file, poll_table *wait)
2332 client_t *client;
2334 client = clientptr(clientid);
2335 if (client == NULL)
2336 return -ENXIO;
2338 if (! snd_seq_write_pool_allocated(client))
2339 return 1;
2340 if (snd_seq_pool_poll_wait(client->pool, file, wait))
2341 return 1;
2342 return 0;
2345 /*---------------------------------------------------------------------------*/
2348 * /proc interface
2350 static void snd_seq_info_dump_subscribers(snd_info_buffer_t *buffer, port_subs_info_t *group, int is_src, char *msg)
2352 struct list_head *p;
2353 subscribers_t *s;
2354 int count = 0;
2356 down_read(&group->list_mutex);
2357 if (list_empty(&group->list_head)) {
2358 up_read(&group->list_mutex);
2359 return;
2361 snd_iprintf(buffer, msg);
2362 list_for_each(p, &group->list_head) {
2363 if (is_src)
2364 s = list_entry(p, subscribers_t, src_list);
2365 else
2366 s = list_entry(p, subscribers_t, dest_list);
2367 if (count++)
2368 snd_iprintf(buffer, ", ");
2369 snd_iprintf(buffer, "%d:%d",
2370 is_src ? s->info.dest.client : s->info.sender.client,
2371 is_src ? s->info.dest.port : s->info.sender.port);
2372 if (s->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)
2373 snd_iprintf(buffer, "[%c:%d]", ((s->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL) ? 'r' : 't'), s->info.queue);
2374 if (group->exclusive)
2375 snd_iprintf(buffer, "[ex]");
2377 up_read(&group->list_mutex);
2378 snd_iprintf(buffer, "\n");
2381 #define FLAG_PERM_RD(perm) ((perm) & SNDRV_SEQ_PORT_CAP_READ ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_READ ? 'R' : 'r') : '-')
2382 #define FLAG_PERM_WR(perm) ((perm) & SNDRV_SEQ_PORT_CAP_WRITE ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_WRITE ? 'W' : 'w') : '-')
2383 #define FLAG_PERM_EX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_NO_EXPORT ? '-' : 'e')
2385 #define FLAG_PERM_DUPLEX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_DUPLEX ? 'X' : '-')
2387 static void snd_seq_info_dump_ports(snd_info_buffer_t *buffer, client_t *client)
2389 struct list_head *l;
2391 down(&client->ports_mutex);
2392 list_for_each(l, &client->ports_list_head) {
2393 client_port_t *p = list_entry(l, client_port_t, list);
2394 snd_iprintf(buffer, " Port %3d : \"%s\" (%c%c%c%c)\n",
2395 p->addr.port, p->name,
2396 FLAG_PERM_RD(p->capability),
2397 FLAG_PERM_WR(p->capability),
2398 FLAG_PERM_EX(p->capability),
2399 FLAG_PERM_DUPLEX(p->capability));
2400 snd_seq_info_dump_subscribers(buffer, &p->c_src, 1, " Connecting To: ");
2401 snd_seq_info_dump_subscribers(buffer, &p->c_dest, 0, " Connected From: ");
2403 up(&client->ports_mutex);
2407 /* exported to seq_info.c */
2408 void snd_seq_info_clients_read(snd_info_entry_t *entry,
2409 snd_info_buffer_t * buffer)
2411 extern void snd_seq_info_pool(snd_info_buffer_t * buffer, pool_t * pool, char *space);
2412 int c;
2413 client_t *client;
2415 snd_iprintf(buffer, "Client info\n");
2416 snd_iprintf(buffer, " cur clients : %d\n", client_usage.cur);
2417 snd_iprintf(buffer, " peak clients : %d\n", client_usage.peak);
2418 snd_iprintf(buffer, " max clients : %d\n", SNDRV_SEQ_MAX_CLIENTS);
2419 snd_iprintf(buffer, "\n");
2421 /* list the client table */
2422 for (c = 0; c < SNDRV_SEQ_MAX_CLIENTS; c++) {
2423 client = snd_seq_client_use_ptr(c);
2424 if (client == NULL)
2425 continue;
2426 if (client->type == NO_CLIENT) {
2427 snd_seq_client_unlock(client);
2428 continue;
2431 snd_iprintf(buffer, "Client %3d : \"%s\" [%s]\n",
2432 c, client->name,
2433 client->type == USER_CLIENT ? "User" : "Kernel");
2434 snd_seq_info_dump_ports(buffer, client);
2435 if (snd_seq_write_pool_allocated(client)) {
2436 snd_iprintf(buffer, " Output pool :\n");
2437 snd_seq_info_pool(buffer, client->pool, " ");
2439 if (client->type == USER_CLIENT && client->data.user.fifo &&
2440 client->data.user.fifo->pool) {
2441 snd_iprintf(buffer, " Input pool :\n");
2442 snd_seq_info_pool(buffer, client->data.user.fifo->pool, " ");
2444 snd_seq_client_unlock(client);
2449 /*---------------------------------------------------------------------------*/
2453 * REGISTRATION PART
2456 static struct file_operations snd_seq_f_ops =
2458 .owner = THIS_MODULE,
2459 .read = snd_seq_read,
2460 .write = snd_seq_write,
2461 .open = snd_seq_open,
2462 .release = snd_seq_release,
2463 .poll = snd_seq_poll,
2464 .unlocked_ioctl = snd_seq_ioctl,
2465 .compat_ioctl = snd_seq_ioctl_compat,
2468 static snd_minor_t snd_seq_reg =
2470 .comment = "sequencer",
2471 .f_ops = &snd_seq_f_ops,
2476 * register sequencer device
2478 int __init snd_sequencer_device_init(void)
2480 int err;
2482 if (down_interruptible(&register_mutex))
2483 return -ERESTARTSYS;
2485 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0, &snd_seq_reg, "seq")) < 0) {
2486 up(&register_mutex);
2487 return err;
2490 up(&register_mutex);
2492 return 0;
2498 * unregister sequencer device
2500 void __exit snd_sequencer_device_done(void)
2502 snd_unregister_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0);