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