Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / isa / sb / emu8000_callback.c
blob1cc4101a17a4b77549d78b69c9bf5270ac34d1da
1 /*
2 * synth callback routines for the emu8000 (AWE32/64)
4 * Copyright (C) 1999 Steve Ratcliffe
5 * Copyright (C) 1999-2000 Takashi Iwai <tiwai@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "emu8000_local.h"
23 #include <sound/asoundef.h>
26 * prototypes
28 static snd_emux_voice_t *get_voice(snd_emux_t *emu, snd_emux_port_t *port);
29 static int start_voice(snd_emux_voice_t *vp);
30 static void trigger_voice(snd_emux_voice_t *vp);
31 static void release_voice(snd_emux_voice_t *vp);
32 static void update_voice(snd_emux_voice_t *vp, int update);
33 static void reset_voice(snd_emux_t *emu, int ch);
34 static void terminate_voice(snd_emux_voice_t *vp);
35 static void sysex(snd_emux_t *emu, char *buf, int len, int parsed, snd_midi_channel_set_t *chset);
36 #ifdef CONFIG_SND_SEQUENCER_OSS
37 static int oss_ioctl(snd_emux_t *emu, int cmd, int p1, int p2);
38 #endif
39 static int load_fx(snd_emux_t *emu, int type, int mode, const void __user *buf, long len);
41 static void set_pitch(emu8000_t *hw, snd_emux_voice_t *vp);
42 static void set_volume(emu8000_t *hw, snd_emux_voice_t *vp);
43 static void set_pan(emu8000_t *hw, snd_emux_voice_t *vp);
44 static void set_fmmod(emu8000_t *hw, snd_emux_voice_t *vp);
45 static void set_tremfreq(emu8000_t *hw, snd_emux_voice_t *vp);
46 static void set_fm2frq2(emu8000_t *hw, snd_emux_voice_t *vp);
47 static void set_filterQ(emu8000_t *hw, snd_emux_voice_t *vp);
48 static void snd_emu8000_tweak_voice(emu8000_t *emu, int ch);
51 * Ensure a value is between two points
52 * macro evaluates its args more than once, so changed to upper-case.
54 #define LIMITVALUE(x, a, b) do { if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b); } while (0)
55 #define LIMITMAX(x, a) do {if ((x) > (a)) (x) = (a); } while (0)
59 * set up operators
61 static snd_emux_operators_t emu8000_ops = {
62 .owner = THIS_MODULE,
63 .get_voice = get_voice,
64 .prepare = start_voice,
65 .trigger = trigger_voice,
66 .release = release_voice,
67 .update = update_voice,
68 .terminate = terminate_voice,
69 .reset = reset_voice,
70 .sample_new = snd_emu8000_sample_new,
71 .sample_free = snd_emu8000_sample_free,
72 .sample_reset = snd_emu8000_sample_reset,
73 .load_fx = load_fx,
74 .sysex = sysex,
75 #ifdef CONFIG_SND_SEQUENCER_OSS
76 .oss_ioctl = oss_ioctl,
77 #endif
80 void
81 snd_emu8000_ops_setup(emu8000_t *hw)
83 hw->emu->ops = emu8000_ops;
89 * Terminate a voice
91 static void
92 release_voice(snd_emux_voice_t *vp)
94 int dcysusv;
95 emu8000_t *hw;
97 hw = vp->hw;
98 dcysusv = 0x8000 | (unsigned char)vp->reg.parm.modrelease;
99 EMU8000_DCYSUS_WRITE(hw, vp->ch, dcysusv);
100 dcysusv = 0x8000 | (unsigned char)vp->reg.parm.volrelease;
101 EMU8000_DCYSUSV_WRITE(hw, vp->ch, dcysusv);
107 static void
108 terminate_voice(snd_emux_voice_t *vp)
110 emu8000_t *hw;
112 hw = vp->hw;
113 EMU8000_DCYSUSV_WRITE(hw, vp->ch, 0x807F);
119 static void
120 update_voice(snd_emux_voice_t *vp, int update)
122 emu8000_t *hw;
124 hw = vp->hw;
125 if (update & SNDRV_EMUX_UPDATE_VOLUME)
126 set_volume(hw, vp);
127 if (update & SNDRV_EMUX_UPDATE_PITCH)
128 set_pitch(hw, vp);
129 if ((update & SNDRV_EMUX_UPDATE_PAN) &&
130 vp->port->ctrls[EMUX_MD_REALTIME_PAN])
131 set_pan(hw, vp);
132 if (update & SNDRV_EMUX_UPDATE_FMMOD)
133 set_fmmod(hw, vp);
134 if (update & SNDRV_EMUX_UPDATE_TREMFREQ)
135 set_tremfreq(hw, vp);
136 if (update & SNDRV_EMUX_UPDATE_FM2FRQ2)
137 set_fm2frq2(hw, vp);
138 if (update & SNDRV_EMUX_UPDATE_Q)
139 set_filterQ(hw, vp);
144 * Find a channel (voice) within the EMU that is not in use or at least
145 * less in use than other channels. Always returns a valid pointer
146 * no matter what. If there is a real shortage of voices then one
147 * will be cut. Such is life.
149 * The channel index (vp->ch) must be initialized in this routine.
150 * In Emu8k, it is identical with the array index.
152 static snd_emux_voice_t *
153 get_voice(snd_emux_t *emu, snd_emux_port_t *port)
155 int i;
156 snd_emux_voice_t *vp;
157 emu8000_t *hw;
159 /* what we are looking for, in order of preference */
160 enum {
161 OFF=0, RELEASED, PLAYING, END
164 /* Keeps track of what we are finding */
165 struct best {
166 unsigned int time;
167 int voice;
168 } best[END];
169 struct best *bp;
171 hw = emu->hw;
173 for (i = 0; i < END; i++) {
174 best[i].time = (unsigned int)(-1); /* XXX MAX_?INT really */;
175 best[i].voice = -1;
179 * Go through them all and get a best one to use.
181 for (i = 0; i < emu->max_voices; i++) {
182 int state, val;
184 vp = &emu->voices[i];
185 state = vp->state;
187 if (state == SNDRV_EMUX_ST_OFF)
188 bp = best + OFF;
189 else if (state == SNDRV_EMUX_ST_RELEASED ||
190 state == SNDRV_EMUX_ST_PENDING) {
191 bp = best + RELEASED;
192 val = (EMU8000_CVCF_READ(hw, vp->ch) >> 16) & 0xffff;
193 if (! val)
194 bp = best + OFF;
196 else if (state & SNDRV_EMUX_ST_ON)
197 bp = best + PLAYING;
198 else
199 continue;
201 /* check if sample is finished playing (non-looping only) */
202 if (state != SNDRV_EMUX_ST_OFF &&
203 (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_SINGLESHOT)) {
204 val = EMU8000_CCCA_READ(hw, vp->ch) & 0xffffff;
205 if (val >= vp->reg.loopstart)
206 bp = best + OFF;
209 if (vp->time < bp->time) {
210 bp->time = vp->time;
211 bp->voice = i;
215 for (i = 0; i < END; i++) {
216 if (best[i].voice >= 0) {
217 vp = &emu->voices[best[i].voice];
218 vp->ch = best[i].voice;
219 return vp;
223 /* not found */
224 return NULL;
229 static int
230 start_voice(snd_emux_voice_t *vp)
232 unsigned int temp;
233 int ch;
234 int addr;
235 snd_midi_channel_t *chan;
236 emu8000_t *hw;
238 hw = vp->hw;
239 ch = vp->ch;
240 chan = vp->chan;
242 /* channel to be silent and idle */
243 EMU8000_DCYSUSV_WRITE(hw, ch, 0x0080);
244 EMU8000_VTFT_WRITE(hw, ch, 0x0000FFFF);
245 EMU8000_CVCF_WRITE(hw, ch, 0x0000FFFF);
246 EMU8000_PTRX_WRITE(hw, ch, 0);
247 EMU8000_CPF_WRITE(hw, ch, 0);
249 /* set pitch offset */
250 set_pitch(hw, vp);
252 /* set envelope parameters */
253 EMU8000_ENVVAL_WRITE(hw, ch, vp->reg.parm.moddelay);
254 EMU8000_ATKHLD_WRITE(hw, ch, vp->reg.parm.modatkhld);
255 EMU8000_DCYSUS_WRITE(hw, ch, vp->reg.parm.moddcysus);
256 EMU8000_ENVVOL_WRITE(hw, ch, vp->reg.parm.voldelay);
257 EMU8000_ATKHLDV_WRITE(hw, ch, vp->reg.parm.volatkhld);
258 /* decay/sustain parameter for volume envelope is used
259 for triggerg the voice */
261 /* cutoff and volume */
262 set_volume(hw, vp);
264 /* modulation envelope heights */
265 EMU8000_PEFE_WRITE(hw, ch, vp->reg.parm.pefe);
267 /* lfo1/2 delay */
268 EMU8000_LFO1VAL_WRITE(hw, ch, vp->reg.parm.lfo1delay);
269 EMU8000_LFO2VAL_WRITE(hw, ch, vp->reg.parm.lfo2delay);
271 /* lfo1 pitch & cutoff shift */
272 set_fmmod(hw, vp);
273 /* lfo1 volume & freq */
274 set_tremfreq(hw, vp);
275 /* lfo2 pitch & freq */
276 set_fm2frq2(hw, vp);
277 /* pan & loop start */
278 set_pan(hw, vp);
280 /* chorus & loop end (chorus 8bit, MSB) */
281 addr = vp->reg.loopend - 1;
282 temp = vp->reg.parm.chorus;
283 temp += (int)chan->control[MIDI_CTL_E3_CHORUS_DEPTH] * 9 / 10;
284 LIMITMAX(temp, 255);
285 temp = (temp <<24) | (unsigned int)addr;
286 EMU8000_CSL_WRITE(hw, ch, temp);
288 /* Q & current address (Q 4bit value, MSB) */
289 addr = vp->reg.start - 1;
290 temp = vp->reg.parm.filterQ;
291 temp = (temp<<28) | (unsigned int)addr;
292 EMU8000_CCCA_WRITE(hw, ch, temp);
294 /* clear unknown registers */
295 EMU8000_00A0_WRITE(hw, ch, 0);
296 EMU8000_0080_WRITE(hw, ch, 0);
298 /* reset volume */
299 temp = vp->vtarget << 16;
300 EMU8000_VTFT_WRITE(hw, ch, temp | vp->ftarget);
301 EMU8000_CVCF_WRITE(hw, ch, temp | 0xff00);
303 return 0;
307 * Start envelope
309 static void
310 trigger_voice(snd_emux_voice_t *vp)
312 int ch = vp->ch;
313 unsigned int temp;
314 emu8000_t *hw;
316 hw = vp->hw;
318 /* set reverb and pitch target */
319 temp = vp->reg.parm.reverb;
320 temp += (int)vp->chan->control[MIDI_CTL_E1_REVERB_DEPTH] * 9 / 10;
321 LIMITMAX(temp, 255);
322 temp = (temp << 8) | (vp->ptarget << 16) | vp->aaux;
323 EMU8000_PTRX_WRITE(hw, ch, temp);
324 EMU8000_CPF_WRITE(hw, ch, vp->ptarget << 16);
325 EMU8000_DCYSUSV_WRITE(hw, ch, vp->reg.parm.voldcysus);
329 * reset voice parameters
331 static void
332 reset_voice(snd_emux_t *emu, int ch)
334 emu8000_t *hw;
336 hw = emu->hw;
337 EMU8000_DCYSUSV_WRITE(hw, ch, 0x807F);
338 snd_emu8000_tweak_voice(hw, ch);
342 * Set the pitch of a possibly playing note.
344 static void
345 set_pitch(emu8000_t *hw, snd_emux_voice_t *vp)
347 EMU8000_IP_WRITE(hw, vp->ch, vp->apitch);
351 * Set the volume of a possibly already playing note
353 static void
354 set_volume(emu8000_t *hw, snd_emux_voice_t *vp)
356 int ifatn;
358 ifatn = (unsigned char)vp->acutoff;
359 ifatn = (ifatn << 8);
360 ifatn |= (unsigned char)vp->avol;
361 EMU8000_IFATN_WRITE(hw, vp->ch, ifatn);
365 * Set pan and loop start address.
367 static void
368 set_pan(emu8000_t *hw, snd_emux_voice_t *vp)
370 unsigned int temp;
372 temp = ((unsigned int)vp->apan<<24) | ((unsigned int)vp->reg.loopstart - 1);
373 EMU8000_PSST_WRITE(hw, vp->ch, temp);
376 #define MOD_SENSE 18
378 static void
379 set_fmmod(emu8000_t *hw, snd_emux_voice_t *vp)
381 unsigned short fmmod;
382 short pitch;
383 unsigned char cutoff;
384 int modulation;
386 pitch = (char)(vp->reg.parm.fmmod>>8);
387 cutoff = (vp->reg.parm.fmmod & 0xff);
388 modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
389 pitch += (MOD_SENSE * modulation) / 1200;
390 LIMITVALUE(pitch, -128, 127);
391 fmmod = ((unsigned char)pitch<<8) | cutoff;
392 EMU8000_FMMOD_WRITE(hw, vp->ch, fmmod);
395 /* set tremolo (lfo1) volume & frequency */
396 static void
397 set_tremfreq(emu8000_t *hw, snd_emux_voice_t *vp)
399 EMU8000_TREMFRQ_WRITE(hw, vp->ch, vp->reg.parm.tremfrq);
402 /* set lfo2 pitch & frequency */
403 static void
404 set_fm2frq2(emu8000_t *hw, snd_emux_voice_t *vp)
406 unsigned short fm2frq2;
407 short pitch;
408 unsigned char freq;
409 int modulation;
411 pitch = (char)(vp->reg.parm.fm2frq2>>8);
412 freq = vp->reg.parm.fm2frq2 & 0xff;
413 modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
414 pitch += (MOD_SENSE * modulation) / 1200;
415 LIMITVALUE(pitch, -128, 127);
416 fm2frq2 = ((unsigned char)pitch<<8) | freq;
417 EMU8000_FM2FRQ2_WRITE(hw, vp->ch, fm2frq2);
420 /* set filterQ */
421 static void
422 set_filterQ(emu8000_t *hw, snd_emux_voice_t *vp)
424 unsigned int addr;
425 addr = EMU8000_CCCA_READ(hw, vp->ch) & 0xffffff;
426 addr |= (vp->reg.parm.filterQ << 28);
427 EMU8000_CCCA_WRITE(hw, vp->ch, addr);
431 * set the envelope & LFO parameters to the default values
433 static void
434 snd_emu8000_tweak_voice(emu8000_t *emu, int i)
436 /* set all mod/vol envelope shape to minimum */
437 EMU8000_ENVVOL_WRITE(emu, i, 0x8000);
438 EMU8000_ENVVAL_WRITE(emu, i, 0x8000);
439 EMU8000_DCYSUS_WRITE(emu, i, 0x7F7F);
440 EMU8000_ATKHLDV_WRITE(emu, i, 0x7F7F);
441 EMU8000_ATKHLD_WRITE(emu, i, 0x7F7F);
442 EMU8000_PEFE_WRITE(emu, i, 0); /* mod envelope height to zero */
443 EMU8000_LFO1VAL_WRITE(emu, i, 0x8000); /* no delay for LFO1 */
444 EMU8000_LFO2VAL_WRITE(emu, i, 0x8000);
445 EMU8000_IP_WRITE(emu, i, 0xE000); /* no pitch shift */
446 EMU8000_IFATN_WRITE(emu, i, 0xFF00); /* volume to minimum */
447 EMU8000_FMMOD_WRITE(emu, i, 0);
448 EMU8000_TREMFRQ_WRITE(emu, i, 0);
449 EMU8000_FM2FRQ2_WRITE(emu, i, 0);
453 * sysex callback
455 static void
456 sysex(snd_emux_t *emu, char *buf, int len, int parsed, snd_midi_channel_set_t *chset)
458 emu8000_t *hw;
460 hw = emu->hw;
462 switch (parsed) {
463 case SNDRV_MIDI_SYSEX_GS_CHORUS_MODE:
464 hw->chorus_mode = chset->gs_chorus_mode;
465 snd_emu8000_update_chorus_mode(hw);
466 break;
468 case SNDRV_MIDI_SYSEX_GS_REVERB_MODE:
469 hw->reverb_mode = chset->gs_reverb_mode;
470 snd_emu8000_update_reverb_mode(hw);
471 break;
476 #ifdef CONFIG_SND_SEQUENCER_OSS
478 * OSS ioctl callback
480 static int
481 oss_ioctl(snd_emux_t *emu, int cmd, int p1, int p2)
483 emu8000_t *hw;
485 hw = emu->hw;
487 switch (cmd) {
488 case _EMUX_OSS_REVERB_MODE:
489 hw->reverb_mode = p1;
490 snd_emu8000_update_reverb_mode(hw);
491 break;
493 case _EMUX_OSS_CHORUS_MODE:
494 hw->chorus_mode = p1;
495 snd_emu8000_update_chorus_mode(hw);
496 break;
498 case _EMUX_OSS_INITIALIZE_CHIP:
499 /* snd_emu8000_init(hw); */ /*ignored*/
500 break;
502 case _EMUX_OSS_EQUALIZER:
503 hw->bass_level = p1;
504 hw->treble_level = p2;
505 snd_emu8000_update_equalizer(hw);
506 break;
508 return 0;
510 #endif
514 * additional patch keys
517 #define SNDRV_EMU8000_LOAD_CHORUS_FX 0x10 /* optarg=mode */
518 #define SNDRV_EMU8000_LOAD_REVERB_FX 0x11 /* optarg=mode */
522 * callback routine
525 static int
526 load_fx(snd_emux_t *emu, int type, int mode, const void __user *buf, long len)
528 emu8000_t *hw;
529 hw = emu->hw;
531 /* skip header */
532 buf += 16;
533 len -= 16;
535 switch (type) {
536 case SNDRV_EMU8000_LOAD_CHORUS_FX:
537 return snd_emu8000_load_chorus_fx(hw, mode, buf, len);
538 case SNDRV_EMU8000_LOAD_REVERB_FX:
539 return snd_emu8000_load_reverb_fx(hw, mode, buf, len);
541 return -EINVAL;