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