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