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/slab.h>
27 #include <sound/core.h>
28 #include <sound/minors.h>
29 #include <linux/kmod.h>
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"
37 #include "seq_system.h"
38 #include <sound/seq_device.h>
39 #if defined(CONFIG_SND_BIT32_EMUL) || defined(CONFIG_SND_BIT32_EMUL_MODULE)
40 #include "../ioctl32/ioctl32.h"
45 * this module handles the connections of userland and kernel clients
49 #define SNDRV_SEQ_LFLG_INPUT 0x0001
50 #define SNDRV_SEQ_LFLG_OUTPUT 0x0002
51 #define SNDRV_SEQ_LFLG_OPEN (SNDRV_SEQ_LFLG_INPUT|SNDRV_SEQ_LFLG_OUTPUT)
53 static spinlock_t clients_lock = SPIN_LOCK_UNLOCKED;
54 static DECLARE_MUTEX(register_mutex);
59 static char clienttablock[SNDRV_SEQ_MAX_CLIENTS];
60 static client_t *clienttab[SNDRV_SEQ_MAX_CLIENTS];
61 static usage_t client_usage;
66 static int bounce_error_event(client_t *client, snd_seq_event_t *event, int err, int atomic, int hop);
67 static int snd_seq_deliver_single_event(client_t *client, snd_seq_event_t *event, int filter, int atomic, int hop);
72 static inline mm_segment_t snd_enter_user(void)
74 mm_segment_t fs = get_fs();
79 static inline void snd_leave_user(mm_segment_t fs)
86 static inline unsigned short snd_seq_file_flags(struct file *file)
88 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
90 return SNDRV_SEQ_LFLG_OUTPUT;
92 return SNDRV_SEQ_LFLG_INPUT;
94 return SNDRV_SEQ_LFLG_OPEN;
98 static inline int snd_seq_write_pool_allocated(client_t *client)
100 return snd_seq_total_cells(client->pool) > 0;
103 /* return pointer to client structure for specified id */
104 static client_t *clientptr(int clientid)
106 if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) {
107 snd_printd("Seq: oops. Trying to get pointer to client %d\n", clientid);
110 return clienttab[clientid];
113 extern int seq_client_load[];
115 client_t *snd_seq_client_use_ptr(int clientid)
120 if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) {
121 snd_printd("Seq: oops. Trying to get pointer to client %d\n", clientid);
124 spin_lock_irqsave(&clients_lock, flags);
125 client = clientptr(clientid);
128 if (clienttablock[clientid]) {
129 spin_unlock_irqrestore(&clients_lock, flags);
132 spin_unlock_irqrestore(&clients_lock, flags);
134 if (!in_interrupt() && current->fs->root) {
135 static char client_requested[64];
136 static char card_requested[SNDRV_CARDS];
140 if (! client_requested[clientid] && current->fs->root) {
141 client_requested[clientid] = 1;
142 for (idx = 0; idx < 64; idx++) {
143 if (seq_client_load[idx] < 0)
145 if (seq_client_load[idx] == clientid) {
146 request_module("snd-seq-client-%i", clientid);
151 } else if (clientid >= 64 && clientid < 128) {
152 int card = (clientid - 64) / 8;
153 if (card < snd_ecards_limit) {
154 if (! card_requested[card]) {
155 card_requested[card] = 1;
156 snd_request_card(card);
158 snd_seq_device_load_drivers();
161 spin_lock_irqsave(&clients_lock, flags);
162 client = clientptr(clientid);
165 spin_unlock_irqrestore(&clients_lock, flags);
171 snd_use_lock_use(&client->use_lock);
172 spin_unlock_irqrestore(&clients_lock, flags);
176 static void usage_alloc(usage_t * res, int num)
179 if (res->cur > res->peak)
180 res->peak = res->cur;
183 static void usage_free(usage_t * res, int num)
188 /* initialise data structures */
189 int __init client_init_data(void)
191 /* zap out the client table */
192 memset(&clienttablock, 0, sizeof(clienttablock));
193 memset(&clienttab, 0, sizeof(clienttab));
198 static client_t *seq_create_client1(int client_index, int poolsize)
204 /* init client data */
205 client = snd_kcalloc(sizeof(client_t), GFP_KERNEL);
208 client->pool = snd_seq_pool_new(poolsize);
209 if (client->pool == NULL) {
213 client->type = NO_CLIENT;
214 snd_use_lock_init(&client->use_lock);
215 rwlock_init(&client->ports_lock);
216 init_MUTEX(&client->ports_mutex);
217 INIT_LIST_HEAD(&client->ports_list_head);
219 /* find free slot in the client table */
220 spin_lock_irqsave(&clients_lock, flags);
221 if (client_index < 0) {
222 for (c = 128; c < SNDRV_SEQ_MAX_CLIENTS; c++) {
223 if (clienttab[c] || clienttablock[c])
225 clienttab[client->number = c] = client;
226 spin_unlock_irqrestore(&clients_lock, flags);
230 if (clienttab[client_index] == NULL && !clienttablock[client_index]) {
231 clienttab[client->number = client_index] = client;
232 spin_unlock_irqrestore(&clients_lock, flags);
236 spin_unlock_irqrestore(&clients_lock, flags);
237 snd_seq_pool_delete(&client->pool);
239 return NULL; /* no free slot found or busy, return failure code */
243 static int seq_free_client1(client_t *client)
247 snd_assert(client != NULL, return -EINVAL);
248 snd_seq_delete_all_ports(client);
249 snd_seq_queue_client_leave(client->number);
250 spin_lock_irqsave(&clients_lock, flags);
251 clienttablock[client->number] = 1;
252 clienttab[client->number] = NULL;
253 spin_unlock_irqrestore(&clients_lock, flags);
254 snd_use_lock_sync(&client->use_lock);
255 snd_seq_queue_client_termination(client->number);
257 snd_seq_pool_delete(&client->pool);
258 spin_lock_irqsave(&clients_lock, flags);
259 clienttablock[client->number] = 0;
260 spin_unlock_irqrestore(&clients_lock, flags);
265 static void seq_free_client(client_t * client)
267 down(®ister_mutex);
268 switch (client->type) {
270 snd_printk(KERN_WARNING "Seq: Trying to free unused client %d\n", client->number);
274 seq_free_client1(client);
275 usage_free(&client_usage, 1);
279 snd_printk(KERN_ERR "Seq: Trying to free client %d with undefined type = %d\n", client->number, client->type);
283 snd_seq_system_client_ev_client_exit(client->number);
288 /* -------------------------------------------------------- */
290 /* create a user client */
291 static int snd_seq_open(struct inode *inode, struct file *file)
293 int c, mode; /* client id */
297 if (down_interruptible(®ister_mutex))
299 client = seq_create_client1(-1, SNDRV_SEQ_DEFAULT_EVENTS);
300 if (client == NULL) {
302 return -ENOMEM; /* failure code */
305 mode = snd_seq_file_flags(file);
306 if (mode & SNDRV_SEQ_LFLG_INPUT)
307 client->accept_input = 1;
308 if (mode & SNDRV_SEQ_LFLG_OUTPUT)
309 client->accept_output = 1;
311 user = &client->data.user;
313 user->fifo_pool_size = 0;
315 if (mode & SNDRV_SEQ_LFLG_INPUT) {
316 user->fifo_pool_size = SNDRV_SEQ_DEFAULT_CLIENT_EVENTS;
317 user->fifo = snd_seq_fifo_new(user->fifo_pool_size);
318 if (user->fifo == NULL) {
319 seq_free_client1(client);
326 usage_alloc(&client_usage, 1);
327 client->type = USER_CLIENT;
331 file->private_data = client;
333 /* fill client data */
335 sprintf(client->name, "Client-%d", c);
337 /* make others aware this new client */
338 snd_seq_system_client_ev_client_start(c);
343 /* delete a user client */
344 static int snd_seq_release(struct inode *inode, struct file *file)
346 client_t *client = (client_t *) file->private_data;
349 seq_free_client(client);
350 if (client->data.user.fifo)
351 snd_seq_fifo_delete(&client->data.user.fifo);
359 /* handle client read() */
360 /* possible error values:
361 * -ENXIO invalid client or file open mode
362 * -ENOSPC FIFO overflow (the flag is cleared after this error report)
363 * -EINVAL no enough user-space buffer to write the whole event
364 * -EFAULT seg. fault during copy to user space
366 static ssize_t snd_seq_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
368 client_t *client = (client_t *) file->private_data;
372 snd_seq_event_cell_t *cell;
374 if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT))
377 if (verify_area(VERIFY_WRITE, buf, count))
380 /* check client structures are in place */
381 snd_assert(client != NULL, return -ENXIO);
383 if (!client->accept_input || (fifo = client->data.user.fifo) == NULL)
386 if (atomic_read(&fifo->overflow) > 0) {
387 /* buffer overflow is detected */
388 snd_seq_fifo_clear(fifo);
389 /* return error code */
395 snd_seq_fifo_lock(fifo);
397 /* while data available in queue */
398 while (count >= sizeof(snd_seq_event_t)) {
401 nonblock = (file->f_flags & O_NONBLOCK) || result > 0;
402 if ((err = snd_seq_fifo_cell_out(fifo, &cell, nonblock)) < 0) {
405 if (snd_seq_ev_is_variable(&cell->event)) {
406 snd_seq_event_t tmpev;
408 tmpev.data.ext.len &= ~SNDRV_SEQ_EXT_MASK;
409 if (copy_to_user(buf, &tmpev, sizeof(snd_seq_event_t))) {
413 count -= sizeof(snd_seq_event_t);
414 buf += sizeof(snd_seq_event_t);
415 err = snd_seq_expand_var_event(&cell->event, count, buf, 0, sizeof(snd_seq_event_t));
422 copy_to_user(buf, &cell->event, sizeof(snd_seq_event_t));
423 count -= sizeof(snd_seq_event_t);
424 buf += sizeof(snd_seq_event_t);
426 snd_seq_cell_free(cell);
427 cell = NULL; /* to be sure */
428 result += sizeof(snd_seq_event_t);
433 snd_seq_fifo_cell_putback(fifo, cell);
434 if (err == -EAGAIN && result > 0)
437 snd_seq_fifo_unlock(fifo);
439 return (err < 0) ? err : result;
444 * check access permission to the port
446 static int check_port_perm(client_port_t *port, unsigned int flags)
448 if ((port->capability & flags) != flags)
454 * check if the destination client is available, and return the pointer
455 * if filter is non-zero, client filter bitmap is tested.
457 static client_t *get_event_dest_client(snd_seq_event_t *event, int filter)
461 dest = snd_seq_client_use_ptr(event->dest.client);
464 if (! dest->accept_input)
466 if ((dest->filter & SNDRV_SEQ_FILTER_USE_EVENT) &&
467 ! test_bit(event->type, dest->event_filter))
469 if (filter && !(dest->filter & filter))
472 return dest; /* ok - accessible */
474 snd_seq_client_unlock(dest);
480 * Return the error event.
482 * If the receiver client is a user client, the original event is
483 * encapsulated in SNDRV_SEQ_EVENT_BOUNCE as variable length event. If
484 * the original event is also variable length, the external data is
485 * copied after the event record.
486 * If the receiver client is a kernel client, the original event is
487 * quoted in SNDRV_SEQ_EVENT_KERNEL_ERROR, since this requires no extra
490 static int bounce_error_event(client_t *client, snd_seq_event_t *event,
491 int err, int atomic, int hop)
493 snd_seq_event_t bounce_ev;
496 if (client == NULL ||
497 ! (client->filter & SNDRV_SEQ_FILTER_BOUNCE) ||
498 ! client->accept_input)
499 return 0; /* ignored */
501 /* set up quoted error */
502 memset(&bounce_ev, 0, sizeof(bounce_ev));
503 bounce_ev.type = SNDRV_SEQ_EVENT_KERNEL_ERROR;
504 bounce_ev.flags = SNDRV_SEQ_EVENT_LENGTH_FIXED;
505 bounce_ev.queue = SNDRV_SEQ_QUEUE_DIRECT;
506 bounce_ev.source.client = SNDRV_SEQ_CLIENT_SYSTEM;
507 bounce_ev.source.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
508 bounce_ev.dest.client = client->number;
509 bounce_ev.dest.port = event->source.port;
510 bounce_ev.data.quote.origin = event->dest;
511 bounce_ev.data.quote.event = event;
512 bounce_ev.data.quote.value = -err; /* use positive value */
513 result = snd_seq_deliver_single_event(NULL, &bounce_ev, 0, atomic, hop + 1);
515 client->event_lost++;
524 * rewrite the time-stamp of the event record with the curren time
525 * of the given queue.
526 * return non-zero if updated.
528 static int update_timestamp_of_queue(snd_seq_event_t *event, int queue, int real_time)
535 event->queue = queue;
536 event->flags &= ~SNDRV_SEQ_TIME_STAMP_MASK;
538 event->time.time = snd_seq_timer_get_cur_time(q->timer);
539 event->flags |= SNDRV_SEQ_TIME_STAMP_REAL;
541 event->time.tick = snd_seq_timer_get_cur_tick(q->timer);
542 event->flags |= SNDRV_SEQ_TIME_STAMP_TICK;
550 * expand a quoted event.
552 static int expand_quoted_event(snd_seq_event_t *event)
554 snd_seq_event_t *quoted;
556 quoted = event->data.quote.event;
557 if (quoted == NULL) {
558 snd_printd("seq: quoted event is NULL\n");
562 event->type = quoted->type;
563 event->tag = quoted->tag;
564 event->source = quoted->source;
565 /* don't use quoted destination */
566 event->data = quoted->data;
567 /* use quoted timestamp only if subscription/port didn't update it */
568 if (event->queue == SNDRV_SEQ_QUEUE_DIRECT) {
569 event->flags = quoted->flags;
570 event->queue = quoted->queue;
571 event->time = quoted->time;
573 event->flags = (event->flags & SNDRV_SEQ_TIME_STAMP_MASK)
574 | (quoted->flags & ~SNDRV_SEQ_TIME_STAMP_MASK);
580 * deliver an event to the specified destination.
581 * if filter is non-zero, client filter bitmap is tested.
583 * RETURN VALUE: 0 : if succeeded
586 static int snd_seq_deliver_single_event(client_t *client,
587 snd_seq_event_t *event,
588 int filter, int atomic, int hop)
590 client_t *dest = NULL;
591 client_port_t *dest_port = NULL;
592 int result = -ENOENT;
593 int direct, quoted = 0;
595 direct = snd_seq_ev_is_direct(event);
597 dest = get_event_dest_client(event, filter);
600 dest_port = snd_seq_port_use_ptr(dest, event->dest.port);
601 if (dest_port == NULL)
604 /* check permission */
605 if (! check_port_perm(dest_port, SNDRV_SEQ_PORT_CAP_WRITE)) {
610 if (dest_port->timestamping)
611 update_timestamp_of_queue(event, dest_port->time_queue,
612 dest_port->time_real);
614 if (event->type == SNDRV_SEQ_EVENT_KERNEL_QUOTE) {
616 if (expand_quoted_event(event) < 0) {
617 result = 0; /* do not send bounce error */
622 switch (dest->type) {
624 if (dest->data.user.fifo)
625 result = snd_seq_fifo_event_in(dest->data.user.fifo, event);
629 if (dest_port->event_input == NULL)
631 result = dest_port->event_input(event, direct, dest_port->private_data, atomic, hop);
639 snd_seq_port_unlock(dest_port);
641 snd_seq_client_unlock(dest);
643 if (result < 0 && !direct) {
645 /* return directly to the original source */
646 dest = snd_seq_client_use_ptr(event->source.client);
647 result = bounce_error_event(dest, event, result, atomic, hop);
648 snd_seq_client_unlock(dest);
650 result = bounce_error_event(client, event, result, atomic, hop);
658 * send the event to all subscribers:
660 static int deliver_to_subscribers(client_t *client,
661 snd_seq_event_t *event,
665 int err = 0, num_ev = 0;
666 snd_seq_event_t event_saved;
667 client_port_t *src_port;
669 port_subs_info_t *grp;
671 src_port = snd_seq_port_use_ptr(client, event->source.port);
672 if (src_port == NULL)
673 return -EINVAL; /* invalid source port */
674 /* save original event record */
675 event_saved = *event;
676 grp = &src_port->c_src;
680 read_lock(&grp->list_lock);
682 down_read(&grp->list_mutex);
683 list_for_each(p, &grp->list_head) {
684 subs = list_entry(p, subscribers_t, src_list);
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,
695 /* restore original event record */
696 *event = event_saved;
699 read_unlock(&grp->list_lock);
701 up_read(&grp->list_mutex);
702 *event = event_saved; /* restore */
703 snd_seq_port_unlock(src_port);
704 return (err < 0) ? err : num_ev;
708 #ifdef SUPPORT_BROADCAST
710 * broadcast to all ports:
712 static int port_broadcast_event(client_t *client,
713 snd_seq_event_t *event,
716 int num_ev = 0, err = 0;
717 client_t *dest_client;
720 dest_client = get_event_dest_client(event, SNDRV_SEQ_FILTER_BROADCAST);
721 if (dest_client == NULL)
722 return 0; /* no matching destination */
724 read_lock(&dest_client->ports_lock);
725 list_for_each(p, &dest_client->ports_list_head) {
726 client_port_t *port = list_entry(p, client_port_t, list);
727 event->dest.port = port->addr.port;
728 /* pass NULL as source client to avoid error bounce */
729 err = snd_seq_deliver_single_event(NULL, event,
730 SNDRV_SEQ_FILTER_BROADCAST,
736 read_unlock(&dest_client->ports_lock);
737 snd_seq_client_unlock(dest_client);
738 event->dest.port = SNDRV_SEQ_ADDRESS_BROADCAST; /* restore */
739 return (err < 0) ? err : num_ev;
743 * send the event to all clients:
744 * if destination port is also ADDRESS_BROADCAST, deliver to all ports.
746 static int broadcast_event(client_t *client,
747 snd_seq_event_t *event, int atomic, int hop)
749 int err = 0, num_ev = 0;
753 addr = event->dest; /* save */
755 for (dest = 0; dest < SNDRV_SEQ_MAX_CLIENTS; dest++) {
756 /* don't send to itself */
757 if (dest == client->number)
759 event->dest.client = dest;
760 event->dest.port = addr.port;
761 if (addr.port == SNDRV_SEQ_ADDRESS_BROADCAST)
762 err = port_broadcast_event(client, event, atomic, hop);
764 /* pass NULL as source client to avoid error bounce */
765 err = snd_seq_deliver_single_event(NULL, event,
766 SNDRV_SEQ_FILTER_BROADCAST,
772 event->dest = addr; /* restore */
773 return (err < 0) ? err : num_ev;
777 /* multicast - not supported yet */
778 static int multicast_event(client_t *client, snd_seq_event_t *event,
781 snd_printd("seq: multicast not supported yet.\n");
782 return 0; /* ignored */
784 #endif /* SUPPORT_BROADCAST */
787 /* deliver an event to the destination port(s).
788 * if the event is to subscribers or broadcast, the event is dispatched
789 * to multiple targets.
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_deliver_event(client_t *client, snd_seq_event_t *event,
801 if (hop >= SNDRV_SEQ_MAX_HOPS) {
802 snd_printd("too long delivery path (%d:%d->%d:%d)\n",
803 event->source.client, event->source.port,
804 event->dest.client, event->dest.port);
808 if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS ||
809 event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS)
810 result = deliver_to_subscribers(client, event, atomic, hop);
811 #ifdef SUPPORT_BROADCAST
812 else if (event->queue == SNDRV_SEQ_ADDRESS_BROADCAST ||
813 event->dest.client == SNDRV_SEQ_ADDRESS_BROADCAST)
814 result = broadcast_event(client, event, atomic, hop);
815 else if (event->dest.client >= SNDRV_SEQ_MAX_CLIENTS)
816 result = multicast_event(client, event, atomic, hop);
817 else if (event->dest.port == SNDRV_SEQ_ADDRESS_BROADCAST)
818 result = port_broadcast_event(client, event, atomic, hop);
821 result = snd_seq_deliver_single_event(client, event, 0, atomic, hop);
827 * dispatch an event cell:
828 * This function is called only from queue check routines in timer
829 * interrupts or after enqueued.
830 * The event cell shall be released or re-queued in this function.
832 * RETURN VALUE: n > 0 : the number of delivered events.
833 * n == 0 : the event was not passed to any client.
834 * n < 0 : error - event was not processed.
836 int snd_seq_dispatch_event(snd_seq_event_cell_t *cell, int atomic, int hop)
841 snd_assert(cell != NULL, return -EINVAL);
843 client = snd_seq_client_use_ptr(cell->event.source.client);
844 if (client == NULL) {
845 snd_seq_cell_free(cell); /* release this cell */
849 if (cell->event.type == SNDRV_SEQ_EVENT_NOTE) {
851 * the event cell is re-used as a NOTE-OFF event and
854 snd_seq_event_t tmpev, *ev;
856 /* reserve this event to enqueue note-off later */
858 tmpev.type = SNDRV_SEQ_EVENT_NOTEON;
859 result = snd_seq_deliver_event(client, &tmpev, atomic, hop);
862 * This was originally a note event. We now re-use the
863 * cell for the note-off event.
867 ev->type = SNDRV_SEQ_EVENT_NOTEOFF;
868 ev->flags |= SNDRV_SEQ_PRIORITY_HIGH;
870 /* add the duration time */
871 switch (ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) {
872 case SNDRV_SEQ_TIME_STAMP_TICK:
873 ev->time.tick += ev->data.note.duration;
875 case SNDRV_SEQ_TIME_STAMP_REAL:
876 /* unit for duration is ms */
877 ev->time.time.tv_nsec += 1000000 * (ev->data.note.duration % 1000);
878 ev->time.time.tv_sec += ev->data.note.duration / 1000 +
879 ev->time.time.tv_nsec / 1000000000;
880 ev->time.time.tv_nsec %= 1000000000;
883 ev->data.note.velocity = ev->data.note.off_velocity;
885 /* Now queue this cell as the note off event */
886 if (snd_seq_enqueue_event(cell, atomic, hop) < 0)
887 snd_seq_cell_free(cell); /* release this cell */
891 * event cell is freed after processing the event
894 result = snd_seq_deliver_event(client, &cell->event, atomic, hop);
895 snd_seq_cell_free(cell);
898 snd_seq_client_unlock(client);
903 /* Allocate a cell from client pool and enqueue it to queue:
904 * if pool is empty and blocking is TRUE, sleep until a new cell is
907 static int snd_seq_client_enqueue_event(client_t *client,
908 snd_seq_event_t *event,
909 struct file *file, int blocking,
912 snd_seq_event_cell_t *cell;
915 /* special queue values - force direct passing */
916 if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) {
917 event->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
918 event->queue = SNDRV_SEQ_QUEUE_DIRECT;
920 #ifdef SUPPORT_BROADCAST
921 if (event->queue == SNDRV_SEQ_ADDRESS_BROADCAST) {
922 event->dest.client = SNDRV_SEQ_ADDRESS_BROADCAST;
923 event->queue = SNDRV_SEQ_QUEUE_DIRECT;
926 if (event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) {
927 /* check presence of source port */
928 client_port_t *src_port = snd_seq_port_use_ptr(client, event->source.port);
929 if (src_port == NULL)
931 snd_seq_port_unlock(src_port);
934 /* direct event processing without enqueued */
935 if (snd_seq_ev_is_direct(event)) {
936 if (event->type == SNDRV_SEQ_EVENT_NOTE)
937 return -EINVAL; /* this event must be enqueued! */
938 return snd_seq_deliver_event(client, event, atomic, hop);
941 /* Not direct, normal queuing */
942 if (snd_seq_queue_is_used(event->queue, client->number) <= 0)
943 return -EINVAL; /* invalid queue */
944 if (! snd_seq_write_pool_allocated(client))
945 return -ENXIO; /* queue is not allocated */
947 /* allocate an event cell */
948 err = snd_seq_event_dup(client->pool, event, &cell, !blocking && !atomic, file);
952 /* we got a cell. enqueue it. */
953 if ((err = snd_seq_enqueue_event(cell, atomic, hop)) < 0) {
954 snd_seq_cell_free(cell);
963 * check validity of event type and data length.
964 * return non-zero if invalid.
966 static int check_event_type_and_length(snd_seq_event_t *ev)
968 switch (snd_seq_ev_length_type(ev)) {
969 case SNDRV_SEQ_EVENT_LENGTH_FIXED:
970 if (snd_seq_ev_is_variable_type(ev))
973 case SNDRV_SEQ_EVENT_LENGTH_VARIABLE:
974 if (! snd_seq_ev_is_variable_type(ev) ||
975 (ev->data.ext.len & ~SNDRV_SEQ_EXT_MASK) >= SNDRV_SEQ_MAX_EVENT_LEN)
978 case SNDRV_SEQ_EVENT_LENGTH_VARUSR:
979 if (! snd_seq_ev_is_instr_type(ev) ||
980 ! snd_seq_ev_is_direct(ev))
989 /* possible error values:
990 * -ENXIO invalid client or file open mode
991 * -ENOMEM malloc failed
992 * -EFAULT seg. fault during copy from user space
993 * -EINVAL invalid event
994 * -EAGAIN no space in output pool
995 * -EINTR interrupts while sleep
996 * -EMLINK too many hops
997 * others depends on return value from driver callback
999 static ssize_t snd_seq_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
1001 client_t *client = (client_t *) file->private_data;
1002 int written = 0, len;
1004 snd_seq_event_t event;
1006 if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT))
1009 /* check client structures are in place */
1010 snd_assert(client != NULL, return -ENXIO);
1012 if (!client->accept_output || client->pool == NULL)
1015 /* allocate the pool now if the pool is not allocated yet */
1016 if (client->pool->size > 0 && !snd_seq_write_pool_allocated(client)) {
1017 if (snd_seq_pool_init(client->pool) < 0)
1021 /* only process whole events */
1022 while (count >= sizeof(snd_seq_event_t)) {
1023 /* Read in the event header from the user */
1024 len = sizeof(event);
1025 if (copy_from_user(&event, buf, len)) {
1029 event.source.client = client->number; /* fill in client number */
1030 /* Check for extension data length */
1031 if (check_event_type_and_length(&event)) {
1036 /* check for special events */
1037 if (event.type == SNDRV_SEQ_EVENT_NONE)
1039 else if (snd_seq_ev_is_reserved(&event)) {
1044 if (snd_seq_ev_is_variable(&event)) {
1045 int extlen = event.data.ext.len & ~SNDRV_SEQ_EXT_MASK;
1046 if ((size_t)(extlen + len) > count) {
1047 /* back out, will get an error this time or next */
1051 /* set user space pointer */
1052 event.data.ext.len = extlen | SNDRV_SEQ_EXT_USRPTR;
1053 event.data.ext.ptr = (char*)buf + sizeof(snd_seq_event_t);
1054 len += extlen; /* increment data length */
1056 #if defined(CONFIG_SND_BIT32_EMUL) || defined(CONFIG_SND_BIT32_EMUL_MODULE)
1057 if (client->convert32 && snd_seq_ev_is_varusr(&event)) {
1058 void *ptr = (void*)A(event.data.raw32.d[1]);
1059 event.data.ext.ptr = ptr;
1064 /* ok, enqueue it */
1065 err = snd_seq_client_enqueue_event(client, &event, file,
1066 !(file->f_flags & O_NONBLOCK),
1072 /* Update pointers and counts */
1078 return written ? written : err;
1085 static unsigned int snd_seq_poll(struct file *file, poll_table * wait)
1087 client_t *client = (client_t *) file->private_data;
1088 unsigned int mask = 0;
1090 /* check client structures are in place */
1091 snd_assert(client != NULL, return -ENXIO);
1093 if ((snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT) &&
1094 client->data.user.fifo) {
1096 /* check if data is available in the outqueue */
1097 if (snd_seq_fifo_poll_wait(client->data.user.fifo, file, wait))
1098 mask |= POLLIN | POLLRDNORM;
1101 if (snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT) {
1103 /* check if data is available in the pool */
1104 if (!snd_seq_write_pool_allocated(client) ||
1105 snd_seq_pool_poll_wait(client->pool, file, wait))
1106 mask |= POLLOUT | POLLWRNORM;
1113 /*-----------------------------------------------------*/
1116 /* SYSTEM_INFO ioctl() */
1117 static int snd_seq_ioctl_system_info(client_t *client, void __user *arg)
1119 snd_seq_system_info_t info;
1121 memset(&info, 0, sizeof(info));
1122 /* fill the info fields */
1123 info.queues = SNDRV_SEQ_MAX_QUEUES;
1124 info.clients = SNDRV_SEQ_MAX_CLIENTS;
1125 info.ports = 256; /* fixed limit */
1126 info.channels = 256; /* fixed limit */
1127 info.cur_clients = client_usage.cur;
1128 info.cur_queues = snd_seq_queue_get_cur_queues();
1130 if (copy_to_user(arg, &info, sizeof(info)))
1136 /* RUNNING_MODE ioctl() */
1137 static int snd_seq_ioctl_running_mode(client_t *client, void __user *arg)
1139 struct sndrv_seq_running_info info;
1143 if (copy_from_user(&info, arg, sizeof(info)))
1146 /* requested client number */
1147 cptr = snd_seq_client_use_ptr(info.client);
1149 return -ENOENT; /* don't change !!! */
1151 #ifdef SNDRV_BIG_ENDIAN
1152 if (! info.big_endian) {
1157 if (info.big_endian) {
1163 if (info.cpu_mode > sizeof(long)) {
1167 cptr->convert32 = (info.cpu_mode < sizeof(long));
1169 snd_seq_client_unlock(cptr);
1173 /* CLIENT_INFO ioctl() */
1174 static void get_client_info(client_t *cptr, snd_seq_client_info_t *info)
1176 info->client = cptr->number;
1178 /* fill the info fields */
1179 info->type = cptr->type;
1180 strcpy(info->name, cptr->name);
1181 info->filter = cptr->filter;
1182 info->event_lost = cptr->event_lost;
1183 memcpy(info->event_filter, cptr->event_filter, 32);
1184 info->num_ports = cptr->num_ports;
1185 memset(info->reserved, 0, sizeof(info->reserved));
1188 static int snd_seq_ioctl_get_client_info(client_t * client, void __user *arg)
1191 snd_seq_client_info_t client_info;
1193 if (copy_from_user(&client_info, arg, sizeof(client_info)))
1196 /* requested client number */
1197 cptr = snd_seq_client_use_ptr(client_info.client);
1199 return -ENOENT; /* don't change !!! */
1201 get_client_info(cptr, &client_info);
1202 snd_seq_client_unlock(cptr);
1204 if (copy_to_user(arg, &client_info, sizeof(client_info)))
1210 /* CLIENT_INFO ioctl() */
1211 static int snd_seq_ioctl_set_client_info(client_t * client, void __user *arg)
1213 snd_seq_client_info_t client_info;
1215 if (copy_from_user(&client_info, arg, sizeof(client_info)))
1218 /* it is not allowed to set the info fields for an another client */
1219 if (client->number != client_info.client)
1221 /* also client type must be set now */
1222 if (client->type != client_info.type)
1225 /* fill the info fields */
1226 if (client_info.name[0])
1227 strlcpy(client->name, client_info.name, sizeof(client->name));
1229 client->filter = client_info.filter;
1230 client->event_lost = client_info.event_lost;
1231 memcpy(client->event_filter, client_info.event_filter, 32);
1238 * CREATE PORT ioctl()
1240 static int snd_seq_ioctl_create_port(client_t * client, void __user *arg)
1242 client_port_t *port;
1243 snd_seq_port_info_t info;
1244 snd_seq_port_callback_t *callback;
1246 if (copy_from_user(&info, arg, sizeof(info)))
1249 /* it is not allowed to create the port for an another client */
1250 if (info.addr.client != client->number)
1253 port = snd_seq_create_port(client, (info.flags & SNDRV_SEQ_PORT_FLG_GIVEN_PORT) ? info.addr.port : -1);
1257 if (client->type == USER_CLIENT && info.kernel) {
1258 snd_seq_delete_port(client, port->addr.port);
1261 if (client->type == KERNEL_CLIENT) {
1262 if ((callback = info.kernel) != NULL) {
1263 if (callback->owner)
1264 port->owner = callback->owner;
1265 port->private_data = callback->private_data;
1266 port->private_free = callback->private_free;
1267 port->callback_all = callback->callback_all;
1268 port->event_input = callback->event_input;
1269 port->c_src.open = callback->subscribe;
1270 port->c_src.close = callback->unsubscribe;
1271 port->c_dest.open = callback->use;
1272 port->c_dest.close = callback->unuse;
1276 info.addr = port->addr;
1278 snd_seq_set_port_info(port, &info);
1279 snd_seq_system_client_ev_port_start(port->addr.client, port->addr.port);
1281 if (copy_to_user(arg, &info, sizeof(info)))
1288 * DELETE PORT ioctl()
1290 static int snd_seq_ioctl_delete_port(client_t * client, void __user *arg)
1292 snd_seq_port_info_t info;
1295 /* set passed parameters */
1296 if (copy_from_user(&info, arg, sizeof(info)))
1299 /* it is not allowed to remove the port for an another client */
1300 if (info.addr.client != client->number)
1303 err = snd_seq_delete_port(client, info.addr.port);
1305 snd_seq_system_client_ev_port_exit(client->number, info.addr.port);
1311 * GET_PORT_INFO ioctl() (on any client)
1313 static int snd_seq_ioctl_get_port_info(client_t *client, void __user *arg)
1316 client_port_t *port;
1317 snd_seq_port_info_t info;
1319 if (copy_from_user(&info, arg, sizeof(info)))
1321 cptr = snd_seq_client_use_ptr(info.addr.client);
1325 port = snd_seq_port_use_ptr(cptr, info.addr.port);
1327 snd_seq_client_unlock(cptr);
1328 return -ENOENT; /* don't change */
1332 snd_seq_get_port_info(port, &info);
1333 snd_seq_port_unlock(port);
1334 snd_seq_client_unlock(cptr);
1336 if (copy_to_user(arg, &info, sizeof(info)))
1343 * SET_PORT_INFO ioctl() (only ports on this/own client)
1345 static int snd_seq_ioctl_set_port_info(client_t * client, void __user *arg)
1347 client_port_t *port;
1348 snd_seq_port_info_t info;
1350 if (copy_from_user(&info, arg, sizeof(info)))
1353 if (info.addr.client != client->number) /* only set our own ports ! */
1355 port = snd_seq_port_use_ptr(client, info.addr.port);
1357 snd_seq_set_port_info(port, &info);
1358 snd_seq_port_unlock(port);
1365 * port subscription (connection)
1367 #define PERM_RD (SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ)
1368 #define PERM_WR (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_SUBS_WRITE)
1370 static int check_subscription_permission(client_t *client, client_port_t *sport,
1371 client_port_t *dport,
1372 snd_seq_port_subscribe_t *subs)
1374 if (client->number != subs->sender.client &&
1375 client->number != subs->dest.client) {
1376 /* connection by third client - check export permission */
1377 if (check_port_perm(sport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))
1379 if (check_port_perm(dport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))
1383 /* check read permission */
1384 /* if sender or receiver is the subscribing client itself,
1385 * no permission check is necessary
1387 if (client->number != subs->sender.client) {
1388 if (! check_port_perm(sport, PERM_RD))
1391 /* check write permission */
1392 if (client->number != subs->dest.client) {
1393 if (! check_port_perm(dport, PERM_WR))
1400 * send an subscription notify event to user client:
1401 * client must be user client.
1403 int snd_seq_client_notify_subscription(int client, int port,
1404 snd_seq_port_subscribe_t *info, int evtype)
1406 snd_seq_event_t event;
1408 memset(&event, 0, sizeof(event));
1409 event.type = evtype;
1410 event.data.connect.dest = info->dest;
1411 event.data.connect.sender = info->sender;
1413 return snd_seq_system_notify(client, port, &event); /* non-atomic */
1418 * add to port's subscription list IOCTL interface
1420 static int snd_seq_ioctl_subscribe_port(client_t * client, void __user *arg)
1422 int result = -EINVAL;
1423 client_t *receiver = NULL, *sender = NULL;
1424 client_port_t *sport = NULL, *dport = NULL;
1425 snd_seq_port_subscribe_t subs;
1427 if (copy_from_user(&subs, arg, sizeof(subs)))
1430 if ((receiver = snd_seq_client_use_ptr(subs.dest.client)) == NULL)
1432 if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)
1434 if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)
1436 if ((dport = snd_seq_port_use_ptr(receiver, subs.dest.port)) == NULL)
1439 result = check_subscription_permission(client, sport, dport, &subs);
1444 result = snd_seq_port_connect(client, sender, sport, receiver, dport, &subs);
1445 if (! result) /* broadcast announce */
1446 snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,
1447 &subs, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
1450 snd_seq_port_unlock(sport);
1452 snd_seq_port_unlock(dport);
1454 snd_seq_client_unlock(sender);
1456 snd_seq_client_unlock(receiver);
1462 * remove from port's subscription list
1464 static int snd_seq_ioctl_unsubscribe_port(client_t * client, void __user *arg)
1466 int result = -ENXIO;
1467 client_t *receiver = NULL, *sender = NULL;
1468 client_port_t *sport = NULL, *dport = NULL;
1469 snd_seq_port_subscribe_t subs;
1471 if (copy_from_user(&subs, arg, sizeof(subs)))
1474 if ((receiver = snd_seq_client_use_ptr(subs.dest.client)) == NULL)
1476 if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)
1478 if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)
1480 if ((dport = snd_seq_port_use_ptr(receiver, subs.dest.port)) == NULL)
1483 result = check_subscription_permission(client, sport, dport, &subs);
1487 result = snd_seq_port_disconnect(client, sender, sport, receiver, dport, &subs);
1488 if (! result) /* broadcast announce */
1489 snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,
1490 &subs, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
1493 snd_seq_port_unlock(sport);
1495 snd_seq_port_unlock(dport);
1497 snd_seq_client_unlock(sender);
1499 snd_seq_client_unlock(receiver);
1504 /* CREATE_QUEUE ioctl() */
1505 static int snd_seq_ioctl_create_queue(client_t *client, void __user *arg)
1507 snd_seq_queue_info_t info;
1511 if (copy_from_user(&info, arg, sizeof(info)))
1514 result = snd_seq_queue_alloc(client->number, info.locked, info.flags);
1518 q = queueptr(result);
1522 info.queue = q->queue;
1523 info.locked = q->locked;
1524 info.owner = q->owner;
1526 /* set queue name */
1528 snprintf(info.name, sizeof(info.name), "Queue-%d", q->queue);
1529 strlcpy(q->name, info.name, sizeof(q->name));
1532 if (copy_to_user(arg, &info, sizeof(info)))
1538 /* DELETE_QUEUE ioctl() */
1539 static int snd_seq_ioctl_delete_queue(client_t *client, void __user *arg)
1541 snd_seq_queue_info_t info;
1543 if (copy_from_user(&info, arg, sizeof(info)))
1546 return snd_seq_queue_delete(client->number, info.queue);
1549 /* GET_QUEUE_INFO ioctl() */
1550 static int snd_seq_ioctl_get_queue_info(client_t *client, void __user *arg)
1552 snd_seq_queue_info_t info;
1555 if (copy_from_user(&info, arg, sizeof(info)))
1558 q = queueptr(info.queue);
1562 memset(&info, 0, sizeof(info));
1563 info.queue = q->queue;
1564 info.owner = q->owner;
1565 info.locked = q->locked;
1566 strlcpy(info.name, q->name, sizeof(info.name));
1569 if (copy_to_user(arg, &info, sizeof(info)))
1575 /* SET_QUEUE_INFO ioctl() */
1576 static int snd_seq_ioctl_set_queue_info(client_t *client, void __user *arg)
1578 snd_seq_queue_info_t info;
1581 if (copy_from_user(&info, arg, sizeof(info)))
1584 if (info.owner != client->number)
1587 /* change owner/locked permission */
1588 if (snd_seq_queue_check_access(info.queue, client->number)) {
1589 if (snd_seq_queue_set_owner(info.queue, client->number, info.locked) < 0)
1592 snd_seq_queue_use(info.queue, client->number, 1);
1597 q = queueptr(info.queue);
1600 if (q->owner != client->number) {
1604 strlcpy(q->name, info.name, sizeof(q->name));
1610 /* GET_NAMED_QUEUE ioctl() */
1611 static int snd_seq_ioctl_get_named_queue(client_t *client, void __user *arg)
1613 snd_seq_queue_info_t info;
1616 if (copy_from_user(&info, arg, sizeof(info)))
1619 q = snd_seq_queue_find_name(info.name);
1622 info.queue = q->queue;
1623 info.owner = q->owner;
1624 info.locked = q->locked;
1627 if (copy_to_user(arg, &info, sizeof(info)))
1633 /* GET_QUEUE_STATUS ioctl() */
1634 static int snd_seq_ioctl_get_queue_status(client_t * client, void __user *arg)
1636 snd_seq_queue_status_t status;
1640 if (copy_from_user(&status, arg, sizeof(status)))
1643 queue = queueptr(status.queue);
1646 memset(&status, 0, sizeof(status));
1647 status.queue = queue->queue;
1650 status.events = queue->tickq->cells + queue->timeq->cells;
1652 status.time = snd_seq_timer_get_cur_time(tmr);
1653 status.tick = snd_seq_timer_get_cur_tick(tmr);
1655 status.running = tmr->running;
1657 status.flags = queue->flags;
1660 if (copy_to_user(arg, &status, sizeof(status)))
1666 /* GET_QUEUE_TEMPO ioctl() */
1667 static int snd_seq_ioctl_get_queue_tempo(client_t * client, void __user *arg)
1669 snd_seq_queue_tempo_t tempo;
1673 if (copy_from_user(&tempo, arg, sizeof(tempo)))
1676 queue = queueptr(tempo.queue);
1679 memset(&tempo, 0, sizeof(tempo));
1680 tempo.queue = queue->queue;
1684 tempo.tempo = tmr->tempo;
1685 tempo.ppq = tmr->ppq;
1686 tempo.skew_value = tmr->skew;
1687 tempo.skew_base = tmr->skew_base;
1690 if (copy_to_user(arg, &tempo, sizeof(tempo)))
1696 /* SET_QUEUE_TEMPO ioctl() */
1697 static int snd_seq_ioctl_set_queue_tempo(client_t * client, void __user *arg)
1700 snd_seq_queue_tempo_t tempo;
1702 if (copy_from_user(&tempo, arg, sizeof(tempo)))
1705 if (snd_seq_queue_check_access(tempo.queue, client->number)) {
1706 result = snd_seq_queue_timer_set_tempo(tempo.queue, client->number, &tempo);
1717 /* GET_QUEUE_TIMER ioctl() */
1718 static int snd_seq_ioctl_get_queue_timer(client_t * client, void __user *arg)
1720 snd_seq_queue_timer_t timer;
1724 if (copy_from_user(&timer, arg, sizeof(timer)))
1727 queue = queueptr(timer.queue);
1731 if (down_interruptible(&queue->timer_mutex)) {
1733 return -ERESTARTSYS;
1736 memset(&timer, 0, sizeof(timer));
1737 timer.queue = queue->queue;
1739 timer.type = tmr->type;
1740 if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
1741 timer.u.alsa.id = tmr->alsa_id;
1742 timer.u.alsa.resolution = tmr->preferred_resolution;
1744 up(&queue->timer_mutex);
1747 if (copy_to_user(arg, &timer, sizeof(timer)))
1753 /* SET_QUEUE_TIMER ioctl() */
1754 static int snd_seq_ioctl_set_queue_timer(client_t * client, void __user *arg)
1757 snd_seq_queue_timer_t timer;
1759 if (copy_from_user(&timer, arg, sizeof(timer)))
1762 if (timer.type != SNDRV_SEQ_TIMER_ALSA)
1765 if (snd_seq_queue_check_access(timer.queue, client->number)) {
1769 q = queueptr(timer.queue);
1772 if (down_interruptible(&q->timer_mutex)) {
1774 return -ERESTARTSYS;
1777 snd_seq_queue_timer_close(timer.queue);
1778 tmr->type = timer.type;
1779 if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
1780 tmr->alsa_id = timer.u.alsa.id;
1781 tmr->preferred_resolution = timer.u.alsa.resolution;
1783 result = snd_seq_queue_timer_open(timer.queue);
1784 up(&q->timer_mutex);
1794 /* GET_QUEUE_CLIENT ioctl() */
1795 static int snd_seq_ioctl_get_queue_client(client_t * client, void __user *arg)
1797 snd_seq_queue_client_t info;
1800 if (copy_from_user(&info, arg, sizeof(info)))
1803 used = snd_seq_queue_is_used(info.queue, client->number);
1807 info.client = client->number;
1809 if (copy_to_user(arg, &info, sizeof(info)))
1815 /* SET_QUEUE_CLIENT ioctl() */
1816 static int snd_seq_ioctl_set_queue_client(client_t * client, void __user *arg)
1819 snd_seq_queue_client_t info;
1821 if (copy_from_user(&info, arg, sizeof(info)))
1824 if (info.used >= 0) {
1825 err = snd_seq_queue_use(info.queue, client->number, info.used);
1830 return snd_seq_ioctl_get_queue_client(client, arg);
1834 /* GET_CLIENT_POOL ioctl() */
1835 static int snd_seq_ioctl_get_client_pool(client_t * client, void __user *arg)
1837 snd_seq_client_pool_t info;
1840 if (copy_from_user(&info, arg, sizeof(info)))
1843 cptr = snd_seq_client_use_ptr(info.client);
1846 memset(&info, 0, sizeof(info));
1847 info.output_pool = cptr->pool->size;
1848 info.output_room = cptr->pool->room;
1849 info.output_free = info.output_pool;
1851 info.output_free = snd_seq_unused_cells(cptr->pool);
1852 if (cptr->type == USER_CLIENT) {
1853 info.input_pool = cptr->data.user.fifo_pool_size;
1854 info.input_free = info.input_pool;
1855 if (cptr->data.user.fifo)
1856 info.input_free = snd_seq_unused_cells(cptr->data.user.fifo->pool);
1858 info.input_pool = 0;
1859 info.input_free = 0;
1861 snd_seq_client_unlock(cptr);
1863 if (copy_to_user(arg, &info, sizeof(info)))
1868 /* SET_CLIENT_POOL ioctl() */
1869 static int snd_seq_ioctl_set_client_pool(client_t * client, void __user *arg)
1871 snd_seq_client_pool_t info;
1874 if (copy_from_user(&info, arg, sizeof(info)))
1877 if (client->number != info.client)
1878 return -EINVAL; /* can't change other clients */
1880 if (info.output_pool >= 1 && info.output_pool <= SNDRV_SEQ_MAX_EVENTS &&
1881 (! snd_seq_write_pool_allocated(client) ||
1882 info.output_pool != client->pool->size)) {
1883 if (snd_seq_write_pool_allocated(client)) {
1884 /* remove all existing cells */
1885 snd_seq_queue_client_leave_cells(client->number);
1886 snd_seq_pool_done(client->pool);
1888 client->pool->size = info.output_pool;
1889 rc = snd_seq_pool_init(client->pool);
1893 if (client->type == USER_CLIENT && client->data.user.fifo != NULL &&
1894 info.input_pool >= 1 &&
1895 info.input_pool <= SNDRV_SEQ_MAX_CLIENT_EVENTS &&
1896 info.input_pool != client->data.user.fifo_pool_size) {
1897 /* change pool size */
1898 rc = snd_seq_fifo_resize(client->data.user.fifo, info.input_pool);
1901 client->data.user.fifo_pool_size = info.input_pool;
1903 if (info.output_room >= 1 &&
1904 info.output_room <= client->pool->size) {
1905 client->pool->room = info.output_room;
1908 return snd_seq_ioctl_get_client_pool(client, arg);
1912 /* REMOVE_EVENTS ioctl() */
1913 static int snd_seq_ioctl_remove_events(client_t * client, void __user *arg)
1915 snd_seq_remove_events_t info;
1917 if (copy_from_user(&info, arg, sizeof(info)))
1921 * Input mostly not implemented XXX.
1923 if (info.remove_mode & SNDRV_SEQ_REMOVE_INPUT) {
1925 * No restrictions so for a user client we can clear
1928 if (client->type == USER_CLIENT)
1929 snd_seq_fifo_clear(client->data.user.fifo);
1932 if (info.remove_mode & SNDRV_SEQ_REMOVE_OUTPUT)
1933 snd_seq_queue_remove_cells(client->number, &info);
1940 * get subscription info
1942 static int snd_seq_ioctl_get_subscription(client_t *client, void __user *arg)
1945 client_t *sender = NULL;
1946 client_port_t *sport = NULL;
1947 snd_seq_port_subscribe_t subs;
1950 if (copy_from_user(&subs, arg, sizeof(subs)))
1954 if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)
1956 if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)
1958 p = snd_seq_port_get_subscription(&sport->c_src, &subs.dest);
1967 snd_seq_port_unlock(sport);
1969 snd_seq_client_unlock(sender);
1971 if (copy_to_user(arg, &subs, sizeof(subs)))
1979 * get subscription info - check only its presence
1981 static int snd_seq_ioctl_query_subs(client_t *client, void __user *arg)
1983 int result = -ENXIO;
1984 client_t *cptr = NULL;
1985 client_port_t *port = NULL;
1986 snd_seq_query_subs_t subs;
1987 port_subs_info_t *group;
1988 struct list_head *p;
1991 if (copy_from_user(&subs, arg, sizeof(subs)))
1994 if ((cptr = snd_seq_client_use_ptr(subs.root.client)) == NULL)
1996 if ((port = snd_seq_port_use_ptr(cptr, subs.root.port)) == NULL)
1999 switch (subs.type) {
2000 case SNDRV_SEQ_QUERY_SUBS_READ:
2001 group = &port->c_src;
2003 case SNDRV_SEQ_QUERY_SUBS_WRITE:
2004 group = &port->c_dest;
2010 down_read(&group->list_mutex);
2011 /* search for the subscriber */
2012 subs.num_subs = group->count;
2015 list_for_each(p, &group->list_head) {
2016 if (i++ == subs.index) {
2019 if (subs.type == SNDRV_SEQ_QUERY_SUBS_READ) {
2020 s = list_entry(p, subscribers_t, src_list);
2021 subs.addr = s->info.dest;
2023 s = list_entry(p, subscribers_t, dest_list);
2024 subs.addr = s->info.sender;
2026 subs.flags = s->info.flags;
2027 subs.queue = s->info.queue;
2032 up_read(&group->list_mutex);
2036 snd_seq_port_unlock(port);
2038 snd_seq_client_unlock(cptr);
2040 if (copy_to_user(arg, &subs, sizeof(subs)))
2050 static int snd_seq_ioctl_query_next_client(client_t *client, void __user *arg)
2052 client_t *cptr = NULL;
2053 snd_seq_client_info_t info;
2055 if (copy_from_user(&info, arg, sizeof(info)))
2058 /* search for next client */
2060 if (info.client < 0)
2062 for (; info.client < SNDRV_SEQ_MAX_CLIENTS; info.client++) {
2063 cptr = snd_seq_client_use_ptr(info.client);
2070 get_client_info(cptr, &info);
2071 snd_seq_client_unlock(cptr);
2073 if (copy_to_user(arg, &info, sizeof(info)))
2081 static int snd_seq_ioctl_query_next_port(client_t *client, void __user *arg)
2084 client_port_t *port = NULL;
2085 snd_seq_port_info_t info;
2087 if (copy_from_user(&info, arg, sizeof(info)))
2089 cptr = snd_seq_client_use_ptr(info.addr.client);
2093 /* search for next port */
2095 port = snd_seq_port_query_nearest(cptr, &info);
2097 snd_seq_client_unlock(cptr);
2102 info.addr = port->addr;
2103 snd_seq_get_port_info(port, &info);
2104 snd_seq_port_unlock(port);
2105 snd_seq_client_unlock(cptr);
2107 if (copy_to_user(arg, &info, sizeof(info)))
2112 /* -------------------------------------------------------- */
2114 static struct seq_ioctl_table {
2116 int (*func)(client_t *client, void __user * arg);
2117 } ioctl_tables[] = {
2118 { SNDRV_SEQ_IOCTL_SYSTEM_INFO, snd_seq_ioctl_system_info },
2119 { SNDRV_SEQ_IOCTL_RUNNING_MODE, snd_seq_ioctl_running_mode },
2120 { SNDRV_SEQ_IOCTL_GET_CLIENT_INFO, snd_seq_ioctl_get_client_info },
2121 { SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, snd_seq_ioctl_set_client_info },
2122 { SNDRV_SEQ_IOCTL_CREATE_PORT, snd_seq_ioctl_create_port },
2123 { SNDRV_SEQ_IOCTL_DELETE_PORT, snd_seq_ioctl_delete_port },
2124 { SNDRV_SEQ_IOCTL_GET_PORT_INFO, snd_seq_ioctl_get_port_info },
2125 { SNDRV_SEQ_IOCTL_SET_PORT_INFO, snd_seq_ioctl_set_port_info },
2126 { SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, snd_seq_ioctl_subscribe_port },
2127 { SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, snd_seq_ioctl_unsubscribe_port },
2128 { SNDRV_SEQ_IOCTL_CREATE_QUEUE, snd_seq_ioctl_create_queue },
2129 { SNDRV_SEQ_IOCTL_DELETE_QUEUE, snd_seq_ioctl_delete_queue },
2130 { SNDRV_SEQ_IOCTL_GET_QUEUE_INFO, snd_seq_ioctl_get_queue_info },
2131 { SNDRV_SEQ_IOCTL_SET_QUEUE_INFO, snd_seq_ioctl_set_queue_info },
2132 { SNDRV_SEQ_IOCTL_GET_NAMED_QUEUE, snd_seq_ioctl_get_named_queue },
2133 { SNDRV_SEQ_IOCTL_GET_QUEUE_STATUS, snd_seq_ioctl_get_queue_status },
2134 { SNDRV_SEQ_IOCTL_GET_QUEUE_TEMPO, snd_seq_ioctl_get_queue_tempo },
2135 { SNDRV_SEQ_IOCTL_SET_QUEUE_TEMPO, snd_seq_ioctl_set_queue_tempo },
2136 { SNDRV_SEQ_IOCTL_GET_QUEUE_TIMER, snd_seq_ioctl_get_queue_timer },
2137 { SNDRV_SEQ_IOCTL_SET_QUEUE_TIMER, snd_seq_ioctl_set_queue_timer },
2138 { SNDRV_SEQ_IOCTL_GET_QUEUE_CLIENT, snd_seq_ioctl_get_queue_client },
2139 { SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT, snd_seq_ioctl_set_queue_client },
2140 { SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, snd_seq_ioctl_get_client_pool },
2141 { SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, snd_seq_ioctl_set_client_pool },
2142 { SNDRV_SEQ_IOCTL_GET_SUBSCRIPTION, snd_seq_ioctl_get_subscription },
2143 { SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT, snd_seq_ioctl_query_next_client },
2144 { SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, snd_seq_ioctl_query_next_port },
2145 { SNDRV_SEQ_IOCTL_REMOVE_EVENTS, snd_seq_ioctl_remove_events },
2146 { SNDRV_SEQ_IOCTL_QUERY_SUBS, snd_seq_ioctl_query_subs },
2150 static int snd_seq_do_ioctl(client_t *client, unsigned int cmd, void __user *arg)
2152 struct seq_ioctl_table *p;
2155 case SNDRV_SEQ_IOCTL_PVERSION:
2156 /* return sequencer version number */
2157 return put_user(SNDRV_SEQ_VERSION, (int __user *)arg) ? -EFAULT : 0;
2158 case SNDRV_SEQ_IOCTL_CLIENT_ID:
2159 /* return the id of this client */
2160 return put_user(client->number, (int __user *)arg) ? -EFAULT : 0;
2165 for (p = ioctl_tables; p->cmd; p++) {
2167 return p->func(client, arg);
2169 snd_printd("seq unknown ioctl() 0x%x (type='%c', number=0x%2x)\n",
2170 cmd, _IOC_TYPE(cmd), _IOC_NR(cmd));
2175 static int snd_seq_ioctl(struct inode *inode, struct file *file,
2176 unsigned int cmd, unsigned long arg)
2178 client_t *client = (client_t *) file->private_data;
2180 snd_assert(client != NULL, return -ENXIO);
2182 return snd_seq_do_ioctl(client, cmd, (void __user *) arg);
2186 /* -------------------------------------------------------- */
2189 /* exported to kernel modules */
2190 int snd_seq_create_kernel_client(snd_card_t *card, int client_index, snd_seq_client_callback_t * callback)
2194 snd_assert(! in_interrupt(), return -EBUSY);
2196 if (callback == NULL)
2198 if (card && client_index > 7)
2200 if (card == NULL && client_index > 63)
2203 client_index += 64 + (card->number << 3);
2205 if (down_interruptible(®ister_mutex))
2206 return -ERESTARTSYS;
2207 /* empty write queue as default */
2208 client = seq_create_client1(client_index, 0);
2209 if (client == NULL) {
2210 up(®ister_mutex);
2211 return -EBUSY; /* failure code */
2213 usage_alloc(&client_usage, 1);
2215 client->accept_input = callback->allow_output;
2216 client->accept_output = callback->allow_input;
2218 /* fill client data */
2219 client->data.kernel.card = card;
2220 client->data.kernel.private_data = callback->private_data;
2221 sprintf(client->name, "Client-%d", client->number);
2223 client->type = KERNEL_CLIENT;
2224 up(®ister_mutex);
2226 /* make others aware this new client */
2227 snd_seq_system_client_ev_client_start(client->number);
2229 /* return client number to caller */
2230 return client->number;
2233 /* exported to kernel modules */
2234 int snd_seq_delete_kernel_client(int client)
2238 snd_assert(! in_interrupt(), return -EBUSY);
2240 ptr = clientptr(client);
2244 seq_free_client(ptr);
2250 /* skeleton to enqueue event, called from snd_seq_kernel_client_enqueue
2251 * and snd_seq_kernel_client_enqueue_blocking
2253 static int kernel_client_enqueue(int client, snd_seq_event_t *ev,
2254 struct file *file, int blocking,
2255 int atomic, int hop)
2260 snd_assert(ev != NULL, return -EINVAL);
2262 if (ev->type == SNDRV_SEQ_EVENT_NONE)
2263 return 0; /* ignore this */
2264 if (ev->type == SNDRV_SEQ_EVENT_KERNEL_ERROR ||
2265 ev->type == SNDRV_SEQ_EVENT_KERNEL_QUOTE)
2266 return -EINVAL; /* quoted events can't be enqueued */
2268 /* fill in client number */
2269 ev->source.client = client;
2271 if (check_event_type_and_length(ev))
2274 cptr = snd_seq_client_use_ptr(client);
2278 if (! cptr->accept_output)
2281 result = snd_seq_client_enqueue_event(cptr, ev, file, blocking, atomic, hop);
2283 snd_seq_client_unlock(cptr);
2288 * exported, called by kernel clients to enqueue events (w/o blocking)
2290 * RETURN VALUE: zero if succeed, negative if error
2292 int snd_seq_kernel_client_enqueue(int client, snd_seq_event_t * ev,
2293 int atomic, int hop)
2295 return kernel_client_enqueue(client, ev, NULL, 0, atomic, hop);
2299 * exported, called by kernel clients to enqueue events (with blocking)
2301 * RETURN VALUE: zero if succeed, negative if error
2303 int snd_seq_kernel_client_enqueue_blocking(int client, snd_seq_event_t * ev,
2305 int atomic, int hop)
2307 return kernel_client_enqueue(client, ev, file, 1, atomic, hop);
2312 * exported, called by kernel clients to dispatch events directly to other
2313 * clients, bypassing the queues. Event time-stamp will be updated.
2315 * RETURN VALUE: negative = delivery failed,
2316 * zero, or positive: the number of delivered events
2318 int snd_seq_kernel_client_dispatch(int client, snd_seq_event_t * ev,
2319 int atomic, int hop)
2324 snd_assert(ev != NULL, return -EINVAL);
2326 /* fill in client number */
2327 ev->queue = SNDRV_SEQ_QUEUE_DIRECT;
2328 ev->source.client = client;
2330 if (check_event_type_and_length(ev))
2333 cptr = snd_seq_client_use_ptr(client);
2337 if (!cptr->accept_output)
2340 result = snd_seq_deliver_event(cptr, ev, atomic, hop);
2342 snd_seq_client_unlock(cptr);
2348 * exported, called by kernel clients to perform same functions as with
2351 int snd_seq_kernel_client_ctl(int clientid, unsigned int cmd, void *arg)
2357 client = clientptr(clientid);
2360 fs = snd_enter_user();
2361 result = snd_seq_do_ioctl(client, cmd, arg);
2367 /* exported (for OSS emulator) */
2368 int snd_seq_kernel_client_write_poll(int clientid, struct file *file, poll_table *wait)
2372 client = clientptr(clientid);
2376 if (! snd_seq_write_pool_allocated(client))
2378 if (snd_seq_pool_poll_wait(client->pool, file, wait))
2383 /*---------------------------------------------------------------------------*/
2388 static void snd_seq_info_dump_subscribers(snd_info_buffer_t *buffer, port_subs_info_t *group, int is_src, char *msg)
2390 struct list_head *p;
2394 down_read(&group->list_mutex);
2395 if (list_empty(&group->list_head)) {
2396 up_read(&group->list_mutex);
2399 snd_iprintf(buffer, msg);
2400 list_for_each(p, &group->list_head) {
2402 s = list_entry(p, subscribers_t, src_list);
2404 s = list_entry(p, subscribers_t, dest_list);
2406 snd_iprintf(buffer, ", ");
2407 snd_iprintf(buffer, "%d:%d",
2408 is_src ? s->info.dest.client : s->info.sender.client,
2409 is_src ? s->info.dest.port : s->info.sender.port);
2410 if (s->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)
2411 snd_iprintf(buffer, "[%c:%d]", ((s->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL) ? 'r' : 't'), s->info.queue);
2412 if (group->exclusive)
2413 snd_iprintf(buffer, "[ex]");
2415 up_read(&group->list_mutex);
2416 snd_iprintf(buffer, "\n");
2419 #define FLAG_PERM_RD(perm) ((perm) & SNDRV_SEQ_PORT_CAP_READ ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_READ ? 'R' : 'r') : '-')
2420 #define FLAG_PERM_WR(perm) ((perm) & SNDRV_SEQ_PORT_CAP_WRITE ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_WRITE ? 'W' : 'w') : '-')
2421 #define FLAG_PERM_EX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_NO_EXPORT ? '-' : 'e')
2423 #define FLAG_PERM_DUPLEX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_DUPLEX ? 'X' : '-')
2425 static void snd_seq_info_dump_ports(snd_info_buffer_t *buffer, client_t *client)
2427 struct list_head *l;
2429 down(&client->ports_mutex);
2430 list_for_each(l, &client->ports_list_head) {
2431 client_port_t *p = list_entry(l, client_port_t, list);
2432 snd_iprintf(buffer, " Port %3d : \"%s\" (%c%c%c%c)\n",
2433 p->addr.port, p->name,
2434 FLAG_PERM_RD(p->capability),
2435 FLAG_PERM_WR(p->capability),
2436 FLAG_PERM_EX(p->capability),
2437 FLAG_PERM_DUPLEX(p->capability));
2438 snd_seq_info_dump_subscribers(buffer, &p->c_src, 1, " Connecting To: ");
2439 snd_seq_info_dump_subscribers(buffer, &p->c_dest, 0, " Connected From: ");
2441 up(&client->ports_mutex);
2445 /* exported to seq_info.c */
2446 void snd_seq_info_clients_read(snd_info_entry_t *entry,
2447 snd_info_buffer_t * buffer)
2449 extern void snd_seq_info_pool(snd_info_buffer_t * buffer, pool_t * pool, char *space);
2453 snd_iprintf(buffer, "Client info\n");
2454 snd_iprintf(buffer, " cur clients : %d\n", client_usage.cur);
2455 snd_iprintf(buffer, " peak clients : %d\n", client_usage.peak);
2456 snd_iprintf(buffer, " max clients : %d\n", SNDRV_SEQ_MAX_CLIENTS);
2457 snd_iprintf(buffer, "\n");
2459 /* list the client table */
2460 for (c = 0; c < SNDRV_SEQ_MAX_CLIENTS; c++) {
2461 client = snd_seq_client_use_ptr(c);
2464 if (client->type == NO_CLIENT) {
2465 snd_seq_client_unlock(client);
2469 snd_iprintf(buffer, "Client %3d : \"%s\" [%s]\n",
2471 client->type == USER_CLIENT ? "User" : "Kernel");
2472 snd_seq_info_dump_ports(buffer, client);
2473 if (snd_seq_write_pool_allocated(client)) {
2474 snd_iprintf(buffer, " Output pool :\n");
2475 snd_seq_info_pool(buffer, client->pool, " ");
2477 if (client->type == USER_CLIENT && client->data.user.fifo &&
2478 client->data.user.fifo->pool) {
2479 snd_iprintf(buffer, " Input pool :\n");
2480 snd_seq_info_pool(buffer, client->data.user.fifo->pool, " ");
2482 snd_seq_client_unlock(client);
2487 /*---------------------------------------------------------------------------*/
2494 static struct file_operations snd_seq_f_ops =
2496 .owner = THIS_MODULE,
2497 .read = snd_seq_read,
2498 .write = snd_seq_write,
2499 .open = snd_seq_open,
2500 .release = snd_seq_release,
2501 .poll = snd_seq_poll,
2502 .ioctl = snd_seq_ioctl,
2505 static snd_minor_t snd_seq_reg =
2507 .comment = "sequencer",
2508 .f_ops = &snd_seq_f_ops,
2513 * register sequencer device
2515 int __init snd_sequencer_device_init(void)
2519 if (down_interruptible(®ister_mutex))
2520 return -ERESTARTSYS;
2522 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0, &snd_seq_reg, "seq")) < 0) {
2523 up(®ister_mutex);
2527 up(®ister_mutex);
2535 * unregister sequencer device
2537 void __exit snd_sequencer_device_done(void)
2539 snd_unregister_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0);