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