| changeset 5876: | 2d6164f0bf0e |
| parent: | d773ad622068 |
| author: | tiwai |
| date: | Tue Feb 19 11:56:06 2008 +0100 (4 years ago) |
| permissions: | -rw-r--r-- |
| description: | Dont touch fs_struct in drivers The sound drivers and the pnpbios core test for current->root != NULL. This test seems to be unnecessary since we always have rootfs mounted before initializing the drivers. Patch-level: Merged Signed-off-by: Jan Blunck <jblunck@suse.de> Acked-by: Christoph Hellwig <hch@lst.de> Cc: Bjorn Helgaas <bjorn.helgaas@hp.com> Cc: Jaroslav Kysela <perex@suse.cz> Acked-by: Takashi Iwai <tiwai@suse.de> Cc: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> |
1/*2 * ALSA sequencer Client Manager3 * 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>6 *7 *8 * This program is free software; you can redistribute it and/or modify9 * it under the terms of the GNU General Public License as published by10 * the Free Software Foundation; either version 2 of the License, or11 * (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 of15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16 * GNU General Public License for more details.17 *18 * You should have received a copy of the GNU General Public License19 * along with this program; if not, write to the Free Software20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA21 *22 */2324#include <linux/init.h>25#include <linux/slab.h>26#include <sound/core.h>27#include <sound/minors.h>28#include <linux/kmod.h>2930#include <sound/seq_kernel.h>31#include "seq_clientmgr.h"32#include "seq_memory.h"33#include "seq_queue.h"34#include "seq_timer.h"35#include "seq_info.h"36#include "seq_system.h"37#include <sound/seq_device.h>38#ifdef CONFIG_COMPAT39#include <linux/compat.h>40#endif4142/* Client Manager4344 * this module handles the connections of userland and kernel clients45 *46 */4748/*49 * There are four ranges of client numbers (last two shared):50 * 0..15: global clients51 * 16..127: statically allocated client numbers for cards 0..2752 * 128..191: dynamically allocated client numbers for cards 28..3153 * 128..191: dynamically allocated client numbers for applications54 */5556/* number of kernel non-card clients */57#define SNDRV_SEQ_GLOBAL_CLIENTS 1658/* clients per cards, for static clients */59#define SNDRV_SEQ_CLIENTS_PER_CARD 460/* dynamically allocated client numbers (both kernel drivers and user space) */61#define SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN 1286263#define SNDRV_SEQ_LFLG_INPUT 0x000164#define SNDRV_SEQ_LFLG_OUTPUT 0x000265#define SNDRV_SEQ_LFLG_OPEN (SNDRV_SEQ_LFLG_INPUT|SNDRV_SEQ_LFLG_OUTPUT)6667static DEFINE_SPINLOCK(clients_lock);68static DEFINE_MUTEX(register_mutex);6970/*71 * client table72 */73static char clienttablock[SNDRV_SEQ_MAX_CLIENTS];74static struct snd_seq_client *clienttab[SNDRV_SEQ_MAX_CLIENTS];75static struct snd_seq_usage client_usage;7677/*78 * prototypes79 */80static int bounce_error_event(struct snd_seq_client *client,81 struct snd_seq_event *event,82 int err, int atomic, int hop);83static int snd_seq_deliver_single_event(struct snd_seq_client *client,84 struct snd_seq_event *event,85 int filter, int atomic, int hop);8687/*88 */8990static inline mm_segment_t snd_enter_user(void)91{92 mm_segment_t fs = get_fs();93 set_fs(get_ds());94 return fs;95}9697static inline void snd_leave_user(mm_segment_t fs)98{99 set_fs(fs);100}101102/*103 */104static inline unsigned short snd_seq_file_flags(struct file *file)105{106 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {107 case FMODE_WRITE:108 return SNDRV_SEQ_LFLG_OUTPUT;109 case FMODE_READ:110 return SNDRV_SEQ_LFLG_INPUT;111 default:112 return SNDRV_SEQ_LFLG_OPEN;113 }114}115116static inline int snd_seq_write_pool_allocated(struct snd_seq_client *client)117{118 return snd_seq_total_cells(client->pool) > 0;119}120121/* return pointer to client structure for specified id */122static struct snd_seq_client *clientptr(int clientid)123{124 if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) {125 snd_printd("Seq: oops. Trying to get pointer to client %d\n",126 clientid);127 return NULL;128 }129 return clienttab[clientid];130}131132struct snd_seq_client *snd_seq_client_use_ptr(int clientid)133{134 unsigned long flags;135 struct snd_seq_client *client;136137 if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) {138 snd_printd("Seq: oops. Trying to get pointer to client %d\n",139 clientid);140 return NULL;141 }142 spin_lock_irqsave(&clients_lock, flags);143 client = clientptr(clientid);144 if (client)145 goto __lock;146 if (clienttablock[clientid]) {147 spin_unlock_irqrestore(&clients_lock, flags);148 return NULL;149 }150 spin_unlock_irqrestore(&clients_lock, flags);151#ifdef CONFIG_KMOD152 if (!in_interrupt()) {153 static char client_requested[SNDRV_SEQ_GLOBAL_CLIENTS];154 static char card_requested[SNDRV_CARDS];155 if (clientid < SNDRV_SEQ_GLOBAL_CLIENTS) {156 int idx;157158 if (!client_requested[clientid]) {159 client_requested[clientid] = 1;160 for (idx = 0; idx < 15; idx++) {161 if (seq_client_load[idx] < 0)162 break;163 if (seq_client_load[idx] == clientid) {164 request_module("snd-seq-client-%i",165 clientid);166 break;167 }168 }169 }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);177 }178 snd_seq_device_load_drivers();179 }180 }181 spin_lock_irqsave(&clients_lock, flags);182 client = clientptr(clientid);183 if (client)184 goto __lock;185 spin_unlock_irqrestore(&clients_lock, flags);186 }187#endif188 return NULL;189190 __lock:191 snd_use_lock_use(&client->use_lock);192 spin_unlock_irqrestore(&clients_lock, flags);193 return client;194}195196static void usage_alloc(struct snd_seq_usage *res, int num)197{198 res->cur += num;199 if (res->cur > res->peak)200 res->peak = res->cur;201}202203static void usage_free(struct snd_seq_usage *res, int num)204{205 res->cur -= num;206}207208/* initialise data structures */209int __init client_init_data(void)210{211 /* zap out the client table */212 memset(&clienttablock, 0, sizeof(clienttablock));213 memset(&clienttab, 0, sizeof(clienttab));214 return 0;215}216217218static struct snd_seq_client *seq_create_client1(int client_index, int poolsize)219{220 unsigned long flags;221 int c;222 struct snd_seq_client *client;223224 /* init client data */225 client = kzalloc(sizeof(*client), GFP_KERNEL);226 if (client == NULL)227 return NULL;228 client->pool = snd_seq_pool_new(poolsize);229 if (client->pool == NULL) {230 kfree(client);231 return NULL;232 }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);238239 /* find free slot in the client table */240 spin_lock_irqsave(&clients_lock, flags);241 if (client_index < 0) {242 for (c = SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN;243 c < SNDRV_SEQ_MAX_CLIENTS;244 c++) {245 if (clienttab[c] || clienttablock[c])246 continue;247 clienttab[client->number = c] = client;248 spin_unlock_irqrestore(&clients_lock, flags);249 return client;250 }251 } else {252 if (clienttab[client_index] == NULL && !clienttablock[client_index]) {253 clienttab[client->number = client_index] = client;254 spin_unlock_irqrestore(&clients_lock, flags);255 return client;256 }257 }258 spin_unlock_irqrestore(&clients_lock, flags);259 snd_seq_pool_delete(&client->pool);260 kfree(client);261 return NULL; /* no free slot found or busy, return failure code */262}263264265static int seq_free_client1(struct snd_seq_client *client)266{267 unsigned long flags;268269 snd_assert(client != NULL, return -EINVAL);270 snd_seq_delete_all_ports(client);271 snd_seq_queue_client_leave(client->number);272 spin_lock_irqsave(&clients_lock, flags);273 clienttablock[client->number] = 1;274 clienttab[client->number] = NULL;275 spin_unlock_irqrestore(&clients_lock, flags);276 snd_use_lock_sync(&client->use_lock);277 snd_seq_queue_client_termination(client->number);278 if (client->pool)279 snd_seq_pool_delete(&client->pool);280 spin_lock_irqsave(&clients_lock, flags);281 clienttablock[client->number] = 0;282 spin_unlock_irqrestore(&clients_lock, flags);283 return 0;284}285286287static void seq_free_client(struct snd_seq_client * client)288{289 mutex_lock(®ister_mutex);290 switch (client->type) {291 case NO_CLIENT:292 snd_printk(KERN_WARNING "Seq: Trying to free unused client %d\n",293 client->number);294 break;295 case USER_CLIENT:296 case KERNEL_CLIENT:297 seq_free_client1(client);298 usage_free(&client_usage, 1);299 break;300301 default:302 snd_printk(KERN_ERR "Seq: Trying to free client %d with undefined type = %d\n",303 client->number, client->type);304 }305 mutex_unlock(®ister_mutex);306307 snd_seq_system_client_ev_client_exit(client->number);308}309310311312/* -------------------------------------------------------- */313314/* create a user client */315static int snd_seq_open(struct inode *inode, struct file *file)316{317 int c, mode; /* client id */318 struct snd_seq_client *client;319 struct snd_seq_user_client *user;320321 if (mutex_lock_interruptible(®ister_mutex))322 return -ERESTARTSYS;323 client = seq_create_client1(-1, SNDRV_SEQ_DEFAULT_EVENTS);324 if (client == NULL) {325 mutex_unlock(®ister_mutex);326 return -ENOMEM; /* failure code */327 }328329 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;334335 user = &client->data.user;336 user->fifo = NULL;337 user->fifo_pool_size = 0;338339 if (mode & SNDRV_SEQ_LFLG_INPUT) {340 user->fifo_pool_size = SNDRV_SEQ_DEFAULT_CLIENT_EVENTS;341 user->fifo = snd_seq_fifo_new(user->fifo_pool_size);342 if (user->fifo == NULL) {343 seq_free_client1(client);344 kfree(client);345 mutex_unlock(®ister_mutex);346 return -ENOMEM;347 }348 }349350 usage_alloc(&client_usage, 1);351 client->type = USER_CLIENT;352 mutex_unlock(®ister_mutex);353354 c = client->number;355 file->private_data = client;356357 /* fill client data */358 user->file = file;359 sprintf(client->name, "Client-%d", c);360361 /* make others aware this new client */362 snd_seq_system_client_ev_client_start(c);363364 return 0;365}366367/* delete a user client */368static int snd_seq_release(struct inode *inode, struct file *file)369{370 struct snd_seq_client *client = file->private_data;371372 if (client) {373 seq_free_client(client);374 if (client->data.user.fifo)375 snd_seq_fifo_delete(&client->data.user.fifo);376 kfree(client);377 }378379 return 0;380}381382383/* handle client read() */384/* possible error values:385 * -ENXIO invalid client or file open mode386 * -ENOSPC FIFO overflow (the flag is cleared after this error report)387 * -EINVAL no enough user-space buffer to write the whole event388 * -EFAULT seg. fault during copy to user space389 */390static ssize_t snd_seq_read(struct file *file, char __user *buf, size_t count,391 loff_t *offset)392{393 struct snd_seq_client *client = file->private_data;394 struct snd_seq_fifo *fifo;395 int err;396 long result = 0;397 struct snd_seq_event_cell *cell;398399 if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT))400 return -ENXIO;401402 if (!access_ok(VERIFY_WRITE, buf, count))403 return -EFAULT;404405 /* check client structures are in place */406 snd_assert(client != NULL, return -ENXIO);407408 if (!client->accept_input || (fifo = client->data.user.fifo) == NULL)409 return -ENXIO;410411 if (atomic_read(&fifo->overflow) > 0) {412 /* buffer overflow is detected */413 snd_seq_fifo_clear(fifo);414 /* return error code */415 return -ENOSPC;416 }417418 cell = NULL;419 err = 0;420 snd_seq_fifo_lock(fifo);421422 /* while data available in queue */423 while (count >= sizeof(struct snd_seq_event)) {424 int nonblock;425426 nonblock = (file->f_flags & O_NONBLOCK) || result > 0;427 if ((err = snd_seq_fifo_cell_out(fifo, &cell, nonblock)) < 0) {428 break;429 }430 if (snd_seq_ev_is_variable(&cell->event)) {431 struct snd_seq_event tmpev;432 tmpev = cell->event;433 tmpev.data.ext.len &= ~SNDRV_SEQ_EXT_MASK;434 if (copy_to_user(buf, &tmpev, sizeof(struct snd_seq_event))) {435 err = -EFAULT;436 break;437 }438 count -= sizeof(struct snd_seq_event);439 buf += sizeof(struct snd_seq_event);440 err = snd_seq_expand_var_event(&cell->event, count,441 (char __force *)buf, 0,442 sizeof(struct snd_seq_event));443 if (err < 0)444 break;445 result += err;446 count -= err;447 buf += err;448 } else {449 if (copy_to_user(buf, &cell->event, sizeof(struct snd_seq_event))) {450 err = -EFAULT;451 break;452 }453 count -= sizeof(struct snd_seq_event);454 buf += sizeof(struct snd_seq_event);455 }456 snd_seq_cell_free(cell);457 cell = NULL; /* to be sure */458 result += sizeof(struct snd_seq_event);459 }460461 if (err < 0) {462 if (cell)463 snd_seq_fifo_cell_putback(fifo, cell);464 if (err == -EAGAIN && result > 0)465 err = 0;466 }467 snd_seq_fifo_unlock(fifo);468469 return (err < 0) ? err : result;470}471472473/*474 * check access permission to the port475 */476static int check_port_perm(struct snd_seq_client_port *port, unsigned int flags)477{478 if ((port->capability & flags) != flags)479 return 0;480 return flags;481}482483/*484 * check if the destination client is available, and return the pointer485 * if filter is non-zero, client filter bitmap is tested.486 */487static struct snd_seq_client *get_event_dest_client(struct snd_seq_event *event,488 int filter)489{490 struct snd_seq_client *dest;491492 dest = snd_seq_client_use_ptr(event->dest.client);493 if (dest == NULL)494 return NULL;495 if (! dest->accept_input)496 goto __not_avail;497 if ((dest->filter & SNDRV_SEQ_FILTER_USE_EVENT) &&498 ! test_bit(event->type, dest->event_filter))499 goto __not_avail;500 if (filter && !(dest->filter & filter))501 goto __not_avail;502503 return dest; /* ok - accessible */504__not_avail:505 snd_seq_client_unlock(dest);506 return NULL;507}508509510/*511 * Return the error event.512 *513 * If the receiver client is a user client, the original event is514 * encapsulated in SNDRV_SEQ_EVENT_BOUNCE as variable length event. If515 * the original event is also variable length, the external data is516 * copied after the event record.517 * If the receiver client is a kernel client, the original event is518 * quoted in SNDRV_SEQ_EVENT_KERNEL_ERROR, since this requires no extra519 * kmalloc.520 */521static int bounce_error_event(struct snd_seq_client *client,522 struct snd_seq_event *event,523 int err, int atomic, int hop)524{525 struct snd_seq_event bounce_ev;526 int result;527528 if (client == NULL ||529 ! (client->filter & SNDRV_SEQ_FILTER_BOUNCE) ||530 ! client->accept_input)531 return 0; /* ignored */532533 /* set up quoted error */534 memset(&bounce_ev, 0, sizeof(bounce_ev));535 bounce_ev.type = SNDRV_SEQ_EVENT_KERNEL_ERROR;536 bounce_ev.flags = SNDRV_SEQ_EVENT_LENGTH_FIXED;537 bounce_ev.queue = SNDRV_SEQ_QUEUE_DIRECT;538 bounce_ev.source.client = SNDRV_SEQ_CLIENT_SYSTEM;539 bounce_ev.source.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;540 bounce_ev.dest.client = client->number;541 bounce_ev.dest.port = event->source.port;542 bounce_ev.data.quote.origin = event->dest;543 bounce_ev.data.quote.event = event;544 bounce_ev.data.quote.value = -err; /* use positive value */545 result = snd_seq_deliver_single_event(NULL, &bounce_ev, 0, atomic, hop + 1);546 if (result < 0) {547 client->event_lost++;548 return result;549 }550551 return result;552}553554555/*556 * rewrite the time-stamp of the event record with the curren time557 * of the given queue.558 * return non-zero if updated.559 */560static int update_timestamp_of_queue(struct snd_seq_event *event,561 int queue, int real_time)562{563 struct snd_seq_queue *q;564565 q = queueptr(queue);566 if (! q)567 return 0;568 event->queue = queue;569 event->flags &= ~SNDRV_SEQ_TIME_STAMP_MASK;570 if (real_time) {571 event->time.time = snd_seq_timer_get_cur_time(q->timer);572 event->flags |= SNDRV_SEQ_TIME_STAMP_REAL;573 } else {574 event->time.tick = snd_seq_timer_get_cur_tick(q->timer);575 event->flags |= SNDRV_SEQ_TIME_STAMP_TICK;576 }577 queuefree(q);578 return 1;579}580581582/*583 * deliver an event to the specified destination.584 * if filter is non-zero, client filter bitmap is tested.585 *586 * RETURN VALUE: 0 : if succeeded587 * <0 : error588 */589static int snd_seq_deliver_single_event(struct snd_seq_client *client,590 struct snd_seq_event *event,591 int filter, int atomic, int hop)592{593 struct snd_seq_client *dest = NULL;594 struct snd_seq_client_port *dest_port = NULL;595 int result = -ENOENT;596 int direct;597598 direct = snd_seq_ev_is_direct(event);599600 dest = get_event_dest_client(event, filter);601 if (dest == NULL)602 goto __skip;603 dest_port = snd_seq_port_use_ptr(dest, event->dest.port);604 if (dest_port == NULL)605 goto __skip;606607 /* check permission */608 if (! check_port_perm(dest_port, SNDRV_SEQ_PORT_CAP_WRITE)) {609 result = -EPERM;610 goto __skip;611 }612613 if (dest_port->timestamping)614 update_timestamp_of_queue(event, dest_port->time_queue,615 dest_port->time_real);616617 switch (dest->type) {618 case USER_CLIENT:619 if (dest->data.user.fifo)620 result = snd_seq_fifo_event_in(dest->data.user.fifo, event);621 break;622623 case KERNEL_CLIENT:624 if (dest_port->event_input == NULL)625 break;626 result = dest_port->event_input(event, direct,627 dest_port->private_data,628 atomic, hop);629 break;630 default:631 break;632 }633634 __skip:635 if (dest_port)636 snd_seq_port_unlock(dest_port);637 if (dest)638 snd_seq_client_unlock(dest);639640 if (result < 0 && !direct) {641 result = bounce_error_event(client, event, result, atomic, hop);642 }643 return result;644}645646647/*648 * send the event to all subscribers:649 */650static int deliver_to_subscribers(struct snd_seq_client *client,651 struct snd_seq_event *event,652 int atomic, int hop)653{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;659660 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;666667 /* lock list */668 if (atomic)669 read_lock(&grp->list_lock);670 else671 down_read(&grp->list_mutex);672 list_for_each_entry(subs, &grp->list_head, src_list) {673 event->dest = subs->info.dest;674 if (subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)675 /* convert time according to flag with subscription */676 update_timestamp_of_queue(event, subs->info.queue,677 subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL);678 err = snd_seq_deliver_single_event(client, event,679 0, atomic, hop);680 if (err < 0)681 break;682 num_ev++;683 /* restore original event record */684 *event = event_saved;685 }686 if (atomic)687 read_unlock(&grp->list_lock);688 else689 up_read(&grp->list_mutex);690 *event = event_saved; /* restore */691 snd_seq_port_unlock(src_port);692 return (err < 0) ? err : num_ev;693}694695696#ifdef SUPPORT_BROADCAST697/*698 * broadcast to all ports:699 */700static int port_broadcast_event(struct snd_seq_client *client,701 struct snd_seq_event *event,702 int atomic, int hop)703{704 int num_ev = 0, err = 0;705 struct snd_seq_client *dest_client;706 struct snd_seq_client_port *port;707708 dest_client = get_event_dest_client(event, SNDRV_SEQ_FILTER_BROADCAST);709 if (dest_client == NULL)710 return 0; /* no matching destination */711712 read_lock(&dest_client->ports_lock);713 list_for_each_entry(port, &dest_client->ports_list_head, list) {714 event->dest.port = port->addr.port;715 /* pass NULL as source client to avoid error bounce */716 err = snd_seq_deliver_single_event(NULL, event,717 SNDRV_SEQ_FILTER_BROADCAST,718 atomic, hop);719 if (err < 0)720 break;721 num_ev++;722 }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;727}728729/*730 * send the event to all clients:731 * if destination port is also ADDRESS_BROADCAST, deliver to all ports.732 */733static int broadcast_event(struct snd_seq_client *client,734 struct snd_seq_event *event, int atomic, int hop)735{736 int err = 0, num_ev = 0;737 int dest;738 struct snd_seq_addr addr;739740 addr = event->dest; /* save */741742 for (dest = 0; dest < SNDRV_SEQ_MAX_CLIENTS; dest++) {743 /* don't send to itself */744 if (dest == client->number)745 continue;746 event->dest.client = dest;747 event->dest.port = addr.port;748 if (addr.port == SNDRV_SEQ_ADDRESS_BROADCAST)749 err = port_broadcast_event(client, event, atomic, hop);750 else751 /* pass NULL as source client to avoid error bounce */752 err = snd_seq_deliver_single_event(NULL, event,753 SNDRV_SEQ_FILTER_BROADCAST,754 atomic, hop);755 if (err < 0)756 break;757 num_ev += err;758 }759 event->dest = addr; /* restore */760 return (err < 0) ? err : num_ev;761}762763764/* multicast - not supported yet */765static int multicast_event(struct snd_seq_client *client, struct snd_seq_event *event,766 int atomic, int hop)767{768 snd_printd("seq: multicast not supported yet.\n");769 return 0; /* ignored */770}771#endif /* SUPPORT_BROADCAST */772773774/* deliver an event to the destination port(s).775 * if the event is to subscribers or broadcast, the event is dispatched776 * to multiple targets.777 *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.781 */782static int snd_seq_deliver_event(struct snd_seq_client *client, struct snd_seq_event *event,783 int atomic, int hop)784{785 int result;786787 hop++;788 if (hop >= SNDRV_SEQ_MAX_HOPS) {789 snd_printd("too long delivery path (%d:%d->%d:%d)\n",790 event->source.client, event->source.port,791 event->dest.client, event->dest.port);792 return -EMLINK;793 }794795 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_BROADCAST799 else if (event->queue == SNDRV_SEQ_ADDRESS_BROADCAST ||800 event->dest.client == SNDRV_SEQ_ADDRESS_BROADCAST)801 result = broadcast_event(client, event, atomic, hop);802 else if (event->dest.client >= SNDRV_SEQ_MAX_CLIENTS)803 result = multicast_event(client, event, atomic, hop);804 else if (event->dest.port == SNDRV_SEQ_ADDRESS_BROADCAST)805 result = port_broadcast_event(client, event, atomic, hop);806#endif807 else808 result = snd_seq_deliver_single_event(client, event, 0, atomic, hop);809810 return result;811}812813/*814 * dispatch an event cell:815 * This function is called only from queue check routines in timer816 * interrupts or after enqueued.817 * The event cell shall be released or re-queued in this function.818 *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.822 */823int snd_seq_dispatch_event(struct snd_seq_event_cell *cell, int atomic, int hop)824{825 struct snd_seq_client *client;826 int result;827828 snd_assert(cell != NULL, return -EINVAL);829830 client = snd_seq_client_use_ptr(cell->event.source.client);831 if (client == NULL) {832 snd_seq_cell_free(cell); /* release this cell */833 return -EINVAL;834 }835836 if (cell->event.type == SNDRV_SEQ_EVENT_NOTE) {837 /* NOTE event:838 * the event cell is re-used as a NOTE-OFF event and839 * enqueued again.840 */841 struct snd_seq_event tmpev, *ev;842843 /* reserve this event to enqueue note-off later */844 tmpev = cell->event;845 tmpev.type = SNDRV_SEQ_EVENT_NOTEON;846 result = snd_seq_deliver_event(client, &tmpev, atomic, hop);847848 /*849 * This was originally a note event. We now re-use the850 * cell for the note-off event.851 */852853 ev = &cell->event;854 ev->type = SNDRV_SEQ_EVENT_NOTEOFF;855 ev->flags |= SNDRV_SEQ_PRIORITY_HIGH;856857 /* add the duration time */858 switch (ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) {859 case SNDRV_SEQ_TIME_STAMP_TICK:860 ev->time.tick += ev->data.note.duration;861 break;862 case SNDRV_SEQ_TIME_STAMP_REAL:863 /* unit for duration is ms */864 ev->time.time.tv_nsec += 1000000 * (ev->data.note.duration % 1000);865 ev->time.time.tv_sec += ev->data.note.duration / 1000 +866 ev->time.time.tv_nsec / 1000000000;867 ev->time.time.tv_nsec %= 1000000000;868 break;869 }870 ev->data.note.velocity = ev->data.note.off_velocity;871872 /* 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 */875876 } else {877 /* Normal events:878 * event cell is freed after processing the event879 */880881 result = snd_seq_deliver_event(client, &cell->event, atomic, hop);882 snd_seq_cell_free(cell);883 }884885 snd_seq_client_unlock(client);886 return result;887}888889890/* 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 is892 * available.893 */894static int snd_seq_client_enqueue_event(struct snd_seq_client *client,895 struct snd_seq_event *event,896 struct file *file, int blocking,897 int atomic, int hop)898{899 struct snd_seq_event_cell *cell;900 int err;901902 /* special queue values - force direct passing */903 if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) {904 event->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;905 event->queue = SNDRV_SEQ_QUEUE_DIRECT;906 } else907#ifdef SUPPORT_BROADCAST908 if (event->queue == SNDRV_SEQ_ADDRESS_BROADCAST) {909 event->dest.client = SNDRV_SEQ_ADDRESS_BROADCAST;910 event->queue = SNDRV_SEQ_QUEUE_DIRECT;911 }912#endif913 if (event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) {914 /* check presence of source port */915 struct snd_seq_client_port *src_port = snd_seq_port_use_ptr(client, event->source.port);916 if (src_port == NULL)917 return -EINVAL;918 snd_seq_port_unlock(src_port);919 }920921 /* 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);926 }927928 /* 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 */933934 /* allocate an event cell */935 err = snd_seq_event_dup(client->pool, event, &cell, !blocking || atomic, file);936 if (err < 0)937 return err;938939 /* we got a cell. enqueue it. */940 if ((err = snd_seq_enqueue_event(cell, atomic, hop)) < 0) {941 snd_seq_cell_free(cell);942 return err;943 }944945 return 0;946}947948949/*950 * check validity of event type and data length.951 * return non-zero if invalid.952 */953static int check_event_type_and_length(struct snd_seq_event *ev)954{955 switch (snd_seq_ev_length_type(ev)) {956 case SNDRV_SEQ_EVENT_LENGTH_FIXED:957 if (snd_seq_ev_is_variable_type(ev))958 return -EINVAL;959 break;960 case SNDRV_SEQ_EVENT_LENGTH_VARIABLE:961 if (! snd_seq_ev_is_variable_type(ev) ||962 (ev->data.ext.len & ~SNDRV_SEQ_EXT_MASK) >= SNDRV_SEQ_MAX_EVENT_LEN)963 return -EINVAL;964 break;965 case SNDRV_SEQ_EVENT_LENGTH_VARUSR:966 if (! snd_seq_ev_is_direct(ev))967 return -EINVAL;968 break;969 }970 return 0;971}972973974/* handle write() */975/* possible error values:976 * -ENXIO invalid client or file open mode977 * -ENOMEM malloc failed978 * -EFAULT seg. fault during copy from user space979 * -EINVAL invalid event980 * -EAGAIN no space in output pool981 * -EINTR interrupts while sleep982 * -EMLINK too many hops983 * others depends on return value from driver callback984 */985static ssize_t snd_seq_write(struct file *file, const char __user *buf,986 size_t count, loff_t *offset)987{988 struct snd_seq_client *client = file->private_data;989 int written = 0, len;990 int err = -EINVAL;991 struct snd_seq_event event;992993 if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT))994 return -ENXIO;995996 /* check client structures are in place */997 snd_assert(client != NULL, return -ENXIO);998999 if (!client->accept_output || client->pool == NULL)1000 return -ENXIO;10011002 /* allocate the pool now if the pool is not allocated yet */1003 if (client->pool->size > 0 && !snd_seq_write_pool_allocated(client)) {1004 if (snd_seq_pool_init(client->pool) < 0)1005 return -ENOMEM;1006 }10071008 /* only process whole events */1009 while (count >= sizeof(struct snd_seq_event)) {1010 /* Read in the event header from the user */1011 len = sizeof(event);1012 if (copy_from_user(&event, buf, len)) {1013 err = -EFAULT;1014 break;1015 }1016 event.source.client = client->number; /* fill in client number */1017 /* Check for extension data length */1018 if (check_event_type_and_length(&event)) {1019 err = -EINVAL;1020 break;1021 }10221023 /* check for special events */1024 if (event.type == SNDRV_SEQ_EVENT_NONE)1025 goto __skip_event;1026 else if (snd_seq_ev_is_reserved(&event)) {1027 err = -EINVAL;1028 break;1029 }10301031 if (snd_seq_ev_is_variable(&event)) {1032 int extlen = event.data.ext.len & ~SNDRV_SEQ_EXT_MASK;1033 if ((size_t)(extlen + len) > count) {1034 /* back out, will get an error this time or next */1035 err = -EINVAL;1036 break;1037 }1038 /* set user space pointer */1039 event.data.ext.len = extlen | SNDRV_SEQ_EXT_USRPTR;1040 event.data.ext.ptr = (char __force *)buf1041 + sizeof(struct snd_seq_event);1042 len += extlen; /* increment data length */1043 } else {1044#ifdef CONFIG_COMPAT1045 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;1048 }1049#endif1050 }10511052 /* ok, enqueue it */1053 err = snd_seq_client_enqueue_event(client, &event, file,1054 !(file->f_flags & O_NONBLOCK),1055 0, 0);1056 if (err < 0)1057 break;10581059 __skip_event:1060 /* Update pointers and counts */1061 count -= len;1062 buf += len;1063 written += len;1064 }10651066 return written ? written : err;1067}106810691070/*1071 * handle polling1072 */1073static unsigned int snd_seq_poll(struct file *file, poll_table * wait)1074{1075 struct snd_seq_client *client = file->private_data;1076 unsigned int mask = 0;10771078 /* check client structures are in place */1079 snd_assert(client != NULL, return -ENXIO);10801081 if ((snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT) &&1082 client->data.user.fifo) {10831084 /* 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;1087 }10881089 if (snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT) {10901091 /* 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;1095 }10961097 return mask;1098}109911001101/*-----------------------------------------------------*/110211031104/* SYSTEM_INFO ioctl() */1105static int snd_seq_ioctl_system_info(struct snd_seq_client *client, void __user *arg)1106{1107 struct snd_seq_system_info info;11081109 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();11171118 if (copy_to_user(arg, &info, sizeof(info)))1119 return -EFAULT;1120 return 0;1121}112211231124/* RUNNING_MODE ioctl() */1125static int snd_seq_ioctl_running_mode(struct snd_seq_client *client, void __user *arg)1126{1127 struct snd_seq_running_info info;1128 struct snd_seq_client *cptr;1129 int err = 0;11301131 if (copy_from_user(&info, arg, sizeof(info)))1132 return -EFAULT;11331134 /* requested client number */1135 cptr = snd_seq_client_use_ptr(info.client);1136 if (cptr == NULL)1137 return -ENOENT; /* don't change !!! */11381139#ifdef SNDRV_BIG_ENDIAN1140 if (! info.big_endian) {1141 err = -EINVAL;1142 goto __err;1143 }1144#else1145 if (info.big_endian) {1146 err = -EINVAL;1147 goto __err;1148 }11491150#endif1151 if (info.cpu_mode > sizeof(long)) {1152 err = -EINVAL;1153 goto __err;1154 }1155 cptr->convert32 = (info.cpu_mode < sizeof(long));1156 __err:1157 snd_seq_client_unlock(cptr);1158 return err;1159}11601161/* CLIENT_INFO ioctl() */1162static void get_client_info(struct snd_seq_client *cptr,1163 struct snd_seq_client_info *info)1164{1165 info->client = cptr->number;11661167 /* 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));1175}11761177static int snd_seq_ioctl_get_client_info(struct snd_seq_client *client,1178 void __user *arg)1179{1180 struct snd_seq_client *cptr;1181 struct snd_seq_client_info client_info;11821183 if (copy_from_user(&client_info, arg, sizeof(client_info)))1184 return -EFAULT;11851186 /* requested client number */1187 cptr = snd_seq_client_use_ptr(client_info.client);1188 if (cptr == NULL)1189 return -ENOENT; /* don't change !!! */11901191 get_client_info(cptr, &client_info);1192 snd_seq_client_unlock(cptr);11931194 if (copy_to_user(arg, &client_info, sizeof(client_info)))1195 return -EFAULT;1196 return 0;1197}119811991200/* CLIENT_INFO ioctl() */1201static int snd_seq_ioctl_set_client_info(struct snd_seq_client *client,1202 void __user *arg)1203{1204 struct snd_seq_client_info client_info;12051206 if (copy_from_user(&client_info, arg, sizeof(client_info)))1207 return -EFAULT;12081209 /* it is not allowed to set the info fields for an another client */1210 if (client->number != client_info.client)1211 return -EPERM;1212 /* also client type must be set now */1213 if (client->type != client_info.type)1214 return -EINVAL;12151216 /* fill the info fields */1217 if (client_info.name[0])1218 strlcpy(client->name, client_info.name, sizeof(client->name));12191220 client->filter = client_info.filter;1221 client->event_lost = client_info.event_lost;1222 memcpy(client->event_filter, client_info.event_filter, 32);12231224 return 0;1225}122612271228/*1229 * CREATE PORT ioctl()1230 */1231static int snd_seq_ioctl_create_port(struct snd_seq_client *client,1232 void __user *arg)1233{1234 struct snd_seq_client_port *port;1235 struct snd_seq_port_info info;1236 struct snd_seq_port_callback *callback;12371238 if (copy_from_user(&info, arg, sizeof(info)))1239 return -EFAULT;12401241 /* it is not allowed to create the port for an another client */1242 if (info.addr.client != client->number)1243 return -EPERM;12441245 port = snd_seq_create_port(client, (info.flags & SNDRV_SEQ_PORT_FLG_GIVEN_PORT) ? info.addr.port : -1);1246 if (port == NULL)1247 return -ENOMEM;12481249 if (client->type == USER_CLIENT && info.kernel) {1250 snd_seq_delete_port(client, port->addr.port);1251 return -EINVAL;1252 }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;1265 }1266 }12671268 info.addr = port->addr;12691270 snd_seq_set_port_info(port, &info);1271 snd_seq_system_client_ev_port_start(port->addr.client, port->addr.port);12721273 if (copy_to_user(arg, &info, sizeof(info)))1274 return -EFAULT;12751276 return 0;1277}12781279/*1280 * DELETE PORT ioctl()1281 */1282static int snd_seq_ioctl_delete_port(struct snd_seq_client *client,1283 void __user *arg)1284{1285 struct snd_seq_port_info info;1286 int err;12871288 /* set passed parameters */1289 if (copy_from_user(&info, arg, sizeof(info)))1290 return -EFAULT;12911292 /* it is not allowed to remove the port for an another client */1293 if (info.addr.client != client->number)1294 return -EPERM;12951296 err = snd_seq_delete_port(client, info.addr.port);1297 if (err >= 0)1298 snd_seq_system_client_ev_port_exit(client->number, info.addr.port);1299 return err;1300}130113021303/*1304 * GET_PORT_INFO ioctl() (on any client)1305 */1306static int snd_seq_ioctl_get_port_info(struct snd_seq_client *client,1307 void __user *arg)1308{1309 struct snd_seq_client *cptr;1310 struct snd_seq_client_port *port;1311 struct snd_seq_port_info info;13121313 if (copy_from_user(&info, arg, sizeof(info)))1314 return -EFAULT;1315 cptr = snd_seq_client_use_ptr(info.addr.client);1316 if (cptr == NULL)1317 return -ENXIO;13181319 port = snd_seq_port_use_ptr(cptr, info.addr.port);1320 if (port == NULL) {1321 snd_seq_client_unlock(cptr);1322 return -ENOENT; /* don't change */1323 }13241325 /* get port info */1326 snd_seq_get_port_info(port, &info);1327 snd_seq_port_unlock(port);1328 snd_seq_client_unlock(cptr);13291330 if (copy_to_user(arg, &info, sizeof(info)))1331 return -EFAULT;1332 return 0;1333}133413351336/*1337 * SET_PORT_INFO ioctl() (only ports on this/own client)1338 */1339static int snd_seq_ioctl_set_port_info(struct snd_seq_client *client,1340 void __user *arg)1341{1342 struct snd_seq_client_port *port;1343 struct snd_seq_port_info info;13441345 if (copy_from_user(&info, arg, sizeof(info)))1346 return -EFAULT;13471348 if (info.addr.client != client->number) /* only set our own ports ! */1349 return -EPERM;1350 port = snd_seq_port_use_ptr(client, info.addr.port);1351 if (port) {1352 snd_seq_set_port_info(port, &info);1353 snd_seq_port_unlock(port);1354 }1355 return 0;1356}135713581359/*1360 * port subscription (connection)1361 */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)13641365static 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)1369{1370 if (client->number != subs->sender.client &&1371 client->number != subs->dest.client) {1372 /* connection by third client - check export permission */1373 if (check_port_perm(sport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))1374 return -EPERM;1375 if (check_port_perm(dport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))1376 return -EPERM;1377 }13781379 /* check read permission */1380 /* if sender or receiver is the subscribing client itself,1381 * no permission check is necessary1382 */1383 if (client->number != subs->sender.client) {1384 if (! check_port_perm(sport, PERM_RD))1385 return -EPERM;1386 }1387 /* check write permission */1388 if (client->number != subs->dest.client) {1389 if (! check_port_perm(dport, PERM_WR))1390 return -EPERM;1391 }1392 return 0;1393}13941395/*1396 * send an subscription notify event to user client:1397 * client must be user client.1398 */1399int snd_seq_client_notify_subscription(int client, int port,1400 struct snd_seq_port_subscribe *info,1401 int evtype)1402{1403 struct snd_seq_event event;14041405 memset(&event, 0, sizeof(event));1406 event.type = evtype;1407 event.data.connect.dest = info->dest;1408 event.data.connect.sender = info->sender;14091410 return snd_seq_system_notify(client, port, &event); /* non-atomic */1411}141214131414/*1415 * add to port's subscription list IOCTL interface1416 */1417static int snd_seq_ioctl_subscribe_port(struct snd_seq_client *client,1418 void __user *arg)1419{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;14241425 if (copy_from_user(&subs, arg, sizeof(subs)))1426 return -EFAULT;14271428 if ((receiver = snd_seq_client_use_ptr(subs.dest.client)) == NULL)1429 goto __end;1430 if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)1431 goto __end;1432 if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)1433 goto __end;1434 if ((dport = snd_seq_port_use_ptr(receiver, subs.dest.port)) == NULL)1435 goto __end;14361437 result = check_subscription_permission(client, sport, dport, &subs);1438 if (result < 0)1439 goto __end;14401441 /* connect them */1442 result = snd_seq_port_connect(client, sender, sport, receiver, dport, &subs);1443 if (! result) /* broadcast announce */1444 snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,1445 &subs, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);1446 __end:1447 if (sport)1448 snd_seq_port_unlock(sport);1449 if (dport)1450 snd_seq_port_unlock(dport);1451 if (sender)1452 snd_seq_client_unlock(sender);1453 if (receiver)1454 snd_seq_client_unlock(receiver);1455 return result;1456}145714581459/*1460 * remove from port's subscription list1461 */1462static int snd_seq_ioctl_unsubscribe_port(struct snd_seq_client *client,1463 void __user *arg)1464{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;14691470 if (copy_from_user(&subs, arg, sizeof(subs)))1471 return -EFAULT;14721473 if ((receiver = snd_seq_client_use_ptr(subs.dest.client)) == NULL)1474 goto __end;1475 if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)1476 goto __end;1477 if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)1478 goto __end;1479 if ((dport = snd_seq_port_use_ptr(receiver, subs.dest.port)) == NULL)1480 goto __end;14811482 result = check_subscription_permission(client, sport, dport, &subs);1483 if (result < 0)1484 goto __end;14851486 result = snd_seq_port_disconnect(client, sender, sport, receiver, dport, &subs);1487 if (! result) /* broadcast announce */1488 snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,1489 &subs, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);1490 __end:1491 if (sport)1492 snd_seq_port_unlock(sport);1493 if (dport)1494 snd_seq_port_unlock(dport);1495 if (sender)1496 snd_seq_client_unlock(sender);1497 if (receiver)1498 snd_seq_client_unlock(receiver);1499 return result;1500}150115021503/* CREATE_QUEUE ioctl() */1504static int snd_seq_ioctl_create_queue(struct snd_seq_client *client,1505 void __user *arg)1506{1507 struct snd_seq_queue_info info;1508 int result;1509 struct snd_seq_queue *q;15101511 if (copy_from_user(&info, arg, sizeof(info)))1512 return -EFAULT;15131514 result = snd_seq_queue_alloc(client->number, info.locked, info.flags);1515 if (result < 0)1516 return result;15171518 q = queueptr(result);1519 if (q == NULL)1520 return -EINVAL;15211522 info.queue = q->queue;1523 info.locked = q->locked;1524 info.owner = q->owner;15251526 /* set queue name */1527 if (! info.name[0])1528 snprintf(info.name, sizeof(info.name), "Queue-%d", q->queue);1529 strlcpy(q->name, info.name, sizeof(q->name));1530 queuefree(q);15311532 if (copy_to_user(arg, &info, sizeof(info)))1533 return -EFAULT;15341535 return 0;1536}15371538/* DELETE_QUEUE ioctl() */1539static int snd_seq_ioctl_delete_queue(struct snd_seq_client *client,1540 void __user *arg)1541{1542 struct snd_seq_queue_info info;15431544 if (copy_from_user(&info, arg, sizeof(info)))1545 return -EFAULT;15461547 return snd_seq_queue_delete(client->number, info.queue);1548}15491550/* GET_QUEUE_INFO ioctl() */1551static int snd_seq_ioctl_get_queue_info(struct snd_seq_client *client,1552 void __user *arg)1553{1554 struct snd_seq_queue_info info;1555 struct snd_seq_queue *q;15561557 if (copy_from_user(&info, arg, sizeof(info)))1558 return -EFAULT;15591560 q = queueptr(info.queue);1561 if (q == NULL)1562 return -EINVAL;15631564 memset(&info, 0, sizeof(info));1565 info.queue = q->queue;1566 info.owner = q->owner;1567 info.locked = q->locked;1568 strlcpy(info.name, q->name, sizeof(info.name));1569 queuefree(q);15701571 if (copy_to_user(arg, &info, sizeof(info)))1572 return -EFAULT;15731574 return 0;1575}15761577/* SET_QUEUE_INFO ioctl() */1578static int snd_seq_ioctl_set_queue_info(struct snd_seq_client *client,1579 void __user *arg)1580{1581 struct snd_seq_queue_info info;1582 struct snd_seq_queue *q;15831584 if (copy_from_user(&info, arg, sizeof(info)))1585 return -EFAULT;15861587 if (info.owner != client->number)1588 return -EINVAL;15891590 /* change owner/locked permission */1591 if (snd_seq_queue_check_access(info.queue, client->number)) {1592 if (snd_seq_queue_set_owner(info.queue, client->number, info.locked) < 0)1593 return -EPERM;1594 if (info.locked)1595 snd_seq_queue_use(info.queue, client->number, 1);1596 } else {1597 return -EPERM;1598 }15991600 q = queueptr(info.queue);1601 if (! q)1602 return -EINVAL;1603 if (q->owner != client->number) {1604 queuefree(q);1605 return -EPERM;1606 }1607 strlcpy(q->name, info.name, sizeof(q->name));1608 queuefree(q);16091610 return 0;1611}16121613/* GET_NAMED_QUEUE ioctl() */1614static int snd_seq_ioctl_get_named_queue(struct snd_seq_client *client, void __user *arg)1615{1616 struct snd_seq_queue_info info;1617 struct snd_seq_queue *q;16181619 if (copy_from_user(&info, arg, sizeof(info)))1620 return -EFAULT;16211622 q = snd_seq_queue_find_name(info.name);1623 if (q == NULL)1624 return -EINVAL;1625 info.queue = q->queue;1626 info.owner = q->owner;1627 info.locked = q->locked;1628 queuefree(q);16291630 if (copy_to_user(arg, &info, sizeof(info)))1631 return -EFAULT;16321633 return 0;1634}16351636/* GET_QUEUE_STATUS ioctl() */1637static int snd_seq_ioctl_get_queue_status(struct snd_seq_client *client,1638 void __user *arg)1639{1640 struct snd_seq_queue_status status;1641 struct snd_seq_queue *queue;1642 struct snd_seq_timer *tmr;16431644 if (copy_from_user(&status, arg, sizeof(status)))1645 return -EFAULT;16461647 queue = queueptr(status.queue);1648 if (queue == NULL)1649 return -EINVAL;1650 memset(&status, 0, sizeof(status));1651 status.queue = queue->queue;16521653 tmr = queue->timer;1654 status.events = queue->tickq->cells + queue->timeq->cells;16551656 status.time = snd_seq_timer_get_cur_time(tmr);1657 status.tick = snd_seq_timer_get_cur_tick(tmr);16581659 status.running = tmr->running;16601661 status.flags = queue->flags;1662 queuefree(queue);16631664 if (copy_to_user(arg, &status, sizeof(status)))1665 return -EFAULT;1666 return 0;1667}166816691670/* GET_QUEUE_TEMPO ioctl() */1671static int snd_seq_ioctl_get_queue_tempo(struct snd_seq_client *client,1672 void __user *arg)1673{1674 struct snd_seq_queue_tempo tempo;1675 struct snd_seq_queue *queue;1676 struct snd_seq_timer *tmr;16771678 if (copy_from_user(&tempo, arg, sizeof(tempo)))1679 return -EFAULT;16801681 queue = queueptr(tempo.queue);1682 if (queue == NULL)1683 return -EINVAL;1684 memset(&tempo, 0, sizeof(tempo));1685 tempo.queue = queue->queue;16861687 tmr = queue->timer;16881689 tempo.tempo = tmr->tempo;1690 tempo.ppq = tmr->ppq;1691 tempo.skew_value = tmr->skew;1692 tempo.skew_base = tmr->skew_base;1693 queuefree(queue);16941695 if (copy_to_user(arg, &tempo, sizeof(tempo)))1696 return -EFAULT;1697 return 0;1698}169917001701/* SET_QUEUE_TEMPO ioctl() */1702int snd_seq_set_queue_tempo(int client, struct snd_seq_queue_tempo *tempo)1703{1704 if (!snd_seq_queue_check_access(tempo->queue, client))1705 return -EPERM;1706 return snd_seq_queue_timer_set_tempo(tempo->queue, client, tempo);1707}17081709EXPORT_SYMBOL(snd_seq_set_queue_tempo);17101711static int snd_seq_ioctl_set_queue_tempo(struct snd_seq_client *client,1712 void __user *arg)1713{1714 int result;1715 struct snd_seq_queue_tempo tempo;17161717 if (copy_from_user(&tempo, arg, sizeof(tempo)))1718 return -EFAULT;17191720 result = snd_seq_set_queue_tempo(client->number, &tempo);1721 return result < 0 ? result : 0;1722}172317241725/* GET_QUEUE_TIMER ioctl() */1726static int snd_seq_ioctl_get_queue_timer(struct snd_seq_client *client,1727 void __user *arg)1728{1729 struct snd_seq_queue_timer timer;1730 struct snd_seq_queue *queue;1731 struct snd_seq_timer *tmr;17321733 if (copy_from_user(&timer, arg, sizeof(timer)))1734 return -EFAULT;17351736 queue = queueptr(timer.queue);1737 if (queue == NULL)1738 return -EINVAL;17391740 if (mutex_lock_interruptible(&queue->timer_mutex)) {1741 queuefree(queue);1742 return -ERESTARTSYS;1743 }1744 tmr = queue->timer;1745 memset(&timer, 0, sizeof(timer));1746 timer.queue = queue->queue;17471748 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;1752 }1753 mutex_unlock(&queue->timer_mutex);1754 queuefree(queue);17551756 if (copy_to_user(arg, &timer, sizeof(timer)))1757 return -EFAULT;1758 return 0;1759}176017611762/* SET_QUEUE_TIMER ioctl() */1763static int snd_seq_ioctl_set_queue_timer(struct snd_seq_client *client,1764 void __user *arg)1765{1766 int result = 0;1767 struct snd_seq_queue_timer timer;17681769 if (copy_from_user(&timer, arg, sizeof(timer)))1770 return -EFAULT;17711772 if (timer.type != SNDRV_SEQ_TIMER_ALSA)1773 return -EINVAL;17741775 if (snd_seq_queue_check_access(timer.queue, client->number)) {1776 struct snd_seq_queue *q;1777 struct snd_seq_timer *tmr;17781779 q = queueptr(timer.queue);1780 if (q == NULL)1781 return -ENXIO;1782 if (mutex_lock_interruptible(&q->timer_mutex)) {1783 queuefree(q);1784 return -ERESTARTSYS;1785 }1786 tmr = q->timer;1787 snd_seq_queue_timer_close(timer.queue);1788 tmr->type = timer.type;1789 if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {1790 tmr->alsa_id = timer.u.alsa.id;1791 tmr->preferred_resolution = timer.u.alsa.resolution;1792 }1793 result = snd_seq_queue_timer_open(timer.queue);1794 mutex_unlock(&q->timer_mutex);1795 queuefree(q);1796 } else {1797 return -EPERM;1798 }17991800 return result;1801}180218031804/* GET_QUEUE_CLIENT ioctl() */1805static int snd_seq_ioctl_get_queue_client(struct snd_seq_client *client,1806 void __user *arg)1807{1808 struct snd_seq_queue_client info;1809 int used;18101811 if (copy_from_user(&info, arg, sizeof(info)))1812 return -EFAULT;18131814 used = snd_seq_queue_is_used(info.queue, client->number);1815 if (used < 0)1816 return -EINVAL;1817 info.used = used;1818 info.client = client->number;18191820 if (copy_to_user(arg, &info, sizeof(info)))1821 return -EFAULT;1822 return 0;1823}182418251826/* SET_QUEUE_CLIENT ioctl() */1827static int snd_seq_ioctl_set_queue_client(struct snd_seq_client *client,1828 void __user *arg)1829{1830 int err;1831 struct snd_seq_queue_client info;18321833 if (copy_from_user(&info, arg, sizeof(info)))1834 return -EFAULT;18351836 if (info.used >= 0) {1837 err = snd_seq_queue_use(info.queue, client->number, info.used);1838 if (err < 0)1839 return err;1840 }18411842 return snd_seq_ioctl_get_queue_client(client, arg);1843}184418451846/* GET_CLIENT_POOL ioctl() */1847static int snd_seq_ioctl_get_client_pool(struct snd_seq_client *client,1848 void __user *arg)1849{1850 struct snd_seq_client_pool info;1851 struct snd_seq_client *cptr;18521853 if (copy_from_user(&info, arg, sizeof(info)))1854 return -EFAULT;18551856 cptr = snd_seq_client_use_ptr(info.client);1857 if (cptr == NULL)1858 return -ENOENT;1859 memset(&info, 0, sizeof(info));1860 info.output_pool = cptr->pool->size;1861 info.output_room = cptr->pool->room;1862 info.output_free = info.output_pool;1863 info.output_free = snd_seq_unused_cells(cptr->pool);1864 if (cptr->type == USER_CLIENT) {1865 info.input_pool = cptr->data.user.fifo_pool_size;1866 info.input_free = info.input_pool;1867 if (cptr->data.user.fifo)1868 info.input_free = snd_seq_unused_cells(cptr->data.user.fifo->pool);1869 } else {1870 info.input_pool = 0;1871 info.input_free = 0;1872 }1873 snd_seq_client_unlock(cptr);18741875 if (copy_to_user(arg, &info, sizeof(info)))1876 return -EFAULT;1877 return 0;1878}18791880/* SET_CLIENT_POOL ioctl() */1881static int snd_seq_ioctl_set_client_pool(struct snd_seq_client *client,1882 void __user *arg)1883{1884 struct snd_seq_client_pool info;1885 int rc;18861887 if (copy_from_user(&info, arg, sizeof(info)))1888 return -EFAULT;18891890 if (client->number != info.client)1891 return -EINVAL; /* can't change other clients */18921893 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);1900 }1901 client->pool->size = info.output_pool;1902 rc = snd_seq_pool_init(client->pool);1903 if (rc < 0)1904 return rc;1905 }1906 if (client->type == USER_CLIENT && client->data.user.fifo != NULL &&1907 info.input_pool >= 1 &&1908 info.input_pool <= SNDRV_SEQ_MAX_CLIENT_EVENTS &&1909 info.input_pool != client->data.user.fifo_pool_size) {1910 /* change pool size */1911 rc = snd_seq_fifo_resize(client->data.user.fifo, info.input_pool);1912 if (rc < 0)1913 return rc;1914 client->data.user.fifo_pool_size = info.input_pool;1915 }1916 if (info.output_room >= 1 &&1917 info.output_room <= client->pool->size) {1918 client->pool->room = info.output_room;1919 }19201921 return snd_seq_ioctl_get_client_pool(client, arg);1922}192319241925/* REMOVE_EVENTS ioctl() */1926static int snd_seq_ioctl_remove_events(struct snd_seq_client *client,1927 void __user *arg)1928{1929 struct snd_seq_remove_events info;19301931 if (copy_from_user(&info, arg, sizeof(info)))1932 return -EFAULT;19331934 /*1935 * Input mostly not implemented XXX.1936 */1937 if (info.remove_mode & SNDRV_SEQ_REMOVE_INPUT) {1938 /*1939 * No restrictions so for a user client we can clear1940 * the whole fifo1941 */1942 if (client->type == USER_CLIENT)1943 snd_seq_fifo_clear(client->data.user.fifo);1944 }19451946 if (info.remove_mode & SNDRV_SEQ_REMOVE_OUTPUT)1947 snd_seq_queue_remove_cells(client->number, &info);19481949 return 0;1950}195119521953/*1954 * get subscription info1955 */1956static int snd_seq_ioctl_get_subscription(struct snd_seq_client *client,1957 void __user *arg)1958{1959 int result;1960 struct snd_seq_client *sender = NULL;1961 struct snd_seq_client_port *sport = NULL;1962 struct snd_seq_port_subscribe subs;1963 struct snd_seq_subscribers *p;19641965 if (copy_from_user(&subs, arg, sizeof(subs)))1966 return -EFAULT;19671968 result = -EINVAL;1969 if ((sender = snd_seq_client_use_ptr(subs.sender.client)) == NULL)1970 goto __end;1971 if ((sport = snd_seq_port_use_ptr(sender, subs.sender.port)) == NULL)1972 goto __end;1973 p = snd_seq_port_get_subscription(&sport->c_src, &subs.dest);1974 if (p) {1975 result = 0;1976 subs = p->info;1977 } else1978 result = -ENOENT;19791980 __end:1981 if (sport)1982 snd_seq_port_unlock(sport);1983 if (sender)1984 snd_seq_client_unlock(sender);1985 if (result >= 0) {1986 if (copy_to_user(arg, &subs, sizeof(subs)))1987 return -EFAULT;1988 }1989 return result;1990}199119921993/*1994 * get subscription info - check only its presence1995 */1996static int snd_seq_ioctl_query_subs(struct snd_seq_client *client,1997 void __user *arg)1998{1999 int result = -ENXIO;2000 struct snd_seq_client *cptr = NULL;2001 struct snd_seq_client_port *port = NULL;2002 struct snd_seq_query_subs subs;2003 struct snd_seq_port_subs_info *group;2004 struct list_head *p;2005 int i;20062007 if (copy_from_user(&subs, arg, sizeof(subs)))2008 return -EFAULT;20092010 if ((cptr = snd_seq_client_use_ptr(subs.root.client)) == NULL)2011 goto __end;2012 if ((port = snd_seq_port_use_ptr(cptr, subs.root.port)) == NULL)2013 goto __end;20142015 switch (subs.type) {2016 case SNDRV_SEQ_QUERY_SUBS_READ:2017 group = &port->c_src;2018 break;2019 case SNDRV_SEQ_QUERY_SUBS_WRITE:2020 group = &port->c_dest;2021 break;2022 default:2023 goto __end;2024 }20252026 down_read(&group->list_mutex);2027 /* search for the subscriber */2028 subs.num_subs = group->count;2029 i = 0;2030 result = -ENOENT;2031 list_for_each(p, &group->list_head) {2032 if (i++ == subs.index) {2033 /* found! */2034 struct snd_seq_subscribers *s;2035 if (subs.type == SNDRV_SEQ_QUERY_SUBS_READ) {2036 s = list_entry(p, struct snd_seq_subscribers, src_list);2037 subs.addr = s->info.dest;2038 } else {2039 s = list_entry(p, struct snd_seq_subscribers, dest_list);2040 subs.addr = s->info.sender;2041 }2042 subs.flags = s->info.flags;2043 subs.queue = s->info.queue;2044 result = 0;2045 break;2046 }2047 }2048 up_read(&group->list_mutex);20492050 __end:2051 if (port)2052 snd_seq_port_unlock(port);2053 if (cptr)2054 snd_seq_client_unlock(cptr);2055 if (result >= 0) {2056 if (copy_to_user(arg, &subs, sizeof(subs)))2057 return -EFAULT;2058 }2059 return result;2060}206120622063/*2064 * query next client2065 */2066static int snd_seq_ioctl_query_next_client(struct snd_seq_client *client,2067 void __user *arg)2068{2069 struct snd_seq_client *cptr = NULL;2070 struct snd_seq_client_info info;20712072 if (copy_from_user(&info, arg, sizeof(info)))2073 return -EFAULT;20742075 /* search for next client */2076 info.client++;2077 if (info.client < 0)2078 info.client = 0;2079 for (; info.client < SNDRV_SEQ_MAX_CLIENTS; info.client++) {2080 cptr = snd_seq_client_use_ptr(info.client);2081 if (cptr)2082 break; /* found */2083 }2084 if (cptr == NULL)2085 return -ENOENT;20862087 get_client_info(cptr, &info);2088 snd_seq_client_unlock(cptr);20892090 if (copy_to_user(arg, &info, sizeof(info)))2091 return -EFAULT;2092 return 0;2093}20942095/*2096 * query next port2097 */2098static int snd_seq_ioctl_query_next_port(struct snd_seq_client *client,2099 void __user *arg)2100{2101 struct snd_seq_client *cptr;2102 struct snd_seq_client_port *port = NULL;2103 struct snd_seq_port_info info;21042105 if (copy_from_user(&info, arg, sizeof(info)))2106 return -EFAULT;2107 cptr = snd_seq_client_use_ptr(info.addr.client);2108 if (cptr == NULL)2109 return -ENXIO;21102111 /* search for next port */2112 info.addr.port++;2113 port = snd_seq_port_query_nearest(cptr, &info);2114 if (port == NULL) {2115 snd_seq_client_unlock(cptr);2116 return -ENOENT;2117 }21182119 /* get port info */2120 info.addr = port->addr;2121 snd_seq_get_port_info(port, &info);2122 snd_seq_port_unlock(port);2123 snd_seq_client_unlock(cptr);21242125 if (copy_to_user(arg, &info, sizeof(info)))2126 return -EFAULT;2127 return 0;2128}21292130/* -------------------------------------------------------- */21312132static struct seq_ioctl_table {2133 unsigned int cmd;2134 int (*func)(struct snd_seq_client *client, void __user * arg);2135} ioctl_tables[] = {2136 { SNDRV_SEQ_IOCTL_SYSTEM_INFO, snd_seq_ioctl_system_info },2137 { SNDRV_SEQ_IOCTL_RUNNING_MODE, snd_seq_ioctl_running_mode },2138 { SNDRV_SEQ_IOCTL_GET_CLIENT_INFO, snd_seq_ioctl_get_client_info },2139 { SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, snd_seq_ioctl_set_client_info },2140 { SNDRV_SEQ_IOCTL_CREATE_PORT, snd_seq_ioctl_create_port },2141 { SNDRV_SEQ_IOCTL_DELETE_PORT, snd_seq_ioctl_delete_port },2142 { SNDRV_SEQ_IOCTL_GET_PORT_INFO, snd_seq_ioctl_get_port_info },2143 { SNDRV_SEQ_IOCTL_SET_PORT_INFO, snd_seq_ioctl_set_port_info },2144 { SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, snd_seq_ioctl_subscribe_port },2145 { SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, snd_seq_ioctl_unsubscribe_port },2146 { SNDRV_SEQ_IOCTL_CREATE_QUEUE, snd_seq_ioctl_create_queue },2147 { SNDRV_SEQ_IOCTL_DELETE_QUEUE, snd_seq_ioctl_delete_queue },2148 { SNDRV_SEQ_IOCTL_GET_QUEUE_INFO, snd_seq_ioctl_get_queue_info },2149 { SNDRV_SEQ_IOCTL_SET_QUEUE_INFO, snd_seq_ioctl_set_queue_info },2150 { SNDRV_SEQ_IOCTL_GET_NAMED_QUEUE, snd_seq_ioctl_get_named_queue },2151 { SNDRV_SEQ_IOCTL_GET_QUEUE_STATUS, snd_seq_ioctl_get_queue_status },2152 { SNDRV_SEQ_IOCTL_GET_QUEUE_TEMPO, snd_seq_ioctl_get_queue_tempo },2153 { SNDRV_SEQ_IOCTL_SET_QUEUE_TEMPO, snd_seq_ioctl_set_queue_tempo },2154 { SNDRV_SEQ_IOCTL_GET_QUEUE_TIMER, snd_seq_ioctl_get_queue_timer },2155 { SNDRV_SEQ_IOCTL_SET_QUEUE_TIMER, snd_seq_ioctl_set_queue_timer },2156 { SNDRV_SEQ_IOCTL_GET_QUEUE_CLIENT, snd_seq_ioctl_get_queue_client },2157 { SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT, snd_seq_ioctl_set_queue_client },2158 { SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, snd_seq_ioctl_get_client_pool },2159 { SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, snd_seq_ioctl_set_client_pool },2160 { SNDRV_SEQ_IOCTL_GET_SUBSCRIPTION, snd_seq_ioctl_get_subscription },2161 { SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT, snd_seq_ioctl_query_next_client },2162 { SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, snd_seq_ioctl_query_next_port },2163 { SNDRV_SEQ_IOCTL_REMOVE_EVENTS, snd_seq_ioctl_remove_events },2164 { SNDRV_SEQ_IOCTL_QUERY_SUBS, snd_seq_ioctl_query_subs },2165 { 0, NULL },2166};21672168static int snd_seq_do_ioctl(struct snd_seq_client *client, unsigned int cmd,2169 void __user *arg)2170{2171 struct seq_ioctl_table *p;21722173 switch (cmd) {2174 case SNDRV_SEQ_IOCTL_PVERSION:2175 /* return sequencer version number */2176 return put_user(SNDRV_SEQ_VERSION, (int __user *)arg) ? -EFAULT : 0;2177 case SNDRV_SEQ_IOCTL_CLIENT_ID:2178 /* return the id of this client */2179 return put_user(client->number, (int __user *)arg) ? -EFAULT : 0;2180 }21812182 if (! arg)2183 return -EFAULT;2184 for (p = ioctl_tables; p->cmd; p++) {2185 if (p->cmd == cmd)2186 return p->func(client, arg);2187 }2188 snd_printd("seq unknown ioctl() 0x%x (type='%c', number=0x%2x)\n",2189 cmd, _IOC_TYPE(cmd), _IOC_NR(cmd));2190 return -ENOTTY;2191}219221932194static long snd_seq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)2195{2196 struct snd_seq_client *client = file->private_data;21972198 snd_assert(client != NULL, return -ENXIO);21992200 return snd_seq_do_ioctl(client, cmd, (void __user *) arg);2201}22022203#ifdef CONFIG_COMPAT2204#include "seq_compat.c"2205#else2206#define snd_seq_ioctl_compat NULL2207#endif22082209/* -------------------------------------------------------- */221022112212/* exported to kernel modules */2213int snd_seq_create_kernel_client(struct snd_card *card, int client_index,2214 const char *name_fmt, ...)2215{2216 struct snd_seq_client *client;2217 va_list args;22182219 snd_assert(! in_interrupt(), return -EBUSY);22202221 if (card && client_index >= SNDRV_SEQ_CLIENTS_PER_CARD)2222 return -EINVAL;2223 if (card == NULL && client_index >= SNDRV_SEQ_GLOBAL_CLIENTS)2224 return -EINVAL;22252226 if (mutex_lock_interruptible(®ister_mutex))2227 return -ERESTARTSYS;22282229 if (card) {2230 client_index += SNDRV_SEQ_GLOBAL_CLIENTS2231 + card->number * SNDRV_SEQ_CLIENTS_PER_CARD;2232 if (client_index >= SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN)2233 client_index = -1;2234 }22352236 /* 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 */2241 }2242 usage_alloc(&client_usage, 1);22432244 client->accept_input = 1;2245 client->accept_output = 1;22462247 va_start(args, name_fmt);2248 vsnprintf(client->name, sizeof(client->name), name_fmt, args);2249 va_end(args);22502251 client->type = KERNEL_CLIENT;2252 mutex_unlock(®ister_mutex);22532254 /* make others aware this new client */2255 snd_seq_system_client_ev_client_start(client->number);22562257 /* return client number to caller */2258 return client->number;2259}22602261EXPORT_SYMBOL(snd_seq_create_kernel_client);22622263/* exported to kernel modules */2264int snd_seq_delete_kernel_client(int client)2265{2266 struct snd_seq_client *ptr;22672268 snd_assert(! in_interrupt(), return -EBUSY);22692270 ptr = clientptr(client);2271 if (ptr == NULL)2272 return -EINVAL;22732274 seq_free_client(ptr);2275 kfree(ptr);2276 return 0;2277}22782279EXPORT_SYMBOL(snd_seq_delete_kernel_client);22802281/* skeleton to enqueue event, called from snd_seq_kernel_client_enqueue2282 * and snd_seq_kernel_client_enqueue_blocking2283 */2284static int kernel_client_enqueue(int client, struct snd_seq_event *ev,2285 struct file *file, int blocking,2286 int atomic, int hop)2287{2288 struct snd_seq_client *cptr;2289 int result;22902291 snd_assert(ev != NULL, return -EINVAL);22922293 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 */22972298 /* fill in client number */2299 ev->source.client = client;23002301 if (check_event_type_and_length(ev))2302 return -EINVAL;23032304 cptr = snd_seq_client_use_ptr(client);2305 if (cptr == NULL)2306 return -EINVAL;23072308 if (! cptr->accept_output)2309 result = -EPERM;2310 else /* send it */2311 result = snd_seq_client_enqueue_event(cptr, ev, file, blocking, atomic, hop);23122313 snd_seq_client_unlock(cptr);2314 return result;2315}23162317/*2318 * exported, called by kernel clients to enqueue events (w/o blocking)2319 *2320 * RETURN VALUE: zero if succeed, negative if error2321 */2322int snd_seq_kernel_client_enqueue(int client, struct snd_seq_event * ev,2323 int atomic, int hop)2324{2325 return kernel_client_enqueue(client, ev, NULL, 0, atomic, hop);2326}23272328EXPORT_SYMBOL(snd_seq_kernel_client_enqueue);23292330/*2331 * exported, called by kernel clients to enqueue events (with blocking)2332 *2333 * RETURN VALUE: zero if succeed, negative if error2334 */2335int snd_seq_kernel_client_enqueue_blocking(int client, struct snd_seq_event * ev,2336 struct file *file,2337 int atomic, int hop)2338{2339 return kernel_client_enqueue(client, ev, file, 1, atomic, hop);2340}23412342EXPORT_SYMBOL(snd_seq_kernel_client_enqueue_blocking);23432344/*2345 * exported, called by kernel clients to dispatch events directly to other2346 * clients, bypassing the queues. Event time-stamp will be updated.2347 *2348 * RETURN VALUE: negative = delivery failed,2349 * zero, or positive: the number of delivered events2350 */2351int snd_seq_kernel_client_dispatch(int client, struct snd_seq_event * ev,2352 int atomic, int hop)2353{2354 struct snd_seq_client *cptr;2355 int result;23562357 snd_assert(ev != NULL, return -EINVAL);23582359 /* fill in client number */2360 ev->queue = SNDRV_SEQ_QUEUE_DIRECT;2361 ev->source.client = client;23622363 if (check_event_type_and_length(ev))2364 return -EINVAL;23652366 cptr = snd_seq_client_use_ptr(client);2367 if (cptr == NULL)2368 return -EINVAL;23692370 if (!cptr->accept_output)2371 result = -EPERM;2372 else2373 result = snd_seq_deliver_event(cptr, ev, atomic, hop);23742375 snd_seq_client_unlock(cptr);2376 return result;2377}23782379EXPORT_SYMBOL(snd_seq_kernel_client_dispatch);23802381/*2382 * exported, called by kernel clients to perform same functions as with2383 * userland ioctl()2384 */2385int snd_seq_kernel_client_ctl(int clientid, unsigned int cmd, void *arg)2386{2387 struct snd_seq_client *client;2388 mm_segment_t fs;2389 int result;23902391 client = clientptr(clientid);2392 if (client == NULL)2393 return -ENXIO;2394 fs = snd_enter_user();2395 result = snd_seq_do_ioctl(client, cmd, (void __user *)arg);2396 snd_leave_user(fs);2397 return result;2398}23992400EXPORT_SYMBOL(snd_seq_kernel_client_ctl);24012402/* exported (for OSS emulator) */2403int snd_seq_kernel_client_write_poll(int clientid, struct file *file, poll_table *wait)2404{2405 struct snd_seq_client *client;24062407 client = clientptr(clientid);2408 if (client == NULL)2409 return -ENXIO;24102411 if (! snd_seq_write_pool_allocated(client))2412 return 1;2413 if (snd_seq_pool_poll_wait(client->pool, file, wait))2414 return 1;2415 return 0;2416}24172418EXPORT_SYMBOL(snd_seq_kernel_client_write_poll);24192420/*---------------------------------------------------------------------------*/24212422#ifdef CONFIG_PROC_FS2423/*2424 * /proc interface2425 */2426static 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)2429{2430 struct list_head *p;2431 struct snd_seq_subscribers *s;2432 int count = 0;24332434 down_read(&group->list_mutex);2435 if (list_empty(&group->list_head)) {2436 up_read(&group->list_mutex);2437 return;2438 }2439 snd_iprintf(buffer, msg);2440 list_for_each(p, &group->list_head) {2441 if (is_src)2442 s = list_entry(p, struct snd_seq_subscribers, src_list);2443 else2444 s = list_entry(p, struct snd_seq_subscribers, dest_list);2445 if (count++)2446 snd_iprintf(buffer, ", ");2447 snd_iprintf(buffer, "%d:%d",2448 is_src ? s->info.dest.client : s->info.sender.client,2449 is_src ? s->info.dest.port : s->info.sender.port);2450 if (s->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)2451 snd_iprintf(buffer, "[%c:%d]", ((s->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL) ? 'r' : 't'), s->info.queue);2452 if (group->exclusive)2453 snd_iprintf(buffer, "[ex]");2454 }2455 up_read(&group->list_mutex);2456 snd_iprintf(buffer, "\n");2457}24582459#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')24622463#define FLAG_PERM_DUPLEX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_DUPLEX ? 'X' : '-')24642465static void snd_seq_info_dump_ports(struct snd_info_buffer *buffer,2466 struct snd_seq_client *client)2467{2468 struct snd_seq_client_port *p;24692470 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: ");2480 }2481 mutex_unlock(&client->ports_mutex);2482}248324842485void snd_seq_info_pool(struct snd_info_buffer *buffer,2486 struct snd_seq_pool *pool, char *space);24872488/* exported to seq_info.c */2489void snd_seq_info_clients_read(struct snd_info_entry *entry,2490 struct snd_info_buffer *buffer)2491{2492 int c;2493 struct snd_seq_client *client;24942495 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");25002501 /* list the client table */2502 for (c = 0; c < SNDRV_SEQ_MAX_CLIENTS; c++) {2503 client = snd_seq_client_use_ptr(c);2504 if (client == NULL)2505 continue;2506 if (client->type == NO_CLIENT) {2507 snd_seq_client_unlock(client);2508 continue;2509 }25102511 snd_iprintf(buffer, "Client %3d : \"%s\" [%s]\n",2512 c, client->name,2513 client->type == USER_CLIENT ? "User" : "Kernel");2514 snd_seq_info_dump_ports(buffer, client);2515 if (snd_seq_write_pool_allocated(client)) {2516 snd_iprintf(buffer, " Output pool :\n");2517 snd_seq_info_pool(buffer, client->pool, " ");2518 }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, " ");2523 }2524 snd_seq_client_unlock(client);2525 }2526}2527#endif /* CONFIG_PROC_FS */25282529/*---------------------------------------------------------------------------*/253025312532/*2533 * REGISTRATION PART2534 */25352536static const struct file_operations snd_seq_f_ops =2537{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,2546};25472548/*2549 * register sequencer device2550 */2551int __init snd_sequencer_device_init(void)2552{2553 int err;25542555 if (mutex_lock_interruptible(®ister_mutex))2556 return -ERESTARTSYS;25572558 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0,2559 &snd_seq_f_ops, NULL, "seq")) < 0) {2560 mutex_unlock(®ister_mutex);2561 return err;2562 }25632564 mutex_unlock(®ister_mutex);25652566 return 0;2567}2568256925702571/*2572 * unregister sequencer device2573 */2574void __exit snd_sequencer_device_done(void)2575{2576 snd_unregister_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0);2577}