xref: /linux/sound/core/seq/seq_clientmgr.c (revision 7fe03f8ff55d33fe6398637f78a8620dd2a78b38)
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_write_pool_allocated(client) ||
1134 		    snd_seq_pool_poll_wait(client->pool, file, wait))
1135 			mask |= EPOLLOUT | EPOLLWRNORM;
1136 	}
1137 
1138 	return mask;
1139 }
1140 
1141 
1142 /*-----------------------------------------------------*/
1143 
1144 static int snd_seq_ioctl_pversion(struct snd_seq_client *client, void *arg)
1145 {
1146 	int *pversion = arg;
1147 
1148 	*pversion = SNDRV_SEQ_VERSION;
1149 	return 0;
1150 }
1151 
1152 static int snd_seq_ioctl_user_pversion(struct snd_seq_client *client, void *arg)
1153 {
1154 	client->user_pversion = *(unsigned int *)arg;
1155 	return 0;
1156 }
1157 
1158 static int snd_seq_ioctl_client_id(struct snd_seq_client *client, void *arg)
1159 {
1160 	int *client_id = arg;
1161 
1162 	*client_id = client->number;
1163 	return 0;
1164 }
1165 
1166 /* SYSTEM_INFO ioctl() */
1167 static int snd_seq_ioctl_system_info(struct snd_seq_client *client, void *arg)
1168 {
1169 	struct snd_seq_system_info *info = arg;
1170 
1171 	memset(info, 0, sizeof(*info));
1172 	/* fill the info fields */
1173 	info->queues = SNDRV_SEQ_MAX_QUEUES;
1174 	info->clients = SNDRV_SEQ_MAX_CLIENTS;
1175 	info->ports = SNDRV_SEQ_MAX_PORTS;
1176 	info->channels = 256;	/* fixed limit */
1177 	info->cur_clients = client_usage.cur;
1178 	info->cur_queues = snd_seq_queue_get_cur_queues();
1179 
1180 	return 0;
1181 }
1182 
1183 
1184 /* RUNNING_MODE ioctl() */
1185 static int snd_seq_ioctl_running_mode(struct snd_seq_client *client, void  *arg)
1186 {
1187 	struct snd_seq_running_info *info = arg;
1188 	struct snd_seq_client *cptr;
1189 	int err = 0;
1190 
1191 	/* requested client number */
1192 	cptr = snd_seq_client_use_ptr(info->client);
1193 	if (cptr == NULL)
1194 		return -ENOENT;		/* don't change !!! */
1195 
1196 #ifdef SNDRV_BIG_ENDIAN
1197 	if (!info->big_endian) {
1198 		err = -EINVAL;
1199 		goto __err;
1200 	}
1201 #else
1202 	if (info->big_endian) {
1203 		err = -EINVAL;
1204 		goto __err;
1205 	}
1206 
1207 #endif
1208 	if (info->cpu_mode > sizeof(long)) {
1209 		err = -EINVAL;
1210 		goto __err;
1211 	}
1212 	cptr->convert32 = (info->cpu_mode < sizeof(long));
1213  __err:
1214 	snd_seq_client_unlock(cptr);
1215 	return err;
1216 }
1217 
1218 /* CLIENT_INFO ioctl() */
1219 static void get_client_info(struct snd_seq_client *cptr,
1220 			    struct snd_seq_client_info *info)
1221 {
1222 	info->client = cptr->number;
1223 
1224 	/* fill the info fields */
1225 	info->type = cptr->type;
1226 	strcpy(info->name, cptr->name);
1227 	info->filter = cptr->filter;
1228 	info->event_lost = cptr->event_lost;
1229 	memcpy(info->event_filter, cptr->event_filter, 32);
1230 	info->group_filter = cptr->group_filter;
1231 	info->num_ports = cptr->num_ports;
1232 
1233 	if (cptr->type == USER_CLIENT)
1234 		info->pid = pid_vnr(cptr->data.user.owner);
1235 	else
1236 		info->pid = -1;
1237 
1238 	if (cptr->type == KERNEL_CLIENT)
1239 		info->card = cptr->data.kernel.card ? cptr->data.kernel.card->number : -1;
1240 	else
1241 		info->card = -1;
1242 
1243 	info->midi_version = cptr->midi_version;
1244 	memset(info->reserved, 0, sizeof(info->reserved));
1245 }
1246 
1247 static int snd_seq_ioctl_get_client_info(struct snd_seq_client *client,
1248 					 void *arg)
1249 {
1250 	struct snd_seq_client_info *client_info = arg;
1251 	struct snd_seq_client *cptr;
1252 
1253 	/* requested client number */
1254 	cptr = snd_seq_client_use_ptr(client_info->client);
1255 	if (cptr == NULL)
1256 		return -ENOENT;		/* don't change !!! */
1257 
1258 	get_client_info(cptr, client_info);
1259 	snd_seq_client_unlock(cptr);
1260 
1261 	return 0;
1262 }
1263 
1264 
1265 /* CLIENT_INFO ioctl() */
1266 static int snd_seq_ioctl_set_client_info(struct snd_seq_client *client,
1267 					 void *arg)
1268 {
1269 	struct snd_seq_client_info *client_info = arg;
1270 
1271 	/* it is not allowed to set the info fields for an another client */
1272 	if (client->number != client_info->client)
1273 		return -EPERM;
1274 	/* also client type must be set now */
1275 	if (client->type != client_info->type)
1276 		return -EINVAL;
1277 
1278 	if (client->user_pversion >= SNDRV_PROTOCOL_VERSION(1, 0, 3)) {
1279 		/* check validity of midi_version field */
1280 		if (client_info->midi_version > SNDRV_SEQ_CLIENT_UMP_MIDI_2_0)
1281 			return -EINVAL;
1282 
1283 		/* check if UMP is supported in kernel */
1284 		if (!IS_ENABLED(CONFIG_SND_SEQ_UMP) &&
1285 		    client_info->midi_version > 0)
1286 			return -EINVAL;
1287 	}
1288 
1289 	/* fill the info fields */
1290 	if (client_info->name[0])
1291 		strscpy(client->name, client_info->name, sizeof(client->name));
1292 
1293 	client->filter = client_info->filter;
1294 	client->event_lost = client_info->event_lost;
1295 	if (client->user_pversion >= SNDRV_PROTOCOL_VERSION(1, 0, 3))
1296 		client->midi_version = client_info->midi_version;
1297 	memcpy(client->event_filter, client_info->event_filter, 32);
1298 	client->group_filter = client_info->group_filter;
1299 
1300 	/* notify the change */
1301 	snd_seq_system_client_ev_client_change(client->number);
1302 
1303 	return 0;
1304 }
1305 
1306 
1307 /*
1308  * CREATE PORT ioctl()
1309  */
1310 static int snd_seq_ioctl_create_port(struct snd_seq_client *client, void *arg)
1311 {
1312 	struct snd_seq_port_info *info = arg;
1313 	struct snd_seq_client_port *port;
1314 	struct snd_seq_port_callback *callback;
1315 	int port_idx, err;
1316 
1317 	/* it is not allowed to create the port for an another client */
1318 	if (info->addr.client != client->number)
1319 		return -EPERM;
1320 	if (client->type == USER_CLIENT && info->kernel)
1321 		return -EINVAL;
1322 	if ((info->capability & SNDRV_SEQ_PORT_CAP_UMP_ENDPOINT) &&
1323 	    client->ump_endpoint_port >= 0)
1324 		return -EBUSY;
1325 
1326 	if (info->flags & SNDRV_SEQ_PORT_FLG_GIVEN_PORT)
1327 		port_idx = info->addr.port;
1328 	else
1329 		port_idx = -1;
1330 	if (port_idx >= SNDRV_SEQ_ADDRESS_UNKNOWN)
1331 		return -EINVAL;
1332 	err = snd_seq_create_port(client, port_idx, &port);
1333 	if (err < 0)
1334 		return err;
1335 
1336 	if (client->type == KERNEL_CLIENT) {
1337 		callback = info->kernel;
1338 		if (callback) {
1339 			if (callback->owner)
1340 				port->owner = callback->owner;
1341 			port->private_data = callback->private_data;
1342 			port->private_free = callback->private_free;
1343 			port->event_input = callback->event_input;
1344 			port->c_src.open = callback->subscribe;
1345 			port->c_src.close = callback->unsubscribe;
1346 			port->c_dest.open = callback->use;
1347 			port->c_dest.close = callback->unuse;
1348 		}
1349 	}
1350 
1351 	info->addr = port->addr;
1352 
1353 	snd_seq_set_port_info(port, info);
1354 	if (info->capability & SNDRV_SEQ_PORT_CAP_UMP_ENDPOINT)
1355 		client->ump_endpoint_port = port->addr.port;
1356 	snd_seq_system_client_ev_port_start(port->addr.client, port->addr.port);
1357 	snd_seq_port_unlock(port);
1358 
1359 	return 0;
1360 }
1361 
1362 /*
1363  * DELETE PORT ioctl()
1364  */
1365 static int snd_seq_ioctl_delete_port(struct snd_seq_client *client, void *arg)
1366 {
1367 	struct snd_seq_port_info *info = arg;
1368 	int err;
1369 
1370 	/* it is not allowed to remove the port for an another client */
1371 	if (info->addr.client != client->number)
1372 		return -EPERM;
1373 
1374 	err = snd_seq_delete_port(client, info->addr.port);
1375 	if (err >= 0) {
1376 		if (client->ump_endpoint_port == info->addr.port)
1377 			client->ump_endpoint_port = -1;
1378 		snd_seq_system_client_ev_port_exit(client->number, info->addr.port);
1379 	}
1380 	return err;
1381 }
1382 
1383 
1384 /*
1385  * GET_PORT_INFO ioctl() (on any client)
1386  */
1387 static int snd_seq_ioctl_get_port_info(struct snd_seq_client *client, void *arg)
1388 {
1389 	struct snd_seq_port_info *info = arg;
1390 	struct snd_seq_client *cptr;
1391 	struct snd_seq_client_port *port;
1392 
1393 	cptr = snd_seq_client_use_ptr(info->addr.client);
1394 	if (cptr == NULL)
1395 		return -ENXIO;
1396 
1397 	port = snd_seq_port_use_ptr(cptr, info->addr.port);
1398 	if (port == NULL) {
1399 		snd_seq_client_unlock(cptr);
1400 		return -ENOENT;			/* don't change */
1401 	}
1402 
1403 	/* get port info */
1404 	snd_seq_get_port_info(port, info);
1405 	snd_seq_port_unlock(port);
1406 	snd_seq_client_unlock(cptr);
1407 
1408 	return 0;
1409 }
1410 
1411 
1412 /*
1413  * SET_PORT_INFO ioctl() (only ports on this/own client)
1414  */
1415 static int snd_seq_ioctl_set_port_info(struct snd_seq_client *client, void *arg)
1416 {
1417 	struct snd_seq_port_info *info = arg;
1418 	struct snd_seq_client_port *port;
1419 
1420 	if (info->addr.client != client->number) /* only set our own ports ! */
1421 		return -EPERM;
1422 	port = snd_seq_port_use_ptr(client, info->addr.port);
1423 	if (port) {
1424 		snd_seq_set_port_info(port, info);
1425 		snd_seq_port_unlock(port);
1426 		/* notify the change */
1427 		snd_seq_system_client_ev_port_change(info->addr.client,
1428 						     info->addr.port);
1429 	}
1430 	return 0;
1431 }
1432 
1433 
1434 /*
1435  * port subscription (connection)
1436  */
1437 #define PERM_RD		(SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ)
1438 #define PERM_WR		(SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_SUBS_WRITE)
1439 
1440 static int check_subscription_permission(struct snd_seq_client *client,
1441 					 struct snd_seq_client_port *sport,
1442 					 struct snd_seq_client_port *dport,
1443 					 struct snd_seq_port_subscribe *subs)
1444 {
1445 	if (client->number != subs->sender.client &&
1446 	    client->number != subs->dest.client) {
1447 		/* connection by third client - check export permission */
1448 		if (check_port_perm(sport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))
1449 			return -EPERM;
1450 		if (check_port_perm(dport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))
1451 			return -EPERM;
1452 	}
1453 
1454 	/* check read permission */
1455 	/* if sender or receiver is the subscribing client itself,
1456 	 * no permission check is necessary
1457 	 */
1458 	if (client->number != subs->sender.client) {
1459 		if (! check_port_perm(sport, PERM_RD))
1460 			return -EPERM;
1461 	}
1462 	/* check write permission */
1463 	if (client->number != subs->dest.client) {
1464 		if (! check_port_perm(dport, PERM_WR))
1465 			return -EPERM;
1466 	}
1467 	return 0;
1468 }
1469 
1470 /*
1471  * send an subscription notify event to user client:
1472  * client must be user client.
1473  */
1474 int snd_seq_client_notify_subscription(int client, int port,
1475 				       struct snd_seq_port_subscribe *info,
1476 				       int evtype)
1477 {
1478 	struct snd_seq_event event;
1479 
1480 	memset(&event, 0, sizeof(event));
1481 	event.type = evtype;
1482 	event.data.connect.dest = info->dest;
1483 	event.data.connect.sender = info->sender;
1484 
1485 	return snd_seq_system_notify(client, port, &event, false);  /* non-atomic */
1486 }
1487 
1488 
1489 /*
1490  * add to port's subscription list IOCTL interface
1491  */
1492 static int snd_seq_ioctl_subscribe_port(struct snd_seq_client *client,
1493 					void *arg)
1494 {
1495 	struct snd_seq_port_subscribe *subs = arg;
1496 	int result = -EINVAL;
1497 	struct snd_seq_client *receiver = NULL, *sender = NULL;
1498 	struct snd_seq_client_port *sport = NULL, *dport = NULL;
1499 
1500 	receiver = snd_seq_client_use_ptr(subs->dest.client);
1501 	if (!receiver)
1502 		goto __end;
1503 	sender = snd_seq_client_use_ptr(subs->sender.client);
1504 	if (!sender)
1505 		goto __end;
1506 	sport = snd_seq_port_use_ptr(sender, subs->sender.port);
1507 	if (!sport)
1508 		goto __end;
1509 	dport = snd_seq_port_use_ptr(receiver, subs->dest.port);
1510 	if (!dport)
1511 		goto __end;
1512 
1513 	result = check_subscription_permission(client, sport, dport, subs);
1514 	if (result < 0)
1515 		goto __end;
1516 
1517 	/* connect them */
1518 	result = snd_seq_port_connect(client, sender, sport, receiver, dport, subs);
1519 	if (! result) /* broadcast announce */
1520 		snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,
1521 						   subs, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
1522       __end:
1523       	if (sport)
1524 		snd_seq_port_unlock(sport);
1525 	if (dport)
1526 		snd_seq_port_unlock(dport);
1527 	if (sender)
1528 		snd_seq_client_unlock(sender);
1529 	if (receiver)
1530 		snd_seq_client_unlock(receiver);
1531 	return result;
1532 }
1533 
1534 
1535 /*
1536  * remove from port's subscription list
1537  */
1538 static int snd_seq_ioctl_unsubscribe_port(struct snd_seq_client *client,
1539 					  void *arg)
1540 {
1541 	struct snd_seq_port_subscribe *subs = arg;
1542 	int result = -ENXIO;
1543 	struct snd_seq_client *receiver = NULL, *sender = NULL;
1544 	struct snd_seq_client_port *sport = NULL, *dport = NULL;
1545 
1546 	receiver = snd_seq_client_use_ptr(subs->dest.client);
1547 	if (!receiver)
1548 		goto __end;
1549 	sender = snd_seq_client_use_ptr(subs->sender.client);
1550 	if (!sender)
1551 		goto __end;
1552 	sport = snd_seq_port_use_ptr(sender, subs->sender.port);
1553 	if (!sport)
1554 		goto __end;
1555 	dport = snd_seq_port_use_ptr(receiver, subs->dest.port);
1556 	if (!dport)
1557 		goto __end;
1558 
1559 	result = check_subscription_permission(client, sport, dport, subs);
1560 	if (result < 0)
1561 		goto __end;
1562 
1563 	result = snd_seq_port_disconnect(client, sender, sport, receiver, dport, subs);
1564 	if (! result) /* broadcast announce */
1565 		snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,
1566 						   subs, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
1567       __end:
1568       	if (sport)
1569 		snd_seq_port_unlock(sport);
1570 	if (dport)
1571 		snd_seq_port_unlock(dport);
1572 	if (sender)
1573 		snd_seq_client_unlock(sender);
1574 	if (receiver)
1575 		snd_seq_client_unlock(receiver);
1576 	return result;
1577 }
1578 
1579 
1580 /* CREATE_QUEUE ioctl() */
1581 static int snd_seq_ioctl_create_queue(struct snd_seq_client *client, void *arg)
1582 {
1583 	struct snd_seq_queue_info *info = arg;
1584 	struct snd_seq_queue *q;
1585 
1586 	q = snd_seq_queue_alloc(client->number, info->locked, info->flags);
1587 	if (IS_ERR(q))
1588 		return PTR_ERR(q);
1589 
1590 	info->queue = q->queue;
1591 	info->locked = q->locked;
1592 	info->owner = q->owner;
1593 
1594 	/* set queue name */
1595 	if (!info->name[0])
1596 		snprintf(info->name, sizeof(info->name), "Queue-%d", q->queue);
1597 	strscpy(q->name, info->name, sizeof(q->name));
1598 	snd_use_lock_free(&q->use_lock);
1599 
1600 	return 0;
1601 }
1602 
1603 /* DELETE_QUEUE ioctl() */
1604 static int snd_seq_ioctl_delete_queue(struct snd_seq_client *client, void *arg)
1605 {
1606 	struct snd_seq_queue_info *info = arg;
1607 
1608 	return snd_seq_queue_delete(client->number, info->queue);
1609 }
1610 
1611 /* GET_QUEUE_INFO ioctl() */
1612 static int snd_seq_ioctl_get_queue_info(struct snd_seq_client *client,
1613 					void *arg)
1614 {
1615 	struct snd_seq_queue_info *info = arg;
1616 	struct snd_seq_queue *q;
1617 
1618 	q = queueptr(info->queue);
1619 	if (q == NULL)
1620 		return -EINVAL;
1621 
1622 	memset(info, 0, sizeof(*info));
1623 	info->queue = q->queue;
1624 	info->owner = q->owner;
1625 	info->locked = q->locked;
1626 	strscpy(info->name, q->name, sizeof(info->name));
1627 	queuefree(q);
1628 
1629 	return 0;
1630 }
1631 
1632 /* SET_QUEUE_INFO ioctl() */
1633 static int snd_seq_ioctl_set_queue_info(struct snd_seq_client *client,
1634 					void *arg)
1635 {
1636 	struct snd_seq_queue_info *info = arg;
1637 	struct snd_seq_queue *q;
1638 
1639 	if (info->owner != client->number)
1640 		return -EINVAL;
1641 
1642 	/* change owner/locked permission */
1643 	if (snd_seq_queue_check_access(info->queue, client->number)) {
1644 		if (snd_seq_queue_set_owner(info->queue, client->number, info->locked) < 0)
1645 			return -EPERM;
1646 		if (info->locked)
1647 			snd_seq_queue_use(info->queue, client->number, 1);
1648 	} else {
1649 		return -EPERM;
1650 	}
1651 
1652 	q = queueptr(info->queue);
1653 	if (! q)
1654 		return -EINVAL;
1655 	if (q->owner != client->number) {
1656 		queuefree(q);
1657 		return -EPERM;
1658 	}
1659 	strscpy(q->name, info->name, sizeof(q->name));
1660 	queuefree(q);
1661 
1662 	return 0;
1663 }
1664 
1665 /* GET_NAMED_QUEUE ioctl() */
1666 static int snd_seq_ioctl_get_named_queue(struct snd_seq_client *client,
1667 					 void *arg)
1668 {
1669 	struct snd_seq_queue_info *info = arg;
1670 	struct snd_seq_queue *q;
1671 
1672 	q = snd_seq_queue_find_name(info->name);
1673 	if (q == NULL)
1674 		return -EINVAL;
1675 	info->queue = q->queue;
1676 	info->owner = q->owner;
1677 	info->locked = q->locked;
1678 	queuefree(q);
1679 
1680 	return 0;
1681 }
1682 
1683 /* GET_QUEUE_STATUS ioctl() */
1684 static int snd_seq_ioctl_get_queue_status(struct snd_seq_client *client,
1685 					  void *arg)
1686 {
1687 	struct snd_seq_queue_status *status = arg;
1688 	struct snd_seq_queue *queue;
1689 	struct snd_seq_timer *tmr;
1690 
1691 	queue = queueptr(status->queue);
1692 	if (queue == NULL)
1693 		return -EINVAL;
1694 	memset(status, 0, sizeof(*status));
1695 	status->queue = queue->queue;
1696 
1697 	tmr = queue->timer;
1698 	status->events = queue->tickq->cells + queue->timeq->cells;
1699 
1700 	status->time = snd_seq_timer_get_cur_time(tmr, true);
1701 	status->tick = snd_seq_timer_get_cur_tick(tmr);
1702 
1703 	status->running = tmr->running;
1704 
1705 	status->flags = queue->flags;
1706 	queuefree(queue);
1707 
1708 	return 0;
1709 }
1710 
1711 
1712 /* GET_QUEUE_TEMPO ioctl() */
1713 static int snd_seq_ioctl_get_queue_tempo(struct snd_seq_client *client,
1714 					 void *arg)
1715 {
1716 	struct snd_seq_queue_tempo *tempo = arg;
1717 	struct snd_seq_queue *queue;
1718 	struct snd_seq_timer *tmr;
1719 
1720 	queue = queueptr(tempo->queue);
1721 	if (queue == NULL)
1722 		return -EINVAL;
1723 	memset(tempo, 0, sizeof(*tempo));
1724 	tempo->queue = queue->queue;
1725 
1726 	tmr = queue->timer;
1727 
1728 	tempo->tempo = tmr->tempo;
1729 	tempo->ppq = tmr->ppq;
1730 	tempo->skew_value = tmr->skew;
1731 	tempo->skew_base = tmr->skew_base;
1732 	if (client->user_pversion >= SNDRV_PROTOCOL_VERSION(1, 0, 4))
1733 		tempo->tempo_base = tmr->tempo_base;
1734 	queuefree(queue);
1735 
1736 	return 0;
1737 }
1738 
1739 
1740 /* SET_QUEUE_TEMPO ioctl() */
1741 int snd_seq_set_queue_tempo(int client, struct snd_seq_queue_tempo *tempo)
1742 {
1743 	if (!snd_seq_queue_check_access(tempo->queue, client))
1744 		return -EPERM;
1745 	return snd_seq_queue_timer_set_tempo(tempo->queue, client, tempo);
1746 }
1747 EXPORT_SYMBOL(snd_seq_set_queue_tempo);
1748 
1749 static int snd_seq_ioctl_set_queue_tempo(struct snd_seq_client *client,
1750 					 void *arg)
1751 {
1752 	struct snd_seq_queue_tempo *tempo = arg;
1753 	int result;
1754 
1755 	if (client->user_pversion < SNDRV_PROTOCOL_VERSION(1, 0, 4))
1756 		tempo->tempo_base = 0;
1757 	result = snd_seq_set_queue_tempo(client->number, tempo);
1758 	return result < 0 ? result : 0;
1759 }
1760 
1761 
1762 /* GET_QUEUE_TIMER ioctl() */
1763 static int snd_seq_ioctl_get_queue_timer(struct snd_seq_client *client,
1764 					 void *arg)
1765 {
1766 	struct snd_seq_queue_timer *timer = arg;
1767 	struct snd_seq_queue *queue;
1768 	struct snd_seq_timer *tmr;
1769 
1770 	queue = queueptr(timer->queue);
1771 	if (queue == NULL)
1772 		return -EINVAL;
1773 
1774 	mutex_lock(&queue->timer_mutex);
1775 	tmr = queue->timer;
1776 	memset(timer, 0, sizeof(*timer));
1777 	timer->queue = queue->queue;
1778 
1779 	timer->type = tmr->type;
1780 	if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
1781 		timer->u.alsa.id = tmr->alsa_id;
1782 		timer->u.alsa.resolution = tmr->preferred_resolution;
1783 	}
1784 	mutex_unlock(&queue->timer_mutex);
1785 	queuefree(queue);
1786 
1787 	return 0;
1788 }
1789 
1790 
1791 /* SET_QUEUE_TIMER ioctl() */
1792 static int snd_seq_ioctl_set_queue_timer(struct snd_seq_client *client,
1793 					 void *arg)
1794 {
1795 	struct snd_seq_queue_timer *timer = arg;
1796 	int result = 0;
1797 
1798 	if (timer->type != SNDRV_SEQ_TIMER_ALSA)
1799 		return -EINVAL;
1800 
1801 	if (snd_seq_queue_check_access(timer->queue, client->number)) {
1802 		struct snd_seq_queue *q;
1803 		struct snd_seq_timer *tmr;
1804 
1805 		q = queueptr(timer->queue);
1806 		if (q == NULL)
1807 			return -ENXIO;
1808 		mutex_lock(&q->timer_mutex);
1809 		tmr = q->timer;
1810 		snd_seq_queue_timer_close(timer->queue);
1811 		tmr->type = timer->type;
1812 		if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
1813 			tmr->alsa_id = timer->u.alsa.id;
1814 			tmr->preferred_resolution = timer->u.alsa.resolution;
1815 		}
1816 		result = snd_seq_queue_timer_open(timer->queue);
1817 		mutex_unlock(&q->timer_mutex);
1818 		queuefree(q);
1819 	} else {
1820 		return -EPERM;
1821 	}
1822 
1823 	return result;
1824 }
1825 
1826 
1827 /* GET_QUEUE_CLIENT ioctl() */
1828 static int snd_seq_ioctl_get_queue_client(struct snd_seq_client *client,
1829 					  void *arg)
1830 {
1831 	struct snd_seq_queue_client *info = arg;
1832 	int used;
1833 
1834 	used = snd_seq_queue_is_used(info->queue, client->number);
1835 	if (used < 0)
1836 		return -EINVAL;
1837 	info->used = used;
1838 	info->client = client->number;
1839 
1840 	return 0;
1841 }
1842 
1843 
1844 /* SET_QUEUE_CLIENT ioctl() */
1845 static int snd_seq_ioctl_set_queue_client(struct snd_seq_client *client,
1846 					  void *arg)
1847 {
1848 	struct snd_seq_queue_client *info = arg;
1849 	int err;
1850 
1851 	if (info->used >= 0) {
1852 		err = snd_seq_queue_use(info->queue, client->number, info->used);
1853 		if (err < 0)
1854 			return err;
1855 	}
1856 
1857 	return snd_seq_ioctl_get_queue_client(client, arg);
1858 }
1859 
1860 
1861 /* GET_CLIENT_POOL ioctl() */
1862 static int snd_seq_ioctl_get_client_pool(struct snd_seq_client *client,
1863 					 void *arg)
1864 {
1865 	struct snd_seq_client_pool *info = arg;
1866 	struct snd_seq_client *cptr;
1867 
1868 	cptr = snd_seq_client_use_ptr(info->client);
1869 	if (cptr == NULL)
1870 		return -ENOENT;
1871 	memset(info, 0, sizeof(*info));
1872 	info->client = cptr->number;
1873 	info->output_pool = cptr->pool->size;
1874 	info->output_room = cptr->pool->room;
1875 	info->output_free = info->output_pool;
1876 	info->output_free = snd_seq_unused_cells(cptr->pool);
1877 	if (cptr->type == USER_CLIENT) {
1878 		info->input_pool = cptr->data.user.fifo_pool_size;
1879 		info->input_free = info->input_pool;
1880 		info->input_free = snd_seq_fifo_unused_cells(cptr->data.user.fifo);
1881 	} else {
1882 		info->input_pool = 0;
1883 		info->input_free = 0;
1884 	}
1885 	snd_seq_client_unlock(cptr);
1886 
1887 	return 0;
1888 }
1889 
1890 /* SET_CLIENT_POOL ioctl() */
1891 static int snd_seq_ioctl_set_client_pool(struct snd_seq_client *client,
1892 					 void *arg)
1893 {
1894 	struct snd_seq_client_pool *info = arg;
1895 	int rc;
1896 
1897 	if (client->number != info->client)
1898 		return -EINVAL; /* can't change other clients */
1899 
1900 	if (info->output_pool >= 1 && info->output_pool <= SNDRV_SEQ_MAX_EVENTS &&
1901 	    (! snd_seq_write_pool_allocated(client) ||
1902 	     info->output_pool != client->pool->size)) {
1903 		if (snd_seq_write_pool_allocated(client)) {
1904 			/* is the pool in use? */
1905 			if (atomic_read(&client->pool->counter))
1906 				return -EBUSY;
1907 			/* remove all existing cells */
1908 			snd_seq_pool_mark_closing(client->pool);
1909 			snd_seq_pool_done(client->pool);
1910 		}
1911 		client->pool->size = info->output_pool;
1912 		rc = snd_seq_pool_init(client->pool);
1913 		if (rc < 0)
1914 			return rc;
1915 	}
1916 	if (client->type == USER_CLIENT && client->data.user.fifo != NULL &&
1917 	    info->input_pool >= 1 &&
1918 	    info->input_pool <= SNDRV_SEQ_MAX_CLIENT_EVENTS &&
1919 	    info->input_pool != client->data.user.fifo_pool_size) {
1920 		/* change pool size */
1921 		rc = snd_seq_fifo_resize(client->data.user.fifo, info->input_pool);
1922 		if (rc < 0)
1923 			return rc;
1924 		client->data.user.fifo_pool_size = info->input_pool;
1925 	}
1926 	if (info->output_room >= 1 &&
1927 	    info->output_room <= client->pool->size) {
1928 		client->pool->room  = info->output_room;
1929 	}
1930 
1931 	return snd_seq_ioctl_get_client_pool(client, arg);
1932 }
1933 
1934 
1935 /* REMOVE_EVENTS ioctl() */
1936 static int snd_seq_ioctl_remove_events(struct snd_seq_client *client,
1937 				       void *arg)
1938 {
1939 	struct snd_seq_remove_events *info = arg;
1940 
1941 	/*
1942 	 * Input mostly not implemented XXX.
1943 	 */
1944 	if (info->remove_mode & SNDRV_SEQ_REMOVE_INPUT) {
1945 		/*
1946 		 * No restrictions so for a user client we can clear
1947 		 * the whole fifo
1948 		 */
1949 		if (client->type == USER_CLIENT && client->data.user.fifo)
1950 			snd_seq_fifo_clear(client->data.user.fifo);
1951 	}
1952 
1953 	if (info->remove_mode & SNDRV_SEQ_REMOVE_OUTPUT)
1954 		snd_seq_queue_remove_cells(client->number, info);
1955 
1956 	return 0;
1957 }
1958 
1959 
1960 /*
1961  * get subscription info
1962  */
1963 static int snd_seq_ioctl_get_subscription(struct snd_seq_client *client,
1964 					  void *arg)
1965 {
1966 	struct snd_seq_port_subscribe *subs = arg;
1967 	int result;
1968 	struct snd_seq_client *sender = NULL;
1969 	struct snd_seq_client_port *sport = NULL;
1970 
1971 	result = -EINVAL;
1972 	sender = snd_seq_client_use_ptr(subs->sender.client);
1973 	if (!sender)
1974 		goto __end;
1975 	sport = snd_seq_port_use_ptr(sender, subs->sender.port);
1976 	if (!sport)
1977 		goto __end;
1978 	result = snd_seq_port_get_subscription(&sport->c_src, &subs->dest,
1979 					       subs);
1980       __end:
1981       	if (sport)
1982 		snd_seq_port_unlock(sport);
1983 	if (sender)
1984 		snd_seq_client_unlock(sender);
1985 
1986 	return result;
1987 }
1988 
1989 
1990 /*
1991  * get subscription info - check only its presence
1992  */
1993 static int snd_seq_ioctl_query_subs(struct snd_seq_client *client, void *arg)
1994 {
1995 	struct snd_seq_query_subs *subs = arg;
1996 	int result = -ENXIO;
1997 	struct snd_seq_client *cptr = NULL;
1998 	struct snd_seq_client_port *port = NULL;
1999 	struct snd_seq_port_subs_info *group;
2000 	struct list_head *p;
2001 	int i;
2002 
2003 	cptr = snd_seq_client_use_ptr(subs->root.client);
2004 	if (!cptr)
2005 		goto __end;
2006 	port = snd_seq_port_use_ptr(cptr, subs->root.port);
2007 	if (!port)
2008 		goto __end;
2009 
2010 	switch (subs->type) {
2011 	case SNDRV_SEQ_QUERY_SUBS_READ:
2012 		group = &port->c_src;
2013 		break;
2014 	case SNDRV_SEQ_QUERY_SUBS_WRITE:
2015 		group = &port->c_dest;
2016 		break;
2017 	default:
2018 		goto __end;
2019 	}
2020 
2021 	down_read(&group->list_mutex);
2022 	/* search for the subscriber */
2023 	subs->num_subs = group->count;
2024 	i = 0;
2025 	result = -ENOENT;
2026 	list_for_each(p, &group->list_head) {
2027 		if (i++ == subs->index) {
2028 			/* found! */
2029 			struct snd_seq_subscribers *s;
2030 			if (subs->type == SNDRV_SEQ_QUERY_SUBS_READ) {
2031 				s = list_entry(p, struct snd_seq_subscribers, src_list);
2032 				subs->addr = s->info.dest;
2033 			} else {
2034 				s = list_entry(p, struct snd_seq_subscribers, dest_list);
2035 				subs->addr = s->info.sender;
2036 			}
2037 			subs->flags = s->info.flags;
2038 			subs->queue = s->info.queue;
2039 			result = 0;
2040 			break;
2041 		}
2042 	}
2043 	up_read(&group->list_mutex);
2044 
2045       __end:
2046    	if (port)
2047 		snd_seq_port_unlock(port);
2048 	if (cptr)
2049 		snd_seq_client_unlock(cptr);
2050 
2051 	return result;
2052 }
2053 
2054 
2055 /*
2056  * query next client
2057  */
2058 static int snd_seq_ioctl_query_next_client(struct snd_seq_client *client,
2059 					   void *arg)
2060 {
2061 	struct snd_seq_client_info *info = arg;
2062 	struct snd_seq_client *cptr = NULL;
2063 
2064 	/* search for next client */
2065 	if (info->client < INT_MAX)
2066 		info->client++;
2067 	if (info->client < 0)
2068 		info->client = 0;
2069 	for (; info->client < SNDRV_SEQ_MAX_CLIENTS; info->client++) {
2070 		cptr = snd_seq_client_use_ptr(info->client);
2071 		if (cptr)
2072 			break; /* found */
2073 	}
2074 	if (cptr == NULL)
2075 		return -ENOENT;
2076 
2077 	get_client_info(cptr, info);
2078 	snd_seq_client_unlock(cptr);
2079 
2080 	return 0;
2081 }
2082 
2083 /*
2084  * query next port
2085  */
2086 static int snd_seq_ioctl_query_next_port(struct snd_seq_client *client,
2087 					 void *arg)
2088 {
2089 	struct snd_seq_port_info *info = arg;
2090 	struct snd_seq_client *cptr;
2091 	struct snd_seq_client_port *port = NULL;
2092 
2093 	cptr = snd_seq_client_use_ptr(info->addr.client);
2094 	if (cptr == NULL)
2095 		return -ENXIO;
2096 
2097 	/* search for next port */
2098 	info->addr.port++;
2099 	port = snd_seq_port_query_nearest(cptr, info);
2100 	if (port == NULL) {
2101 		snd_seq_client_unlock(cptr);
2102 		return -ENOENT;
2103 	}
2104 
2105 	/* get port info */
2106 	info->addr = port->addr;
2107 	snd_seq_get_port_info(port, info);
2108 	snd_seq_port_unlock(port);
2109 	snd_seq_client_unlock(cptr);
2110 
2111 	return 0;
2112 }
2113 
2114 #if IS_ENABLED(CONFIG_SND_SEQ_UMP)
2115 #define NUM_UMP_INFOS (SNDRV_UMP_MAX_BLOCKS + 1)
2116 
2117 static void free_ump_info(struct snd_seq_client *client)
2118 {
2119 	int i;
2120 
2121 	if (!client->ump_info)
2122 		return;
2123 	for (i = 0; i < NUM_UMP_INFOS; i++)
2124 		kfree(client->ump_info[i]);
2125 	kfree(client->ump_info);
2126 	client->ump_info = NULL;
2127 }
2128 
2129 static void terminate_ump_info_strings(void *p, int type)
2130 {
2131 	if (type == SNDRV_SEQ_CLIENT_UMP_INFO_ENDPOINT) {
2132 		struct snd_ump_endpoint_info *ep = p;
2133 		ep->name[sizeof(ep->name) - 1] = 0;
2134 	} else {
2135 		struct snd_ump_block_info *bp = p;
2136 		bp->name[sizeof(bp->name) - 1] = 0;
2137 	}
2138 }
2139 
2140 #ifdef CONFIG_SND_PROC_FS
2141 static void dump_ump_info(struct snd_info_buffer *buffer,
2142 			  struct snd_seq_client *client)
2143 {
2144 	struct snd_ump_endpoint_info *ep;
2145 	struct snd_ump_block_info *bp;
2146 	int i;
2147 
2148 	if (!client->ump_info)
2149 		return;
2150 	ep = client->ump_info[SNDRV_SEQ_CLIENT_UMP_INFO_ENDPOINT];
2151 	if (ep && *ep->name)
2152 		snd_iprintf(buffer, "  UMP Endpoint: \"%s\"\n", ep->name);
2153 	for (i = 0; i < SNDRV_UMP_MAX_BLOCKS; i++) {
2154 		bp = client->ump_info[i + 1];
2155 		if (bp && *bp->name) {
2156 			snd_iprintf(buffer, "  UMP Block %d: \"%s\" [%s]\n",
2157 				    i, bp->name,
2158 				    bp->active ? "Active" : "Inactive");
2159 			snd_iprintf(buffer, "    Groups: %d-%d\n",
2160 				    bp->first_group + 1,
2161 				    bp->first_group + bp->num_groups);
2162 		}
2163 	}
2164 }
2165 #endif
2166 
2167 /* UMP-specific ioctls -- called directly without data copy */
2168 static int snd_seq_ioctl_client_ump_info(struct snd_seq_client *caller,
2169 					 unsigned int cmd,
2170 					 unsigned long arg)
2171 {
2172 	struct snd_seq_client_ump_info __user *argp =
2173 		(struct snd_seq_client_ump_info __user *)arg;
2174 	struct snd_seq_client *cptr;
2175 	int client, type, err = 0;
2176 	size_t size;
2177 	void *p;
2178 
2179 	if (get_user(client, &argp->client) || get_user(type, &argp->type))
2180 		return -EFAULT;
2181 	if (cmd == SNDRV_SEQ_IOCTL_SET_CLIENT_UMP_INFO &&
2182 	    caller->number != client)
2183 		return -EPERM;
2184 	if (type < 0 || type >= NUM_UMP_INFOS)
2185 		return -EINVAL;
2186 	if (type == SNDRV_SEQ_CLIENT_UMP_INFO_ENDPOINT)
2187 		size = sizeof(struct snd_ump_endpoint_info);
2188 	else
2189 		size = sizeof(struct snd_ump_block_info);
2190 	cptr = snd_seq_client_use_ptr(client);
2191 	if (!cptr)
2192 		return -ENOENT;
2193 
2194 	mutex_lock(&cptr->ioctl_mutex);
2195 	if (!cptr->midi_version) {
2196 		err = -EBADFD;
2197 		goto error;
2198 	}
2199 
2200 	if (cmd == SNDRV_SEQ_IOCTL_GET_CLIENT_UMP_INFO) {
2201 		if (!cptr->ump_info)
2202 			p = NULL;
2203 		else
2204 			p = cptr->ump_info[type];
2205 		if (!p) {
2206 			err = -ENODEV;
2207 			goto error;
2208 		}
2209 		if (copy_to_user(argp->info, p, size)) {
2210 			err = -EFAULT;
2211 			goto error;
2212 		}
2213 	} else {
2214 		if (cptr->type != USER_CLIENT) {
2215 			err = -EBADFD;
2216 			goto error;
2217 		}
2218 		if (!cptr->ump_info) {
2219 			cptr->ump_info = kcalloc(NUM_UMP_INFOS,
2220 						 sizeof(void *), GFP_KERNEL);
2221 			if (!cptr->ump_info) {
2222 				err = -ENOMEM;
2223 				goto error;
2224 			}
2225 		}
2226 		p = memdup_user(argp->info, size);
2227 		if (IS_ERR(p)) {
2228 			err = PTR_ERR(p);
2229 			goto error;
2230 		}
2231 		kfree(cptr->ump_info[type]);
2232 		terminate_ump_info_strings(p, type);
2233 		cptr->ump_info[type] = p;
2234 	}
2235 
2236  error:
2237 	mutex_unlock(&cptr->ioctl_mutex);
2238 	snd_seq_client_unlock(cptr);
2239 	if (!err && cmd == SNDRV_SEQ_IOCTL_SET_CLIENT_UMP_INFO) {
2240 		if (type == SNDRV_SEQ_CLIENT_UMP_INFO_ENDPOINT)
2241 			snd_seq_system_ump_notify(client, 0,
2242 						  SNDRV_SEQ_EVENT_UMP_EP_CHANGE,
2243 						  false);
2244 		else
2245 			snd_seq_system_ump_notify(client, type - 1,
2246 						  SNDRV_SEQ_EVENT_UMP_BLOCK_CHANGE,
2247 						  false);
2248 	}
2249 	return err;
2250 }
2251 #endif
2252 
2253 /* -------------------------------------------------------- */
2254 
2255 static const struct ioctl_handler {
2256 	unsigned int cmd;
2257 	int (*func)(struct snd_seq_client *client, void *arg);
2258 } ioctl_handlers[] = {
2259 	{ SNDRV_SEQ_IOCTL_PVERSION, snd_seq_ioctl_pversion },
2260 	{ SNDRV_SEQ_IOCTL_USER_PVERSION, snd_seq_ioctl_user_pversion },
2261 	{ SNDRV_SEQ_IOCTL_CLIENT_ID, snd_seq_ioctl_client_id },
2262 	{ SNDRV_SEQ_IOCTL_SYSTEM_INFO, snd_seq_ioctl_system_info },
2263 	{ SNDRV_SEQ_IOCTL_RUNNING_MODE, snd_seq_ioctl_running_mode },
2264 	{ SNDRV_SEQ_IOCTL_GET_CLIENT_INFO, snd_seq_ioctl_get_client_info },
2265 	{ SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, snd_seq_ioctl_set_client_info },
2266 	{ SNDRV_SEQ_IOCTL_CREATE_PORT, snd_seq_ioctl_create_port },
2267 	{ SNDRV_SEQ_IOCTL_DELETE_PORT, snd_seq_ioctl_delete_port },
2268 	{ SNDRV_SEQ_IOCTL_GET_PORT_INFO, snd_seq_ioctl_get_port_info },
2269 	{ SNDRV_SEQ_IOCTL_SET_PORT_INFO, snd_seq_ioctl_set_port_info },
2270 	{ SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, snd_seq_ioctl_subscribe_port },
2271 	{ SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, snd_seq_ioctl_unsubscribe_port },
2272 	{ SNDRV_SEQ_IOCTL_CREATE_QUEUE, snd_seq_ioctl_create_queue },
2273 	{ SNDRV_SEQ_IOCTL_DELETE_QUEUE, snd_seq_ioctl_delete_queue },
2274 	{ SNDRV_SEQ_IOCTL_GET_QUEUE_INFO, snd_seq_ioctl_get_queue_info },
2275 	{ SNDRV_SEQ_IOCTL_SET_QUEUE_INFO, snd_seq_ioctl_set_queue_info },
2276 	{ SNDRV_SEQ_IOCTL_GET_NAMED_QUEUE, snd_seq_ioctl_get_named_queue },
2277 	{ SNDRV_SEQ_IOCTL_GET_QUEUE_STATUS, snd_seq_ioctl_get_queue_status },
2278 	{ SNDRV_SEQ_IOCTL_GET_QUEUE_TEMPO, snd_seq_ioctl_get_queue_tempo },
2279 	{ SNDRV_SEQ_IOCTL_SET_QUEUE_TEMPO, snd_seq_ioctl_set_queue_tempo },
2280 	{ SNDRV_SEQ_IOCTL_GET_QUEUE_TIMER, snd_seq_ioctl_get_queue_timer },
2281 	{ SNDRV_SEQ_IOCTL_SET_QUEUE_TIMER, snd_seq_ioctl_set_queue_timer },
2282 	{ SNDRV_SEQ_IOCTL_GET_QUEUE_CLIENT, snd_seq_ioctl_get_queue_client },
2283 	{ SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT, snd_seq_ioctl_set_queue_client },
2284 	{ SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, snd_seq_ioctl_get_client_pool },
2285 	{ SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, snd_seq_ioctl_set_client_pool },
2286 	{ SNDRV_SEQ_IOCTL_GET_SUBSCRIPTION, snd_seq_ioctl_get_subscription },
2287 	{ SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT, snd_seq_ioctl_query_next_client },
2288 	{ SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, snd_seq_ioctl_query_next_port },
2289 	{ SNDRV_SEQ_IOCTL_REMOVE_EVENTS, snd_seq_ioctl_remove_events },
2290 	{ SNDRV_SEQ_IOCTL_QUERY_SUBS, snd_seq_ioctl_query_subs },
2291 	{ 0, NULL },
2292 };
2293 
2294 static long snd_seq_ioctl(struct file *file, unsigned int cmd,
2295 			  unsigned long arg)
2296 {
2297 	struct snd_seq_client *client = file->private_data;
2298 	/* To use kernel stack for ioctl data. */
2299 	union {
2300 		int pversion;
2301 		int client_id;
2302 		struct snd_seq_system_info	system_info;
2303 		struct snd_seq_running_info	running_info;
2304 		struct snd_seq_client_info	client_info;
2305 		struct snd_seq_port_info	port_info;
2306 		struct snd_seq_port_subscribe	port_subscribe;
2307 		struct snd_seq_queue_info	queue_info;
2308 		struct snd_seq_queue_status	queue_status;
2309 		struct snd_seq_queue_tempo	tempo;
2310 		struct snd_seq_queue_timer	queue_timer;
2311 		struct snd_seq_queue_client	queue_client;
2312 		struct snd_seq_client_pool	client_pool;
2313 		struct snd_seq_remove_events	remove_events;
2314 		struct snd_seq_query_subs	query_subs;
2315 	} buf;
2316 	const struct ioctl_handler *handler;
2317 	unsigned long size;
2318 	int err;
2319 
2320 	if (snd_BUG_ON(!client))
2321 		return -ENXIO;
2322 
2323 #if IS_ENABLED(CONFIG_SND_SEQ_UMP)
2324 	/* exception - handling large data */
2325 	switch (cmd) {
2326 	case SNDRV_SEQ_IOCTL_GET_CLIENT_UMP_INFO:
2327 	case SNDRV_SEQ_IOCTL_SET_CLIENT_UMP_INFO:
2328 		return snd_seq_ioctl_client_ump_info(client, cmd, arg);
2329 	}
2330 #endif
2331 
2332 	for (handler = ioctl_handlers; handler->cmd > 0; ++handler) {
2333 		if (handler->cmd == cmd)
2334 			break;
2335 	}
2336 	if (handler->cmd == 0)
2337 		return -ENOTTY;
2338 
2339 	memset(&buf, 0, sizeof(buf));
2340 
2341 	/*
2342 	 * All of ioctl commands for ALSA sequencer get an argument of size
2343 	 * within 13 bits. We can safely pick up the size from the command.
2344 	 */
2345 	size = _IOC_SIZE(handler->cmd);
2346 	if (handler->cmd & IOC_IN) {
2347 		if (copy_from_user(&buf, (const void __user *)arg, size))
2348 			return -EFAULT;
2349 	}
2350 
2351 	mutex_lock(&client->ioctl_mutex);
2352 	err = handler->func(client, &buf);
2353 	mutex_unlock(&client->ioctl_mutex);
2354 	if (err >= 0) {
2355 		/* Some commands includes a bug in 'dir' field. */
2356 		if (handler->cmd == SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT ||
2357 		    handler->cmd == SNDRV_SEQ_IOCTL_SET_CLIENT_POOL ||
2358 		    (handler->cmd & IOC_OUT))
2359 			if (copy_to_user((void __user *)arg, &buf, size))
2360 				return -EFAULT;
2361 	}
2362 
2363 	return err;
2364 }
2365 
2366 #ifdef CONFIG_COMPAT
2367 #include "seq_compat.c"
2368 #else
2369 #define snd_seq_ioctl_compat	NULL
2370 #endif
2371 
2372 /* -------------------------------------------------------- */
2373 
2374 
2375 /* exported to kernel modules */
2376 int snd_seq_create_kernel_client(struct snd_card *card, int client_index,
2377 				 const char *name_fmt, ...)
2378 {
2379 	struct snd_seq_client *client;
2380 	va_list args;
2381 
2382 	if (snd_BUG_ON(in_interrupt()))
2383 		return -EBUSY;
2384 
2385 	if (card && client_index >= SNDRV_SEQ_CLIENTS_PER_CARD)
2386 		return -EINVAL;
2387 	if (card == NULL && client_index >= SNDRV_SEQ_GLOBAL_CLIENTS)
2388 		return -EINVAL;
2389 
2390 	mutex_lock(&register_mutex);
2391 
2392 	if (card) {
2393 		client_index += SNDRV_SEQ_GLOBAL_CLIENTS
2394 			+ card->number * SNDRV_SEQ_CLIENTS_PER_CARD;
2395 		if (client_index >= SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN)
2396 			client_index = -1;
2397 	}
2398 
2399 	/* empty write queue as default */
2400 	client = seq_create_client1(client_index, 0);
2401 	if (client == NULL) {
2402 		mutex_unlock(&register_mutex);
2403 		return -EBUSY;	/* failure code */
2404 	}
2405 	usage_alloc(&client_usage, 1);
2406 
2407 	client->accept_input = 1;
2408 	client->accept_output = 1;
2409 	client->data.kernel.card = card;
2410 	client->user_pversion = SNDRV_SEQ_VERSION;
2411 
2412 	va_start(args, name_fmt);
2413 	vsnprintf(client->name, sizeof(client->name), name_fmt, args);
2414 	va_end(args);
2415 
2416 	client->type = KERNEL_CLIENT;
2417 	mutex_unlock(&register_mutex);
2418 
2419 	/* make others aware this new client */
2420 	snd_seq_system_client_ev_client_start(client->number);
2421 
2422 	/* return client number to caller */
2423 	return client->number;
2424 }
2425 EXPORT_SYMBOL(snd_seq_create_kernel_client);
2426 
2427 /* exported to kernel modules */
2428 int snd_seq_delete_kernel_client(int client)
2429 {
2430 	struct snd_seq_client *ptr;
2431 
2432 	if (snd_BUG_ON(in_interrupt()))
2433 		return -EBUSY;
2434 
2435 	ptr = clientptr(client);
2436 	if (ptr == NULL)
2437 		return -EINVAL;
2438 
2439 	seq_free_client(ptr);
2440 	kfree(ptr);
2441 	return 0;
2442 }
2443 EXPORT_SYMBOL(snd_seq_delete_kernel_client);
2444 
2445 /*
2446  * exported, called by kernel clients to enqueue events (w/o blocking)
2447  *
2448  * RETURN VALUE: zero if succeed, negative if error
2449  */
2450 int snd_seq_kernel_client_enqueue(int client, struct snd_seq_event *ev,
2451 				  struct file *file, bool blocking)
2452 {
2453 	struct snd_seq_client *cptr;
2454 	int result;
2455 
2456 	if (snd_BUG_ON(!ev))
2457 		return -EINVAL;
2458 
2459 	if (!snd_seq_ev_is_ump(ev)) {
2460 		if (ev->type == SNDRV_SEQ_EVENT_NONE)
2461 			return 0; /* ignore this */
2462 		if (ev->type == SNDRV_SEQ_EVENT_KERNEL_ERROR)
2463 			return -EINVAL; /* quoted events can't be enqueued */
2464 	}
2465 
2466 	/* fill in client number */
2467 	ev->source.client = client;
2468 
2469 	if (check_event_type_and_length(ev))
2470 		return -EINVAL;
2471 
2472 	cptr = snd_seq_client_use_ptr(client);
2473 	if (cptr == NULL)
2474 		return -EINVAL;
2475 
2476 	if (!cptr->accept_output) {
2477 		result = -EPERM;
2478 	} else { /* send it */
2479 		mutex_lock(&cptr->ioctl_mutex);
2480 		result = snd_seq_client_enqueue_event(cptr, ev, file, blocking,
2481 						      false, 0,
2482 						      &cptr->ioctl_mutex);
2483 		mutex_unlock(&cptr->ioctl_mutex);
2484 	}
2485 
2486 	snd_seq_client_unlock(cptr);
2487 	return result;
2488 }
2489 EXPORT_SYMBOL(snd_seq_kernel_client_enqueue);
2490 
2491 /*
2492  * exported, called by kernel clients to dispatch events directly to other
2493  * clients, bypassing the queues.  Event time-stamp will be updated.
2494  *
2495  * RETURN VALUE: negative = delivery failed,
2496  *		 zero, or positive: the number of delivered events
2497  */
2498 int snd_seq_kernel_client_dispatch(int client, struct snd_seq_event * ev,
2499 				   int atomic, int hop)
2500 {
2501 	struct snd_seq_client *cptr;
2502 	int result;
2503 
2504 	if (snd_BUG_ON(!ev))
2505 		return -EINVAL;
2506 
2507 	/* fill in client number */
2508 	ev->queue = SNDRV_SEQ_QUEUE_DIRECT;
2509 	ev->source.client = client;
2510 
2511 	if (check_event_type_and_length(ev))
2512 		return -EINVAL;
2513 
2514 	cptr = snd_seq_client_use_ptr(client);
2515 	if (cptr == NULL)
2516 		return -EINVAL;
2517 
2518 	if (!cptr->accept_output)
2519 		result = -EPERM;
2520 	else
2521 		result = snd_seq_deliver_event(cptr, ev, atomic, hop);
2522 
2523 	snd_seq_client_unlock(cptr);
2524 	return result;
2525 }
2526 EXPORT_SYMBOL(snd_seq_kernel_client_dispatch);
2527 
2528 /**
2529  * snd_seq_kernel_client_ctl - operate a command for a client with data in
2530  *			       kernel space.
2531  * @clientid:	A numerical ID for a client.
2532  * @cmd:	An ioctl(2) command for ALSA sequencer operation.
2533  * @arg:	A pointer to data in kernel space.
2534  *
2535  * Against its name, both kernel/application client can be handled by this
2536  * kernel API. A pointer of 'arg' argument should be in kernel space.
2537  *
2538  * Return: 0 at success. Negative error code at failure.
2539  */
2540 int snd_seq_kernel_client_ctl(int clientid, unsigned int cmd, void *arg)
2541 {
2542 	const struct ioctl_handler *handler;
2543 	struct snd_seq_client *client;
2544 
2545 	client = clientptr(clientid);
2546 	if (client == NULL)
2547 		return -ENXIO;
2548 
2549 	for (handler = ioctl_handlers; handler->cmd > 0; ++handler) {
2550 		if (handler->cmd == cmd)
2551 			return handler->func(client, arg);
2552 	}
2553 
2554 	pr_debug("ALSA: seq unknown ioctl() 0x%x (type='%c', number=0x%02x)\n",
2555 		 cmd, _IOC_TYPE(cmd), _IOC_NR(cmd));
2556 	return -ENOTTY;
2557 }
2558 EXPORT_SYMBOL(snd_seq_kernel_client_ctl);
2559 
2560 /* exported (for OSS emulator) */
2561 int snd_seq_kernel_client_write_poll(int clientid, struct file *file, poll_table *wait)
2562 {
2563 	struct snd_seq_client *client;
2564 
2565 	client = clientptr(clientid);
2566 	if (client == NULL)
2567 		return -ENXIO;
2568 
2569 	if (! snd_seq_write_pool_allocated(client))
2570 		return 1;
2571 	if (snd_seq_pool_poll_wait(client->pool, file, wait))
2572 		return 1;
2573 	return 0;
2574 }
2575 EXPORT_SYMBOL(snd_seq_kernel_client_write_poll);
2576 
2577 /* get a sequencer client object; for internal use from a kernel client */
2578 struct snd_seq_client *snd_seq_kernel_client_get(int id)
2579 {
2580 	return snd_seq_client_use_ptr(id);
2581 }
2582 EXPORT_SYMBOL_GPL(snd_seq_kernel_client_get);
2583 
2584 /* put a sequencer client object; for internal use from a kernel client */
2585 void snd_seq_kernel_client_put(struct snd_seq_client *cptr)
2586 {
2587 	if (cptr)
2588 		snd_seq_client_unlock(cptr);
2589 }
2590 EXPORT_SYMBOL_GPL(snd_seq_kernel_client_put);
2591 
2592 /*---------------------------------------------------------------------------*/
2593 
2594 #ifdef CONFIG_SND_PROC_FS
2595 /*
2596  *  /proc interface
2597  */
2598 static void snd_seq_info_dump_subscribers(struct snd_info_buffer *buffer,
2599 					  struct snd_seq_port_subs_info *group,
2600 					  int is_src, char *msg)
2601 {
2602 	struct list_head *p;
2603 	struct snd_seq_subscribers *s;
2604 	int count = 0;
2605 
2606 	down_read(&group->list_mutex);
2607 	if (list_empty(&group->list_head)) {
2608 		up_read(&group->list_mutex);
2609 		return;
2610 	}
2611 	snd_iprintf(buffer, msg);
2612 	list_for_each(p, &group->list_head) {
2613 		if (is_src)
2614 			s = list_entry(p, struct snd_seq_subscribers, src_list);
2615 		else
2616 			s = list_entry(p, struct snd_seq_subscribers, dest_list);
2617 		if (count++)
2618 			snd_iprintf(buffer, ", ");
2619 		snd_iprintf(buffer, "%d:%d",
2620 			    is_src ? s->info.dest.client : s->info.sender.client,
2621 			    is_src ? s->info.dest.port : s->info.sender.port);
2622 		if (s->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)
2623 			snd_iprintf(buffer, "[%c:%d]", ((s->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL) ? 'r' : 't'), s->info.queue);
2624 		if (group->exclusive)
2625 			snd_iprintf(buffer, "[ex]");
2626 	}
2627 	up_read(&group->list_mutex);
2628 	snd_iprintf(buffer, "\n");
2629 }
2630 
2631 #define FLAG_PERM_RD(perm) ((perm) & SNDRV_SEQ_PORT_CAP_READ ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_READ ? 'R' : 'r') : '-')
2632 #define FLAG_PERM_WR(perm) ((perm) & SNDRV_SEQ_PORT_CAP_WRITE ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_WRITE ? 'W' : 'w') : '-')
2633 #define FLAG_PERM_EX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_NO_EXPORT ? '-' : 'e')
2634 
2635 #define FLAG_PERM_DUPLEX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_DUPLEX ? 'X' : '-')
2636 
2637 static const char *port_direction_name(unsigned char dir)
2638 {
2639 	static const char *names[4] = {
2640 		"-", "In", "Out", "In/Out"
2641 	};
2642 
2643 	if (dir > SNDRV_SEQ_PORT_DIR_BIDIRECTION)
2644 		return "Invalid";
2645 	return names[dir];
2646 }
2647 
2648 static void snd_seq_info_dump_ports(struct snd_info_buffer *buffer,
2649 				    struct snd_seq_client *client)
2650 {
2651 	struct snd_seq_client_port *p;
2652 
2653 	mutex_lock(&client->ports_mutex);
2654 	list_for_each_entry(p, &client->ports_list_head, list) {
2655 		if (p->capability & SNDRV_SEQ_PORT_CAP_INACTIVE)
2656 			continue;
2657 		snd_iprintf(buffer, "  Port %3d : \"%s\" (%c%c%c%c) [%s]",
2658 			    p->addr.port, p->name,
2659 			    FLAG_PERM_RD(p->capability),
2660 			    FLAG_PERM_WR(p->capability),
2661 			    FLAG_PERM_EX(p->capability),
2662 			    FLAG_PERM_DUPLEX(p->capability),
2663 			    port_direction_name(p->direction));
2664 #if IS_ENABLED(CONFIG_SND_SEQ_UMP)
2665 		if (snd_seq_client_is_midi2(client) && p->is_midi1)
2666 			snd_iprintf(buffer, " [MIDI1]");
2667 #endif
2668 		snd_iprintf(buffer, "\n");
2669 		snd_seq_info_dump_subscribers(buffer, &p->c_src, 1, "    Connecting To: ");
2670 		snd_seq_info_dump_subscribers(buffer, &p->c_dest, 0, "    Connected From: ");
2671 	}
2672 	mutex_unlock(&client->ports_mutex);
2673 }
2674 
2675 static const char *midi_version_string(unsigned int version)
2676 {
2677 	switch (version) {
2678 	case SNDRV_SEQ_CLIENT_LEGACY_MIDI:
2679 		return "Legacy";
2680 	case SNDRV_SEQ_CLIENT_UMP_MIDI_1_0:
2681 		return "UMP MIDI1";
2682 	case SNDRV_SEQ_CLIENT_UMP_MIDI_2_0:
2683 		return "UMP MIDI2";
2684 	default:
2685 		return "Unknown";
2686 	}
2687 }
2688 
2689 /* exported to seq_info.c */
2690 void snd_seq_info_clients_read(struct snd_info_entry *entry,
2691 			       struct snd_info_buffer *buffer)
2692 {
2693 	int c;
2694 	struct snd_seq_client *client;
2695 
2696 	snd_iprintf(buffer, "Client info\n");
2697 	snd_iprintf(buffer, "  cur  clients : %d\n", client_usage.cur);
2698 	snd_iprintf(buffer, "  peak clients : %d\n", client_usage.peak);
2699 	snd_iprintf(buffer, "  max  clients : %d\n", SNDRV_SEQ_MAX_CLIENTS);
2700 	snd_iprintf(buffer, "\n");
2701 
2702 	/* list the client table */
2703 	for (c = 0; c < SNDRV_SEQ_MAX_CLIENTS; c++) {
2704 		client = snd_seq_client_use_ptr(c);
2705 		if (client == NULL)
2706 			continue;
2707 		if (client->type == NO_CLIENT) {
2708 			snd_seq_client_unlock(client);
2709 			continue;
2710 		}
2711 
2712 		snd_iprintf(buffer, "Client %3d : \"%s\" [%s %s]\n",
2713 			    c, client->name,
2714 			    client->type == USER_CLIENT ? "User" : "Kernel",
2715 			    midi_version_string(client->midi_version));
2716 #if IS_ENABLED(CONFIG_SND_SEQ_UMP)
2717 		dump_ump_info(buffer, client);
2718 #endif
2719 		snd_seq_info_dump_ports(buffer, client);
2720 		if (snd_seq_write_pool_allocated(client)) {
2721 			snd_iprintf(buffer, "  Output pool :\n");
2722 			snd_seq_info_pool(buffer, client->pool, "    ");
2723 		}
2724 		if (client->type == USER_CLIENT && client->data.user.fifo &&
2725 		    client->data.user.fifo->pool) {
2726 			snd_iprintf(buffer, "  Input pool :\n");
2727 			snd_seq_info_pool(buffer, client->data.user.fifo->pool, "    ");
2728 		}
2729 		snd_seq_client_unlock(client);
2730 	}
2731 }
2732 #endif /* CONFIG_SND_PROC_FS */
2733 
2734 /*---------------------------------------------------------------------------*/
2735 
2736 
2737 /*
2738  *  REGISTRATION PART
2739  */
2740 
2741 static const struct file_operations snd_seq_f_ops =
2742 {
2743 	.owner =	THIS_MODULE,
2744 	.read =		snd_seq_read,
2745 	.write =	snd_seq_write,
2746 	.open =		snd_seq_open,
2747 	.release =	snd_seq_release,
2748 	.poll =		snd_seq_poll,
2749 	.unlocked_ioctl =	snd_seq_ioctl,
2750 	.compat_ioctl =	snd_seq_ioctl_compat,
2751 };
2752 
2753 static struct device *seq_dev;
2754 
2755 /*
2756  * register sequencer device
2757  */
2758 int __init snd_sequencer_device_init(void)
2759 {
2760 	int err;
2761 
2762 	err = snd_device_alloc(&seq_dev, NULL);
2763 	if (err < 0)
2764 		return err;
2765 	dev_set_name(seq_dev, "seq");
2766 
2767 	mutex_lock(&register_mutex);
2768 	err = snd_register_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0,
2769 				  &snd_seq_f_ops, NULL, seq_dev);
2770 	mutex_unlock(&register_mutex);
2771 	if (err < 0) {
2772 		put_device(seq_dev);
2773 		return err;
2774 	}
2775 
2776 	return 0;
2777 }
2778 
2779 
2780 
2781 /*
2782  * unregister sequencer device
2783  */
2784 void snd_sequencer_device_done(void)
2785 {
2786 	snd_unregister_device(seq_dev);
2787 	put_device(seq_dev);
2788 }
2789