ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / pci / emu10k1 / voice.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *                   Creative Labs, Inc.
4  *  Routines for control of EMU10K1 chips - voice manager
5  *
6  *  BUGS:
7  *    --
8  *
9  *  TODO:
10  *    --
11  *
12  *   This program is free software; you can redistribute it and/or modify
13  *   it under the terms of the GNU General Public License as published by
14  *   the Free Software Foundation; either version 2 of the License, or
15  *   (at your option) any later version.
16  *
17  *   This program is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with this program; if not, write to the Free Software
24  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25  *
26  */
27
28 #include <sound/driver.h>
29 #include <linux/time.h>
30 #include <sound/core.h>
31 #include <sound/emu10k1.h>
32
33 static int voice_alloc(emu10k1_t *emu, emu10k1_voice_type_t type, int pair, emu10k1_voice_t **rvoice)
34 {
35         emu10k1_voice_t *voice, *voice2;
36         int idx;
37
38         *rvoice = NULL;
39         for (idx = 0; idx < 64; idx += pair ? 2 : 1) {
40                 voice = &emu->voices[idx];
41                 voice2 = pair ? &emu->voices[idx+1] : NULL;
42                 if (voice->use || (voice2 && voice2->use))
43                         continue;
44                 voice->use = 1;
45                 if (voice2)
46                         voice2->use = 1;
47                 switch (type) {
48                 case EMU10K1_PCM:
49                         voice->pcm = 1;
50                         if (voice2)
51                                 voice2->pcm = 1;
52                         break;
53                 case EMU10K1_SYNTH:
54                         voice->synth = 1;
55                         break;
56                 case EMU10K1_MIDI:
57                         voice->midi = 1;
58                         break;
59                 }
60                 // printk("voice alloc - %i, pair = %i\n", voice->number, pair);
61                 *rvoice = voice;
62                 return 0;
63         }
64         return -ENOMEM;
65 }
66
67 int snd_emu10k1_voice_alloc(emu10k1_t *emu, emu10k1_voice_type_t type, int pair, emu10k1_voice_t **rvoice)
68 {
69         unsigned long flags;
70         int result;
71
72         snd_assert(rvoice != NULL, return -EINVAL);
73         snd_assert(!pair || type == EMU10K1_PCM, return -EINVAL);
74
75         spin_lock_irqsave(&emu->voice_lock, flags);
76         for (;;) {
77                 result = voice_alloc(emu, type, pair, rvoice);
78                 if (result == 0 || type != EMU10K1_PCM)
79                         break;
80
81                 /* free a voice from synth */
82                 if (emu->get_synth_voice) {
83                         result = emu->get_synth_voice(emu);
84                         if (result >= 0) {
85                                 emu10k1_voice_t *pvoice = &emu->voices[result];
86                                 pvoice->interrupt = NULL;
87                                 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
88                                 pvoice->epcm = NULL;
89                         }
90                 }
91                 if (result < 0)
92                         break;
93         }
94         spin_unlock_irqrestore(&emu->voice_lock, flags);
95
96         return result;
97 }
98
99 int snd_emu10k1_voice_free(emu10k1_t *emu, emu10k1_voice_t *pvoice)
100 {
101         unsigned long flags;
102
103         snd_assert(pvoice != NULL, return -EINVAL);
104         spin_lock_irqsave(&emu->voice_lock, flags);
105         pvoice->interrupt = NULL;
106         pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
107         pvoice->epcm = NULL;
108         snd_emu10k1_voice_init(emu, pvoice->number);
109         spin_unlock_irqrestore(&emu->voice_lock, flags);
110         return 0;
111 }