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"
36 #include "seq_system.h"
37 #include <sound/seq_device.h>
39 #include <linux/compat.h>
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
);
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
;
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();
97 static inline void snd_leave_user(mm_segment_t fs
)
104 static inline unsigned short snd_seq_file_flags(struct file
*file
)
106 switch (file
->f_mode
& (FMODE_READ
| FMODE_WRITE
)) {
108 return SNDRV_SEQ_LFLG_OUTPUT
;
110 return SNDRV_SEQ_LFLG_INPUT
;
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",
129 return clienttab
[clientid
];
132 struct snd_seq_client
*snd_seq_client_use_ptr(int clientid
)
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",
142 spin_lock_irqsave(&clients_lock
, flags
);
143 client
= clientptr(clientid
);
146 if (clienttablock
[clientid
]) {
147 spin_unlock_irqrestore(&clients_lock
, flags
);
150 spin_unlock_irqrestore(&clients_lock
, flags
);
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
) {
158 if (!client_requested
[clientid
]) {
159 client_requested
[clientid
] = 1;
160 for (idx
= 0; idx
< 15; idx
++) {
161 if (seq_client_load
[idx
] < 0)
163 if (seq_client_load
[idx
] == clientid
) {
164 request_module("snd-seq-client-%i",
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
);
185 spin_unlock_irqrestore(&clients_lock
, flags
);
191 snd_use_lock_use(&client
->use_lock
);
192 spin_unlock_irqrestore(&clients_lock
, flags
);
196 static void usage_alloc(struct snd_seq_usage
*res
, int num
)
199 if (res
->cur
> res
->peak
)
200 res
->peak
= res
->cur
;
203 static void usage_free(struct snd_seq_usage
*res
, int 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
));
218 static struct snd_seq_client
*seq_create_client1(int client_index
, int poolsize
)
222 struct snd_seq_client
*client
;
224 /* init client data */
225 client
= kzalloc(sizeof(*client
), GFP_KERNEL
);
228 client
->pool
= snd_seq_pool_new(poolsize
);
229 if (client
->pool
== 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
;
245 if (clienttab
[c
] || clienttablock
[c
])
247 clienttab
[client
->number
= c
] = client
;
248 spin_unlock_irqrestore(&clients_lock
, flags
);
252 if (clienttab
[client_index
] == NULL
&& !clienttablock
[client_index
]) {
253 clienttab
[client
->number
= client_index
] = client
;
254 spin_unlock_irqrestore(&clients_lock
, flags
);
258 spin_unlock_irqrestore(&clients_lock
, flags
);
259 snd_seq_pool_delete(&client
->pool
);
261 return NULL
; /* no free slot found or busy, return failure code */
265 static int seq_free_client1(struct snd_seq_client
*client
)
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
);
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
);
287 static void seq_free_client(struct snd_seq_client
* client
)
289 mutex_lock(®ister_mutex
);
290 switch (client
->type
) {
292 snd_printk(KERN_WARNING
"Seq: Trying to free unused client %d\n",
297 seq_free_client1(client
);
298 usage_free(&client_usage
, 1);
302 snd_printk(KERN_ERR
"Seq: Trying to free client %d with undefined type = %d\n",
303 client
->number
, client
->type
);
305 mutex_unlock(®ister_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(®ister_mutex
))
323 client
= seq_create_client1(-1, SNDRV_SEQ_DEFAULT_EVENTS
);
324 if (client
== NULL
) {
325 mutex_unlock(®ister_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
;
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
);
345 mutex_unlock(®ister_mutex
);
350 usage_alloc(&client_usage
, 1);
351 client
->type
= USER_CLIENT
;
352 mutex_unlock(®ister_mutex
);
355 file
->private_data
= client
;
357 /* fill client data */
359 sprintf(client
->name
, "Client-%d", c
);
361 /* make others aware this new client */
362 snd_seq_system_client_ev_client_start(c
);
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
;
373 seq_free_client(client
);
374 if (client
->data
.user
.fifo
)
375 snd_seq_fifo_delete(&client
->data
.user
.fifo
);
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
,
393 struct snd_seq_client
*client
= file
->private_data
;
394 struct snd_seq_fifo
*fifo
;
397 struct snd_seq_event_cell
*cell
;
399 if (!(snd_seq_file_flags(file
) & SNDRV_SEQ_LFLG_INPUT
))
402 if (!access_ok(VERIFY_WRITE
, buf
, count
))
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
)
411 if (atomic_read(&fifo
->overflow
) > 0) {
412 /* buffer overflow is detected */
413 snd_seq_fifo_clear(fifo
);
414 /* return error code */
420 snd_seq_fifo_lock(fifo
);
422 /* while data available in queue */
423 while (count
>= sizeof(struct snd_seq_event
)) {
426 nonblock
= (file
->f_flags
& O_NONBLOCK
) || result
> 0;
427 if ((err
= snd_seq_fifo_cell_out(fifo
, &cell
, nonblock
)) < 0) {
430 if (snd_seq_ev_is_variable(&cell
->event
)) {
431 struct snd_seq_event tmpev
;
433 tmpev
.data
.ext
.len
&= ~SNDRV_SEQ_EXT_MASK
;
434 if (copy_to_user(buf
, &tmpev
, sizeof(struct snd_seq_event
))) {
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
));
449 if (copy_to_user(buf
, &cell
->event
, sizeof(struct snd_seq_event
))) {
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
);
463 snd_seq_fifo_cell_putback(fifo
, cell
);
464 if (err
== -EAGAIN
&& result
> 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
)
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
,
490 struct snd_seq_client
*dest
;
492 dest
= snd_seq_client_use_ptr(event
->dest
.client
);
495 if (! dest
->accept_input
)
497 if ((dest
->filter
& SNDRV_SEQ_FILTER_USE_EVENT
) &&
498 ! test_bit(event
->type
, dest
->event_filter
))
500 if (filter
&& !(dest
->filter
& filter
))
503 return dest
; /* ok - accessible */
505 snd_seq_client_unlock(dest
);
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
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
;
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);
547 client
->event_lost
++;
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
;
568 event
->queue
= queue
;
569 event
->flags
&= ~SNDRV_SEQ_TIME_STAMP_MASK
;
571 event
->time
.time
= snd_seq_timer_get_cur_time(q
->timer
);
572 event
->flags
|= SNDRV_SEQ_TIME_STAMP_REAL
;
574 event
->time
.tick
= snd_seq_timer_get_cur_tick(q
->timer
);
575 event
->flags
|= SNDRV_SEQ_TIME_STAMP_TICK
;
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
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
;
598 direct
= snd_seq_ev_is_direct(event
);
600 dest
= get_event_dest_client(event
, filter
);
603 dest_port
= snd_seq_port_use_ptr(dest
, event
->dest
.port
);
604 if (dest_port
== NULL
)
607 /* check permission */
608 if (! check_port_perm(dest_port
, SNDRV_SEQ_PORT_CAP_WRITE
)) {
613 if (dest_port
->timestamping
)
614 update_timestamp_of_queue(event
, dest_port
->time_queue
,
615 dest_port
->time_real
);
617 switch (dest
->type
) {
619 if (dest
->data
.user
.fifo
)
620 result
= snd_seq_fifo_event_in(dest
->data
.user
.fifo
, event
);
624 if (dest_port
->event_input
== NULL
)
626 result
= dest_port
->event_input(event
, direct
,
627 dest_port
->private_data
,
636 snd_seq_port_unlock(dest_port
);
638 snd_seq_client_unlock(dest
);
640 if (result
< 0 && !direct
) {
641 result
= bounce_error_event(client
, event
, result
, atomic
, hop
);
648 * send the event to all subscribers:
650 static int deliver_to_subscribers(struct snd_seq_client
*client
,
651 struct snd_seq_event
*event
,
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
;
669 read_lock(&grp
->list_lock
);
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
,
683 /* restore original event record */
684 *event
= event_saved
;
687 read_unlock(&grp
->list_lock
);
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
,
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
,
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;
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
)
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
);
751 /* pass NULL as source client to avoid error bounce */
752 err
= snd_seq_deliver_single_event(NULL
, event
,
753 SNDRV_SEQ_FILTER_BROADCAST
,
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
,
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
,
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
);
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
);
808 result
= snd_seq_deliver_single_event(client
, event
, 0, atomic
, hop
);
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
;
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 */
836 if (cell
->event
.type
== SNDRV_SEQ_EVENT_NOTE
) {
838 * the event cell is re-used as a NOTE-OFF event and
841 struct snd_seq_event tmpev
, *ev
;
843 /* reserve this event to enqueue note-off later */
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.
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
;
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;
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 */
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
);
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
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
,
899 struct snd_seq_event_cell
*cell
;
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
;
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
;
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
)
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
);
939 /* we got a cell. enqueue it. */
940 if ((err
= snd_seq_enqueue_event(cell
, atomic
, hop
)) < 0) {
941 snd_seq_cell_free(cell
);
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
))
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
)
965 case SNDRV_SEQ_EVENT_LENGTH_VARUSR
:
966 if (! snd_seq_ev_is_direct(ev
))
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
;
991 struct snd_seq_event event
;
993 if (!(snd_seq_file_flags(file
) & SNDRV_SEQ_LFLG_OUTPUT
))
996 /* check client structures are in place */
997 snd_assert(client
!= NULL
, return -ENXIO
);
999 if (!client
->accept_output
|| client
->pool
== NULL
)
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)
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
)) {
1016 event
.source
.client
= client
->number
; /* fill in client number */
1017 /* Check for extension data length */
1018 if (check_event_type_and_length(&event
)) {
1023 /* check for special events */
1024 if (event
.type
== SNDRV_SEQ_EVENT_NONE
)
1026 else if (snd_seq_ev_is_reserved(&event
)) {
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 */
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 */
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
;
1052 /* ok, enqueue it */
1053 err
= snd_seq_client_enqueue_event(client
, &event
, file
,
1054 !(file
->f_flags
& O_NONBLOCK
),
1060 /* Update pointers and counts */
1066 return written
? written
: err
;
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
;
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
)))
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
;
1131 if (copy_from_user(&info
, arg
, sizeof(info
)))
1134 /* requested client number */
1135 cptr
= snd_seq_client_use_ptr(info
.client
);
1137 return -ENOENT
; /* don't change !!! */
1139 #ifdef SNDRV_BIG_ENDIAN
1140 if (! info
.big_endian
) {
1145 if (info
.big_endian
) {
1151 if (info
.cpu_mode
> sizeof(long)) {
1155 cptr
->convert32
= (info
.cpu_mode
< sizeof(long));
1157 snd_seq_client_unlock(cptr
);
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
,
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
)))
1186 /* requested client number */
1187 cptr
= snd_seq_client_use_ptr(client_info
.client
);
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
)))
1200 /* CLIENT_INFO ioctl() */
1201 static int snd_seq_ioctl_set_client_info(struct snd_seq_client
*client
,
1204 struct snd_seq_client_info client_info
;
1206 if (copy_from_user(&client_info
, arg
, sizeof(client_info
)))
1209 /* it is not allowed to set the info fields for an another client */
1210 if (client
->number
!= client_info
.client
)
1212 /* also client type must be set now */
1213 if (client
->type
!= client_info
.type
)
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);
1229 * CREATE PORT ioctl()
1231 static int snd_seq_ioctl_create_port(struct snd_seq_client
*client
,
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
)))
1241 /* it is not allowed to create the port for an another client */
1242 if (info
.addr
.client
!= client
->number
)
1245 port
= snd_seq_create_port(client
, (info
.flags
& SNDRV_SEQ_PORT_FLG_GIVEN_PORT
) ? info
.addr
.port
: -1);
1249 if (client
->type
== USER_CLIENT
&& info
.kernel
) {
1250 snd_seq_delete_port(client
, port
->addr
.port
);
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
)))
1280 * DELETE PORT ioctl()
1282 static int snd_seq_ioctl_delete_port(struct snd_seq_client
*client
,
1285 struct snd_seq_port_info info
;
1288 /* set passed parameters */
1289 if (copy_from_user(&info
, arg
, sizeof(info
)))
1292 /* it is not allowed to remove the port for an another client */
1293 if (info
.addr
.client
!= client
->number
)
1296 err
= snd_seq_delete_port(client
, info
.addr
.port
);
1298 snd_seq_system_client_ev_port_exit(client
->number
, info
.addr
.port
);
1304 * GET_PORT_INFO ioctl() (on any client)
1306 static int snd_seq_ioctl_get_port_info(struct snd_seq_client
*client
,
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
)))
1315 cptr
= snd_seq_client_use_ptr(info
.addr
.client
);
1319 port
= snd_seq_port_use_ptr(cptr
, info
.addr
.port
);
1321 snd_seq_client_unlock(cptr
);
1322 return -ENOENT
; /* don't change */
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
)))
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
,
1342 struct snd_seq_client_port
*port
;
1343 struct snd_seq_port_info info
;
1345 if (copy_from_user(&info
, arg
, sizeof(info
)))
1348 if (info
.addr
.client
!= client
->number
) /* only set our own ports ! */
1350 port
= snd_seq_port_use_ptr(client
, info
.addr
.port
);
1352 snd_seq_set_port_info(port
, &info
);
1353 snd_seq_port_unlock(port
);
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
))
1375 if (check_port_perm(dport
, SNDRV_SEQ_PORT_CAP_NO_EXPORT
))
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
))
1387 /* check write permission */
1388 if (client
->number
!= subs
->dest
.client
) {
1389 if (! check_port_perm(dport
, PERM_WR
))
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
,
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
,
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
)))
1428 if ((receiver
= snd_seq_client_use_ptr(subs
.dest
.client
)) == NULL
)
1430 if ((sender
= snd_seq_client_use_ptr(subs
.sender
.client
)) == NULL
)
1432 if ((sport
= snd_seq_port_use_ptr(sender
, subs
.sender
.port
)) == NULL
)
1434 if ((dport
= snd_seq_port_use_ptr(receiver
, subs
.dest
.port
)) == NULL
)
1437 result
= check_subscription_permission(client
, sport
, dport
, &subs
);
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
);
1448 snd_seq_port_unlock(sport
);
1450 snd_seq_port_unlock(dport
);
1452 snd_seq_client_unlock(sender
);
1454 snd_seq_client_unlock(receiver
);
1460 * remove from port's subscription list
1462 static int snd_seq_ioctl_unsubscribe_port(struct snd_seq_client
*client
,
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
)))
1473 if ((receiver
= snd_seq_client_use_ptr(subs
.dest
.client
)) == NULL
)
1475 if ((sender
= snd_seq_client_use_ptr(subs
.sender
.client
)) == NULL
)
1477 if ((sport
= snd_seq_port_use_ptr(sender
, subs
.sender
.port
)) == NULL
)
1479 if ((dport
= snd_seq_port_use_ptr(receiver
, subs
.dest
.port
)) == NULL
)
1482 result
= check_subscription_permission(client
, sport
, dport
, &subs
);
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
);
1492 snd_seq_port_unlock(sport
);
1494 snd_seq_port_unlock(dport
);
1496 snd_seq_client_unlock(sender
);
1498 snd_seq_client_unlock(receiver
);
1503 /* CREATE_QUEUE ioctl() */
1504 static int snd_seq_ioctl_create_queue(struct snd_seq_client
*client
,
1507 struct snd_seq_queue_info info
;
1509 struct snd_seq_queue
*q
;
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(struct snd_seq_client
*client
,
1542 struct snd_seq_queue_info info
;
1544 if (copy_from_user(&info
, arg
, sizeof(info
)))
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
,
1554 struct snd_seq_queue_info info
;
1555 struct snd_seq_queue
*q
;
1557 if (copy_from_user(&info
, arg
, sizeof(info
)))
1560 q
= queueptr(info
.queue
);
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
));
1571 if (copy_to_user(arg
, &info
, sizeof(info
)))
1577 /* SET_QUEUE_INFO ioctl() */
1578 static int snd_seq_ioctl_set_queue_info(struct snd_seq_client
*client
,
1581 struct snd_seq_queue_info info
;
1582 struct snd_seq_queue
*q
;
1584 if (copy_from_user(&info
, arg
, sizeof(info
)))
1587 if (info
.owner
!= client
->number
)
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)
1595 snd_seq_queue_use(info
.queue
, client
->number
, 1);
1600 q
= queueptr(info
.queue
);
1603 if (q
->owner
!= client
->number
) {
1607 strlcpy(q
->name
, info
.name
, sizeof(q
->name
));
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
)))
1622 q
= snd_seq_queue_find_name(info
.name
);
1625 info
.queue
= q
->queue
;
1626 info
.owner
= q
->owner
;
1627 info
.locked
= q
->locked
;
1630 if (copy_to_user(arg
, &info
, sizeof(info
)))
1636 /* GET_QUEUE_STATUS ioctl() */
1637 static int snd_seq_ioctl_get_queue_status(struct snd_seq_client
*client
,
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
)))
1647 queue
= queueptr(status
.queue
);
1650 memset(&status
, 0, sizeof(status
));
1651 status
.queue
= queue
->queue
;
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
;
1664 if (copy_to_user(arg
, &status
, sizeof(status
)))
1670 /* GET_QUEUE_TEMPO ioctl() */
1671 static int snd_seq_ioctl_get_queue_tempo(struct snd_seq_client
*client
,
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
)))
1681 queue
= queueptr(tempo
.queue
);
1684 memset(&tempo
, 0, sizeof(tempo
));
1685 tempo
.queue
= queue
->queue
;
1689 tempo
.tempo
= tmr
->tempo
;
1690 tempo
.ppq
= tmr
->ppq
;
1691 tempo
.skew_value
= tmr
->skew
;
1692 tempo
.skew_base
= tmr
->skew_base
;
1695 if (copy_to_user(arg
, &tempo
, sizeof(tempo
)))
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
))
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
,
1715 struct snd_seq_queue_tempo tempo
;
1717 if (copy_from_user(&tempo
, arg
, sizeof(tempo
)))
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
,
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
)))
1736 queue
= queueptr(timer
.queue
);
1740 if (mutex_lock_interruptible(&queue
->timer_mutex
)) {
1742 return -ERESTARTSYS
;
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
);
1756 if (copy_to_user(arg
, &timer
, sizeof(timer
)))
1762 /* SET_QUEUE_TIMER ioctl() */
1763 static int snd_seq_ioctl_set_queue_timer(struct snd_seq_client
*client
,
1767 struct snd_seq_queue_timer timer
;
1769 if (copy_from_user(&timer
, arg
, sizeof(timer
)))
1772 if (timer
.type
!= SNDRV_SEQ_TIMER_ALSA
)
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
);
1782 if (mutex_lock_interruptible(&q
->timer_mutex
)) {
1784 return -ERESTARTSYS
;
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
);
1804 /* GET_QUEUE_CLIENT ioctl() */
1805 static int snd_seq_ioctl_get_queue_client(struct snd_seq_client
*client
,
1808 struct snd_seq_queue_client info
;
1811 if (copy_from_user(&info
, arg
, sizeof(info
)))
1814 used
= snd_seq_queue_is_used(info
.queue
, client
->number
);
1818 info
.client
= client
->number
;
1820 if (copy_to_user(arg
, &info
, sizeof(info
)))
1826 /* SET_QUEUE_CLIENT ioctl() */
1827 static int snd_seq_ioctl_set_queue_client(struct snd_seq_client
*client
,
1831 struct snd_seq_queue_client info
;
1833 if (copy_from_user(&info
, arg
, sizeof(info
)))
1836 if (info
.used
>= 0) {
1837 err
= snd_seq_queue_use(info
.queue
, client
->number
, info
.used
);
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
,
1850 struct snd_seq_client_pool info
;
1851 struct snd_seq_client
*cptr
;
1853 if (copy_from_user(&info
, arg
, sizeof(info
)))
1856 cptr
= snd_seq_client_use_ptr(info
.client
);
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
);
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
)))
1880 /* SET_CLIENT_POOL ioctl() */
1881 static int snd_seq_ioctl_set_client_pool(struct snd_seq_client
*client
,
1884 struct snd_seq_client_pool info
;
1887 if (copy_from_user(&info
, arg
, sizeof(info
)))
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
);
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
);
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
,
1929 struct snd_seq_remove_events info
;
1931 if (copy_from_user(&info
, arg
, sizeof(info
)))
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
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
);
1954 * get subscription info
1956 static int snd_seq_ioctl_get_subscription(struct snd_seq_client
*client
,
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
)))
1969 if ((sender
= snd_seq_client_use_ptr(subs
.sender
.client
)) == NULL
)
1971 if ((sport
= snd_seq_port_use_ptr(sender
, subs
.sender
.port
)) == NULL
)
1973 p
= snd_seq_port_get_subscription(&sport
->c_src
, &subs
.dest
);
1982 snd_seq_port_unlock(sport
);
1984 snd_seq_client_unlock(sender
);
1986 if (copy_to_user(arg
, &subs
, sizeof(subs
)))
1994 * get subscription info - check only its presence
1996 static int snd_seq_ioctl_query_subs(struct snd_seq_client
*client
,
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
;
2007 if (copy_from_user(&subs
, arg
, sizeof(subs
)))
2010 if ((cptr
= snd_seq_client_use_ptr(subs
.root
.client
)) == NULL
)
2012 if ((port
= snd_seq_port_use_ptr(cptr
, subs
.root
.port
)) == NULL
)
2015 switch (subs
.type
) {
2016 case SNDRV_SEQ_QUERY_SUBS_READ
:
2017 group
= &port
->c_src
;
2019 case SNDRV_SEQ_QUERY_SUBS_WRITE
:
2020 group
= &port
->c_dest
;
2026 down_read(&group
->list_mutex
);
2027 /* search for the subscriber */
2028 subs
.num_subs
= group
->count
;
2031 list_for_each(p
, &group
->list_head
) {
2032 if (i
++ == subs
.index
) {
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
;
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
;
2048 up_read(&group
->list_mutex
);
2052 snd_seq_port_unlock(port
);
2054 snd_seq_client_unlock(cptr
);
2056 if (copy_to_user(arg
, &subs
, sizeof(subs
)))
2066 static int snd_seq_ioctl_query_next_client(struct snd_seq_client
*client
,
2069 struct snd_seq_client
*cptr
= NULL
;
2070 struct snd_seq_client_info info
;
2072 if (copy_from_user(&info
, arg
, sizeof(info
)))
2075 /* search for next client */
2077 if (info
.client
< 0)
2079 for (; info
.client
< SNDRV_SEQ_MAX_CLIENTS
; info
.client
++) {
2080 cptr
= snd_seq_client_use_ptr(info
.client
);
2087 get_client_info(cptr
, &info
);
2088 snd_seq_client_unlock(cptr
);
2090 if (copy_to_user(arg
, &info
, sizeof(info
)))
2098 static int snd_seq_ioctl_query_next_port(struct snd_seq_client
*client
,
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
)))
2107 cptr
= snd_seq_client_use_ptr(info
.addr
.client
);
2111 /* search for next port */
2113 port
= snd_seq_port_query_nearest(cptr
, &info
);
2115 snd_seq_client_unlock(cptr
);
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
)))
2130 /* -------------------------------------------------------- */
2132 static struct seq_ioctl_table
{
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
},
2168 static int snd_seq_do_ioctl(struct snd_seq_client
*client
, unsigned int cmd
,
2171 struct seq_ioctl_table
*p
;
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;
2184 for (p
= ioctl_tables
; p
->cmd
; p
++) {
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
));
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"
2206 #define snd_seq_ioctl_compat NULL
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
;
2219 snd_assert(! in_interrupt(), return -EBUSY
);
2221 if (card
&& client_index
>= SNDRV_SEQ_CLIENTS_PER_CARD
)
2223 if (card
== NULL
&& client_index
>= SNDRV_SEQ_GLOBAL_CLIENTS
)
2226 if (mutex_lock_interruptible(®ister_mutex
))
2227 return -ERESTARTSYS
;
2230 client_index
+= SNDRV_SEQ_GLOBAL_CLIENTS
2231 + card
->number
* SNDRV_SEQ_CLIENTS_PER_CARD
;
2232 if (client_index
>= SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN
)
2236 /* empty write queue as default */
2237 client
= seq_create_client1(client_index
, 0);
2238 if (client
== NULL
) {
2239 mutex_unlock(®ister_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
);
2251 client
->type
= KERNEL_CLIENT
;
2252 mutex_unlock(®ister_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
);
2274 seq_free_client(ptr
);
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
;
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
))
2304 cptr
= snd_seq_client_use_ptr(client
);
2308 if (! cptr
->accept_output
)
2311 result
= snd_seq_client_enqueue_event(cptr
, ev
, file
, blocking
, atomic
, hop
);
2313 snd_seq_client_unlock(cptr
);
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
,
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
;
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
))
2366 cptr
= snd_seq_client_use_ptr(client
);
2370 if (!cptr
->accept_output
)
2373 result
= snd_seq_deliver_event(cptr
, ev
, atomic
, hop
);
2375 snd_seq_client_unlock(cptr
);
2379 EXPORT_SYMBOL(snd_seq_kernel_client_dispatch
);
2382 * exported, called by kernel clients to perform same functions as with
2385 int snd_seq_kernel_client_ctl(int clientid
, unsigned int cmd
, void *arg
)
2387 struct snd_seq_client
*client
;
2391 client
= clientptr(clientid
);
2394 fs
= snd_enter_user();
2395 result
= snd_seq_do_ioctl(client
, cmd
, (void __user
*)arg
);
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
);
2411 if (! snd_seq_write_pool_allocated(client
))
2413 if (snd_seq_pool_poll_wait(client
->pool
, file
, wait
))
2418 EXPORT_SYMBOL(snd_seq_kernel_client_write_poll
);
2420 /*---------------------------------------------------------------------------*/
2422 #ifdef CONFIG_PROC_FS
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
;
2434 down_read(&group
->list_mutex
);
2435 if (list_empty(&group
->list_head
)) {
2436 up_read(&group
->list_mutex
);
2439 snd_iprintf(buffer
, msg
);
2440 list_for_each(p
, &group
->list_head
) {
2442 s
= list_entry(p
, struct snd_seq_subscribers
, src_list
);
2444 s
= list_entry(p
, struct snd_seq_subscribers
, dest_list
);
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
)
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
);
2506 if (client
->type
== NO_CLIENT
) {
2507 snd_seq_client_unlock(client
);
2511 snd_iprintf(buffer
, "Client %3d : \"%s\" [%s]\n",
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 /*---------------------------------------------------------------------------*/
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)
2555 if (mutex_lock_interruptible(®ister_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(®ister_mutex
);
2564 mutex_unlock(®ister_mutex
);
2572 * unregister sequencer device
2574 void __exit
snd_sequencer_device_done(void)
2576 snd_unregister_device(SNDRV_DEVICE_TYPE_SEQUENCER
, NULL
, 0);