1 | /*
|
---|
2 | * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
|
---|
3 | * Creative Labs, Inc.
|
---|
4 | * Lee Revell <rlrevell@joe-job.com>
|
---|
5 | * Routines for control of EMU10K1 chips - voice manager
|
---|
6 | *
|
---|
7 | * Rewrote voice allocator for multichannel support - rlrevell 12/2004
|
---|
8 | *
|
---|
9 | * BUGS:
|
---|
10 | * --
|
---|
11 | *
|
---|
12 | * TODO:
|
---|
13 | * --
|
---|
14 | *
|
---|
15 | * This program is free software; you can redistribute it and/or modify
|
---|
16 | * it under the terms of the GNU General Public License as published by
|
---|
17 | * the Free Software Foundation; either version 2 of the License, or
|
---|
18 | * (at your option) any later version.
|
---|
19 | *
|
---|
20 | * This program is distributed in the hope that it will be useful,
|
---|
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
23 | * GNU General Public License for more details.
|
---|
24 | *
|
---|
25 | * You should have received a copy of the GNU General Public License
|
---|
26 | * along with this program; if not, write to the Free Software
|
---|
27 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
28 | *
|
---|
29 | */
|
---|
30 |
|
---|
31 | #define __NO_VERSION__
|
---|
32 | #include <sound/driver.h>
|
---|
33 | #include <linux/time.h>
|
---|
34 | #include <sound/core.h>
|
---|
35 | #include <sound/emu10k1.h>
|
---|
36 |
|
---|
37 | /* Previously the voice allocator started at 0 every time. The new voice
|
---|
38 | * allocator uses a round robin scheme. The next free voice is tracked in
|
---|
39 | * the card record and each allocation begins where the last left off. The
|
---|
40 | * hardware requires stereo interleaved voices be aligned to an even/odd
|
---|
41 | * boundary. For multichannel voice allocation we ensure than the block of
|
---|
42 | * voices does not cross the 32 voice boundary. This simplifies the
|
---|
43 | * multichannel support and ensures we can use a single write to the
|
---|
44 | * (set|clear)_loop_stop registers. Otherwise (for example) the voices would
|
---|
45 | * get out of sync when pausing/resuming a stream.
|
---|
46 | * --rlrevell
|
---|
47 | */
|
---|
48 |
|
---|
49 | static int voice_alloc(emu10k1_t *emu, emu10k1_voice_type_t type, int number, emu10k1_voice_t **rvoice)
|
---|
50 | {
|
---|
51 | emu10k1_voice_t *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 | // printk("i %d j %d next free %d!\n", i, j, emu->next_free_voice);
|
---|
58 | i %= NUM_G;
|
---|
59 | /* stereo voices must be even/odd */
|
---|
60 | if ((number == 2) && (i % 2)) {
|
---|
61 | i++;
|
---|
62 | continue;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /* make sure the block of voices does not cross the 32 voice boundary */
|
---|
66 | //if (((i % 32) + number) > 32)
|
---|
67 | // continue;
|
---|
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 | }
|
---|
75 | }
|
---|
76 | if (!skip) {
|
---|
77 | // printk("allocated voice %d\n", i);
|
---|
78 | first_voice = i;
|
---|
79 | last_voice = (i + number) % NUM_G;
|
---|
80 | emu->next_free_voice = last_voice;
|
---|
81 | break;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (first_voice == last_voice)
|
---|
86 | return -ENOMEM;
|
---|
87 |
|
---|
88 | for (i=0; i < number; i++) {
|
---|
89 | voice = &emu->voices[(first_voice + i) % NUM_G];
|
---|
90 | // printk("voice alloc - %i, %i of %i\n", voice->number, idx-first_voice+1, number);
|
---|
91 | voice->use = 1;
|
---|
92 | switch (type) {
|
---|
93 | case EMU10K1_PCM:
|
---|
94 | voice->pcm = 1;
|
---|
95 | break;
|
---|
96 | case EMU10K1_SYNTH:
|
---|
97 | voice->synth = 1;
|
---|
98 | break;
|
---|
99 | case EMU10K1_MIDI:
|
---|
100 | voice->midi = 1;
|
---|
101 | break;
|
---|
102 | case EMU10K1_EFX:
|
---|
103 | voice->efx = 1;
|
---|
104 | break;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | *rvoice = &emu->voices[first_voice];
|
---|
108 | return 0;
|
---|
109 | }
|
---|
110 |
|
---|
111 | int snd_emu10k1_voice_alloc(emu10k1_t *emu, emu10k1_voice_type_t type, int number, emu10k1_voice_t **rvoice)
|
---|
112 | {
|
---|
113 | unsigned long flags;
|
---|
114 | int result;
|
---|
115 |
|
---|
116 | snd_assert(rvoice != NULL, return -EINVAL);
|
---|
117 | snd_assert(number, return -EINVAL);
|
---|
118 |
|
---|
119 | spin_lock_irqsave(&emu->voice_lock, flags);
|
---|
120 | for (;;) {
|
---|
121 | result = voice_alloc(emu, type, number, rvoice);
|
---|
122 | if (result == 0 || type == EMU10K1_SYNTH || type == EMU10K1_MIDI)
|
---|
123 | break;
|
---|
124 |
|
---|
125 | /* free a voice from synth */
|
---|
126 | if (emu->get_synth_voice) {
|
---|
127 | result = emu->get_synth_voice(emu);
|
---|
128 | if (result >= 0) {
|
---|
129 | emu10k1_voice_t *pvoice = &emu->voices[result];
|
---|
130 | pvoice->interrupt = NULL;
|
---|
131 | pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
|
---|
132 | pvoice->epcm = NULL;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | if (result < 0)
|
---|
136 | break;
|
---|
137 | }
|
---|
138 | spin_unlock_irqrestore(&emu->voice_lock, flags);
|
---|
139 |
|
---|
140 | return result;
|
---|
141 | }
|
---|
142 |
|
---|
143 | int snd_emu10k1_voice_free(emu10k1_t *emu, emu10k1_voice_t *pvoice)
|
---|
144 | {
|
---|
145 | unsigned long flags;
|
---|
146 |
|
---|
147 | snd_assert(pvoice != NULL, return -EINVAL);
|
---|
148 | spin_lock_irqsave(&emu->voice_lock, flags);
|
---|
149 | pvoice->interrupt = NULL;
|
---|
150 | pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
|
---|
151 | pvoice->epcm = NULL;
|
---|
152 | snd_emu10k1_voice_init(emu, pvoice->number);
|
---|
153 | spin_unlock_irqrestore(&emu->voice_lock, flags);
|
---|
154 | return 0;
|
---|
155 | }
|
---|