1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Lee Revell <rlrevell@joe-job.com>
5 * Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
8 * Routines for control of EMU10K1 chips - voice manager
11 #include <linux/time.h>
12 #include <linux/export.h>
13 #include <sound/core.h>
14 #include <sound/emu10k1.h>
16 /* Previously the voice allocator started at 0 every time. The new voice
17 * allocator uses a round robin scheme. The next free voice is tracked in
18 * the card record and each allocation begins where the last left off. The
19 * hardware requires stereo interleaved voices be aligned to an even/odd
24 static int voice_alloc(struct snd_emu10k1 *emu, int type, int number,
25 struct snd_emu10k1_pcm *epcm, struct snd_emu10k1_voice **rvoice)
27 struct snd_emu10k1_voice *voice;
30 for (i = emu->next_free_voice, j = 0; j < NUM_G; i = (i + skip) % NUM_G, j += skip) {
32 dev_dbg(emu->card->dev, "i %d j %d next free %d!\n",
33 i, j, emu->next_free_voice);
36 /* stereo voices must be even/odd */
37 if ((number > 1) && (i % 2)) {
42 for (k = 0; k < number; k++) {
43 voice = &emu->voices[i + k];
50 for (k = 0; k < number; k++) {
51 voice = &emu->voices[i + k];
54 /* dev_dbg(emu->card->dev, "allocated voice %d\n", i + k); */
58 *rvoice = &emu->voices[i];
59 emu->next_free_voice = (i + number) % NUM_G;
64 return -ENOMEM; // -EBUSY would have been better
67 static void voice_free(struct snd_emu10k1 *emu,
68 struct snd_emu10k1_voice *pvoice)
71 snd_emu10k1_voice_init(emu, pvoice->number);
72 pvoice->interrupt = NULL;
73 pvoice->use = pvoice->dirty = pvoice->last = 0;
77 int snd_emu10k1_voice_alloc(struct snd_emu10k1 *emu, int type, int count, int channels,
78 struct snd_emu10k1_pcm *epcm, struct snd_emu10k1_voice **rvoice)
83 if (snd_BUG_ON(!rvoice))
85 if (snd_BUG_ON(!count))
87 if (snd_BUG_ON(!channels))
90 spin_lock_irqsave(&emu->voice_lock, flags);
91 for (int got = 0; got < channels; ) {
92 result = voice_alloc(emu, type, count, epcm, &rvoice[got]);
96 dev_dbg(emu->card->dev, "voice alloc - %i, %i of %i\n",
97 rvoice[got - 1]->number, got, want);
101 if (type != EMU10K1_SYNTH && emu->get_synth_voice) {
102 /* free a voice from synth */
103 result = emu->get_synth_voice(emu);
105 voice_free(emu, &emu->voices[result]);
109 for (int i = 0; i < got; i++) {
110 for (int j = 0; j < count; j++)
111 voice_free(emu, rvoice[i] + j);
116 spin_unlock_irqrestore(&emu->voice_lock, flags);
121 EXPORT_SYMBOL(snd_emu10k1_voice_alloc);
123 int snd_emu10k1_voice_free(struct snd_emu10k1 *emu,
124 struct snd_emu10k1_voice *pvoice)
129 if (snd_BUG_ON(!pvoice))
131 spin_lock_irqsave(&emu->voice_lock, flags);
134 voice_free(emu, pvoice++);
136 spin_unlock_irqrestore(&emu->voice_lock, flags);
140 EXPORT_SYMBOL(snd_emu10k1_voice_free);