x86: mmconf: fix section mismatch warning
[linux-2.6/linux-loongson.git] / sound / core / seq / seq_clientmgr.c
blob7a1545d2d9533f4ad53b6e1693969e34cb067965
1 /*
2 * ALSA sequencer Client Manager
3 * Copyright (c) 1998-2001 by Frank van de Pol <fvdpol@coil.demon.nl>
4 * Jaroslav Kysela <perex@perex.cz>
5 * Takashi Iwai <tiwai@suse.de>
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 <linux/init.h>
25 #include <linux/slab.h>
26 #include <sound/core.h>
27 #include <sound/minors.h>
28 #include <linux/kmod.h>
30 #include <sound/seq_kernel.h>
31 #include "seq_clientmgr.h"
32 #include "seq_memory.h"
33 #include "seq_queue.h"
34 #include "seq_timer.h"
35 #include "seq_info.h"
36 #include "seq_system.h"
37 #include <sound/seq_device.h>
38 #ifdef CONFIG_COMPAT
39 #include <linux/compat.h>
40 #endif
42 /* Client Manager
44 * this module handles the connections of userland and kernel clients
49 * There are four ranges of client numbers (last two shared):
50 * 0..15: global clients
51 * 16..127: statically allocated client numbers for cards 0..27
52 * 128..191: dynamically allocated client numbers for cards 28..31
53 * 128..191: dynamically allocated client numbers for applications
56 /* number of kernel non-card clients */
57 #define SNDRV_SEQ_GLOBAL_CLIENTS 16
58 /* clients per cards, for static clients */
59 #define SNDRV_SEQ_CLIENTS_PER_CARD 4
60 /* dynamically allocated client numbers (both kernel drivers and user space) */
61 #define SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN 128
63 #define SNDRV_SEQ_LFLG_INPUT 0x0001
64 #define SNDRV_SEQ_LFLG_OUTPUT 0x0002
65 #define SNDRV_SEQ_LFLG_OPEN (SNDRV_SEQ_LFLG_INPUT|SNDRV_SEQ_LFLG_OUTPUT)
67 static DEFINE_SPINLOCK(clients_lock);
68 static DEFINE_MUTEX(register_mutex);
71 * client table
73 static char clienttablock[SNDRV_SEQ_MAX_CLIENTS];
74 static struct snd_seq_client *clienttab[SNDRV_SEQ_MAX_CLIENTS];
75 static struct snd_seq_usage client_usage;
78 * prototypes
80 static int bounce_error_event(struct snd_seq_client *client,
81 struct snd_seq_event *event,
82 int err, int atomic, int hop);
83 static int snd_seq_deliver_single_event(struct snd_seq_client *client,
84 struct snd_seq_event *event,
85 int filter, int atomic, int hop);
90 static inline mm_segment_t snd_enter_user(void)
92 mm_segment_t fs = get_fs();
93 set_fs(get_ds());
94 return fs;
97 static inline void snd_leave_user(mm_segment_t fs)
99 set_fs(fs);
104 static inline unsigned short snd_seq_file_flags(struct file *file)
106 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
107 case FMODE_WRITE:
108 return SNDRV_SEQ_LFLG_OUTPUT;
109 case FMODE_READ:
110 return SNDRV_SEQ_LFLG_INPUT;
111 default:
112 return SNDRV_SEQ_LFLG_OPEN;
116 static inline int snd_seq_write_pool_allocated(struct snd_seq_client *client)
118 return snd_seq_total_cells(client->pool) > 0;
121 /* return pointer to client structure for specified id */
122 static struct snd_seq_client *clientptr(int clientid)
124 if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) {
125 snd_printd("Seq: oops. Trying to get pointer to client %d\n",
126 clientid);
127 return NULL;
129 return clienttab[clientid];
132 struct snd_seq_client *snd_seq_client_use_ptr(int clientid)
134 unsigned long flags;
135 struct snd_seq_client *client;
137 if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) {
138 snd_printd("Seq: oops. Trying to get pointer to client %d\n",
139 clientid);
140 return NULL;
142 spin_lock_irqsave(&clients_lock, flags);
143 client = clientptr(clientid);
144 if (client)
145 goto __lock;
146 if (clienttablock[clientid]) {
147 spin_unlock_irqrestore(&clients_lock, flags);
148 return NULL;
150 spin_unlock_irqrestore(&clients_lock, flags);
151 #ifdef CONFIG_MODULES
152 if (!in_interrupt()) {
153 static char client_requested[SNDRV_SEQ_GLOBAL_CLIENTS];
154 static char card_requested[SNDRV_CARDS];
155 if (clientid < SNDRV_SEQ_GLOBAL_CLIENTS) {
156 int idx;
158 if (!client_requested[clientid]) {
159 client_requested[clientid] = 1;
160 for (idx = 0; idx < 15; idx++) {
161 if (seq_client_load[idx] < 0)
162 break;
163 if (seq_client_load[idx] == clientid) {
164 request_module("snd-seq-client-%i",
165 clientid);
166 break;
170 } else if (clientid < SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN) {
171 int card = (clientid - SNDRV_SEQ_GLOBAL_CLIENTS) /
172 SNDRV_SEQ_CLIENTS_PER_CARD;
173 if (card < snd_ecards_limit) {
174 if (! card_requested[card]) {
175 card_requested[card] = 1;
176 snd_request_card(card);
178 snd_seq_device_load_drivers();
181 spin_lock_irqsave(&clients_lock, flags);
182 client = clientptr(clientid);
183 if (client)
184 goto __lock;
185 spin_unlock_irqrestore(&clients_lock, flags);
187 #endif
188 return NULL;
190 __lock:
191 snd_use_lock_use(&client->use_lock);
192 spin_unlock_irqrestore(&clients_lock, flags);
193 return client;
196 static void usage_alloc(struct snd_seq_usage *res, int num)
198 res->cur += num;
199 if (res->cur > res->peak)
200 res->peak = res->cur;
203 static void usage_free(struct snd_seq_usage *res, int num)
205 res->cur -= num;
208 /* initialise data structures */
209 int __init client_init_data(void)
211 /* zap out the client table */
212 memset(&clienttablock, 0, sizeof(clienttablock));
213 memset(&clienttab, 0, sizeof(clienttab));
214 return 0;
218 static struct snd_seq_client *seq_create_client1(int client_index, int poolsize)
220 unsigned long flags;
221 int c;
222 struct snd_seq_client *client;
224 /* init client data */
225 client = kzalloc(sizeof(*client), GFP_KERNEL);
226 if (client == NULL)
227 return NULL;
228 client->pool = snd_seq_pool_new(poolsize);
229 if (client->pool == NULL) {
230 kfree(client);
231 return NULL;
233 client->type = NO_CLIENT;
234 snd_use_lock_init(&client->use_lock);
235 rwlock_init(&client->ports_lock);
236 mutex_init(&client->ports_mutex);
237 INIT_LIST_HEAD(&client->ports_list_head);
239 /* find free slot in the client table */
240 spin_lock_irqsave(&clients_lock, flags);
241 if (client_index < 0) {
242 for (c = SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN;
243 c < SNDRV_SEQ_MAX_CLIENTS;
244 c++) {
245 if (clienttab[c] || clienttablock[c])
246 continue;
247 clienttab[client->number = c] = client;
248 spin_unlock_irqrestore(&clients_lock, flags);
249 return client;
251 } else {
252 if (clienttab[client_index] == NULL && !clienttablock[client_index]) {
253 clienttab[client->number = client_index] = client;
254 spin_unlock_irqrestore(&clients_lock, flags);
255 return client;
258 spin_unlock_irqrestore(&clients_lock, flags);
259 snd_seq_pool_delete(&client->pool);
260 kfree(client);
261 return NULL; /* no free slot found or busy, return failure code */
265 static int seq_free_client1(struct snd_seq_client *client)
267 unsigned long flags;
269 snd_assert(client != NULL, return -EINVAL);
270 snd_seq_delete_all_ports(client);
271 snd_seq_queue_client_leave(client->number);
272 spin_lock_irqsave(&clients_lock, flags);
273 clienttablock[client->number] = 1;
274 clienttab[client->number] = NULL;
275 spin_unlock_irqrestore(&clients_lock, flags);
276 snd_use_lock_sync(&client->use_lock);
277 snd_seq_queue_client_termination(client->number);
278 if (client->pool)
279 snd_seq_pool_delete(&client->pool);
280 spin_lock_irqsave(&clients_lock, flags);
281 clienttablock[client->number] = 0;
282 spin_unlock_irqrestore(&clients_lock, flags);
283 return 0;
287 static void seq_free_client(struct snd_seq_client * client)
289 mutex_lock(&register_mutex);
290 switch (client->type) {
291 case NO_CLIENT:
292 snd_printk(KERN_WARNING "Seq: Trying to free unused client %d\n",
293 client->number);
294 break;
295 case USER_CLIENT:
296 case KERNEL_CLIENT:
297 seq_free_client1(client);
298 usage_free(&client_usage, 1);
299 break;
301 default:
302 snd_printk(KERN_ERR "Seq: Trying to free client %d with undefined type = %d\n",
303 client->number, client->type);
305 mutex_unlock(&register_mutex);
307 snd_seq_system_client_ev_client_exit(client->number);
312 /* -------------------------------------------------------- */
314 /* create a user client */
315 static int snd_seq_open(struct inode *inode, struct file *file)
317 int c, mode; /* client id */
318 struct snd_seq_client *client;
319 struct snd_seq_user_client *user;
321 if (mutex_lock_interruptible(&register_mutex))
322 return -ERESTARTSYS;
323 client = seq_create_client1(-1, SNDRV_SEQ_DEFAULT_EVENTS);
324 if (client == NULL) {
325 mutex_unlock(&register_mutex);
326 return -ENOMEM; /* failure code */
329 mode = snd_seq_file_flags(file);
330 if (mode & SNDRV_SEQ_LFLG_INPUT)
331 client->accept_input = 1;
332 if (mode & SNDRV_SEQ_LFLG_OUTPUT)
333 client->accept_output = 1;
335 user = &client->data.user;
336 user->fifo = NULL;
337 user->fifo_pool_size = 0;
339 if (mode & SNDRV_SEQ_LFLG_INPUT) {
340 user->fifo_pool_size = SNDRV_SEQ_DEFAULT_CLIENT_EVENTS;
341 user->fifo = snd_seq_fifo_new(user->fifo_pool_size);
342 if (user->fifo == NULL) {
343 seq_free_client1(client);
344 kfree(client);
345 mutex_unlock(&register_mutex);
346 return -ENOMEM;
350 usage_alloc(&client_usage, 1);
351 client->type = USER_CLIENT;
352 mutex_unlock(&register_mutex);
354 c = client->number;
355 file->private_data = client;
357 /* fill client data */
358 user->file = file;
359 sprintf(client->name, "Client-%d", c);
361 /* make others aware this new client */
362 snd_seq_system_client_ev_client_start(c);
364 return 0;
367 /* delete a user client */
368 static int snd_seq_release(struct inode *inode, struct file *file)
370 struct snd_seq_client *client = file->private_data;
372 if (client) {
373 seq_free_client(client);
374 if (client->data.user.fifo)
375 snd_seq_fifo_delete(&client->data.user.fifo);
376 kfree(client);
379 return 0;
383 /* handle client read() */
384 /* possible error values:
385 * -ENXIO invalid client or file open mode
386 * -ENOSPC FIFO overflow (the flag is cleared after this error report)
387 * -EINVAL no enough user-space buffer to write the whole event
388 * -EFAULT seg. fault during copy to user space
390 static ssize_t snd_seq_read(struct file *file, char __user *buf, size_t count,
391 loff_t *offset)
393 struct snd_seq_client *client = file->private_data;
394 struct snd_seq_fifo *fifo;
395 int err;
396 long result = 0;
397 struct snd_seq_event_cell *cell;
399 if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT))
400 return -ENXIO;
402 if (!access_ok(VERIFY_WRITE, buf, count))
403 return -EFAULT;
405 /* check client structures are in place */
406 snd_assert(client != NULL, return -ENXIO);
408 if (!client->accept_input || (fifo = client->data.user.fifo) == NULL)
409 return -ENXIO;
411 if (atomic_read(&fifo->overflow) > 0) {
412 /* buffer overflow is detected */
413 snd_seq_fifo_clear(fifo);
414 /* return error code */
415 return -ENOSPC;
418 cell = NULL;
419 err = 0;
420 snd_seq_fifo_lock(fifo);
422 /* while data available in queue */
423 while (count >= sizeof(struct snd_seq_event)) {
424 int nonblock;
426 nonblock = (file->f_flags & O_NONBLOCK) || result > 0;
427 if ((err = snd_seq_fifo_cell_out(fifo, &cell, nonblock)) < 0) {
428 break;
430 if (snd_seq_ev_is_variable(&cell->event)) {
431 struct snd_seq_event tmpev;
432 tmpev = cell->event;
433 tmpev.data.ext.len &= ~SNDRV_SEQ_EXT_MASK;
434 if (copy_to_user(buf, &tmpev, sizeof(struct snd_seq_event))) {
435 err = -EFAULT;
436 break;
438 count -= sizeof(struct snd_seq_event);
439 buf += sizeof(struct snd_seq_event);
440 err = snd_seq_expand_var_event(&cell->event, count,
441 (char __force *)buf, 0,
442 sizeof(struct snd_seq_event));
443 if (err < 0)
444 break;
445 result += err;
446 count -= err;
447 buf += err;
448 } else {
449 if (copy_to_user(buf, &cell->event, sizeof(struct snd_seq_event))) {
450 err = -EFAULT;
451 break;
453 count -= sizeof(struct snd_seq_event);
454 buf += sizeof(struct snd_seq_event);
456 snd_seq_cell_free(cell);
457 cell = NULL; /* to be sure */
458 result += sizeof(struct snd_seq_event);
461 if (err < 0) {
462 if (cell)
463 snd_seq_fifo_cell_putback(fifo, cell);
464 if (err == -EAGAIN && result > 0)
465 err = 0;
467 snd_seq_fifo_unlock(fifo);
469 return (err < 0) ? err : result;
474 * check access permission to the port
476 static int check_port_perm(struct snd_seq_client_port *port, unsigned int flags)
478 if ((port->capability & flags) != flags)
479 return 0;
480 return flags;
484 * check if the destination client is available, and return the pointer
485 * if filter is non-zero, client filter bitmap is tested.
487 static struct snd_seq_client *get_event_dest_client(struct snd_seq_event *event,
488 int filter)
490 struct snd_seq_client *dest;
492 dest = snd_seq_client_use_ptr(event->dest.client);
493 if (dest == NULL)
494 return NULL;
495 if (! dest->accept_input)
496 goto __not_avail;
497 if ((dest->filter & SNDRV_SEQ_FILTER_USE_EVENT) &&
498 ! test_bit(event->type, dest->event_filter))
499 goto __not_avail;
500 if (filter && !(dest->filter & filter))
501 goto __not_avail;
503 return dest; /* ok - accessible */
504 __not_avail:
505 snd_seq_client_unlock(dest);
506 return NULL;
511 * Return the error event.
513 * If the receiver client is a user client, the original event is
514 * encapsulated in SNDRV_SEQ_EVENT_BOUNCE as variable length event. If
515 * the original event is also variable length, the external data is
516 * copied after the event record.
517 * If the receiver client is a kernel client, the original event is
518 * quoted in SNDRV_SEQ_EVENT_KERNEL_ERROR, since this requires no extra
519 * kmalloc.
521 static int bounce_error_event(struct snd_seq_client *client,
522 struct snd_seq_event *event,
523 int err, int atomic, int hop)
525 struct snd_seq_event bounce_ev;
526 int result;
528 if (client == NULL ||
529 ! (client->filter & SNDRV_SEQ_FILTER_BOUNCE) ||
530 ! client->accept_input)
531 return 0; /* ignored */
533 /* set up quoted error */
534 memset(&bounce_ev, 0, sizeof(bounce_ev));
535 bounce_ev.type = SNDRV_SEQ_EVENT_KERNEL_ERROR;
536 bounce_ev.flags = SNDRV_SEQ_EVENT_LENGTH_FIXED;
537 bounce_ev.queue = SNDRV_SEQ_QUEUE_DIRECT;
538 bounce_ev.source.client = SNDRV_SEQ_CLIENT_SYSTEM;
539 bounce_ev.source.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
540 bounce_ev.dest.client = client->number;
541 bounce_ev.dest.port = event->source.port;
542 bounce_ev.data.quote.origin = event->dest;
543 bounce_ev.data.quote.event = event;
544 bounce_ev.data.quote.value = -err; /* use positive value */
545 result = snd_seq_deliver_single_event(NULL, &bounce_ev, 0, atomic, hop + 1);
546 if (result < 0) {
547 client->event_lost++;
548 return result;
551 return result;
556 * rewrite the time-stamp of the event record with the curren time
557 * of the given queue.
558 * return non-zero if updated.
560 static int update_timestamp_of_queue(struct snd_seq_event *event,
561 int queue, int real_time)
563 struct snd_seq_queue *q;
565 q = queueptr(queue);
566 if (! q)
567 return 0;
568 event->queue = queue;
569 event->flags &= ~SNDRV_SEQ_TIME_STAMP_MASK;
570 if (real_time) {
571 event->time.time = snd_seq_timer_get_cur_time(q->timer);
572 event->flags |= SNDRV_SEQ_TIME_STAMP_REAL;
573 } else {
574 event->time.tick = snd_seq_timer_get_cur_tick(q->timer);
575 event->flags |= SNDRV_SEQ_TIME_STAMP_TICK;
577 queuefree(q);
578 return 1;
583 * deliver an event to the specified destination.
584 * if filter is non-zero, client filter bitmap is tested.
586 * RETURN VALUE: 0 : if succeeded
587 * <0 : error
589 static int snd_seq_deliver_single_event(struct snd_seq_client *client,
590 struct snd_seq_event *event,
591 int filter, int atomic, int hop)
593 struct snd_seq_client *dest = NULL;
594 struct snd_seq_client_port *dest_port = NULL;
595 int result = -ENOENT;
596 int direct;
598 direct = snd_seq_ev_is_direct(event);
600 dest = get_event_dest_client(event, filter);
601 if (dest == NULL)
602 goto __skip;
603 dest_port = snd_seq_port_use_ptr(dest, event->dest.port);
604 if (dest_port == NULL)
605 goto __skip;
607 /* check permission */
608 if (! check_port_perm(dest_port, SNDRV_SEQ_PORT_CAP_WRITE)) {
609 result = -EPERM;
610 goto __skip;
613 if (dest_port->timestamping)
614 update_timestamp_of_queue(event, dest_port->time_queue,
615 dest_port->time_real);
617 switch (dest->type) {
618 case USER_CLIENT:
619 if (dest->data.user.fifo)
620 result = snd_seq_fifo_event_in(dest->data.user.fifo, event);
621 break;
623 case KERNEL_CLIENT:
624 if (dest_port->event_input == NULL)
625 break;
626 result = dest_port->event_input(event, direct,
627 dest_port->private_data,
628 atomic, hop);
629 break;
630 default:
631 break;
634 __skip:
635 if (dest_port)
636 snd_seq_port_unlock(dest_port);
637 if (dest)
638 snd_seq_client_unlock(dest);
640 if (result < 0 && !direct) {
641 result = bounce_error_event(client, event, result, atomic, hop);
643 return result;
648 * send the event to all subscribers:
650 static int deliver_to_subscribers(struct snd_seq_client *client,
651 struct snd_seq_event *event,
652 int atomic, int hop)
654 struct snd_seq_subscribers *subs;
655 int err = 0, num_ev = 0;
656 struct snd_seq_event event_saved;
657 struct snd_seq_client_port *src_port;
658 struct snd_seq_port_subs_info *grp;
660 src_port = snd_seq_port_use_ptr(client, event->source.port);
661 if (src_port == NULL)
662 return -EINVAL; /* invalid source port */
663 /* save original event record */
664 event_saved = *event;
665 grp = &src_port->c_src;
667 /* lock list */
668 if (atomic)
669 read_lock(&grp->list_lock);
670 else
671 down_read(&grp->list_mutex);
672 list_for_each_entry(subs, &grp->list_head, src_list) {
673 event->dest = subs->info.dest;
674 if (subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)
675 /* convert time according to flag with subscription */
676 update_timestamp_of_queue(event, subs->info.queue,
677 subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL);
678 err = snd_seq_deliver_single_event(client, event,
679 0, atomic, hop);
680 if (err < 0)
681 break;
682 num_ev++;
683 /* restore original event record */
684 *event = event_saved;
686 if (atomic)
687 read_unlock(&grp->list_lock);
688 else
689 up_read(&grp->list_mutex);
690 *event = event_saved; /* restore */
691 snd_seq_port_unlock(src_port);
692 return (err < 0) ? err : num_ev;
696 #ifdef SUPPORT_BROADCAST
698 * broadcast to all ports:
700 static int port_broadcast_event(struct snd_seq_client *client,
701 struct snd_seq_event *event,
702 int atomic, int hop)
704 int num_ev = 0, err = 0;
705 struct snd_seq_client *dest_client;
706 struct snd_seq_client_port *port;
708 dest_client = get_event_dest_client(event, SNDRV_SEQ_FILTER_BROADCAST);
709 if (dest_client == NULL)
710 return 0; /* no matching destination */
712 read_lock(&dest_client->ports_lock);
713 list_for_each_entry(port, &dest_client->ports_list_head, list) {
714 event->dest.port = port->addr.port;
715 /* pass NULL as source client to avoid error bounce */
716 err = snd_seq_deliver_single_event(NULL, event,
717 SNDRV_SEQ_FILTER_BROADCAST,
718 atomic, hop);
719 if (err < 0)
720 break;
721 num_ev++;
723 read_unlock(&dest_client->ports_lock);
724 snd_seq_client_unlock(dest_client);
725 event->dest.port = SNDRV_SEQ_ADDRESS_BROADCAST; /* restore */
726 return (err < 0) ? err : num_ev;
730 * send the event to all clients:
731 * if destination port is also ADDRESS_BROADCAST, deliver to all ports.
733 static int broadcast_event(struct snd_seq_client *client,
734 struct snd_seq_event *event, int atomic, int hop)
736 int err = 0, num_ev = 0;
737 int dest;
738 struct snd_seq_addr addr;
740 addr = event->dest; /* save */
742 for (dest = 0; dest < SNDRV_SEQ_MAX_CLIENTS; dest++) {
743 /* don't send to itself */
744 if (dest == client->number)
745 continue;
746 event->dest.client = dest;
747 event->dest.port = addr.port;
748 if (addr.port == SNDRV_SEQ_ADDRESS_BROADCAST)
749 err = port_broadcast_event(client, event, atomic, hop);
750 else
751 /* pass NULL as source client to avoid error bounce */
752 err = snd_seq_deliver_single_event(NULL, event,
753 SNDRV_SEQ_FILTER_BROADCAST,
754 atomic, hop);
755 if (err < 0)
756 break;
757 num_ev += err;
759 event->dest = addr; /* restore */
760 return (err < 0) ? err : num_ev;
764 /* multicast - not supported yet */
765 static int multicast_event(struct snd_seq_client *client, struct snd_seq_event *event,
766 int atomic, int hop)
768 snd_printd("seq: multicast not supported yet.\n");
769 return 0; /* ignored */
771 #endif /* SUPPORT_BROADCAST */
774 /* deliver an event to the destination port(s).
775 * if the event is to subscribers or broadcast, the event is dispatched
776 * to multiple targets.
778 * RETURN VALUE: n > 0 : the number of delivered events.
779 * n == 0 : the event was not passed to any client.
780 * n < 0 : error - event was not processed.
782 static int snd_seq_deliver_event(struct snd_seq_client *client, struct snd_seq_event *event,
783 int atomic, int hop)
785 int result;
787 hop++;
788 if (hop >= SNDRV_SEQ_MAX_HOPS) {
789 snd_printd("too long delivery path (%d:%d->%d:%d)\n",
790 event->source.client, event->source.port,
791 event->dest.client, event->dest.port);
792 return -EMLINK;
795 if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS ||
796 event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS)
797 result = deliver_to_subscribers(client, event, atomic, hop);
798 #ifdef SUPPORT_BROADCAST
799 else if (event->queue == SNDRV_SEQ_ADDRESS_BROADCAST ||
800 event->dest.client == SNDRV_SEQ_ADDRESS_BROADCAST)
801 result = broadcast_event(client, event, atomic, hop);
802 else if (event->dest.client >= SNDRV_SEQ_MAX_CLIENTS)
803 result = multicast_event(client, event, atomic, hop);
804 else if (event->dest.port == SNDRV_SEQ_ADDRESS_BROADCAST)
805 result = port_broadcast_event(client, event, atomic, hop);
806 #endif
807 else
808 result = snd_seq_deliver_single_event(client, event, 0, atomic, hop);
810 return result;
814 * dispatch an event cell:
815 * This function is called only from queue check routines in timer
816 * interrupts or after enqueued.
817 * The event cell shall be released or re-queued in this function.
819 * RETURN VALUE: n > 0 : the number of delivered events.
820 * n == 0 : the event was not passed to any client.
821 * n < 0 : error - event was not processed.
823 int snd_seq_dispatch_event(struct snd_seq_event_cell *cell, int atomic, int hop)
825 struct snd_seq_client *client;
826 int result;
828 snd_assert(cell != NULL, return -EINVAL);
830 client = snd_seq_client_use_ptr(cell->event.source.client);
831 if (client == NULL) {
832 snd_seq_cell_free(cell); /* release this cell */
833 return -EINVAL;
836 if (cell->event.type == SNDRV_SEQ_EVENT_NOTE) {
837 /* NOTE event:
838 * the event cell is re-used as a NOTE-OFF event and
839 * enqueued again.
841 struct snd_seq_event tmpev, *ev;
843 /* reserve this event to enqueue note-off later */
844 tmpev = cell->event;
845 tmpev.type = SNDRV_SEQ_EVENT_NOTEON;
846 result = snd_seq_deliver_event(client, &tmpev, atomic, hop);
849 * This was originally a note event. We now re-use the
850 * cell for the note-off event.
853 ev = &cell->event;
854 ev->type = SNDRV_SEQ_EVENT_NOTEOFF;
855 ev->flags |= SNDRV_SEQ_PRIORITY_HIGH;
857 /* add the duration time */
858 switch (ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) {
859 case SNDRV_SEQ_TIME_STAMP_TICK:
860 ev->time.tick += ev->data.note.duration;
861 break;
862 case SNDRV_SEQ_TIME_STAMP_REAL:
863 /* unit for duration is ms */
864 ev->time.time.tv_nsec += 1000000 * (ev->data.note.duration % 1000);
865 ev->time.time.tv_sec += ev->data.note.duration / 1000 +
866 ev->time.time.tv_nsec / 1000000000;
867 ev->time.time.tv_nsec %= 1000000000;
868 break;
870 ev->data.note.velocity = ev->data.note.off_velocity;
872 /* Now queue this cell as the note off event */
873 if (snd_seq_enqueue_event(cell, atomic, hop) < 0)
874 snd_seq_cell_free(cell); /* release this cell */
876 } else {
877 /* Normal events:
878 * event cell is freed after processing the event
881 result = snd_seq_deliver_event(client, &cell->event, atomic, hop);
882 snd_seq_cell_free(cell);
885 snd_seq_client_unlock(client);
886 return result;
890 /* Allocate a cell from client pool and enqueue it to queue:
891 * if pool is empty and blocking is TRUE, sleep until a new cell is
892 * available.
894 static int snd_seq_client_enqueue_event(struct snd_seq_client *client,
895 struct snd_seq_event *event,
896 struct file *file, int blocking,
897 int atomic, int hop)
899 struct snd_seq_event_cell *cell;
900 int err;
902 /* special queue values - force direct passing */
903 if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) {
904 event->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
905 event->queue = SNDRV_SEQ_QUEUE_DIRECT;
906 } else
907 #ifdef SUPPORT_BROADCAST
908 if (event->queue == SNDRV_SEQ_ADDRESS_BROADCAST) {
909 event->dest.client = SNDRV_SEQ_ADDRESS_BROADCAST;
910 event->queue = SNDRV_SEQ_QUEUE_DIRECT;
912 #endif
913 if (event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) {
914 /* check presence of source port */
915 struct snd_seq_client_port *src_port = snd_seq_port_use_ptr(client, event->source.port);
916 if (src_port == NULL)
917 return -EINVAL;
918 snd_seq_port_unlock(src_port);
921 /* direct event processing without enqueued */
922 if (snd_seq_ev_is_direct(event)) {
923 if (event->type == SNDRV_SEQ_EVENT_NOTE)
924 return -EINVAL; /* this event must be enqueued! */
925 return snd_seq_deliver_event(client, event, atomic, hop);
928 /* Not direct, normal queuing */
929 if (snd_seq_queue_is_used(event->queue, client->number) <= 0)
930 return -EINVAL; /* invalid queue */
931 if (! snd_seq_write_pool_allocated(client))
932 return -ENXIO; /* queue is not allocated */
934 /* allocate an event cell */
935 err = snd_seq_event_dup(client->pool, event, &cell, !blocking || atomic, file);
936 if (err < 0)
937 return err;
939 /* we got a cell. enqueue it. */
940 if ((err = snd_seq_enqueue_event(cell, atomic, hop)) < 0) {
941 snd_seq_cell_free(cell);
942 return err;
945 return 0;
950 * check validity of event type and data length.
951 * return non-zero if invalid.
953 static int check_event_type_and_length(struct snd_seq_event *ev)
955 switch (snd_seq_ev_length_type(ev)) {
956 case SNDRV_SEQ_EVENT_LENGTH_FIXED:
957 if (snd_seq_ev_is_variable_type(ev))
958 return -EINVAL;
959 break;
960 case SNDRV_SEQ_EVENT_LENGTH_VARIABLE:
961 if (! snd_seq_ev_is_variable_type(ev) ||
962 (ev->data.ext.len & ~SNDRV_SEQ_EXT_MASK) >= SNDRV_SEQ_MAX_EVENT_LEN)
963 return -EINVAL;
964 break;
965 case SNDRV_SEQ_EVENT_LENGTH_VARUSR:
966 if (! snd_seq_ev_is_direct(ev))
967 return -EINVAL;
968 break;
970 return 0;
974 /* handle write() */
975 /* possible error values:
976 * -ENXIO invalid client or file open mode
977 * -ENOMEM malloc failed
978 * -EFAULT seg. fault during copy from user space
979 * -EINVAL invalid event
980 * -EAGAIN no space in output pool
981 * -EINTR interrupts while sleep
982 * -EMLINK too many hops
983 * others depends on return value from driver callback
985 static ssize_t snd_seq_write(struct file *file, const char __user *buf,
986 size_t count, loff_t *offset)
988 struct snd_seq_client *client = file->private_data;
989 int written = 0, len;
990 int err = -EINVAL;
991 struct snd_seq_event event;
993 if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT))
994 return -ENXIO;
996 /* check client structures are in place */
997 snd_assert(client != NULL, return -ENXIO);
999 if (!client->accept_output || client->pool == NULL)
1000 return -ENXIO;
1002 /* allocate the pool now if the pool is not allocated yet */
1003 if (client->pool->size > 0 && !snd_seq_write_pool_allocated(client)) {
1004 if (snd_seq_pool_init(client->pool) < 0)
1005 return -ENOMEM;
1008 /* only process whole events */
1009 while (count >= sizeof(struct snd_seq_event)) {
1010 /* Read in the event header from the user */
1011 len = sizeof(event);
1012 if (copy_from_user(&event, buf, len)) {
1013 err = -EFAULT;
1014 break;
1016 event.source.client = client->number; /* fill in client number */
1017 /* Check for extension data length */
1018 if (check_event_type_and_length(&event)) {
1019 err = -EINVAL;
1020 break;
1023 /* check for special events */
1024 if (event.type == SNDRV_SEQ_EVENT_NONE)
1025 goto __skip_event;
1026 else if (snd_seq_ev_is_reserved(&event)) {
1027 err = -EINVAL;
1028 break;
1031 if (snd_seq_ev_is_variable(&event)) {
1032 int extlen = event.data.ext.len & ~SNDRV_SEQ_EXT_MASK;
1033 if ((size_t)(extlen + len) > count) {
1034 /* back out, will get an error this time or next */
1035 err = -EINVAL;
1036 break;
1038 /* set user space pointer */
1039 event.data.ext.len = extlen | SNDRV_SEQ_EXT_USRPTR;
1040 event.data.ext.ptr = (char __force *)buf
1041 + sizeof(struct snd_seq_event);
1042 len += extlen; /* increment data length */
1043 } else {
1044 #ifdef CONFIG_COMPAT
1045 if (client->convert32 && snd_seq_ev_is_varusr(&event)) {
1046 void *ptr = compat_ptr(event.data.raw32.d[1]);
1047 event.data.ext.ptr = ptr;
1049 #endif
1052 /* ok, enqueue it */
1053 err = snd_seq_client_enqueue_event(client, &event, file,
1054 !(file->f_flags & O_NONBLOCK),
1055 0, 0);
1056 if (err < 0)
1057 break;
1059 __skip_event:
1060 /* Update pointers and counts */
1061 count -= len;
1062 buf += len;
1063 written += len;
1066 return written ? written : err;
1071 * handle polling
1073 static unsigned int snd_seq_poll(struct file *file, poll_table * wait)
1075 struct snd_seq_client *client = file->private_data;
1076 unsigned int mask = 0;
1078 /* check client structures are in place */
1079 snd_assert(client != NULL, return -ENXIO);
1081 if ((snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT) &&
1082 client->data.user.fifo) {
1084 /* check if data is available in the outqueue */
1085 if (snd_seq_fifo_poll_wait(client->data.user.fifo, file, wait))
1086 mask |= POLLIN | POLLRDNORM;
1089 if (snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT) {
1091 /* check if data is available in the pool */
1092 if (!snd_seq_write_pool_allocated(client) ||
1093 snd_seq_pool_poll_wait(client->pool, file, wait))
1094 mask |= POLLOUT | POLLWRNORM;
1097 return mask;
1101 /*-----------------------------------------------------*/
1104 /* SYSTEM_INFO ioctl() */
1105 static int snd_seq_ioctl_system_info(struct snd_seq_client *client, void __user *arg)
1107 struct snd_seq_system_info info;
1109 memset(&info, 0, sizeof(info));
1110 /* fill the info fields */
1111 info.queues = SNDRV_SEQ_MAX_QUEUES;
1112 info.clients = SNDRV_SEQ_MAX_CLIENTS;
1113 info.ports = 256; /* fixed limit */
1114 info.channels = 256; /* fixed limit */
1115 info.cur_clients = client_usage.cur;
1116 info.cur_queues = snd_seq_queue_get_cur_queues();
1118 if (copy_to_user(arg, &info, sizeof(info)))
1119 return -EFAULT;
1120 return 0;
1124 /* RUNNING_MODE ioctl() */
1125 static int snd_seq_ioctl_running_mode(struct snd_seq_client *client, void __user *arg)
1127 struct snd_seq_running_info info;
1128 struct snd_seq_client *cptr;
1129 int err = 0;
1131 if (copy_from_user(&info, arg, sizeof(info)))
1132 return -EFAULT;
1134 /* requested client number */
1135 cptr = snd_seq_client_use_ptr(info.client);
1136 if (cptr == NULL)
1137 return -ENOENT; /* don't change !!! */
1139 #ifdef SNDRV_BIG_ENDIAN
1140 if (! info.big_endian) {
1141 err = -EINVAL;
1142 goto __err;
1144 #else
1145 if (info.big_endian) {
1146 err = -EINVAL;
1147 goto __err;
1150 #endif
1151 if (info.cpu_mode > sizeof(long)) {
1152 err = -EINVAL;
1153 goto __err;
1155 cptr->convert32 = (info.cpu_mode < sizeof(long));
1156 __err:
1157 snd_seq_client_unlock(cptr);
1158 return err;
1161 /* CLIENT_INFO ioctl() */
1162 static void get_client_info(struct snd_seq_client *cptr,
1163 struct snd_seq_client_info *info)
1165 info->client = cptr->number;
1167 /* fill the info fields */
1168 info->type = cptr->type;
1169 strcpy(info->name, cptr->name);
1170 info->filter = cptr->filter;
1171 info->event_lost = cptr->event_lost;
1172 memcpy(info->event_filter, cptr->event_filter, 32);
1173 info->num_ports = cptr->num_ports;
1174 memset(info->reserved, 0, sizeof(info->reserved));
1177 static int snd_seq_ioctl_get_client_info(struct snd_seq_client *client,
1178 void __user *arg)
1180 struct snd_seq_client *cptr;
1181 struct snd_seq_client_info client_info;
1183 if (copy_from_user(&client_info, arg, sizeof(client_info)))
1184 return -EFAULT;
1186 /* requested client number */
1187 cptr = snd_seq_client_use_ptr(client_info.client);
1188 if (cptr == NULL)
1189 return -ENOENT; /* don't change !!! */
1191 get_client_info(cptr, &client_info);
1192 snd_seq_client_unlock(cptr);
1194 if (copy_to_user(arg, &client_info, sizeof(client_info)))
1195 return -EFAULT;
1196 return 0;
1200 /* CLIENT_INFO ioctl() */
1201 static int snd_seq_ioctl_set_client_info(struct snd_seq_client *client,
1202 void __user *arg)
1204 struct snd_seq_client_info client_info;
1206 if (copy_from_user(&client_info, arg, sizeof(client_info)))
1207 return -EFAULT;
1209 /* it is not allowed to set the info fields for an another client */
1210 if (client->number != client_info.client)
1211 return -EPERM;
1212 /* also client type must be set now */
1213 if (client->type != client_info.type)
1214 return -EINVAL;
1216 /* fill the info fields */
1217 if (client_info.name[0])
1218 strlcpy(client->name, client_info.name, sizeof(client->name));
1220 client->filter = client_info.filter;
1221 client->event_lost = client_info.event_lost;
1222 memcpy(client->event_filter, client_info.event_filter, 32);
1224 return 0;
1229 * CREATE PORT ioctl()
1231 static int snd_seq_ioctl_create_port(struct snd_seq_client *client,
1232 void __user *arg)
1234 struct snd_seq_client_port *port;
1235 struct snd_seq_port_info info;
1236 struct snd_seq_port_callback *callback;
1238 if (copy_from_user(&info, arg, sizeof(info)))
1239 return -EFAULT;
1241 /* it is not allowed to create the port for an another client */
1242 if (info.addr.client != client->number)
1243 return -EPERM;
1245 port = snd_seq_create_port(client, (info.flags & SNDRV_SEQ_PORT_FLG_GIVEN_PORT) ? info.addr.port : -1);
1246 if (port == NULL)
1247 return -ENOMEM;
1249 if (client->type == USER_CLIENT && info.kernel) {
1250 snd_seq_delete_port(client, port->addr.port);
1251 return -EINVAL;
1253 if (client->type == KERNEL_CLIENT) {
1254 if ((callback = info.kernel) != NULL) {
1255 if (callback->owner)
1256 port->owner = callback->owner;
1257 port->private_data = callback->private_data;
1258 port->private_free = callback->private_free;
1259 port->callback_all = callback->callback_all;
1260 port->event_input = callback->event_input;
1261 port->c_src.open = callback->subscribe;
1262 port->c_src.close = callback->unsubscribe;
1263 port->c_dest.open = callback->use;
1264 port->c_dest.close = callback->unuse;
1268 info.addr = port->addr;
1270 snd_seq_set_port_info(port, &info);
1271 snd_seq_system_client_ev_port_start(port->addr.client, port->addr.port);
1273 if (copy_to_user(arg, &info, sizeof(info)))
1274 return -EFAULT;
1276 return 0;
1280 * DELETE PORT ioctl()
1282 static int snd_seq_ioctl_delete_port(struct snd_seq_client *client,
1283 void __user *arg)
1285 struct snd_seq_port_info info;
1286 int err;
1288 /* set passed parameters */
1289 if (copy_from_user(&info, arg, sizeof(info)))
1290 return -EFAULT;
1292 /* it is not allowed to remove the port for an another client */
1293 if (info.addr.client != client->number)
1294 return -EPERM;
1296 err = snd_seq_delete_port(client, info.addr.port);
1297 if (err >= 0)
1298 snd_seq_system_client_ev_port_exit(client->number, info.addr.port);
1299 return err;
1304 * GET_PORT_INFO ioctl() (on any client)
1306 static int snd_seq_ioctl_get_port_info(struct snd_seq_client *client,
1307 void __user *arg)
1309 struct snd_seq_client *cptr;
1310 struct snd_seq_client_port *port;
1311 struct snd_seq_port_info info;
1313 if (copy_from_user(&info, arg, sizeof(info)))
1314 return -EFAULT;
1315 cptr = snd_seq_client_use_ptr(info.addr.client);
1316 if (cptr == NULL)
1317 return -ENXIO;
1319 port = snd_seq_port_use_ptr(cptr, info.addr.port);
1320 if (port == NULL) {
1321 snd_seq_client_unlock(cptr);
1322 return -ENOENT; /* don't change */
1325 /* get port info */
1326 snd_seq_get_port_info(port, &info);
1327 snd_seq_port_unlock(port);
1328 snd_seq_client_unlock(cptr);
1330 if (copy_to_user(arg, &info, sizeof(info)))
1331 return -EFAULT;
1332 return 0;
1337 * SET_PORT_INFO ioctl() (only ports on this/own client)
1339 static int snd_seq_ioctl_set_port_info(struct snd_seq_client *client,
1340 void __user *arg)
1342 struct snd_seq_client_port *port;
1343 struct snd_seq_port_info info;
1345 if (copy_from_user(&info, arg, sizeof(info)))
1346 return -EFAULT;
1348 if (info.addr.client != client->number) /* only set our own ports ! */
1349 return -EPERM;
1350 port = snd_seq_port_use_ptr(client, info.addr.port);
1351 if (port) {
1352 snd_seq_set_port_info(port, &info);
1353 snd_seq_port_unlock(port);
1355 return 0;
1360 * port subscription (connection)
1362 #define PERM_RD (SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ)
1363 #define PERM_WR (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_SUBS_WRITE)
1365 static int check_subscription_permission(struct snd_seq_client *client,
1366 struct snd_seq_client_port *sport,
1367 struct snd_seq_client_port *dport,
1368 struct snd_seq_port_subscribe *subs)
1370 if (client->number != subs->sender.client &&
1371 client->number != subs->dest.client) {
1372 /* connection by third client - check export permission */
1373 if (check_port_perm(sport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))
1374 return -EPERM;
1375 if (check_port_perm(dport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))
1376 return -EPERM;
1379 /* check read permission */
1380 /* if sender or receiver is the subscribing client itself,
1381 * no permission check is necessary
1383 if (client->number != subs->sender.client) {
1384 if (! check_port_perm(sport, PERM_RD))
1385 return -EPERM;
1387 /* check write permission */
1388 if (client->number != subs->dest.client) {
1389 if (! check_port_perm(dport, PERM_WR))
1390 return -EPERM;
1392 return 0;
1396 * send an subscription notify event to user client:
1397 * client must be user client.
1399 int snd_seq_client_notify_subscription(int client, int port,
1400 struct snd_seq_port_subscribe *info,
1401 int evtype)
1403 struct snd_seq_event event;
1405 memset(&event, 0, sizeof(event));
1406 event.type = evtype;
1407 event.data.connect.dest = info->dest;
1408 event.data.connect.sender = info->sender;
1410 return snd_seq_system_notify(client, port, &event); /* non-atomic */
1415 * add to port's subscription list IOCTL interface
1417 static int snd_seq_ioctl_subscribe_port(struct snd_seq_client *client,
1418 void __user *arg)
1420 int result = -EINVAL;
1421 struct snd_seq_client *receiver = NULL, *sender = NULL;
1422 struct snd_seq_client_port *sport = NULL, *dport = NULL;
1423 struct snd_seq_port_subscribe subs;
1425 if (copy_from_user(&subs, arg, sizeof(subs)))
1426 return -EFAULT;
1428 if ((receiver = snd_seq_client_use_ptr(subs.dest.client)) == NULL)
1429 goto __end;
1430 if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)
1431 goto __end;
1432 if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)
1433 goto __end;
1434 if ((dport = snd_seq_port_use_ptr(receiver, subs.dest.port)) == NULL)
1435 goto __end;
1437 result = check_subscription_permission(client, sport, dport, &subs);
1438 if (result < 0)
1439 goto __end;
1441 /* connect them */
1442 result = snd_seq_port_connect(client, sender, sport, receiver, dport, &subs);
1443 if (! result) /* broadcast announce */
1444 snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,
1445 &subs, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
1446 __end:
1447 if (sport)
1448 snd_seq_port_unlock(sport);
1449 if (dport)
1450 snd_seq_port_unlock(dport);
1451 if (sender)
1452 snd_seq_client_unlock(sender);
1453 if (receiver)
1454 snd_seq_client_unlock(receiver);
1455 return result;
1460 * remove from port's subscription list
1462 static int snd_seq_ioctl_unsubscribe_port(struct snd_seq_client *client,
1463 void __user *arg)
1465 int result = -ENXIO;
1466 struct snd_seq_client *receiver = NULL, *sender = NULL;
1467 struct snd_seq_client_port *sport = NULL, *dport = NULL;
1468 struct snd_seq_port_subscribe subs;
1470 if (copy_from_user(&subs, arg, sizeof(subs)))
1471 return -EFAULT;
1473 if ((receiver = snd_seq_client_use_ptr(subs.dest.client)) == NULL)
1474 goto __end;
1475 if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)
1476 goto __end;
1477 if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)
1478 goto __end;
1479 if ((dport = snd_seq_port_use_ptr(receiver, subs.dest.port)) == NULL)
1480 goto __end;
1482 result = check_subscription_permission(client, sport, dport, &subs);
1483 if (result < 0)
1484 goto __end;
1486 result = snd_seq_port_disconnect(client, sender, sport, receiver, dport, &subs);
1487 if (! result) /* broadcast announce */
1488 snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,
1489 &subs, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
1490 __end:
1491 if (sport)
1492 snd_seq_port_unlock(sport);
1493 if (dport)
1494 snd_seq_port_unlock(dport);
1495 if (sender)
1496 snd_seq_client_unlock(sender);
1497 if (receiver)
1498 snd_seq_client_unlock(receiver);
1499 return result;
1503 /* CREATE_QUEUE ioctl() */
1504 static int snd_seq_ioctl_create_queue(struct snd_seq_client *client,
1505 void __user *arg)
1507 struct snd_seq_queue_info info;
1508 int result;
1509 struct snd_seq_queue *q;
1511 if (copy_from_user(&info, arg, sizeof(info)))
1512 return -EFAULT;
1514 result = snd_seq_queue_alloc(client->number, info.locked, info.flags);
1515 if (result < 0)
1516 return result;
1518 q = queueptr(result);
1519 if (q == NULL)
1520 return -EINVAL;
1522 info.queue = q->queue;
1523 info.locked = q->locked;
1524 info.owner = q->owner;
1526 /* set queue name */
1527 if (! info.name[0])
1528 snprintf(info.name, sizeof(info.name), "Queue-%d", q->queue);
1529 strlcpy(q->name, info.name, sizeof(q->name));
1530 queuefree(q);
1532 if (copy_to_user(arg, &info, sizeof(info)))
1533 return -EFAULT;
1535 return 0;
1538 /* DELETE_QUEUE ioctl() */
1539 static int snd_seq_ioctl_delete_queue(struct snd_seq_client *client,
1540 void __user *arg)
1542 struct snd_seq_queue_info info;
1544 if (copy_from_user(&info, arg, sizeof(info)))
1545 return -EFAULT;
1547 return snd_seq_queue_delete(client->number, info.queue);
1550 /* GET_QUEUE_INFO ioctl() */
1551 static int snd_seq_ioctl_get_queue_info(struct snd_seq_client *client,
1552 void __user *arg)
1554 struct snd_seq_queue_info info;
1555 struct snd_seq_queue *q;
1557 if (copy_from_user(&info, arg, sizeof(info)))
1558 return -EFAULT;
1560 q = queueptr(info.queue);
1561 if (q == NULL)
1562 return -EINVAL;
1564 memset(&info, 0, sizeof(info));
1565 info.queue = q->queue;
1566 info.owner = q->owner;
1567 info.locked = q->locked;
1568 strlcpy(info.name, q->name, sizeof(info.name));
1569 queuefree(q);
1571 if (copy_to_user(arg, &info, sizeof(info)))
1572 return -EFAULT;
1574 return 0;
1577 /* SET_QUEUE_INFO ioctl() */
1578 static int snd_seq_ioctl_set_queue_info(struct snd_seq_client *client,
1579 void __user *arg)
1581 struct snd_seq_queue_info info;
1582 struct snd_seq_queue *q;
1584 if (copy_from_user(&info, arg, sizeof(info)))
1585 return -EFAULT;
1587 if (info.owner != client->number)
1588 return -EINVAL;
1590 /* change owner/locked permission */
1591 if (snd_seq_queue_check_access(info.queue, client->number)) {
1592 if (snd_seq_queue_set_owner(info.queue, client->number, info.locked) < 0)
1593 return -EPERM;
1594 if (info.locked)
1595 snd_seq_queue_use(info.queue, client->number, 1);
1596 } else {
1597 return -EPERM;
1600 q = queueptr(info.queue);
1601 if (! q)
1602 return -EINVAL;
1603 if (q->owner != client->number) {
1604 queuefree(q);
1605 return -EPERM;
1607 strlcpy(q->name, info.name, sizeof(q->name));
1608 queuefree(q);
1610 return 0;
1613 /* GET_NAMED_QUEUE ioctl() */
1614 static int snd_seq_ioctl_get_named_queue(struct snd_seq_client *client, void __user *arg)
1616 struct snd_seq_queue_info info;
1617 struct snd_seq_queue *q;
1619 if (copy_from_user(&info, arg, sizeof(info)))
1620 return -EFAULT;
1622 q = snd_seq_queue_find_name(info.name);
1623 if (q == NULL)
1624 return -EINVAL;
1625 info.queue = q->queue;
1626 info.owner = q->owner;
1627 info.locked = q->locked;
1628 queuefree(q);
1630 if (copy_to_user(arg, &info, sizeof(info)))
1631 return -EFAULT;
1633 return 0;
1636 /* GET_QUEUE_STATUS ioctl() */
1637 static int snd_seq_ioctl_get_queue_status(struct snd_seq_client *client,
1638 void __user *arg)
1640 struct snd_seq_queue_status status;
1641 struct snd_seq_queue *queue;
1642 struct snd_seq_timer *tmr;
1644 if (copy_from_user(&status, arg, sizeof(status)))
1645 return -EFAULT;
1647 queue = queueptr(status.queue);
1648 if (queue == NULL)
1649 return -EINVAL;
1650 memset(&status, 0, sizeof(status));
1651 status.queue = queue->queue;
1653 tmr = queue->timer;
1654 status.events = queue->tickq->cells + queue->timeq->cells;
1656 status.time = snd_seq_timer_get_cur_time(tmr);
1657 status.tick = snd_seq_timer_get_cur_tick(tmr);
1659 status.running = tmr->running;
1661 status.flags = queue->flags;
1662 queuefree(queue);
1664 if (copy_to_user(arg, &status, sizeof(status)))
1665 return -EFAULT;
1666 return 0;
1670 /* GET_QUEUE_TEMPO ioctl() */
1671 static int snd_seq_ioctl_get_queue_tempo(struct snd_seq_client *client,
1672 void __user *arg)
1674 struct snd_seq_queue_tempo tempo;
1675 struct snd_seq_queue *queue;
1676 struct snd_seq_timer *tmr;
1678 if (copy_from_user(&tempo, arg, sizeof(tempo)))
1679 return -EFAULT;
1681 queue = queueptr(tempo.queue);
1682 if (queue == NULL)
1683 return -EINVAL;
1684 memset(&tempo, 0, sizeof(tempo));
1685 tempo.queue = queue->queue;
1687 tmr = queue->timer;
1689 tempo.tempo = tmr->tempo;
1690 tempo.ppq = tmr->ppq;
1691 tempo.skew_value = tmr->skew;
1692 tempo.skew_base = tmr->skew_base;
1693 queuefree(queue);
1695 if (copy_to_user(arg, &tempo, sizeof(tempo)))
1696 return -EFAULT;
1697 return 0;
1701 /* SET_QUEUE_TEMPO ioctl() */
1702 int snd_seq_set_queue_tempo(int client, struct snd_seq_queue_tempo *tempo)
1704 if (!snd_seq_queue_check_access(tempo->queue, client))
1705 return -EPERM;
1706 return snd_seq_queue_timer_set_tempo(tempo->queue, client, tempo);
1709 EXPORT_SYMBOL(snd_seq_set_queue_tempo);
1711 static int snd_seq_ioctl_set_queue_tempo(struct snd_seq_client *client,
1712 void __user *arg)
1714 int result;
1715 struct snd_seq_queue_tempo tempo;
1717 if (copy_from_user(&tempo, arg, sizeof(tempo)))
1718 return -EFAULT;
1720 result = snd_seq_set_queue_tempo(client->number, &tempo);
1721 return result < 0 ? result : 0;
1725 /* GET_QUEUE_TIMER ioctl() */
1726 static int snd_seq_ioctl_get_queue_timer(struct snd_seq_client *client,
1727 void __user *arg)
1729 struct snd_seq_queue_timer timer;
1730 struct snd_seq_queue *queue;
1731 struct snd_seq_timer *tmr;
1733 if (copy_from_user(&timer, arg, sizeof(timer)))
1734 return -EFAULT;
1736 queue = queueptr(timer.queue);
1737 if (queue == NULL)
1738 return -EINVAL;
1740 if (mutex_lock_interruptible(&queue->timer_mutex)) {
1741 queuefree(queue);
1742 return -ERESTARTSYS;
1744 tmr = queue->timer;
1745 memset(&timer, 0, sizeof(timer));
1746 timer.queue = queue->queue;
1748 timer.type = tmr->type;
1749 if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
1750 timer.u.alsa.id = tmr->alsa_id;
1751 timer.u.alsa.resolution = tmr->preferred_resolution;
1753 mutex_unlock(&queue->timer_mutex);
1754 queuefree(queue);
1756 if (copy_to_user(arg, &timer, sizeof(timer)))
1757 return -EFAULT;
1758 return 0;
1762 /* SET_QUEUE_TIMER ioctl() */
1763 static int snd_seq_ioctl_set_queue_timer(struct snd_seq_client *client,
1764 void __user *arg)
1766 int result = 0;
1767 struct snd_seq_queue_timer timer;
1769 if (copy_from_user(&timer, arg, sizeof(timer)))
1770 return -EFAULT;
1772 if (timer.type != SNDRV_SEQ_TIMER_ALSA)
1773 return -EINVAL;
1775 if (snd_seq_queue_check_access(timer.queue, client->number)) {
1776 struct snd_seq_queue *q;
1777 struct snd_seq_timer *tmr;
1779 q = queueptr(timer.queue);
1780 if (q == NULL)
1781 return -ENXIO;
1782 if (mutex_lock_interruptible(&q->timer_mutex)) {
1783 queuefree(q);
1784 return -ERESTARTSYS;
1786 tmr = q->timer;
1787 snd_seq_queue_timer_close(timer.queue);
1788 tmr->type = timer.type;
1789 if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
1790 tmr->alsa_id = timer.u.alsa.id;
1791 tmr->preferred_resolution = timer.u.alsa.resolution;
1793 result = snd_seq_queue_timer_open(timer.queue);
1794 mutex_unlock(&q->timer_mutex);
1795 queuefree(q);
1796 } else {
1797 return -EPERM;
1800 return result;
1804 /* GET_QUEUE_CLIENT ioctl() */
1805 static int snd_seq_ioctl_get_queue_client(struct snd_seq_client *client,
1806 void __user *arg)
1808 struct snd_seq_queue_client info;
1809 int used;
1811 if (copy_from_user(&info, arg, sizeof(info)))
1812 return -EFAULT;
1814 used = snd_seq_queue_is_used(info.queue, client->number);
1815 if (used < 0)
1816 return -EINVAL;
1817 info.used = used;
1818 info.client = client->number;
1820 if (copy_to_user(arg, &info, sizeof(info)))
1821 return -EFAULT;
1822 return 0;
1826 /* SET_QUEUE_CLIENT ioctl() */
1827 static int snd_seq_ioctl_set_queue_client(struct snd_seq_client *client,
1828 void __user *arg)
1830 int err;
1831 struct snd_seq_queue_client info;
1833 if (copy_from_user(&info, arg, sizeof(info)))
1834 return -EFAULT;
1836 if (info.used >= 0) {
1837 err = snd_seq_queue_use(info.queue, client->number, info.used);
1838 if (err < 0)
1839 return err;
1842 return snd_seq_ioctl_get_queue_client(client, arg);
1846 /* GET_CLIENT_POOL ioctl() */
1847 static int snd_seq_ioctl_get_client_pool(struct snd_seq_client *client,
1848 void __user *arg)
1850 struct snd_seq_client_pool info;
1851 struct snd_seq_client *cptr;
1853 if (copy_from_user(&info, arg, sizeof(info)))
1854 return -EFAULT;
1856 cptr = snd_seq_client_use_ptr(info.client);
1857 if (cptr == NULL)
1858 return -ENOENT;
1859 memset(&info, 0, sizeof(info));
1860 info.output_pool = cptr->pool->size;
1861 info.output_room = cptr->pool->room;
1862 info.output_free = info.output_pool;
1863 info.output_free = snd_seq_unused_cells(cptr->pool);
1864 if (cptr->type == USER_CLIENT) {
1865 info.input_pool = cptr->data.user.fifo_pool_size;
1866 info.input_free = info.input_pool;
1867 if (cptr->data.user.fifo)
1868 info.input_free = snd_seq_unused_cells(cptr->data.user.fifo->pool);
1869 } else {
1870 info.input_pool = 0;
1871 info.input_free = 0;
1873 snd_seq_client_unlock(cptr);
1875 if (copy_to_user(arg, &info, sizeof(info)))
1876 return -EFAULT;
1877 return 0;
1880 /* SET_CLIENT_POOL ioctl() */
1881 static int snd_seq_ioctl_set_client_pool(struct snd_seq_client *client,
1882 void __user *arg)
1884 struct snd_seq_client_pool info;
1885 int rc;
1887 if (copy_from_user(&info, arg, sizeof(info)))
1888 return -EFAULT;
1890 if (client->number != info.client)
1891 return -EINVAL; /* can't change other clients */
1893 if (info.output_pool >= 1 && info.output_pool <= SNDRV_SEQ_MAX_EVENTS &&
1894 (! snd_seq_write_pool_allocated(client) ||
1895 info.output_pool != client->pool->size)) {
1896 if (snd_seq_write_pool_allocated(client)) {
1897 /* remove all existing cells */
1898 snd_seq_queue_client_leave_cells(client->number);
1899 snd_seq_pool_done(client->pool);
1901 client->pool->size = info.output_pool;
1902 rc = snd_seq_pool_init(client->pool);
1903 if (rc < 0)
1904 return rc;
1906 if (client->type == USER_CLIENT && client->data.user.fifo != NULL &&
1907 info.input_pool >= 1 &&
1908 info.input_pool <= SNDRV_SEQ_MAX_CLIENT_EVENTS &&
1909 info.input_pool != client->data.user.fifo_pool_size) {
1910 /* change pool size */
1911 rc = snd_seq_fifo_resize(client->data.user.fifo, info.input_pool);
1912 if (rc < 0)
1913 return rc;
1914 client->data.user.fifo_pool_size = info.input_pool;
1916 if (info.output_room >= 1 &&
1917 info.output_room <= client->pool->size) {
1918 client->pool->room = info.output_room;
1921 return snd_seq_ioctl_get_client_pool(client, arg);
1925 /* REMOVE_EVENTS ioctl() */
1926 static int snd_seq_ioctl_remove_events(struct snd_seq_client *client,
1927 void __user *arg)
1929 struct snd_seq_remove_events info;
1931 if (copy_from_user(&info, arg, sizeof(info)))
1932 return -EFAULT;
1935 * Input mostly not implemented XXX.
1937 if (info.remove_mode & SNDRV_SEQ_REMOVE_INPUT) {
1939 * No restrictions so for a user client we can clear
1940 * the whole fifo
1942 if (client->type == USER_CLIENT)
1943 snd_seq_fifo_clear(client->data.user.fifo);
1946 if (info.remove_mode & SNDRV_SEQ_REMOVE_OUTPUT)
1947 snd_seq_queue_remove_cells(client->number, &info);
1949 return 0;
1954 * get subscription info
1956 static int snd_seq_ioctl_get_subscription(struct snd_seq_client *client,
1957 void __user *arg)
1959 int result;
1960 struct snd_seq_client *sender = NULL;
1961 struct snd_seq_client_port *sport = NULL;
1962 struct snd_seq_port_subscribe subs;
1963 struct snd_seq_subscribers *p;
1965 if (copy_from_user(&subs, arg, sizeof(subs)))
1966 return -EFAULT;
1968 result = -EINVAL;
1969 if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)
1970 goto __end;
1971 if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)
1972 goto __end;
1973 p = snd_seq_port_get_subscription(&sport->c_src, &subs.dest);
1974 if (p) {
1975 result = 0;
1976 subs = p->info;
1977 } else
1978 result = -ENOENT;
1980 __end:
1981 if (sport)
1982 snd_seq_port_unlock(sport);
1983 if (sender)
1984 snd_seq_client_unlock(sender);
1985 if (result >= 0) {
1986 if (copy_to_user(arg, &subs, sizeof(subs)))
1987 return -EFAULT;
1989 return result;
1994 * get subscription info - check only its presence
1996 static int snd_seq_ioctl_query_subs(struct snd_seq_client *client,
1997 void __user *arg)
1999 int result = -ENXIO;
2000 struct snd_seq_client *cptr = NULL;
2001 struct snd_seq_client_port *port = NULL;
2002 struct snd_seq_query_subs subs;
2003 struct snd_seq_port_subs_info *group;
2004 struct list_head *p;
2005 int i;
2007 if (copy_from_user(&subs, arg, sizeof(subs)))
2008 return -EFAULT;
2010 if ((cptr = snd_seq_client_use_ptr(subs.root.client)) == NULL)
2011 goto __end;
2012 if ((port = snd_seq_port_use_ptr(cptr, subs.root.port)) == NULL)
2013 goto __end;
2015 switch (subs.type) {
2016 case SNDRV_SEQ_QUERY_SUBS_READ:
2017 group = &port->c_src;
2018 break;
2019 case SNDRV_SEQ_QUERY_SUBS_WRITE:
2020 group = &port->c_dest;
2021 break;
2022 default:
2023 goto __end;
2026 down_read(&group->list_mutex);
2027 /* search for the subscriber */
2028 subs.num_subs = group->count;
2029 i = 0;
2030 result = -ENOENT;
2031 list_for_each(p, &group->list_head) {
2032 if (i++ == subs.index) {
2033 /* found! */
2034 struct snd_seq_subscribers *s;
2035 if (subs.type == SNDRV_SEQ_QUERY_SUBS_READ) {
2036 s = list_entry(p, struct snd_seq_subscribers, src_list);
2037 subs.addr = s->info.dest;
2038 } else {
2039 s = list_entry(p, struct snd_seq_subscribers, dest_list);
2040 subs.addr = s->info.sender;
2042 subs.flags = s->info.flags;
2043 subs.queue = s->info.queue;
2044 result = 0;
2045 break;
2048 up_read(&group->list_mutex);
2050 __end:
2051 if (port)
2052 snd_seq_port_unlock(port);
2053 if (cptr)
2054 snd_seq_client_unlock(cptr);
2055 if (result >= 0) {
2056 if (copy_to_user(arg, &subs, sizeof(subs)))
2057 return -EFAULT;
2059 return result;
2064 * query next client
2066 static int snd_seq_ioctl_query_next_client(struct snd_seq_client *client,
2067 void __user *arg)
2069 struct snd_seq_client *cptr = NULL;
2070 struct snd_seq_client_info info;
2072 if (copy_from_user(&info, arg, sizeof(info)))
2073 return -EFAULT;
2075 /* search for next client */
2076 info.client++;
2077 if (info.client < 0)
2078 info.client = 0;
2079 for (; info.client < SNDRV_SEQ_MAX_CLIENTS; info.client++) {
2080 cptr = snd_seq_client_use_ptr(info.client);
2081 if (cptr)
2082 break; /* found */
2084 if (cptr == NULL)
2085 return -ENOENT;
2087 get_client_info(cptr, &info);
2088 snd_seq_client_unlock(cptr);
2090 if (copy_to_user(arg, &info, sizeof(info)))
2091 return -EFAULT;
2092 return 0;
2096 * query next port
2098 static int snd_seq_ioctl_query_next_port(struct snd_seq_client *client,
2099 void __user *arg)
2101 struct snd_seq_client *cptr;
2102 struct snd_seq_client_port *port = NULL;
2103 struct snd_seq_port_info info;
2105 if (copy_from_user(&info, arg, sizeof(info)))
2106 return -EFAULT;
2107 cptr = snd_seq_client_use_ptr(info.addr.client);
2108 if (cptr == NULL)
2109 return -ENXIO;
2111 /* search for next port */
2112 info.addr.port++;
2113 port = snd_seq_port_query_nearest(cptr, &info);
2114 if (port == NULL) {
2115 snd_seq_client_unlock(cptr);
2116 return -ENOENT;
2119 /* get port info */
2120 info.addr = port->addr;
2121 snd_seq_get_port_info(port, &info);
2122 snd_seq_port_unlock(port);
2123 snd_seq_client_unlock(cptr);
2125 if (copy_to_user(arg, &info, sizeof(info)))
2126 return -EFAULT;
2127 return 0;
2130 /* -------------------------------------------------------- */
2132 static struct seq_ioctl_table {
2133 unsigned int cmd;
2134 int (*func)(struct snd_seq_client *client, void __user * arg);
2135 } ioctl_tables[] = {
2136 { SNDRV_SEQ_IOCTL_SYSTEM_INFO, snd_seq_ioctl_system_info },
2137 { SNDRV_SEQ_IOCTL_RUNNING_MODE, snd_seq_ioctl_running_mode },
2138 { SNDRV_SEQ_IOCTL_GET_CLIENT_INFO, snd_seq_ioctl_get_client_info },
2139 { SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, snd_seq_ioctl_set_client_info },
2140 { SNDRV_SEQ_IOCTL_CREATE_PORT, snd_seq_ioctl_create_port },
2141 { SNDRV_SEQ_IOCTL_DELETE_PORT, snd_seq_ioctl_delete_port },
2142 { SNDRV_SEQ_IOCTL_GET_PORT_INFO, snd_seq_ioctl_get_port_info },
2143 { SNDRV_SEQ_IOCTL_SET_PORT_INFO, snd_seq_ioctl_set_port_info },
2144 { SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, snd_seq_ioctl_subscribe_port },
2145 { SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, snd_seq_ioctl_unsubscribe_port },
2146 { SNDRV_SEQ_IOCTL_CREATE_QUEUE, snd_seq_ioctl_create_queue },
2147 { SNDRV_SEQ_IOCTL_DELETE_QUEUE, snd_seq_ioctl_delete_queue },
2148 { SNDRV_SEQ_IOCTL_GET_QUEUE_INFO, snd_seq_ioctl_get_queue_info },
2149 { SNDRV_SEQ_IOCTL_SET_QUEUE_INFO, snd_seq_ioctl_set_queue_info },
2150 { SNDRV_SEQ_IOCTL_GET_NAMED_QUEUE, snd_seq_ioctl_get_named_queue },
2151 { SNDRV_SEQ_IOCTL_GET_QUEUE_STATUS, snd_seq_ioctl_get_queue_status },
2152 { SNDRV_SEQ_IOCTL_GET_QUEUE_TEMPO, snd_seq_ioctl_get_queue_tempo },
2153 { SNDRV_SEQ_IOCTL_SET_QUEUE_TEMPO, snd_seq_ioctl_set_queue_tempo },
2154 { SNDRV_SEQ_IOCTL_GET_QUEUE_TIMER, snd_seq_ioctl_get_queue_timer },
2155 { SNDRV_SEQ_IOCTL_SET_QUEUE_TIMER, snd_seq_ioctl_set_queue_timer },
2156 { SNDRV_SEQ_IOCTL_GET_QUEUE_CLIENT, snd_seq_ioctl_get_queue_client },
2157 { SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT, snd_seq_ioctl_set_queue_client },
2158 { SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, snd_seq_ioctl_get_client_pool },
2159 { SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, snd_seq_ioctl_set_client_pool },
2160 { SNDRV_SEQ_IOCTL_GET_SUBSCRIPTION, snd_seq_ioctl_get_subscription },
2161 { SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT, snd_seq_ioctl_query_next_client },
2162 { SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, snd_seq_ioctl_query_next_port },
2163 { SNDRV_SEQ_IOCTL_REMOVE_EVENTS, snd_seq_ioctl_remove_events },
2164 { SNDRV_SEQ_IOCTL_QUERY_SUBS, snd_seq_ioctl_query_subs },
2165 { 0, NULL },
2168 static int snd_seq_do_ioctl(struct snd_seq_client *client, unsigned int cmd,
2169 void __user *arg)
2171 struct seq_ioctl_table *p;
2173 switch (cmd) {
2174 case SNDRV_SEQ_IOCTL_PVERSION:
2175 /* return sequencer version number */
2176 return put_user(SNDRV_SEQ_VERSION, (int __user *)arg) ? -EFAULT : 0;
2177 case SNDRV_SEQ_IOCTL_CLIENT_ID:
2178 /* return the id of this client */
2179 return put_user(client->number, (int __user *)arg) ? -EFAULT : 0;
2182 if (! arg)
2183 return -EFAULT;
2184 for (p = ioctl_tables; p->cmd; p++) {
2185 if (p->cmd == cmd)
2186 return p->func(client, arg);
2188 snd_printd("seq unknown ioctl() 0x%x (type='%c', number=0x%2x)\n",
2189 cmd, _IOC_TYPE(cmd), _IOC_NR(cmd));
2190 return -ENOTTY;
2194 static long snd_seq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2196 struct snd_seq_client *client = file->private_data;
2198 snd_assert(client != NULL, return -ENXIO);
2200 return snd_seq_do_ioctl(client, cmd, (void __user *) arg);
2203 #ifdef CONFIG_COMPAT
2204 #include "seq_compat.c"
2205 #else
2206 #define snd_seq_ioctl_compat NULL
2207 #endif
2209 /* -------------------------------------------------------- */
2212 /* exported to kernel modules */
2213 int snd_seq_create_kernel_client(struct snd_card *card, int client_index,
2214 const char *name_fmt, ...)
2216 struct snd_seq_client *client;
2217 va_list args;
2219 snd_assert(! in_interrupt(), return -EBUSY);
2221 if (card && client_index >= SNDRV_SEQ_CLIENTS_PER_CARD)
2222 return -EINVAL;
2223 if (card == NULL && client_index >= SNDRV_SEQ_GLOBAL_CLIENTS)
2224 return -EINVAL;
2226 if (mutex_lock_interruptible(&register_mutex))
2227 return -ERESTARTSYS;
2229 if (card) {
2230 client_index += SNDRV_SEQ_GLOBAL_CLIENTS
2231 + card->number * SNDRV_SEQ_CLIENTS_PER_CARD;
2232 if (client_index >= SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN)
2233 client_index = -1;
2236 /* empty write queue as default */
2237 client = seq_create_client1(client_index, 0);
2238 if (client == NULL) {
2239 mutex_unlock(&register_mutex);
2240 return -EBUSY; /* failure code */
2242 usage_alloc(&client_usage, 1);
2244 client->accept_input = 1;
2245 client->accept_output = 1;
2247 va_start(args, name_fmt);
2248 vsnprintf(client->name, sizeof(client->name), name_fmt, args);
2249 va_end(args);
2251 client->type = KERNEL_CLIENT;
2252 mutex_unlock(&register_mutex);
2254 /* make others aware this new client */
2255 snd_seq_system_client_ev_client_start(client->number);
2257 /* return client number to caller */
2258 return client->number;
2261 EXPORT_SYMBOL(snd_seq_create_kernel_client);
2263 /* exported to kernel modules */
2264 int snd_seq_delete_kernel_client(int client)
2266 struct snd_seq_client *ptr;
2268 snd_assert(! in_interrupt(), return -EBUSY);
2270 ptr = clientptr(client);
2271 if (ptr == NULL)
2272 return -EINVAL;
2274 seq_free_client(ptr);
2275 kfree(ptr);
2276 return 0;
2279 EXPORT_SYMBOL(snd_seq_delete_kernel_client);
2281 /* skeleton to enqueue event, called from snd_seq_kernel_client_enqueue
2282 * and snd_seq_kernel_client_enqueue_blocking
2284 static int kernel_client_enqueue(int client, struct snd_seq_event *ev,
2285 struct file *file, int blocking,
2286 int atomic, int hop)
2288 struct snd_seq_client *cptr;
2289 int result;
2291 snd_assert(ev != NULL, return -EINVAL);
2293 if (ev->type == SNDRV_SEQ_EVENT_NONE)
2294 return 0; /* ignore this */
2295 if (ev->type == SNDRV_SEQ_EVENT_KERNEL_ERROR)
2296 return -EINVAL; /* quoted events can't be enqueued */
2298 /* fill in client number */
2299 ev->source.client = client;
2301 if (check_event_type_and_length(ev))
2302 return -EINVAL;
2304 cptr = snd_seq_client_use_ptr(client);
2305 if (cptr == NULL)
2306 return -EINVAL;
2308 if (! cptr->accept_output)
2309 result = -EPERM;
2310 else /* send it */
2311 result = snd_seq_client_enqueue_event(cptr, ev, file, blocking, atomic, hop);
2313 snd_seq_client_unlock(cptr);
2314 return result;
2318 * exported, called by kernel clients to enqueue events (w/o blocking)
2320 * RETURN VALUE: zero if succeed, negative if error
2322 int snd_seq_kernel_client_enqueue(int client, struct snd_seq_event * ev,
2323 int atomic, int hop)
2325 return kernel_client_enqueue(client, ev, NULL, 0, atomic, hop);
2328 EXPORT_SYMBOL(snd_seq_kernel_client_enqueue);
2331 * exported, called by kernel clients to enqueue events (with blocking)
2333 * RETURN VALUE: zero if succeed, negative if error
2335 int snd_seq_kernel_client_enqueue_blocking(int client, struct snd_seq_event * ev,
2336 struct file *file,
2337 int atomic, int hop)
2339 return kernel_client_enqueue(client, ev, file, 1, atomic, hop);
2342 EXPORT_SYMBOL(snd_seq_kernel_client_enqueue_blocking);
2345 * exported, called by kernel clients to dispatch events directly to other
2346 * clients, bypassing the queues. Event time-stamp will be updated.
2348 * RETURN VALUE: negative = delivery failed,
2349 * zero, or positive: the number of delivered events
2351 int snd_seq_kernel_client_dispatch(int client, struct snd_seq_event * ev,
2352 int atomic, int hop)
2354 struct snd_seq_client *cptr;
2355 int result;
2357 snd_assert(ev != NULL, return -EINVAL);
2359 /* fill in client number */
2360 ev->queue = SNDRV_SEQ_QUEUE_DIRECT;
2361 ev->source.client = client;
2363 if (check_event_type_and_length(ev))
2364 return -EINVAL;
2366 cptr = snd_seq_client_use_ptr(client);
2367 if (cptr == NULL)
2368 return -EINVAL;
2370 if (!cptr->accept_output)
2371 result = -EPERM;
2372 else
2373 result = snd_seq_deliver_event(cptr, ev, atomic, hop);
2375 snd_seq_client_unlock(cptr);
2376 return result;
2379 EXPORT_SYMBOL(snd_seq_kernel_client_dispatch);
2382 * exported, called by kernel clients to perform same functions as with
2383 * userland ioctl()
2385 int snd_seq_kernel_client_ctl(int clientid, unsigned int cmd, void *arg)
2387 struct snd_seq_client *client;
2388 mm_segment_t fs;
2389 int result;
2391 client = clientptr(clientid);
2392 if (client == NULL)
2393 return -ENXIO;
2394 fs = snd_enter_user();
2395 result = snd_seq_do_ioctl(client, cmd, (void __user *)arg);
2396 snd_leave_user(fs);
2397 return result;
2400 EXPORT_SYMBOL(snd_seq_kernel_client_ctl);
2402 /* exported (for OSS emulator) */
2403 int snd_seq_kernel_client_write_poll(int clientid, struct file *file, poll_table *wait)
2405 struct snd_seq_client *client;
2407 client = clientptr(clientid);
2408 if (client == NULL)
2409 return -ENXIO;
2411 if (! snd_seq_write_pool_allocated(client))
2412 return 1;
2413 if (snd_seq_pool_poll_wait(client->pool, file, wait))
2414 return 1;
2415 return 0;
2418 EXPORT_SYMBOL(snd_seq_kernel_client_write_poll);
2420 /*---------------------------------------------------------------------------*/
2422 #ifdef CONFIG_PROC_FS
2424 * /proc interface
2426 static void snd_seq_info_dump_subscribers(struct snd_info_buffer *buffer,
2427 struct snd_seq_port_subs_info *group,
2428 int is_src, char *msg)
2430 struct list_head *p;
2431 struct snd_seq_subscribers *s;
2432 int count = 0;
2434 down_read(&group->list_mutex);
2435 if (list_empty(&group->list_head)) {
2436 up_read(&group->list_mutex);
2437 return;
2439 snd_iprintf(buffer, msg);
2440 list_for_each(p, &group->list_head) {
2441 if (is_src)
2442 s = list_entry(p, struct snd_seq_subscribers, src_list);
2443 else
2444 s = list_entry(p, struct snd_seq_subscribers, dest_list);
2445 if (count++)
2446 snd_iprintf(buffer, ", ");
2447 snd_iprintf(buffer, "%d:%d",
2448 is_src ? s->info.dest.client : s->info.sender.client,
2449 is_src ? s->info.dest.port : s->info.sender.port);
2450 if (s->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)
2451 snd_iprintf(buffer, "[%c:%d]", ((s->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL) ? 'r' : 't'), s->info.queue);
2452 if (group->exclusive)
2453 snd_iprintf(buffer, "[ex]");
2455 up_read(&group->list_mutex);
2456 snd_iprintf(buffer, "\n");
2459 #define FLAG_PERM_RD(perm) ((perm) & SNDRV_SEQ_PORT_CAP_READ ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_READ ? 'R' : 'r') : '-')
2460 #define FLAG_PERM_WR(perm) ((perm) & SNDRV_SEQ_PORT_CAP_WRITE ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_WRITE ? 'W' : 'w') : '-')
2461 #define FLAG_PERM_EX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_NO_EXPORT ? '-' : 'e')
2463 #define FLAG_PERM_DUPLEX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_DUPLEX ? 'X' : '-')
2465 static void snd_seq_info_dump_ports(struct snd_info_buffer *buffer,
2466 struct snd_seq_client *client)
2468 struct snd_seq_client_port *p;
2470 mutex_lock(&client->ports_mutex);
2471 list_for_each_entry(p, &client->ports_list_head, list) {
2472 snd_iprintf(buffer, " Port %3d : \"%s\" (%c%c%c%c)\n",
2473 p->addr.port, p->name,
2474 FLAG_PERM_RD(p->capability),
2475 FLAG_PERM_WR(p->capability),
2476 FLAG_PERM_EX(p->capability),
2477 FLAG_PERM_DUPLEX(p->capability));
2478 snd_seq_info_dump_subscribers(buffer, &p->c_src, 1, " Connecting To: ");
2479 snd_seq_info_dump_subscribers(buffer, &p->c_dest, 0, " Connected From: ");
2481 mutex_unlock(&client->ports_mutex);
2485 void snd_seq_info_pool(struct snd_info_buffer *buffer,
2486 struct snd_seq_pool *pool, char *space);
2488 /* exported to seq_info.c */
2489 void snd_seq_info_clients_read(struct snd_info_entry *entry,
2490 struct snd_info_buffer *buffer)
2492 int c;
2493 struct snd_seq_client *client;
2495 snd_iprintf(buffer, "Client info\n");
2496 snd_iprintf(buffer, " cur clients : %d\n", client_usage.cur);
2497 snd_iprintf(buffer, " peak clients : %d\n", client_usage.peak);
2498 snd_iprintf(buffer, " max clients : %d\n", SNDRV_SEQ_MAX_CLIENTS);
2499 snd_iprintf(buffer, "\n");
2501 /* list the client table */
2502 for (c = 0; c < SNDRV_SEQ_MAX_CLIENTS; c++) {
2503 client = snd_seq_client_use_ptr(c);
2504 if (client == NULL)
2505 continue;
2506 if (client->type == NO_CLIENT) {
2507 snd_seq_client_unlock(client);
2508 continue;
2511 snd_iprintf(buffer, "Client %3d : \"%s\" [%s]\n",
2512 c, client->name,
2513 client->type == USER_CLIENT ? "User" : "Kernel");
2514 snd_seq_info_dump_ports(buffer, client);
2515 if (snd_seq_write_pool_allocated(client)) {
2516 snd_iprintf(buffer, " Output pool :\n");
2517 snd_seq_info_pool(buffer, client->pool, " ");
2519 if (client->type == USER_CLIENT && client->data.user.fifo &&
2520 client->data.user.fifo->pool) {
2521 snd_iprintf(buffer, " Input pool :\n");
2522 snd_seq_info_pool(buffer, client->data.user.fifo->pool, " ");
2524 snd_seq_client_unlock(client);
2527 #endif /* CONFIG_PROC_FS */
2529 /*---------------------------------------------------------------------------*/
2533 * REGISTRATION PART
2536 static const struct file_operations snd_seq_f_ops =
2538 .owner = THIS_MODULE,
2539 .read = snd_seq_read,
2540 .write = snd_seq_write,
2541 .open = snd_seq_open,
2542 .release = snd_seq_release,
2543 .poll = snd_seq_poll,
2544 .unlocked_ioctl = snd_seq_ioctl,
2545 .compat_ioctl = snd_seq_ioctl_compat,
2549 * register sequencer device
2551 int __init snd_sequencer_device_init(void)
2553 int err;
2555 if (mutex_lock_interruptible(&register_mutex))
2556 return -ERESTARTSYS;
2558 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0,
2559 &snd_seq_f_ops, NULL, "seq")) < 0) {
2560 mutex_unlock(&register_mutex);
2561 return err;
2564 mutex_unlock(&register_mutex);
2566 return 0;
2572 * unregister sequencer device
2574 void __exit snd_sequencer_device_done(void)
2576 snd_unregister_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0);