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