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