2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 * Lee Revell <rlrevell@joe-job.com>
5 * Routines for control of EMU10K1 chips - voice manager
7 * Rewrote voice allocator for multichannel support - rlrevell 12/2004
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <sound/driver.h>
32 #include <linux/time.h>
33 #include <sound/core.h>
34 #include <sound/emu10k1.h>
36 /* Previously the voice allocator started at 0 every time. The new voice
37 * allocator uses a round robin scheme. The next free voice is tracked in
38 * the card record and each allocation begins where the last left off. The
39 * hardware requires stereo interleaved voices be aligned to an even/odd
40 * boundary. For multichannel voice allocation we ensure than the block of
41 * voices does not cross the 32 voice boundary. This simplifies the
42 * multichannel support and ensures we can use a single write to the
43 * (set|clear)_loop_stop registers. Otherwise (for example) the voices would
44 * get out of sync when pausing/resuming a stream.
48 static int voice_alloc(emu10k1_t
*emu
, emu10k1_voice_type_t type
, int number
, emu10k1_voice_t
**rvoice
)
50 emu10k1_voice_t
*voice
;
51 int i
, j
, k
, first_voice
, last_voice
, skip
;
54 first_voice
= last_voice
= 0;
55 for (i
= emu
->next_free_voice
, j
= 0; j
< NUM_G
; i
+= number
, j
+= number
) {
56 // printk("i %d j %d next free %d!\n", i, j, emu->next_free_voice);
59 /* stereo voices must be even/odd */
60 if ((number
== 2) && (i
% 2)) {
66 for (k
= 0; k
< number
; k
++) {
67 voice
= &emu
->voices
[(i
+k
) % NUM_G
];
74 // printk("allocated voice %d\n", i);
76 last_voice
= (i
+ number
) % NUM_G
;
77 emu
->next_free_voice
= last_voice
;
82 if (first_voice
== last_voice
)
85 for (i
=0; i
< number
; i
++) {
86 voice
= &emu
->voices
[(first_voice
+ i
) % NUM_G
];
87 // printk("voice alloc - %i, %i of %i\n", voice->number, idx-first_voice+1, number);
104 *rvoice
= &emu
->voices
[first_voice
];
108 int snd_emu10k1_voice_alloc(emu10k1_t
*emu
, emu10k1_voice_type_t type
, int number
, emu10k1_voice_t
**rvoice
)
113 snd_assert(rvoice
!= NULL
, return -EINVAL
);
114 snd_assert(number
, return -EINVAL
);
116 spin_lock_irqsave(&emu
->voice_lock
, flags
);
118 result
= voice_alloc(emu
, type
, number
, rvoice
);
119 if (result
== 0 || type
== EMU10K1_SYNTH
|| type
== EMU10K1_MIDI
)
122 /* free a voice from synth */
123 if (emu
->get_synth_voice
) {
124 result
= emu
->get_synth_voice(emu
);
126 emu10k1_voice_t
*pvoice
= &emu
->voices
[result
];
127 pvoice
->interrupt
= NULL
;
128 pvoice
->use
= pvoice
->pcm
= pvoice
->synth
= pvoice
->midi
= pvoice
->efx
= 0;
135 spin_unlock_irqrestore(&emu
->voice_lock
, flags
);
140 int snd_emu10k1_voice_free(emu10k1_t
*emu
, emu10k1_voice_t
*pvoice
)
144 snd_assert(pvoice
!= NULL
, return -EINVAL
);
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
);