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