2 * Generic Instrument routines for ALSA sequencer
3 * Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <sound/driver.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <sound/core.h>
25 #include "seq_clientmgr.h"
26 #include <sound/seq_instr.h>
27 #include <sound/initval.h>
29 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
30 MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer instrument library.");
31 MODULE_LICENSE("GPL");
34 static void snd_instr_lock_ops(snd_seq_kinstr_list_t *list)
36 if (!(list->flags & SNDRV_SEQ_INSTR_FLG_DIRECT)) {
37 spin_lock_irqsave(&list->ops_lock, list->ops_flags);
39 down(&list->ops_mutex);
43 static void snd_instr_unlock_ops(snd_seq_kinstr_list_t *list)
45 if (!(list->flags & SNDRV_SEQ_INSTR_FLG_DIRECT)) {
46 spin_unlock_irqrestore(&list->ops_lock, list->ops_flags);
52 snd_seq_kcluster_t *snd_seq_cluster_new(int atomic)
54 return kcalloc(1, sizeof(snd_seq_kcluster_t), atomic ? GFP_ATOMIC : GFP_KERNEL);
57 void snd_seq_cluster_free(snd_seq_kcluster_t *cluster, int atomic)
64 snd_seq_kinstr_t *snd_seq_instr_new(int add_len, int atomic)
66 snd_seq_kinstr_t *instr;
68 instr = kcalloc(1, sizeof(snd_seq_kinstr_t) + add_len, atomic ? GFP_ATOMIC : GFP_KERNEL);
71 instr->add_len = add_len;
75 int snd_seq_instr_free(snd_seq_kinstr_t *instr, int atomic)
81 if (instr->ops && instr->ops->remove)
82 result = instr->ops->remove(instr->ops->private_data, instr, 1);
88 snd_seq_kinstr_list_t *snd_seq_instr_list_new(void)
90 snd_seq_kinstr_list_t *list;
92 list = kcalloc(1, sizeof(snd_seq_kinstr_list_t), GFP_KERNEL);
95 spin_lock_init(&list->lock);
96 spin_lock_init(&list->ops_lock);
97 init_MUTEX(&list->ops_mutex);
102 void snd_seq_instr_list_free(snd_seq_kinstr_list_t **list_ptr)
104 snd_seq_kinstr_list_t *list;
105 snd_seq_kinstr_t *instr;
106 snd_seq_kcluster_t *cluster;
110 if (list_ptr == NULL)
117 for (idx = 0; idx < SNDRV_SEQ_INSTR_HASH_SIZE; idx++) {
118 while ((instr = list->hash[idx]) != NULL) {
119 list->hash[idx] = instr->next;
121 spin_lock_irqsave(&list->lock, flags);
123 spin_unlock_irqrestore(&list->lock, flags);
124 set_current_state(TASK_INTERRUPTIBLE);
126 spin_lock_irqsave(&list->lock, flags);
128 spin_unlock_irqrestore(&list->lock, flags);
129 if (snd_seq_instr_free(instr, 0)<0)
130 snd_printk(KERN_WARNING "instrument free problem\n");
132 while ((cluster = list->chash[idx]) != NULL) {
133 list->chash[idx] = cluster->next;
135 snd_seq_cluster_free(cluster, 0);
141 static int instr_free_compare(snd_seq_kinstr_t *instr,
142 snd_seq_instr_header_t *ifree,
145 switch (ifree->cmd) {
146 case SNDRV_SEQ_INSTR_FREE_CMD_ALL:
147 /* all, except private for other clients */
148 if ((instr->instr.std & 0xff000000) == 0)
150 if (((instr->instr.std >> 24) & 0xff) == client)
153 case SNDRV_SEQ_INSTR_FREE_CMD_PRIVATE:
154 /* all my private instruments */
155 if ((instr->instr.std & 0xff000000) == 0)
157 if (((instr->instr.std >> 24) & 0xff) == client)
160 case SNDRV_SEQ_INSTR_FREE_CMD_CLUSTER:
161 /* all my private instruments */
162 if ((instr->instr.std & 0xff000000) == 0) {
163 if (instr->instr.cluster == ifree->id.cluster)
167 if (((instr->instr.std >> 24) & 0xff) == client) {
168 if (instr->instr.cluster == ifree->id.cluster)
176 int snd_seq_instr_list_free_cond(snd_seq_kinstr_list_t *list,
177 snd_seq_instr_header_t *ifree,
181 snd_seq_kinstr_t *instr, *prev, *next, *flist;
185 snd_instr_lock_ops(list);
186 for (idx = 0; idx < SNDRV_SEQ_INSTR_HASH_SIZE; idx++) {
187 spin_lock_irqsave(&list->lock, flags);
188 instr = list->hash[idx];
191 while (instr && instr_free_compare(instr, ifree, (unsigned int)client)) {
197 if (instr->ops && instr->ops->notify)
198 instr->ops->notify(instr->ops->private_data, instr, SNDRV_SEQ_INSTR_NOTIFY_REMOVE);
201 list->hash[idx] = next;
210 spin_unlock_irqrestore(&list->lock, flags);
215 set_current_state(TASK_INTERRUPTIBLE);
218 if (snd_seq_instr_free(instr, atomic)<0)
219 snd_printk(KERN_WARNING "instrument free problem\n");
223 snd_instr_unlock_ops(list);
227 static int compute_hash_instr_key(snd_seq_instr_t *instr)
231 result = instr->bank | (instr->prg << 16);
232 result += result >> 24;
233 result += result >> 16;
234 result += result >> 8;
235 return result & (SNDRV_SEQ_INSTR_HASH_SIZE-1);
239 static int compute_hash_cluster_key(snd_seq_instr_cluster_t cluster)
244 result += result >> 24;
245 result += result >> 16;
246 result += result >> 8;
247 return result & (SNDRV_SEQ_INSTR_HASH_SIZE-1);
251 static int compare_instr(snd_seq_instr_t *i1, snd_seq_instr_t *i2, int exact)
254 if (i1->cluster != i2->cluster ||
255 i1->bank != i2->bank ||
258 if ((i1->std & 0xff000000) != (i2->std & 0xff000000))
260 if (!(i1->std & i2->std))
264 unsigned int client_check;
266 if (i2->cluster && i1->cluster != i2->cluster)
268 client_check = i2->std & 0xff000000;
270 if ((i1->std & 0xff000000) != client_check)
273 if ((i1->std & i2->std) != i2->std)
276 return i1->bank != i2->bank || i1->prg != i2->prg;
280 snd_seq_kinstr_t *snd_seq_instr_find(snd_seq_kinstr_list_t *list,
281 snd_seq_instr_t *instr,
287 snd_seq_kinstr_t *result;
289 if (list == NULL || instr == NULL)
291 spin_lock_irqsave(&list->lock, flags);
293 result = list->hash[compute_hash_instr_key(instr)];
295 if (!compare_instr(&result->instr, instr, exact)) {
296 if (follow_alias && (result->type == SNDRV_SEQ_INSTR_ATYPE_ALIAS)) {
297 instr = (snd_seq_instr_t *)KINSTR_DATA(result);
303 spin_unlock_irqrestore(&list->lock, flags);
306 result = result->next;
309 spin_unlock_irqrestore(&list->lock, flags);
313 void snd_seq_instr_free_use(snd_seq_kinstr_list_t *list,
314 snd_seq_kinstr_t *instr)
318 if (list == NULL || instr == NULL)
320 spin_lock_irqsave(&list->lock, flags);
321 if (instr->use <= 0) {
322 snd_printk(KERN_ERR "free_use: fatal!!! use = %i, name = '%s'\n", instr->use, instr->name);
326 spin_unlock_irqrestore(&list->lock, flags);
329 static snd_seq_kinstr_ops_t *instr_ops(snd_seq_kinstr_ops_t *ops, char *instr_type)
332 if (!strcmp(ops->instr_type, instr_type))
339 static int instr_result(snd_seq_event_t *ev,
340 int type, int result,
345 memset(&sev, 0, sizeof(sev));
346 sev.type = SNDRV_SEQ_EVENT_RESULT;
347 sev.flags = SNDRV_SEQ_TIME_STAMP_REAL | SNDRV_SEQ_EVENT_LENGTH_FIXED |
348 SNDRV_SEQ_PRIORITY_NORMAL;
349 sev.source = ev->dest;
350 sev.dest = ev->source;
351 sev.data.result.event = type;
352 sev.data.result.result = result;
354 printk("instr result - type = %i, result = %i, queue = %i, source.client:port = %i:%i, dest.client:port = %i:%i\n",
357 sev.source.client, sev.source.port,
358 sev.dest.client, sev.dest.port);
360 return snd_seq_kernel_client_dispatch(sev.source.client, &sev, atomic, 0);
363 static int instr_begin(snd_seq_kinstr_ops_t *ops,
364 snd_seq_kinstr_list_t *list,
370 spin_lock_irqsave(&list->lock, flags);
371 if (list->owner >= 0 && list->owner != ev->source.client) {
372 spin_unlock_irqrestore(&list->lock, flags);
373 return instr_result(ev, SNDRV_SEQ_EVENT_INSTR_BEGIN, -EBUSY, atomic);
375 list->owner = ev->source.client;
376 spin_unlock_irqrestore(&list->lock, flags);
377 return instr_result(ev, SNDRV_SEQ_EVENT_INSTR_BEGIN, 0, atomic);
380 static int instr_end(snd_seq_kinstr_ops_t *ops,
381 snd_seq_kinstr_list_t *list,
387 /* TODO: timeout handling */
388 spin_lock_irqsave(&list->lock, flags);
389 if (list->owner == ev->source.client) {
391 spin_unlock_irqrestore(&list->lock, flags);
392 return instr_result(ev, SNDRV_SEQ_EVENT_INSTR_END, 0, atomic);
394 spin_unlock_irqrestore(&list->lock, flags);
395 return instr_result(ev, SNDRV_SEQ_EVENT_INSTR_END, -EINVAL, atomic);
398 static int instr_info(snd_seq_kinstr_ops_t *ops,
399 snd_seq_kinstr_list_t *list,
406 static int instr_format_info(snd_seq_kinstr_ops_t *ops,
407 snd_seq_kinstr_list_t *list,
414 static int instr_reset(snd_seq_kinstr_ops_t *ops,
415 snd_seq_kinstr_list_t *list,
422 static int instr_status(snd_seq_kinstr_ops_t *ops,
423 snd_seq_kinstr_list_t *list,
430 static int instr_put(snd_seq_kinstr_ops_t *ops,
431 snd_seq_kinstr_list_t *list,
436 snd_seq_instr_header_t put;
437 snd_seq_kinstr_t *instr;
438 int result = -EINVAL, len, key;
440 if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARUSR)
443 if (ev->data.ext.len < sizeof(snd_seq_instr_header_t))
445 if (copy_from_user(&put, ev->data.ext.ptr, sizeof(snd_seq_instr_header_t))) {
449 snd_instr_lock_ops(list);
450 if (put.id.instr.std & 0xff000000) { /* private instrument */
451 put.id.instr.std &= 0x00ffffff;
452 put.id.instr.std |= (unsigned int)ev->source.client << 24;
454 if ((instr = snd_seq_instr_find(list, &put.id.instr, 1, 0))) {
455 snd_seq_instr_free_use(list, instr);
456 snd_instr_unlock_ops(list);
460 ops = instr_ops(ops, put.data.data.format);
462 snd_instr_unlock_ops(list);
466 if (put.data.type == SNDRV_SEQ_INSTR_ATYPE_ALIAS)
467 len = sizeof(snd_seq_instr_t);
468 instr = snd_seq_instr_new(len, atomic);
470 snd_instr_unlock_ops(list);
475 instr->instr = put.id.instr;
476 strlcpy(instr->name, put.data.name, sizeof(instr->name));
477 instr->type = put.data.type;
478 if (instr->type == SNDRV_SEQ_INSTR_ATYPE_DATA) {
479 result = ops->put(ops->private_data,
481 ev->data.ext.ptr + sizeof(snd_seq_instr_header_t),
482 ev->data.ext.len - sizeof(snd_seq_instr_header_t),
486 snd_seq_instr_free(instr, atomic);
487 snd_instr_unlock_ops(list);
491 key = compute_hash_instr_key(&instr->instr);
492 spin_lock_irqsave(&list->lock, flags);
493 instr->next = list->hash[key];
494 list->hash[key] = instr;
496 spin_unlock_irqrestore(&list->lock, flags);
497 snd_instr_unlock_ops(list);
500 instr_result(ev, SNDRV_SEQ_EVENT_INSTR_PUT, result, atomic);
504 static int instr_get(snd_seq_kinstr_ops_t *ops,
505 snd_seq_kinstr_list_t *list,
512 static int instr_free(snd_seq_kinstr_ops_t *ops,
513 snd_seq_kinstr_list_t *list,
517 snd_seq_instr_header_t ifree;
518 snd_seq_kinstr_t *instr, *prev;
519 int result = -EINVAL;
523 if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARUSR)
526 if (ev->data.ext.len < sizeof(snd_seq_instr_header_t))
528 if (copy_from_user(&ifree, ev->data.ext.ptr, sizeof(snd_seq_instr_header_t))) {
532 if (ifree.cmd == SNDRV_SEQ_INSTR_FREE_CMD_ALL ||
533 ifree.cmd == SNDRV_SEQ_INSTR_FREE_CMD_PRIVATE ||
534 ifree.cmd == SNDRV_SEQ_INSTR_FREE_CMD_CLUSTER) {
535 result = snd_seq_instr_list_free_cond(list, &ifree, ev->dest.client, atomic);
538 if (ifree.cmd == SNDRV_SEQ_INSTR_FREE_CMD_SINGLE) {
539 if (ifree.id.instr.std & 0xff000000) {
540 ifree.id.instr.std &= 0x00ffffff;
541 ifree.id.instr.std |= (unsigned int)ev->source.client << 24;
543 hash = compute_hash_instr_key(&ifree.id.instr);
544 snd_instr_lock_ops(list);
545 spin_lock_irqsave(&list->lock, flags);
546 instr = list->hash[hash];
549 if (!compare_instr(&instr->instr, &ifree.id.instr, 1))
555 spin_unlock_irqrestore(&list->lock, flags);
556 snd_instr_unlock_ops(list);
561 prev->next = instr->next;
563 list->hash[hash] = instr->next;
565 if (instr->ops && instr->ops->notify)
566 instr->ops->notify(instr->ops->private_data, instr, SNDRV_SEQ_INSTR_NOTIFY_REMOVE);
568 spin_unlock_irqrestore(&list->lock, flags);
569 set_current_state(TASK_INTERRUPTIBLE);
571 spin_lock_irqsave(&list->lock, flags);
573 spin_unlock_irqrestore(&list->lock, flags);
574 result = snd_seq_instr_free(instr, atomic);
575 snd_instr_unlock_ops(list);
580 instr_result(ev, SNDRV_SEQ_EVENT_INSTR_FREE, result, atomic);
584 static int instr_list(snd_seq_kinstr_ops_t *ops,
585 snd_seq_kinstr_list_t *list,
592 static int instr_cluster(snd_seq_kinstr_ops_t *ops,
593 snd_seq_kinstr_list_t *list,
600 int snd_seq_instr_event(snd_seq_kinstr_ops_t *ops,
601 snd_seq_kinstr_list_t *list,
609 snd_assert(ops != NULL && list != NULL && ev != NULL, return -EINVAL);
610 if (snd_seq_ev_is_direct(ev)) {
613 case SNDRV_SEQ_EVENT_INSTR_BEGIN:
614 return instr_begin(ops, list, ev, atomic, hop);
615 case SNDRV_SEQ_EVENT_INSTR_END:
616 return instr_end(ops, list, ev, atomic, hop);
619 if ((list->flags & SNDRV_SEQ_INSTR_FLG_DIRECT) && !direct)
622 case SNDRV_SEQ_EVENT_INSTR_INFO:
623 return instr_info(ops, list, ev, atomic, hop);
624 case SNDRV_SEQ_EVENT_INSTR_FINFO:
625 return instr_format_info(ops, list, ev, atomic, hop);
626 case SNDRV_SEQ_EVENT_INSTR_RESET:
627 return instr_reset(ops, list, ev, atomic, hop);
628 case SNDRV_SEQ_EVENT_INSTR_STATUS:
629 return instr_status(ops, list, ev, atomic, hop);
630 case SNDRV_SEQ_EVENT_INSTR_PUT:
631 return instr_put(ops, list, ev, atomic, hop);
632 case SNDRV_SEQ_EVENT_INSTR_GET:
633 return instr_get(ops, list, ev, atomic, hop);
634 case SNDRV_SEQ_EVENT_INSTR_FREE:
635 return instr_free(ops, list, ev, atomic, hop);
636 case SNDRV_SEQ_EVENT_INSTR_LIST:
637 return instr_list(ops, list, ev, atomic, hop);
638 case SNDRV_SEQ_EVENT_INSTR_CLUSTER:
639 return instr_cluster(ops, list, ev, atomic, hop);
648 static int __init alsa_seq_instr_init(void)
653 static void __exit alsa_seq_instr_exit(void)
657 module_init(alsa_seq_instr_init)
658 module_exit(alsa_seq_instr_exit)
660 EXPORT_SYMBOL(snd_seq_instr_list_new);
661 EXPORT_SYMBOL(snd_seq_instr_list_free);
662 EXPORT_SYMBOL(snd_seq_instr_list_free_cond);
663 EXPORT_SYMBOL(snd_seq_instr_find);
664 EXPORT_SYMBOL(snd_seq_instr_free_use);
665 EXPORT_SYMBOL(snd_seq_instr_event);