1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <linux/time.h>
32#include <linux/export.h>
33#include <sound/core.h>
34#include <sound/emu10k1.h>
35
36
37
38
39
40
41
42
43
44
45
46
47
48static int voice_alloc(struct snd_emu10k1 *emu, int type, int number,
49 struct snd_emu10k1_voice **rvoice)
50{
51 struct snd_emu10k1_voice *voice;
52 int i, j, k, first_voice, last_voice, skip;
53
54 *rvoice = NULL;
55 first_voice = last_voice = 0;
56 for (i = emu->next_free_voice, j = 0; j < NUM_G ; i += number, j += number) {
57
58
59
60
61 i %= NUM_G;
62
63
64 if ((number == 2) && (i % 2)) {
65 i++;
66 continue;
67 }
68
69 skip = 0;
70 for (k = 0; k < number; k++) {
71 voice = &emu->voices[(i+k) % NUM_G];
72 if (voice->use) {
73 skip = 1;
74 break;
75 }
76 }
77 if (!skip) {
78
79 first_voice = i;
80 last_voice = (i + number) % NUM_G;
81 emu->next_free_voice = last_voice;
82 break;
83 }
84 }
85
86 if (first_voice == last_voice)
87 return -ENOMEM;
88
89 for (i = 0; i < number; i++) {
90 voice = &emu->voices[(first_voice + i) % NUM_G];
91
92
93
94
95 voice->use = 1;
96 switch (type) {
97 case EMU10K1_PCM:
98 voice->pcm = 1;
99 break;
100 case EMU10K1_SYNTH:
101 voice->synth = 1;
102 break;
103 case EMU10K1_MIDI:
104 voice->midi = 1;
105 break;
106 case EMU10K1_EFX:
107 voice->efx = 1;
108 break;
109 }
110 }
111 *rvoice = &emu->voices[first_voice];
112 return 0;
113}
114
115int snd_emu10k1_voice_alloc(struct snd_emu10k1 *emu, int type, int number,
116 struct snd_emu10k1_voice **rvoice)
117{
118 unsigned long flags;
119 int result;
120
121 if (snd_BUG_ON(!rvoice))
122 return -EINVAL;
123 if (snd_BUG_ON(!number))
124 return -EINVAL;
125
126 spin_lock_irqsave(&emu->voice_lock, flags);
127 for (;;) {
128 result = voice_alloc(emu, type, number, rvoice);
129 if (result == 0 || type == EMU10K1_SYNTH || type == EMU10K1_MIDI)
130 break;
131
132
133 if (emu->get_synth_voice) {
134 result = emu->get_synth_voice(emu);
135 if (result >= 0) {
136 struct snd_emu10k1_voice *pvoice = &emu->voices[result];
137 pvoice->interrupt = NULL;
138 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
139 pvoice->epcm = NULL;
140 }
141 }
142 if (result < 0)
143 break;
144 }
145 spin_unlock_irqrestore(&emu->voice_lock, flags);
146
147 return result;
148}
149
150EXPORT_SYMBOL(snd_emu10k1_voice_alloc);
151
152int snd_emu10k1_voice_free(struct snd_emu10k1 *emu,
153 struct snd_emu10k1_voice *pvoice)
154{
155 unsigned long flags;
156
157 if (snd_BUG_ON(!pvoice))
158 return -EINVAL;
159 spin_lock_irqsave(&emu->voice_lock, flags);
160 pvoice->interrupt = NULL;
161 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
162 pvoice->epcm = NULL;
163 snd_emu10k1_voice_init(emu, pvoice->number);
164 spin_unlock_irqrestore(&emu->voice_lock, flags);
165 return 0;
166}
167
168EXPORT_SYMBOL(snd_emu10k1_voice_free);
169