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