[PATCH] w1: Added default generic read/write operations.
[linux-2.6/verdex.git] / sound / pci / emu10k1 / emu10k1_callback.c
blob01965bd99966e0e76e13cbc0d90683b86bb4492a
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 *emu, struct snd_emu10k1 *hw,
39 struct best_voice *best, int active_only);
40 static struct snd_emux_voice *get_voice(struct snd_emux *emu,
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);
49 static void set_fmmod(struct snd_emu10k1 *hw, struct snd_emux_voice *vp);
50 static void set_fm2frq2(struct snd_emu10k1 *hw, struct snd_emux_voice *vp);
51 static void set_filterQ(struct snd_emu10k1 *hw, struct snd_emux_voice *vp);
54 * Ensure a value is between two points
55 * macro evaluates its args more than once, so changed to upper-case.
57 #define LIMITVALUE(x, a, b) do { if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b); } while (0)
58 #define LIMITMAX(x, a) do {if ((x) > (a)) (x) = (a); } while (0)
62 * set up operators
64 static struct snd_emux_operators emu10k1_ops = {
65 .owner = THIS_MODULE,
66 .get_voice = get_voice,
67 .prepare = start_voice,
68 .trigger = trigger_voice,
69 .release = release_voice,
70 .update = update_voice,
71 .terminate = terminate_voice,
72 .free_voice = free_voice,
73 .sample_new = snd_emu10k1_sample_new,
74 .sample_free = snd_emu10k1_sample_free,
77 void
78 snd_emu10k1_ops_setup(struct snd_emux *emu)
80 emu->ops = emu10k1_ops;
85 * get more voice for pcm
87 * terminate most inactive voice and give it as a pcm voice.
89 int
90 snd_emu10k1_synth_get_voice(struct snd_emu10k1 *hw)
92 struct snd_emux *emu;
93 struct snd_emux_voice *vp;
94 struct best_voice best[V_END];
95 unsigned long flags;
96 int i;
98 emu = hw->synth;
100 spin_lock_irqsave(&emu->voice_lock, flags);
101 lookup_voices(emu, hw, best, 1); /* no OFF voices */
102 for (i = 0; i < V_END; i++) {
103 if (best[i].voice >= 0) {
104 int ch;
105 vp = &emu->voices[best[i].voice];
106 if ((ch = vp->ch) < 0) {
107 //printk("synth_get_voice: ch < 0 (%d) ??", i);
108 continue;
110 vp->emu->num_voices--;
111 vp->ch = -1;
112 vp->state = SNDRV_EMUX_ST_OFF;
113 spin_unlock_irqrestore(&emu->voice_lock, flags);
114 return ch;
117 spin_unlock_irqrestore(&emu->voice_lock, flags);
119 /* not found */
120 return -ENOMEM;
125 * turn off the voice (not terminated)
127 static void
128 release_voice(struct snd_emux_voice *vp)
130 int dcysusv;
131 struct snd_emu10k1 *hw;
133 hw = vp->hw;
134 dcysusv = 0x8000 | (unsigned char)vp->reg.parm.modrelease;
135 snd_emu10k1_ptr_write(hw, DCYSUSM, vp->ch, dcysusv);
136 dcysusv = 0x8000 | (unsigned char)vp->reg.parm.volrelease | DCYSUSV_CHANNELENABLE_MASK;
137 snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, dcysusv);
142 * terminate the voice
144 static void
145 terminate_voice(struct snd_emux_voice *vp)
147 struct snd_emu10k1 *hw;
149 snd_assert(vp, return);
150 hw = vp->hw;
151 snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, 0x807f | DCYSUSV_CHANNELENABLE_MASK);
152 if (vp->block) {
153 struct snd_emu10k1_memblk *emem;
154 emem = (struct snd_emu10k1_memblk *)vp->block;
155 if (emem->map_locked > 0)
156 emem->map_locked--;
161 * release the voice to system
163 static void
164 free_voice(struct snd_emux_voice *vp)
166 struct snd_emu10k1 *hw;
168 hw = vp->hw;
169 if (vp->ch >= 0) {
170 snd_emu10k1_ptr_write(hw, IFATN, vp->ch, 0xff00);
171 snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, 0x807f | DCYSUSV_CHANNELENABLE_MASK);
172 // snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, 0);
173 snd_emu10k1_ptr_write(hw, VTFT, vp->ch, 0xffff);
174 snd_emu10k1_ptr_write(hw, CVCF, vp->ch, 0xffff);
175 snd_emu10k1_voice_free(hw, &hw->voices[vp->ch]);
176 vp->emu->num_voices--;
177 vp->ch = -1;
183 * update registers
185 static void
186 update_voice(struct snd_emux_voice *vp, int update)
188 struct snd_emu10k1 *hw;
190 hw = vp->hw;
191 if (update & SNDRV_EMUX_UPDATE_VOLUME)
192 snd_emu10k1_ptr_write(hw, IFATN_ATTENUATION, vp->ch, vp->avol);
193 if (update & SNDRV_EMUX_UPDATE_PITCH)
194 snd_emu10k1_ptr_write(hw, IP, vp->ch, vp->apitch);
195 if (update & SNDRV_EMUX_UPDATE_PAN) {
196 snd_emu10k1_ptr_write(hw, PTRX_FXSENDAMOUNT_A, vp->ch, vp->apan);
197 snd_emu10k1_ptr_write(hw, PTRX_FXSENDAMOUNT_B, vp->ch, vp->aaux);
199 if (update & SNDRV_EMUX_UPDATE_FMMOD)
200 set_fmmod(hw, vp);
201 if (update & SNDRV_EMUX_UPDATE_TREMFREQ)
202 snd_emu10k1_ptr_write(hw, TREMFRQ, vp->ch, vp->reg.parm.tremfrq);
203 if (update & SNDRV_EMUX_UPDATE_FM2FRQ2)
204 set_fm2frq2(hw, vp);
205 if (update & SNDRV_EMUX_UPDATE_Q)
206 set_filterQ(hw, vp);
211 * look up voice table - get the best voice in order of preference
213 /* spinlock held! */
214 static void
215 lookup_voices(struct snd_emux *emu, struct snd_emu10k1 *hw,
216 struct best_voice *best, int active_only)
218 struct snd_emux_voice *vp;
219 struct best_voice *bp;
220 int i;
222 for (i = 0; i < V_END; i++) {
223 best[i].time = (unsigned int)-1; /* XXX MAX_?INT really */;
224 best[i].voice = -1;
228 * Go through them all and get a best one to use.
229 * NOTE: could also look at volume and pick the quietest one.
231 for (i = 0; i < emu->max_voices; i++) {
232 int state, val;
234 vp = &emu->voices[i];
235 state = vp->state;
236 if (state == SNDRV_EMUX_ST_OFF) {
237 if (vp->ch < 0) {
238 if (active_only)
239 continue;
240 bp = best + V_FREE;
241 } else
242 bp = best + V_OFF;
244 else if (state == SNDRV_EMUX_ST_RELEASED ||
245 state == SNDRV_EMUX_ST_PENDING) {
246 bp = best + V_RELEASED;
247 #if 1
248 val = snd_emu10k1_ptr_read(hw, CVCF_CURRENTVOL, vp->ch);
249 if (! val)
250 bp = best + V_OFF;
251 #endif
253 else if (state == SNDRV_EMUX_ST_STANDBY)
254 continue;
255 else if (state & SNDRV_EMUX_ST_ON)
256 bp = best + V_PLAYING;
257 else
258 continue;
260 /* check if sample is finished playing (non-looping only) */
261 if (bp != best + V_OFF && bp != best + V_FREE &&
262 (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_SINGLESHOT)) {
263 val = snd_emu10k1_ptr_read(hw, CCCA_CURRADDR, vp->ch);
264 if (val >= vp->reg.loopstart)
265 bp = best + V_OFF;
268 if (vp->time < bp->time) {
269 bp->time = vp->time;
270 bp->voice = i;
276 * get an empty voice
278 * emu->voice_lock is already held.
280 static struct snd_emux_voice *
281 get_voice(struct snd_emux *emu, struct snd_emux_port *port)
283 struct snd_emu10k1 *hw;
284 struct snd_emux_voice *vp;
285 struct best_voice best[V_END];
286 int i;
288 hw = emu->hw;
290 lookup_voices(emu, hw, best, 0);
291 for (i = 0; i < V_END; i++) {
292 if (best[i].voice >= 0) {
293 vp = &emu->voices[best[i].voice];
294 if (vp->ch < 0) {
295 /* allocate a voice */
296 struct snd_emu10k1_voice *hwvoice;
297 if (snd_emu10k1_voice_alloc(hw, EMU10K1_SYNTH, 1, &hwvoice) < 0 || hwvoice == NULL)
298 continue;
299 vp->ch = hwvoice->number;
300 emu->num_voices++;
302 return vp;
306 /* not found */
307 return NULL;
311 * prepare envelopes and LFOs
313 static int
314 start_voice(struct snd_emux_voice *vp)
316 unsigned int temp;
317 int ch;
318 unsigned int addr, mapped_offset;
319 struct snd_midi_channel *chan;
320 struct snd_emu10k1 *hw;
321 struct snd_emu10k1_memblk *emem;
323 hw = vp->hw;
324 ch = vp->ch;
325 snd_assert(ch >= 0, return -EINVAL);
326 chan = vp->chan;
328 emem = (struct snd_emu10k1_memblk *)vp->block;
329 if (emem == NULL)
330 return -EINVAL;
331 emem->map_locked++;
332 if (snd_emu10k1_memblk_map(hw, emem) < 0) {
333 // printk("emu: cannot map!\n");
334 return -ENOMEM;
336 mapped_offset = snd_emu10k1_memblk_offset(emem) >> 1;
337 vp->reg.start += mapped_offset;
338 vp->reg.end += mapped_offset;
339 vp->reg.loopstart += mapped_offset;
340 vp->reg.loopend += mapped_offset;
342 /* set channel routing */
343 /* A = left(0), B = right(1), C = reverb(c), D = chorus(d) */
344 if (hw->audigy) {
345 temp = FXBUS_MIDI_LEFT | (FXBUS_MIDI_RIGHT << 8) |
346 (FXBUS_MIDI_REVERB << 16) | (FXBUS_MIDI_CHORUS << 24);
347 snd_emu10k1_ptr_write(hw, A_FXRT1, ch, temp);
348 } else {
349 temp = (FXBUS_MIDI_LEFT << 16) | (FXBUS_MIDI_RIGHT << 20) |
350 (FXBUS_MIDI_REVERB << 24) | (FXBUS_MIDI_CHORUS << 28);
351 snd_emu10k1_ptr_write(hw, FXRT, ch, temp);
354 /* channel to be silent and idle */
355 snd_emu10k1_ptr_write(hw, DCYSUSV, ch, 0x0000);
356 snd_emu10k1_ptr_write(hw, VTFT, ch, 0x0000FFFF);
357 snd_emu10k1_ptr_write(hw, CVCF, ch, 0x0000FFFF);
358 snd_emu10k1_ptr_write(hw, PTRX, ch, 0);
359 snd_emu10k1_ptr_write(hw, CPF, ch, 0);
361 /* set pitch offset */
362 snd_emu10k1_ptr_write(hw, IP, vp->ch, vp->apitch);
364 /* set envelope parameters */
365 snd_emu10k1_ptr_write(hw, ENVVAL, ch, vp->reg.parm.moddelay);
366 snd_emu10k1_ptr_write(hw, ATKHLDM, ch, vp->reg.parm.modatkhld);
367 snd_emu10k1_ptr_write(hw, DCYSUSM, ch, vp->reg.parm.moddcysus);
368 snd_emu10k1_ptr_write(hw, ENVVOL, ch, vp->reg.parm.voldelay);
369 snd_emu10k1_ptr_write(hw, ATKHLDV, ch, vp->reg.parm.volatkhld);
370 /* decay/sustain parameter for volume envelope is used
371 for triggerg the voice */
373 /* cutoff and volume */
374 temp = (unsigned int)vp->acutoff << 8 | (unsigned char)vp->avol;
375 snd_emu10k1_ptr_write(hw, IFATN, vp->ch, temp);
377 /* modulation envelope heights */
378 snd_emu10k1_ptr_write(hw, PEFE, ch, vp->reg.parm.pefe);
380 /* lfo1/2 delay */
381 snd_emu10k1_ptr_write(hw, LFOVAL1, ch, vp->reg.parm.lfo1delay);
382 snd_emu10k1_ptr_write(hw, LFOVAL2, ch, vp->reg.parm.lfo2delay);
384 /* lfo1 pitch & cutoff shift */
385 set_fmmod(hw, vp);
386 /* lfo1 volume & freq */
387 snd_emu10k1_ptr_write(hw, TREMFRQ, vp->ch, vp->reg.parm.tremfrq);
388 /* lfo2 pitch & freq */
389 set_fm2frq2(hw, vp);
391 /* reverb and loop start (reverb 8bit, MSB) */
392 temp = vp->reg.parm.reverb;
393 temp += (int)vp->chan->control[MIDI_CTL_E1_REVERB_DEPTH] * 9 / 10;
394 LIMITMAX(temp, 255);
395 addr = vp->reg.loopstart;
396 snd_emu10k1_ptr_write(hw, PSST, vp->ch, (temp << 24) | addr);
398 /* chorus & loop end (chorus 8bit, MSB) */
399 addr = vp->reg.loopend;
400 temp = vp->reg.parm.chorus;
401 temp += (int)chan->control[MIDI_CTL_E3_CHORUS_DEPTH] * 9 / 10;
402 LIMITMAX(temp, 255);
403 temp = (temp <<24) | addr;
404 snd_emu10k1_ptr_write(hw, DSL, ch, temp);
406 /* clear filter delay memory */
407 snd_emu10k1_ptr_write(hw, Z1, ch, 0);
408 snd_emu10k1_ptr_write(hw, Z2, ch, 0);
410 /* invalidate maps */
411 temp = (hw->silent_page.addr << 1) | MAP_PTI_MASK;
412 snd_emu10k1_ptr_write(hw, MAPA, ch, temp);
413 snd_emu10k1_ptr_write(hw, MAPB, ch, temp);
414 #if 0
415 /* cache */
417 unsigned int val, sample;
418 val = 32;
419 if (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_8BITS)
420 sample = 0x80808080;
421 else {
422 sample = 0;
423 val *= 2;
426 /* cache */
427 snd_emu10k1_ptr_write(hw, CCR, ch, 0x1c << 16);
428 snd_emu10k1_ptr_write(hw, CDE, ch, sample);
429 snd_emu10k1_ptr_write(hw, CDF, ch, sample);
431 /* invalidate maps */
432 temp = ((unsigned int)hw->silent_page.addr << 1) | MAP_PTI_MASK;
433 snd_emu10k1_ptr_write(hw, MAPA, ch, temp);
434 snd_emu10k1_ptr_write(hw, MAPB, ch, temp);
436 /* fill cache */
437 val -= 4;
438 val <<= 25;
439 val |= 0x1c << 16;
440 snd_emu10k1_ptr_write(hw, CCR, ch, val);
442 #endif
444 /* Q & current address (Q 4bit value, MSB) */
445 addr = vp->reg.start;
446 temp = vp->reg.parm.filterQ;
447 temp = (temp<<28) | addr;
448 if (vp->apitch < 0xe400)
449 temp |= CCCA_INTERPROM_0;
450 else {
451 unsigned int shift = (vp->apitch - 0xe000) >> 10;
452 temp |= shift << 25;
454 if (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_8BITS)
455 temp |= CCCA_8BITSELECT;
456 snd_emu10k1_ptr_write(hw, CCCA, ch, temp);
458 /* reset volume */
459 temp = (unsigned int)vp->vtarget << 16;
460 snd_emu10k1_ptr_write(hw, VTFT, ch, temp | vp->ftarget);
461 snd_emu10k1_ptr_write(hw, CVCF, ch, temp | 0xff00);
462 return 0;
466 * Start envelope
468 static void
469 trigger_voice(struct snd_emux_voice *vp)
471 unsigned int temp, ptarget;
472 struct snd_emu10k1 *hw;
473 struct snd_emu10k1_memblk *emem;
475 hw = vp->hw;
477 emem = (struct snd_emu10k1_memblk *)vp->block;
478 if (! emem || emem->mapped_page < 0)
479 return; /* not mapped */
481 #if 0
482 ptarget = (unsigned int)vp->ptarget << 16;
483 #else
484 ptarget = IP_TO_CP(vp->apitch);
485 #endif
486 /* set pitch target and pan (volume) */
487 temp = ptarget | (vp->apan << 8) | vp->aaux;
488 snd_emu10k1_ptr_write(hw, PTRX, vp->ch, temp);
490 /* pitch target */
491 snd_emu10k1_ptr_write(hw, CPF, vp->ch, ptarget);
493 /* trigger voice */
494 snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, vp->reg.parm.voldcysus|DCYSUSV_CHANNELENABLE_MASK);
497 #define MOD_SENSE 18
499 /* set lfo1 modulation height and cutoff */
500 static void
501 set_fmmod(struct snd_emu10k1 *hw, struct snd_emux_voice *vp)
503 unsigned short fmmod;
504 short pitch;
505 unsigned char cutoff;
506 int modulation;
508 pitch = (char)(vp->reg.parm.fmmod>>8);
509 cutoff = (vp->reg.parm.fmmod & 0xff);
510 modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
511 pitch += (MOD_SENSE * modulation) / 1200;
512 LIMITVALUE(pitch, -128, 127);
513 fmmod = ((unsigned char)pitch<<8) | cutoff;
514 snd_emu10k1_ptr_write(hw, FMMOD, vp->ch, fmmod);
517 /* set lfo2 pitch & frequency */
518 static void
519 set_fm2frq2(struct snd_emu10k1 *hw, struct snd_emux_voice *vp)
521 unsigned short fm2frq2;
522 short pitch;
523 unsigned char freq;
524 int modulation;
526 pitch = (char)(vp->reg.parm.fm2frq2>>8);
527 freq = vp->reg.parm.fm2frq2 & 0xff;
528 modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
529 pitch += (MOD_SENSE * modulation) / 1200;
530 LIMITVALUE(pitch, -128, 127);
531 fm2frq2 = ((unsigned char)pitch<<8) | freq;
532 snd_emu10k1_ptr_write(hw, FM2FRQ2, vp->ch, fm2frq2);
535 /* set filterQ */
536 static void
537 set_filterQ(struct snd_emu10k1 *hw, struct snd_emux_voice *vp)
539 unsigned int val;
540 val = snd_emu10k1_ptr_read(hw, CCCA, vp->ch) & ~CCCA_RESONANCE;
541 val |= (vp->reg.parm.filterQ << 28);
542 snd_emu10k1_ptr_write(hw, CCCA, vp->ch, val);