1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5 * Lee Revell <rlrevell@joe-job.com>
6 * Routines for control of EMU10K1 chips - voice manager
8 * Rewrote voice allocator for multichannel support - rlrevell 12/2004
17 #include <linux/time.h>
18 #include <linux/export.h>
19 #include <sound/core.h>
20 #include <sound/emu10k1.h>
22 /* Previously the voice allocator started at 0 every time. The new voice
23 * allocator uses a round robin scheme. The next free voice is tracked in
24 * the card record and each allocation begins where the last left off. The
25 * hardware requires stereo interleaved voices be aligned to an even/odd
26 * boundary. For multichannel voice allocation we ensure than the block of
27 * voices does not cross the 32 voice boundary. This simplifies the
28 * multichannel support and ensures we can use a single write to the
29 * (set|clear)_loop_stop registers. Otherwise (for example) the voices would
30 * get out of sync when pausing/resuming a stream.
34 static int voice_alloc(struct snd_emu10k1 *emu, int type, int number,
35 struct snd_emu10k1_voice **rvoice)
37 struct snd_emu10k1_voice *voice;
38 int i, j, k, first_voice, last_voice, skip;
41 first_voice = last_voice = 0;
42 for (i = emu->next_free_voice, j = 0; j < NUM_G ; i += number, j += number) {
44 dev_dbg(emu->card->dev, "i %d j %d next free %d!\n",
45 i, j, emu->next_free_voice);
49 /* stereo voices must be even/odd */
50 if ((number == 2) && (i % 2)) {
56 for (k = 0; k < number; k++) {
57 voice = &emu->voices[(i+k) % NUM_G];
64 /* dev_dbg(emu->card->dev, "allocated voice %d\n", i); */
66 last_voice = (i + number) % NUM_G;
67 emu->next_free_voice = last_voice;
72 if (first_voice == last_voice)
75 for (i = 0; i < number; i++) {
76 voice = &emu->voices[(first_voice + i) % NUM_G];
78 dev_dbg(emu->card->dev, "voice alloc - %i, %i of %i\n",
79 voice->number, idx-first_voice+1, number);
97 *rvoice = &emu->voices[first_voice];
101 int snd_emu10k1_voice_alloc(struct snd_emu10k1 *emu, int type, int number,
102 struct snd_emu10k1_voice **rvoice)
107 if (snd_BUG_ON(!rvoice))
109 if (snd_BUG_ON(!number))
112 spin_lock_irqsave(&emu->voice_lock, flags);
114 result = voice_alloc(emu, type, number, rvoice);
115 if (result == 0 || type == EMU10K1_SYNTH || type == EMU10K1_MIDI)
118 /* free a voice from synth */
119 if (emu->get_synth_voice) {
120 result = emu->get_synth_voice(emu);
122 struct snd_emu10k1_voice *pvoice = &emu->voices[result];
123 pvoice->interrupt = NULL;
124 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
131 spin_unlock_irqrestore(&emu->voice_lock, flags);
136 EXPORT_SYMBOL(snd_emu10k1_voice_alloc);
138 int snd_emu10k1_voice_free(struct snd_emu10k1 *emu,
139 struct snd_emu10k1_voice *pvoice)
143 if (snd_BUG_ON(!pvoice))
145 spin_lock_irqsave(&emu->voice_lock, flags);
146 pvoice->interrupt = NULL;
147 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
149 snd_emu10k1_voice_init(emu, pvoice->number);
150 spin_unlock_irqrestore(&emu->voice_lock, flags);
154 EXPORT_SYMBOL(snd_emu10k1_voice_free);