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