2 **********************************************************************
3 * voicemgr.c - Voice manager for emu10k1 driver
4 * Copyright 1999, 2000 Creative Labs, Inc.
6 **********************************************************************
8 * Date Author Summary of changes
9 * ---- ------ ------------------
10 * October 20, 1999 Bertrand Lee base code release
12 **********************************************************************
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public
25 * License along with this program; if not, write to the Free
26 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
29 **********************************************************************
35 #define PITCH_48000 0x00004000
36 #define PITCH_96000 0x00008000
37 #define PITCH_85000 0x00007155
38 #define PITCH_80726 0x00006ba2
39 #define PITCH_67882 0x00005a82
40 #define PITCH_57081 0x00004c1c
42 u32 emu10k1_select_interprom(struct emu10k1_card *card, struct emu_voice *voice)
44 if(voice->pitch_target==PITCH_48000)
45 return CCCA_INTERPROM_0;
46 else if(voice->pitch_target<PITCH_48000)
47 return CCCA_INTERPROM_1;
48 else if(voice->pitch_target>=PITCH_96000)
49 return CCCA_INTERPROM_0;
50 else if(voice->pitch_target>=PITCH_85000)
51 return CCCA_INTERPROM_6;
52 else if(voice->pitch_target>=PITCH_80726)
53 return CCCA_INTERPROM_5;
54 else if(voice->pitch_target>=PITCH_67882)
55 return CCCA_INTERPROM_4;
56 else if(voice->pitch_target>=PITCH_57081)
57 return CCCA_INTERPROM_3;
59 return CCCA_INTERPROM_2;
64 * emu10k1_voice_alloc_buffer -
66 * allocates the memory buffer for a voice. Two page tables are kept for each buffer.
67 * One (dma_handle) keeps track of the host memory pages used and the other (virtualpagetable)
68 * is passed to the device so that it can do DMA to host memory.
71 int emu10k1_voice_alloc_buffer(struct emu10k1_card *card, struct voice_mem *mem, u32 pages)
73 u32 pageindex, pagecount;
77 DPD(2, "requested pages is: %d\n", pages);
79 if ((mem->emupageindex = emu10k1_addxmgr_alloc(pages * PAGE_SIZE, card)) < 0)
81 DPF(1, "couldn't allocate emu10k1 address space\n");
85 /* Fill in virtual memory table */
86 for (pagecount = 0; pagecount < pages; pagecount++) {
87 if ((mem->addr[pagecount] = pci_alloc_consistent(card->pci_dev, PAGE_SIZE, &mem->dma_handle[pagecount]))
89 mem->pages = pagecount;
90 DPF(1, "couldn't allocate dma memory\n");
94 DPD(2, "Virtual Addx: %p\n", mem->addr[pagecount]);
96 for (i = 0; i < PAGE_SIZE / EMUPAGESIZE; i++) {
97 busaddx = (u32) mem->dma_handle[pagecount] + i * EMUPAGESIZE;
99 DPD(3, "Bus Addx: %#x\n", busaddx);
101 pageindex = mem->emupageindex + pagecount * PAGE_SIZE / EMUPAGESIZE + i;
103 ((u32 *) card->virtualpagetable.addr)[pageindex] = cpu_to_le32((busaddx * 2) | pageindex);
107 mem->pages = pagecount;
113 * emu10k1_voice_free_buffer -
115 * frees the memory buffer for a voice.
117 void emu10k1_voice_free_buffer(struct emu10k1_card *card, struct voice_mem *mem)
119 u32 pagecount, pageindex;
122 if (mem->emupageindex < 0)
125 for (pagecount = 0; pagecount < mem->pages; pagecount++) {
126 pci_free_consistent(card->pci_dev, PAGE_SIZE,
127 mem->addr[pagecount],
128 mem->dma_handle[pagecount]);
130 for (i = 0; i < PAGE_SIZE / EMUPAGESIZE; i++) {
131 pageindex = mem->emupageindex + pagecount * PAGE_SIZE / EMUPAGESIZE + i;
132 ((u32 *) card->virtualpagetable.addr)[pageindex] =
133 cpu_to_le32(((u32) card->silentpage.dma_handle * 2) | pageindex);
137 emu10k1_addxmgr_free(card, mem->emupageindex);
138 mem->emupageindex = -1;
141 int emu10k1_voice_alloc(struct emu10k1_card *card, struct emu_voice *voice)
143 u8 *voicetable = card->voicetable;
147 DPF(2, "emu10k1_voice_alloc()\n");
149 spin_lock_irqsave(&card->lock, flags);
151 if (voice->flags & VOICE_FLAGS_STEREO) {
152 for (i = 0; i < NUM_G; i += 2)
153 if ((voicetable[i] == VOICE_USAGE_FREE) && (voicetable[i + 1] == VOICE_USAGE_FREE)) {
154 voicetable[i] = voice->usage;
155 voicetable[i + 1] = voice->usage;
159 for (i = 0; i < NUM_G; i++)
160 if (voicetable[i] == VOICE_USAGE_FREE) {
161 voicetable[i] = voice->usage;
166 spin_unlock_irqrestore(&card->lock, flags);
174 for (i = 0; i < (voice->flags & VOICE_FLAGS_STEREO ? 2 : 1); i++) {
175 DPD(2, " voice allocated -> %d\n", voice->num + i);
177 sblive_writeptr_tag(card, voice->num + i, IFATN, 0xffff,
187 void emu10k1_voice_free(struct emu_voice *voice)
189 struct emu10k1_card *card = voice->card;
193 DPF(2, "emu10k1_voice_free()\n");
195 if (voice->usage == VOICE_USAGE_FREE)
198 for (i = 0; i < (voice->flags & VOICE_FLAGS_STEREO ? 2 : 1); i++) {
199 DPD(2, " voice released -> %d\n", voice->num + i);
201 sblive_writeptr_tag(card, voice->num + i, DCYSUSV, 0,
208 sblive_writeptr(card, CPF, voice->num + i, 0);
211 voice->usage = VOICE_USAGE_FREE;
213 spin_lock_irqsave(&card->lock, flags);
215 card->voicetable[voice->num] = VOICE_USAGE_FREE;
217 if (voice->flags & VOICE_FLAGS_STEREO)
218 card->voicetable[voice->num + 1] = VOICE_USAGE_FREE;
220 spin_unlock_irqrestore(&card->lock, flags);
223 void emu10k1_voice_playback_setup(struct emu_voice *voice)
225 struct emu10k1_card *card = voice->card;
229 DPF(2, "emu10k1_voice_playback_setup()\n");
231 if (voice->flags & VOICE_FLAGS_STEREO) {
234 sblive_writeptr(card, CPF, voice->num, CPF_STEREO_MASK);
235 sblive_writeptr(card, CPF, voice->num + 1, CPF_STEREO_MASK);
238 sblive_writeptr(card, CPF, voice->num, 0);
241 if(!(voice->flags & VOICE_FLAGS_16BIT))
244 voice->start += start;
246 for (i = 0; i < (voice->flags & VOICE_FLAGS_STEREO ? 2 : 1); i++) {
247 if (card->is_audigy) {
248 sblive_writeptr(card, A_FXRT1, voice->num + i, voice->params[i].send_routing);
249 sblive_writeptr(card, A_FXRT2, voice->num + i, voice->params[i].send_routing2);
250 sblive_writeptr(card, A_SENDAMOUNTS, voice->num + i, voice->params[i].send_hgfe);
252 sblive_writeptr(card, FXRT, voice->num + i, voice->params[i].send_routing << 16);
256 /* Assumption that PT is already 0 so no harm overwriting */
257 sblive_writeptr(card, PTRX, voice->num + i, ((voice->params[i].send_dcba & 0xff) << 8)
258 | ((voice->params[i].send_dcba & 0xff00) >> 8));
260 sblive_writeptr_tag(card, voice->num + i,
262 DSL, voice->endloop | (voice->params[i].send_dcba & 0xff000000),
263 PSST, voice->startloop | ((voice->params[i].send_dcba & 0x00ff0000) << 8),
264 CCCA, (voice->start) | emu10k1_select_interprom(card,voice) |
265 ((voice->flags & VOICE_FLAGS_16BIT) ? 0 : CCCA_8BITSELECT),
266 /* Clear filter delay memory */
269 /* Invalidate maps */
270 MAPA, MAP_PTI_MASK | ((u32) card->silentpage.dma_handle * 2),
271 MAPB, MAP_PTI_MASK | ((u32) card->silentpage.dma_handle * 2),
272 /* modulation envelope */
283 /* volume envelope */
286 /* filter envelope */
287 PEFE_FILTERAMOUNT, 0x7f,
289 PEFE_PITCHAMOUNT, 0, TAGLIST_END);
291 voice->params[i].fc_target = 0xffff;
295 void emu10k1_voices_start(struct emu_voice *first_voice, unsigned int num_voices, int set)
297 struct emu10k1_card *card = first_voice->card;
298 struct emu_voice *voice;
299 unsigned int voicenum;
302 DPF(2, "emu10k1_voices_start()\n");
304 for (voicenum = 0; voicenum < num_voices; voicenum++)
306 voice = first_voice + voicenum;
309 u32 cra, ccis, cs, sample;
310 if (voice->flags & VOICE_FLAGS_STEREO) {
320 if(voice->flags & VOICE_FLAGS_16BIT) {
327 for(j = 0; j < cs; j++)
328 sblive_writeptr(card, CD0 + j, voice->num, sample);
331 sblive_writeptr(card, CCR_CACHEINVALIDSIZE, voice->num, 0);
332 if (voice->flags & VOICE_FLAGS_STEREO)
333 sblive_writeptr(card, CCR_CACHEINVALIDSIZE, voice->num + 1, 0);
335 sblive_writeptr(card, CCR_READADDRESS, voice->num, cra);
337 if (voice->flags & VOICE_FLAGS_STEREO)
338 sblive_writeptr(card, CCR_READADDRESS, voice->num + 1, cra);
341 sblive_writeptr(card, CCR_CACHEINVALIDSIZE, voice->num, ccis);
344 for (j = 0; j < (voice->flags & VOICE_FLAGS_STEREO ? 2 : 1); j++) {
345 sblive_writeptr_tag(card, voice->num + j,
346 IFATN, (voice->params[j].initial_fc << 8) | voice->params[j].initial_attn,
347 VTFT, (voice->params[j].volume_target << 16) | voice->params[j].fc_target,
348 CVCF, (voice->params[j].volume_target << 16) | voice->params[j].fc_target,
349 DCYSUSV, (voice->params[j].byampl_env_sustain << 8) | voice->params[j].byampl_env_decay,
352 emu10k1_clear_stop_on_loop(card, voice->num + j);
357 for (voicenum = 0; voicenum < num_voices; voicenum++)
359 voice = first_voice + voicenum;
361 for (j = 0; j < (voice->flags & VOICE_FLAGS_STEREO ? 2 : 1); j++) {
362 sblive_writeptr(card, PTRX_PITCHTARGET, voice->num + j, voice->pitch_target);
365 sblive_writeptr(card, CPF_CURRENTPITCH, voice->num, voice->pitch_target);
367 sblive_writeptr(card, IP, voice->num + j, voice->initial_pitch);
372 void emu10k1_voices_stop(struct emu_voice *first_voice, int num_voices)
374 struct emu10k1_card *card = first_voice->card;
375 struct emu_voice *voice;
376 unsigned int voice_num;
379 DPF(2, "emu10k1_voice_stop()\n");
381 for (voice_num = 0; voice_num < num_voices; voice_num++)
383 voice = first_voice + voice_num;
385 for (j = 0; j < (voice->flags & VOICE_FLAGS_STEREO ? 2 : 1); j++) {
386 sblive_writeptr_tag(card, voice->num + j,