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