Merge branch 'merge' of git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
[wrt350n-kernel.git] / sound / pci / emu10k1 / emu10k1_callback.c
blob45088ebcce5041a9b25e147d41be6add78351f05
1 /*
2 * synth callback routines for Emu10k1
4 * Copyright (C) 2000 Takashi Iwai <tiwai@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "emu10k1_synth_local.h"
22 #include <sound/asoundef.h>
24 /* voice status */
25 enum {
26 V_FREE=0, V_OFF, V_RELEASED, V_PLAYING, V_END
29 /* Keeps track of what we are finding */
30 struct best_voice {
31 unsigned int time;
32 int voice;
36 * prototypes
38 static void lookup_voices(struct snd_emux *emux, struct snd_emu10k1 *hw,
39 struct best_voice *best, int active_only);
40 static struct snd_emux_voice *get_voice(struct snd_emux *emux,
41 struct snd_emux_port *port);
42 static int start_voice(struct snd_emux_voice *vp);
43 static void trigger_voice(struct snd_emux_voice *vp);
44 static void release_voice(struct snd_emux_voice *vp);
45 static void update_voice(struct snd_emux_voice *vp, int update);
46 static void terminate_voice(struct snd_emux_voice *vp);
47 static void free_voice(struct snd_emux_voice *vp);
48 static void set_fmmod(struct snd_emu10k1 *hw, struct snd_emux_voice *vp);
49 static void set_fm2frq2(struct snd_emu10k1 *hw, struct snd_emux_voice *vp);
50 static void set_filterQ(struct snd_emu10k1 *hw, struct snd_emux_voice *vp);
53 * Ensure a value is between two points
54 * macro evaluates its args more than once, so changed to upper-case.
56 #define LIMITVALUE(x, a, b) do { if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b); } while (0)
57 #define LIMITMAX(x, a) do {if ((x) > (a)) (x) = (a); } while (0)
61 * set up operators
63 static struct snd_emux_operators emu10k1_ops = {
64 .owner = THIS_MODULE,
65 .get_voice = get_voice,
66 .prepare = start_voice,
67 .trigger = trigger_voice,
68 .release = release_voice,
69 .update = update_voice,
70 .terminate = terminate_voice,
71 .free_voice = free_voice,
72 .sample_new = snd_emu10k1_sample_new,
73 .sample_free = snd_emu10k1_sample_free,
76 void
77 snd_emu10k1_ops_setup(struct snd_emux *emux)
79 emux->ops = emu10k1_ops;
84 * get more voice for pcm
86 * terminate most inactive voice and give it as a pcm voice.
88 int
89 snd_emu10k1_synth_get_voice(struct snd_emu10k1 *hw)
91 struct snd_emux *emu;
92 struct snd_emux_voice *vp;
93 struct best_voice best[V_END];
94 unsigned long flags;
95 int i;
97 emu = hw->synth;
99 spin_lock_irqsave(&emu->voice_lock, flags);
100 lookup_voices(emu, hw, best, 1); /* no OFF voices */
101 for (i = 0; i < V_END; i++) {
102 if (best[i].voice >= 0) {
103 int ch;
104 vp = &emu->voices[best[i].voice];
105 if ((ch = vp->ch) < 0) {
106 //printk("synth_get_voice: ch < 0 (%d) ??", i);
107 continue;
109 vp->emu->num_voices--;
110 vp->ch = -1;
111 vp->state = SNDRV_EMUX_ST_OFF;
112 spin_unlock_irqrestore(&emu->voice_lock, flags);
113 return ch;
116 spin_unlock_irqrestore(&emu->voice_lock, flags);
118 /* not found */
119 return -ENOMEM;
124 * turn off the voice (not terminated)
126 static void
127 release_voice(struct snd_emux_voice *vp)
129 int dcysusv;
130 struct snd_emu10k1 *hw;
132 hw = vp->hw;
133 dcysusv = 0x8000 | (unsigned char)vp->reg.parm.modrelease;
134 snd_emu10k1_ptr_write(hw, DCYSUSM, vp->ch, dcysusv);
135 dcysusv = 0x8000 | (unsigned char)vp->reg.parm.volrelease | DCYSUSV_CHANNELENABLE_MASK;
136 snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, dcysusv);
141 * terminate the voice
143 static void
144 terminate_voice(struct snd_emux_voice *vp)
146 struct snd_emu10k1 *hw;
148 snd_assert(vp, return);
149 hw = vp->hw;
150 snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, 0x807f | DCYSUSV_CHANNELENABLE_MASK);
151 if (vp->block) {
152 struct snd_emu10k1_memblk *emem;
153 emem = (struct snd_emu10k1_memblk *)vp->block;
154 if (emem->map_locked > 0)
155 emem->map_locked--;
160 * release the voice to system
162 static void
163 free_voice(struct snd_emux_voice *vp)
165 struct snd_emu10k1 *hw;
167 hw = vp->hw;
168 /* FIXME: emu10k1_synth is broken. */
169 /* This can get called with hw == 0 */
170 /* Problem apparent on plug, unplug then plug */
171 /* on the Audigy 2 ZS Notebook. */
172 if (hw && (vp->ch >= 0)) {
173 snd_emu10k1_ptr_write(hw, IFATN, vp->ch, 0xff00);
174 snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, 0x807f | DCYSUSV_CHANNELENABLE_MASK);
175 // snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, 0);
176 snd_emu10k1_ptr_write(hw, VTFT, vp->ch, 0xffff);
177 snd_emu10k1_ptr_write(hw, CVCF, vp->ch, 0xffff);
178 snd_emu10k1_voice_free(hw, &hw->voices[vp->ch]);
179 vp->emu->num_voices--;
180 vp->ch = -1;
186 * update registers
188 static void
189 update_voice(struct snd_emux_voice *vp, int update)
191 struct snd_emu10k1 *hw;
193 hw = vp->hw;
194 if (update & SNDRV_EMUX_UPDATE_VOLUME)
195 snd_emu10k1_ptr_write(hw, IFATN_ATTENUATION, vp->ch, vp->avol);
196 if (update & SNDRV_EMUX_UPDATE_PITCH)
197 snd_emu10k1_ptr_write(hw, IP, vp->ch, vp->apitch);
198 if (update & SNDRV_EMUX_UPDATE_PAN) {
199 snd_emu10k1_ptr_write(hw, PTRX_FXSENDAMOUNT_A, vp->ch, vp->apan);
200 snd_emu10k1_ptr_write(hw, PTRX_FXSENDAMOUNT_B, vp->ch, vp->aaux);
202 if (update & SNDRV_EMUX_UPDATE_FMMOD)
203 set_fmmod(hw, vp);
204 if (update & SNDRV_EMUX_UPDATE_TREMFREQ)
205 snd_emu10k1_ptr_write(hw, TREMFRQ, vp->ch, vp->reg.parm.tremfrq);
206 if (update & SNDRV_EMUX_UPDATE_FM2FRQ2)
207 set_fm2frq2(hw, vp);
208 if (update & SNDRV_EMUX_UPDATE_Q)
209 set_filterQ(hw, vp);
214 * look up voice table - get the best voice in order of preference
216 /* spinlock held! */
217 static void
218 lookup_voices(struct snd_emux *emu, struct snd_emu10k1 *hw,
219 struct best_voice *best, int active_only)
221 struct snd_emux_voice *vp;
222 struct best_voice *bp;
223 int i;
225 for (i = 0; i < V_END; i++) {
226 best[i].time = (unsigned int)-1; /* XXX MAX_?INT really */;
227 best[i].voice = -1;
231 * Go through them all and get a best one to use.
232 * NOTE: could also look at volume and pick the quietest one.
234 for (i = 0; i < emu->max_voices; i++) {
235 int state, val;
237 vp = &emu->voices[i];
238 state = vp->state;
239 if (state == SNDRV_EMUX_ST_OFF) {
240 if (vp->ch < 0) {
241 if (active_only)
242 continue;
243 bp = best + V_FREE;
244 } else
245 bp = best + V_OFF;
247 else if (state == SNDRV_EMUX_ST_RELEASED ||
248 state == SNDRV_EMUX_ST_PENDING) {
249 bp = best + V_RELEASED;
250 #if 1
251 val = snd_emu10k1_ptr_read(hw, CVCF_CURRENTVOL, vp->ch);
252 if (! val)
253 bp = best + V_OFF;
254 #endif
256 else if (state == SNDRV_EMUX_ST_STANDBY)
257 continue;
258 else if (state & SNDRV_EMUX_ST_ON)
259 bp = best + V_PLAYING;
260 else
261 continue;
263 /* check if sample is finished playing (non-looping only) */
264 if (bp != best + V_OFF && bp != best + V_FREE &&
265 (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_SINGLESHOT)) {
266 val = snd_emu10k1_ptr_read(hw, CCCA_CURRADDR, vp->ch);
267 if (val >= vp->reg.loopstart)
268 bp = best + V_OFF;
271 if (vp->time < bp->time) {
272 bp->time = vp->time;
273 bp->voice = i;
279 * get an empty voice
281 * emu->voice_lock is already held.
283 static struct snd_emux_voice *
284 get_voice(struct snd_emux *emu, struct snd_emux_port *port)
286 struct snd_emu10k1 *hw;
287 struct snd_emux_voice *vp;
288 struct best_voice best[V_END];
289 int i;
291 hw = emu->hw;
293 lookup_voices(emu, hw, best, 0);
294 for (i = 0; i < V_END; i++) {
295 if (best[i].voice >= 0) {
296 vp = &emu->voices[best[i].voice];
297 if (vp->ch < 0) {
298 /* allocate a voice */
299 struct snd_emu10k1_voice *hwvoice;
300 if (snd_emu10k1_voice_alloc(hw, EMU10K1_SYNTH, 1, &hwvoice) < 0 || hwvoice == NULL)
301 continue;
302 vp->ch = hwvoice->number;
303 emu->num_voices++;
305 return vp;
309 /* not found */
310 return NULL;
314 * prepare envelopes and LFOs
316 static int
317 start_voice(struct snd_emux_voice *vp)
319 unsigned int temp;
320 int ch;
321 unsigned int addr, mapped_offset;
322 struct snd_midi_channel *chan;
323 struct snd_emu10k1 *hw;
324 struct snd_emu10k1_memblk *emem;
326 hw = vp->hw;
327 ch = vp->ch;
328 snd_assert(ch >= 0, return -EINVAL);
329 chan = vp->chan;
331 emem = (struct snd_emu10k1_memblk *)vp->block;
332 if (emem == NULL)
333 return -EINVAL;
334 emem->map_locked++;
335 if (snd_emu10k1_memblk_map(hw, emem) < 0) {
336 // printk("emu: cannot map!\n");
337 return -ENOMEM;
339 mapped_offset = snd_emu10k1_memblk_offset(emem) >> 1;
340 vp->reg.start += mapped_offset;
341 vp->reg.end += mapped_offset;
342 vp->reg.loopstart += mapped_offset;
343 vp->reg.loopend += mapped_offset;
345 /* set channel routing */
346 /* A = left(0), B = right(1), C = reverb(c), D = chorus(d) */
347 if (hw->audigy) {
348 temp = FXBUS_MIDI_LEFT | (FXBUS_MIDI_RIGHT << 8) |
349 (FXBUS_MIDI_REVERB << 16) | (FXBUS_MIDI_CHORUS << 24);
350 snd_emu10k1_ptr_write(hw, A_FXRT1, ch, temp);
351 } else {
352 temp = (FXBUS_MIDI_LEFT << 16) | (FXBUS_MIDI_RIGHT << 20) |
353 (FXBUS_MIDI_REVERB << 24) | (FXBUS_MIDI_CHORUS << 28);
354 snd_emu10k1_ptr_write(hw, FXRT, ch, temp);
357 /* channel to be silent and idle */
358 snd_emu10k1_ptr_write(hw, DCYSUSV, ch, 0x0000);
359 snd_emu10k1_ptr_write(hw, VTFT, ch, 0x0000FFFF);
360 snd_emu10k1_ptr_write(hw, CVCF, ch, 0x0000FFFF);
361 snd_emu10k1_ptr_write(hw, PTRX, ch, 0);
362 snd_emu10k1_ptr_write(hw, CPF, ch, 0);
364 /* set pitch offset */
365 snd_emu10k1_ptr_write(hw, IP, vp->ch, vp->apitch);
367 /* set envelope parameters */
368 snd_emu10k1_ptr_write(hw, ENVVAL, ch, vp->reg.parm.moddelay);
369 snd_emu10k1_ptr_write(hw, ATKHLDM, ch, vp->reg.parm.modatkhld);
370 snd_emu10k1_ptr_write(hw, DCYSUSM, ch, vp->reg.parm.moddcysus);
371 snd_emu10k1_ptr_write(hw, ENVVOL, ch, vp->reg.parm.voldelay);
372 snd_emu10k1_ptr_write(hw, ATKHLDV, ch, vp->reg.parm.volatkhld);
373 /* decay/sustain parameter for volume envelope is used
374 for triggerg the voice */
376 /* cutoff and volume */
377 temp = (unsigned int)vp->acutoff << 8 | (unsigned char)vp->avol;
378 snd_emu10k1_ptr_write(hw, IFATN, vp->ch, temp);
380 /* modulation envelope heights */
381 snd_emu10k1_ptr_write(hw, PEFE, ch, vp->reg.parm.pefe);
383 /* lfo1/2 delay */
384 snd_emu10k1_ptr_write(hw, LFOVAL1, ch, vp->reg.parm.lfo1delay);
385 snd_emu10k1_ptr_write(hw, LFOVAL2, ch, vp->reg.parm.lfo2delay);
387 /* lfo1 pitch & cutoff shift */
388 set_fmmod(hw, vp);
389 /* lfo1 volume & freq */
390 snd_emu10k1_ptr_write(hw, TREMFRQ, vp->ch, vp->reg.parm.tremfrq);
391 /* lfo2 pitch & freq */
392 set_fm2frq2(hw, vp);
394 /* reverb and loop start (reverb 8bit, MSB) */
395 temp = vp->reg.parm.reverb;
396 temp += (int)vp->chan->control[MIDI_CTL_E1_REVERB_DEPTH] * 9 / 10;
397 LIMITMAX(temp, 255);
398 addr = vp->reg.loopstart;
399 snd_emu10k1_ptr_write(hw, PSST, vp->ch, (temp << 24) | addr);
401 /* chorus & loop end (chorus 8bit, MSB) */
402 addr = vp->reg.loopend;
403 temp = vp->reg.parm.chorus;
404 temp += (int)chan->control[MIDI_CTL_E3_CHORUS_DEPTH] * 9 / 10;
405 LIMITMAX(temp, 255);
406 temp = (temp <<24) | addr;
407 snd_emu10k1_ptr_write(hw, DSL, ch, temp);
409 /* clear filter delay memory */
410 snd_emu10k1_ptr_write(hw, Z1, ch, 0);
411 snd_emu10k1_ptr_write(hw, Z2, ch, 0);
413 /* invalidate maps */
414 temp = (hw->silent_page.addr << 1) | MAP_PTI_MASK;
415 snd_emu10k1_ptr_write(hw, MAPA, ch, temp);
416 snd_emu10k1_ptr_write(hw, MAPB, ch, temp);
417 #if 0
418 /* cache */
420 unsigned int val, sample;
421 val = 32;
422 if (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_8BITS)
423 sample = 0x80808080;
424 else {
425 sample = 0;
426 val *= 2;
429 /* cache */
430 snd_emu10k1_ptr_write(hw, CCR, ch, 0x1c << 16);
431 snd_emu10k1_ptr_write(hw, CDE, ch, sample);
432 snd_emu10k1_ptr_write(hw, CDF, ch, sample);
434 /* invalidate maps */
435 temp = ((unsigned int)hw->silent_page.addr << 1) | MAP_PTI_MASK;
436 snd_emu10k1_ptr_write(hw, MAPA, ch, temp);
437 snd_emu10k1_ptr_write(hw, MAPB, ch, temp);
439 /* fill cache */
440 val -= 4;
441 val <<= 25;
442 val |= 0x1c << 16;
443 snd_emu10k1_ptr_write(hw, CCR, ch, val);
445 #endif
447 /* Q & current address (Q 4bit value, MSB) */
448 addr = vp->reg.start;
449 temp = vp->reg.parm.filterQ;
450 temp = (temp<<28) | addr;
451 if (vp->apitch < 0xe400)
452 temp |= CCCA_INTERPROM_0;
453 else {
454 unsigned int shift = (vp->apitch - 0xe000) >> 10;
455 temp |= shift << 25;
457 if (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_8BITS)
458 temp |= CCCA_8BITSELECT;
459 snd_emu10k1_ptr_write(hw, CCCA, ch, temp);
461 /* reset volume */
462 temp = (unsigned int)vp->vtarget << 16;
463 snd_emu10k1_ptr_write(hw, VTFT, ch, temp | vp->ftarget);
464 snd_emu10k1_ptr_write(hw, CVCF, ch, temp | 0xff00);
465 return 0;
469 * Start envelope
471 static void
472 trigger_voice(struct snd_emux_voice *vp)
474 unsigned int temp, ptarget;
475 struct snd_emu10k1 *hw;
476 struct snd_emu10k1_memblk *emem;
478 hw = vp->hw;
480 emem = (struct snd_emu10k1_memblk *)vp->block;
481 if (! emem || emem->mapped_page < 0)
482 return; /* not mapped */
484 #if 0
485 ptarget = (unsigned int)vp->ptarget << 16;
486 #else
487 ptarget = IP_TO_CP(vp->apitch);
488 #endif
489 /* set pitch target and pan (volume) */
490 temp = ptarget | (vp->apan << 8) | vp->aaux;
491 snd_emu10k1_ptr_write(hw, PTRX, vp->ch, temp);
493 /* pitch target */
494 snd_emu10k1_ptr_write(hw, CPF, vp->ch, ptarget);
496 /* trigger voice */
497 snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, vp->reg.parm.voldcysus|DCYSUSV_CHANNELENABLE_MASK);
500 #define MOD_SENSE 18
502 /* set lfo1 modulation height and cutoff */
503 static void
504 set_fmmod(struct snd_emu10k1 *hw, struct snd_emux_voice *vp)
506 unsigned short fmmod;
507 short pitch;
508 unsigned char cutoff;
509 int modulation;
511 pitch = (char)(vp->reg.parm.fmmod>>8);
512 cutoff = (vp->reg.parm.fmmod & 0xff);
513 modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
514 pitch += (MOD_SENSE * modulation) / 1200;
515 LIMITVALUE(pitch, -128, 127);
516 fmmod = ((unsigned char)pitch<<8) | cutoff;
517 snd_emu10k1_ptr_write(hw, FMMOD, vp->ch, fmmod);
520 /* set lfo2 pitch & frequency */
521 static void
522 set_fm2frq2(struct snd_emu10k1 *hw, struct snd_emux_voice *vp)
524 unsigned short fm2frq2;
525 short pitch;
526 unsigned char freq;
527 int modulation;
529 pitch = (char)(vp->reg.parm.fm2frq2>>8);
530 freq = vp->reg.parm.fm2frq2 & 0xff;
531 modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
532 pitch += (MOD_SENSE * modulation) / 1200;
533 LIMITVALUE(pitch, -128, 127);
534 fm2frq2 = ((unsigned char)pitch<<8) | freq;
535 snd_emu10k1_ptr_write(hw, FM2FRQ2, vp->ch, fm2frq2);
538 /* set filterQ */
539 static void
540 set_filterQ(struct snd_emu10k1 *hw, struct snd_emux_voice *vp)
542 unsigned int val;
543 val = snd_emu10k1_ptr_read(hw, CCCA, vp->ch) & ~CCCA_RESONANCE;
544 val |= (vp->reg.parm.filterQ << 28);
545 snd_emu10k1_ptr_write(hw, CCCA, vp->ch, val);