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