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