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