2019-05-27 14:55:05 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2007-10-15 15:50:19 +08:00
|
|
|
* Copyright (c) by Jaroslav Kysela <perex@perex.cz>
|
2005-04-17 06:20:36 +08:00
|
|
|
* Creative Labs, Inc.
|
|
|
|
* Lee Revell <rlrevell@joe-job.com>
|
|
|
|
* Routines for control of EMU10K1 chips - voice manager
|
|
|
|
*
|
|
|
|
* Rewrote voice allocator for multichannel support - rlrevell 12/2004
|
|
|
|
*
|
|
|
|
* BUGS:
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* --
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/time.h>
|
2011-09-22 21:34:58 +08:00
|
|
|
#include <linux/export.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <sound/core.h>
|
|
|
|
#include <sound/emu10k1.h>
|
|
|
|
|
|
|
|
/* Previously the voice allocator started at 0 every time. The new voice
|
|
|
|
* allocator uses a round robin scheme. The next free voice is tracked in
|
|
|
|
* the card record and each allocation begins where the last left off. The
|
|
|
|
* hardware requires stereo interleaved voices be aligned to an even/odd
|
|
|
|
* boundary. For multichannel voice allocation we ensure than the block of
|
|
|
|
* voices does not cross the 32 voice boundary. This simplifies the
|
|
|
|
* multichannel support and ensures we can use a single write to the
|
|
|
|
* (set|clear)_loop_stop registers. Otherwise (for example) the voices would
|
|
|
|
* get out of sync when pausing/resuming a stream.
|
|
|
|
* --rlrevell
|
|
|
|
*/
|
|
|
|
|
2005-11-17 21:50:13 +08:00
|
|
|
static int voice_alloc(struct snd_emu10k1 *emu, int type, int number,
|
|
|
|
struct snd_emu10k1_voice **rvoice)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 21:50:13 +08:00
|
|
|
struct snd_emu10k1_voice *voice;
|
2005-04-17 06:20:36 +08:00
|
|
|
int i, j, k, first_voice, last_voice, skip;
|
|
|
|
|
|
|
|
*rvoice = NULL;
|
|
|
|
first_voice = last_voice = 0;
|
|
|
|
for (i = emu->next_free_voice, j = 0; j < NUM_G ; i += number, j += number) {
|
2009-02-05 23:08:14 +08:00
|
|
|
/*
|
2014-02-26 00:02:09 +08:00
|
|
|
dev_dbg(emu->card->dev, "i %d j %d next free %d!\n",
|
2009-02-05 23:08:14 +08:00
|
|
|
i, j, emu->next_free_voice);
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
i %= NUM_G;
|
|
|
|
|
|
|
|
/* stereo voices must be even/odd */
|
|
|
|
if ((number == 2) && (i % 2)) {
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
skip = 0;
|
|
|
|
for (k = 0; k < number; k++) {
|
|
|
|
voice = &emu->voices[(i+k) % NUM_G];
|
|
|
|
if (voice->use) {
|
|
|
|
skip = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!skip) {
|
2014-02-26 00:02:09 +08:00
|
|
|
/* dev_dbg(emu->card->dev, "allocated voice %d\n", i); */
|
2005-04-17 06:20:36 +08:00
|
|
|
first_voice = i;
|
|
|
|
last_voice = (i + number) % NUM_G;
|
|
|
|
emu->next_free_voice = last_voice;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first_voice == last_voice)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2006-10-01 17:48:04 +08:00
|
|
|
for (i = 0; i < number; i++) {
|
2005-04-17 06:20:36 +08:00
|
|
|
voice = &emu->voices[(first_voice + i) % NUM_G];
|
2009-02-05 23:08:14 +08:00
|
|
|
/*
|
2014-02-26 00:02:09 +08:00
|
|
|
dev_dbg(emu->card->dev, "voice alloc - %i, %i of %i\n",
|
2009-02-05 23:08:14 +08:00
|
|
|
voice->number, idx-first_voice+1, number);
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
voice->use = 1;
|
|
|
|
switch (type) {
|
|
|
|
case EMU10K1_PCM:
|
|
|
|
voice->pcm = 1;
|
|
|
|
break;
|
|
|
|
case EMU10K1_SYNTH:
|
|
|
|
voice->synth = 1;
|
|
|
|
break;
|
|
|
|
case EMU10K1_MIDI:
|
|
|
|
voice->midi = 1;
|
|
|
|
break;
|
|
|
|
case EMU10K1_EFX:
|
|
|
|
voice->efx = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*rvoice = &emu->voices[first_voice];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-17 21:50:13 +08:00
|
|
|
int snd_emu10k1_voice_alloc(struct snd_emu10k1 *emu, int type, int number,
|
|
|
|
struct snd_emu10k1_voice **rvoice)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
int result;
|
|
|
|
|
2008-08-08 23:12:14 +08:00
|
|
|
if (snd_BUG_ON(!rvoice))
|
|
|
|
return -EINVAL;
|
|
|
|
if (snd_BUG_ON(!number))
|
|
|
|
return -EINVAL;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
spin_lock_irqsave(&emu->voice_lock, flags);
|
|
|
|
for (;;) {
|
|
|
|
result = voice_alloc(emu, type, number, rvoice);
|
|
|
|
if (result == 0 || type == EMU10K1_SYNTH || type == EMU10K1_MIDI)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* free a voice from synth */
|
|
|
|
if (emu->get_synth_voice) {
|
|
|
|
result = emu->get_synth_voice(emu);
|
|
|
|
if (result >= 0) {
|
2005-11-17 21:50:13 +08:00
|
|
|
struct snd_emu10k1_voice *pvoice = &emu->voices[result];
|
2005-04-17 06:20:36 +08:00
|
|
|
pvoice->interrupt = NULL;
|
|
|
|
pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
|
|
|
|
pvoice->epcm = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&emu->voice_lock, flags);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-04-28 21:13:39 +08:00
|
|
|
EXPORT_SYMBOL(snd_emu10k1_voice_alloc);
|
|
|
|
|
2005-11-17 21:50:13 +08:00
|
|
|
int snd_emu10k1_voice_free(struct snd_emu10k1 *emu,
|
|
|
|
struct snd_emu10k1_voice *pvoice)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
2008-08-08 23:12:14 +08:00
|
|
|
if (snd_BUG_ON(!pvoice))
|
|
|
|
return -EINVAL;
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_lock_irqsave(&emu->voice_lock, flags);
|
|
|
|
pvoice->interrupt = NULL;
|
|
|
|
pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
|
|
|
|
pvoice->epcm = NULL;
|
|
|
|
snd_emu10k1_voice_init(emu, pvoice->number);
|
|
|
|
spin_unlock_irqrestore(&emu->voice_lock, flags);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-04-28 21:13:39 +08:00
|
|
|
|
|
|
|
EXPORT_SYMBOL(snd_emu10k1_voice_free);
|