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