/*1* Copyright (c) by Jaroslav Kysela <[email protected]>2* Creative Labs, Inc.3* Lee Revell <[email protected]>4* Routines for control of EMU10K1 chips - voice manager5*6* Rewrote voice allocator for multichannel support - rlrevell 12/20047*8* BUGS:9* --10*11* TODO:12* --13*14* This program is free software; you can redistribute it and/or modify15* it under the terms of the GNU General Public License as published by16* the Free Software Foundation; either version 2 of the License, or17* (at your option) any later version.18*19* This program is distributed in the hope that it will be useful,20* but WITHOUT ANY WARRANTY; without even the implied warranty of21* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the22* GNU General Public License for more details.23*24* You should have received a copy of the GNU General Public License25* along with this program; if not, write to the Free Software26* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA27*28*/2930#include <linux/time.h>31#include <sound/core.h>32#include <sound/emu10k1.h>3334/* Previously the voice allocator started at 0 every time. The new voice35* allocator uses a round robin scheme. The next free voice is tracked in36* the card record and each allocation begins where the last left off. The37* hardware requires stereo interleaved voices be aligned to an even/odd38* boundary. For multichannel voice allocation we ensure than the block of39* voices does not cross the 32 voice boundary. This simplifies the40* multichannel support and ensures we can use a single write to the41* (set|clear)_loop_stop registers. Otherwise (for example) the voices would42* get out of sync when pausing/resuming a stream.43* --rlrevell44*/4546static int voice_alloc(struct snd_emu10k1 *emu, int type, int number,47struct snd_emu10k1_voice **rvoice)48{49struct snd_emu10k1_voice *voice;50int i, j, k, first_voice, last_voice, skip;5152*rvoice = NULL;53first_voice = last_voice = 0;54for (i = emu->next_free_voice, j = 0; j < NUM_G ; i += number, j += number) {55/*56printk(KERN_DEBUG "i %d j %d next free %d!\n",57i, j, emu->next_free_voice);58*/59i %= NUM_G;6061/* stereo voices must be even/odd */62if ((number == 2) && (i % 2)) {63i++;64continue;65}6667skip = 0;68for (k = 0; k < number; k++) {69voice = &emu->voices[(i+k) % NUM_G];70if (voice->use) {71skip = 1;72break;73}74}75if (!skip) {76/* printk(KERN_DEBUG "allocated voice %d\n", i); */77first_voice = i;78last_voice = (i + number) % NUM_G;79emu->next_free_voice = last_voice;80break;81}82}8384if (first_voice == last_voice)85return -ENOMEM;8687for (i = 0; i < number; i++) {88voice = &emu->voices[(first_voice + i) % NUM_G];89/*90printk(kERN_DEBUG "voice alloc - %i, %i of %i\n",91voice->number, idx-first_voice+1, number);92*/93voice->use = 1;94switch (type) {95case EMU10K1_PCM:96voice->pcm = 1;97break;98case EMU10K1_SYNTH:99voice->synth = 1;100break;101case EMU10K1_MIDI:102voice->midi = 1;103break;104case EMU10K1_EFX:105voice->efx = 1;106break;107}108}109*rvoice = &emu->voices[first_voice];110return 0;111}112113int snd_emu10k1_voice_alloc(struct snd_emu10k1 *emu, int type, int number,114struct snd_emu10k1_voice **rvoice)115{116unsigned long flags;117int result;118119if (snd_BUG_ON(!rvoice))120return -EINVAL;121if (snd_BUG_ON(!number))122return -EINVAL;123124spin_lock_irqsave(&emu->voice_lock, flags);125for (;;) {126result = voice_alloc(emu, type, number, rvoice);127if (result == 0 || type == EMU10K1_SYNTH || type == EMU10K1_MIDI)128break;129130/* free a voice from synth */131if (emu->get_synth_voice) {132result = emu->get_synth_voice(emu);133if (result >= 0) {134struct snd_emu10k1_voice *pvoice = &emu->voices[result];135pvoice->interrupt = NULL;136pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;137pvoice->epcm = NULL;138}139}140if (result < 0)141break;142}143spin_unlock_irqrestore(&emu->voice_lock, flags);144145return result;146}147148EXPORT_SYMBOL(snd_emu10k1_voice_alloc);149150int snd_emu10k1_voice_free(struct snd_emu10k1 *emu,151struct snd_emu10k1_voice *pvoice)152{153unsigned long flags;154155if (snd_BUG_ON(!pvoice))156return -EINVAL;157spin_lock_irqsave(&emu->voice_lock, flags);158pvoice->interrupt = NULL;159pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;160pvoice->epcm = NULL;161snd_emu10k1_voice_init(emu, pvoice->number);162spin_unlock_irqrestore(&emu->voice_lock, flags);163return 0;164}165166EXPORT_SYMBOL(snd_emu10k1_voice_free);167168169