My first commit.
[dragonfly.git] / sys / i386 / gnu / isa / sound / awe_wave.c
1 /*
2  * sound/awe_wave.c
3  *
4  * The low level driver for the AWE32/Sound Blaster 32 wave table synth.
5  *   version 0.4.2c; Oct. 7, 1997
6  *
7  * Copyright (C) 1996,1997 Takashi Iwai
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $DragonFly: src/sys/i386/gnu/isa/sound/Attic/awe_wave.c,v 1.4 2004/12/08 20:36:39 joerg Exp $
24  */
25
26 #include "opt_depricated.h"
27 #ifndef I_WANT_DEPRICATED_STUFF
28 #error "Add options I_WANT_DEPRICATED_STUFF to your kernel config and send a mail to kernel@"
29 #endif
30
31 #include <stddef.h>
32
33 #if defined(__DragonFly__) || defined(__FreeBSD__)
34 #  include "awe_config.h"
35 #else
36 #  include "awe_config.h"
37 #endif
38
39 /*----------------------------------------------------------------*/
40
41 #ifdef CONFIG_AWE32_SYNTH
42
43 #if defined(__DragonFly__) || defined(__FreeBSD__)
44 #  include "awe_hw.h"
45 #  include "awe_version.h"
46 #  include "awe_voice.h"
47 #else
48 #  include "awe_hw.h"
49 #  include "awe_version.h"
50 #  include "awe_voice.h"
51 #endif
52
53 #ifdef AWE_HAS_GUS_COMPATIBILITY
54 /* include finetune table */
55
56 #if defined(__DragonFly__) || defined(__FreeBSD__)
57 #  ifdef AWE_OBSOLETE_VOXWARE
58 #    define SEQUENCER_C
59 #  endif
60 #  include <i386/isa/sound/tuning.h>
61 #else
62 #   ifdef AWE_OBSOLETE_VOXWARE
63 #     include "tuning.h"
64 #   else
65 #     include "../tuning.h"
66 #   endif
67 #endif
68
69 #ifdef linux
70 #  include <linux/ultrasound.h>
71 #elif defined(__DragonFly__) || defined(__FreeBSD__)
72 #  include <machine/ultrasound.h>
73 #endif
74
75 #endif /* AWE_HAS_GUS_COMPATIBILITY */
76
77
78 /*----------------------------------------------------------------
79  * debug message
80  *----------------------------------------------------------------*/
81
82 static int debug_mode = 0;
83 #ifdef AWE_DEBUG_ON
84 #define AWE_DEBUG(LVL,XXX)      {if (debug_mode > LVL) { XXX; }}
85 #define ERRMSG(XXX)     {if (debug_mode) { XXX; }}
86 #define FATALERR(XXX)   XXX
87 #else
88 #define AWE_DEBUG(LVL,XXX) /**/
89 #define ERRMSG(XXX)     XXX
90 #define FATALERR(XXX)   XXX
91 #endif
92
93 /*----------------------------------------------------------------
94  * bank and voice record
95  *----------------------------------------------------------------*/
96
97 /* soundfont record */
98 typedef struct _sf_list {
99         unsigned short sf_id;
100         unsigned short type;
101         int num_info;           /* current info table index */
102         int num_sample;         /* current sample table index */
103         int mem_ptr;            /* current word byte pointer */
104         int infos;
105         int samples;
106         /*char name[AWE_PATCH_NAME_LEN];*/
107 } sf_list;
108
109 /* bank record */
110 typedef struct _awe_voice_list {
111         int next;       /* linked list with same sf_id */
112         unsigned char bank, instr;      /* preset number information */
113         char type, disabled;    /* type=normal/mapped, disabled=boolean */
114         awe_voice_info v;       /* voice information */
115         int next_instr; /* preset table list */
116         int next_bank;  /* preset table list */
117 } awe_voice_list;
118
119 /* voice list type */
120 #define V_ST_NORMAL     0
121 #define V_ST_MAPPED     1
122
123 typedef struct _awe_sample_list {
124         int next;       /* linked list with same sf_id */
125         awe_sample_info v;      /* sample information */
126 } awe_sample_list;
127
128 /* sample and information table */
129 static int current_sf_id = 0;
130 static int locked_sf_id = 0;
131 static int max_sfs;
132 static sf_list *sflists = NULL;
133
134 #define awe_free_mem_ptr() (current_sf_id <= 0 ? 0 : sflists[current_sf_id-1].mem_ptr)
135 #define awe_free_info() (current_sf_id <= 0 ? 0 : sflists[current_sf_id-1].num_info)
136 #define awe_free_sample() (current_sf_id <= 0 ? 0 : sflists[current_sf_id-1].num_sample)
137
138 static int max_samples;
139 static awe_sample_list *samples = NULL;
140
141 static int max_infos;
142 static awe_voice_list *infos = NULL;
143
144
145 #define AWE_MAX_PRESETS         256
146 #define AWE_DEFAULT_PRESET      0
147 #define AWE_DEFAULT_BANK        0
148 #define AWE_DEFAULT_DRUM        0
149 #define AWE_DRUM_BANK           128
150
151 #define MAX_LAYERS      AWE_MAX_VOICES
152
153 /* preset table index */
154 static int preset_table[AWE_MAX_PRESETS];
155
156 /*----------------------------------------------------------------
157  * voice table
158  *----------------------------------------------------------------*/
159
160 /* effects table */
161 typedef struct FX_Rec { /* channel effects */
162         unsigned char flags[AWE_FX_END];
163         short val[AWE_FX_END];
164 } FX_Rec;
165
166
167 /* channel parameters */
168 typedef struct _awe_chan_info {
169         int channel;            /* channel number */
170         int bank;               /* current tone bank */
171         int instr;              /* current program */
172         int bender;             /* midi pitchbend (-8192 - 8192) */
173         int bender_range;       /* midi bender range (x100) */
174         int panning;            /* panning (0-127) */
175         int main_vol;           /* channel volume (0-127) */
176         int expression_vol;     /* midi expression (0-127) */
177         int chan_press;         /* channel pressure */
178         int vrec;               /* instrument list */
179         int def_vrec;           /* default instrument list */
180         int sustained;          /* sustain status in MIDI */
181         FX_Rec fx;              /* effects */
182         FX_Rec fx_layer[MAX_LAYERS]; /* layer effects */
183 } awe_chan_info;
184
185 /* voice parameters */
186 typedef struct _voice_info {
187         int state;
188 #define AWE_ST_OFF              (1<<0)  /* no sound */
189 #define AWE_ST_ON               (1<<1)  /* playing */
190 #define AWE_ST_STANDBY          (1<<2)  /* stand by for playing */
191 #define AWE_ST_SUSTAINED        (1<<3)  /* sustained */
192 #define AWE_ST_MARK             (1<<4)  /* marked for allocation */
193 #define AWE_ST_DRAM             (1<<5)  /* DRAM read/write */
194 #define AWE_ST_FM               (1<<6)  /* reserved for FM */
195 #define AWE_ST_RELEASED         (1<<7)  /* released */
196
197         int ch;                 /* midi channel */
198         int key;                /* internal key for search */
199         int layer;              /* layer number (for channel mode only) */
200         int time;               /* allocated time */
201         awe_chan_info   *cinfo; /* channel info */
202
203         int note;               /* midi key (0-127) */
204         int velocity;           /* midi velocity (0-127) */
205         int sostenuto;          /* sostenuto on/off */
206         awe_voice_info *sample; /* assigned voice */
207
208         /* EMU8000 parameters */
209         int apitch;             /* pitch parameter */
210         int avol;               /* volume parameter */
211         int apan;               /* panning parameter */
212 } voice_info;
213
214 /* voice information */
215 static voice_info *voices;
216
217 #define IS_NO_SOUND(v)  (voices[v].state & (AWE_ST_OFF|AWE_ST_RELEASED|AWE_ST_STANDBY|AWE_ST_SUSTAINED))
218 #define IS_NO_EFFECT(v) (voices[v].state != AWE_ST_ON)
219 #define IS_PLAYING(v)   (voices[v].state & (AWE_ST_ON|AWE_ST_SUSTAINED|AWE_ST_RELEASED))
220 #define IS_EMPTY(v)     (voices[v].state & (AWE_ST_OFF|AWE_ST_MARK|AWE_ST_DRAM|AWE_ST_FM))
221
222
223 /* MIDI channel effects information (for hw control) */
224 static awe_chan_info *channels;
225
226
227 /*----------------------------------------------------------------
228  * global variables
229  *----------------------------------------------------------------*/
230
231 #ifndef AWE_DEFAULT_BASE_ADDR
232 #define AWE_DEFAULT_BASE_ADDR   0       /* autodetect */
233 #endif
234
235 #ifndef AWE_DEFAULT_MEM_SIZE
236 #define AWE_DEFAULT_MEM_SIZE    0       /* autodetect */
237 #endif
238
239 /* awe32 base address (overwritten at initialization) */
240 static int awe_base = AWE_DEFAULT_BASE_ADDR;
241 /* memory byte size */
242 static int awe_mem_size = AWE_DEFAULT_MEM_SIZE;
243 /* DRAM start offset */
244 static int awe_mem_start = AWE_DRAM_OFFSET;
245
246 /* maximum channels for playing */
247 static int awe_max_voices = AWE_MAX_VOICES;
248
249 static int patch_opened = 0;            /* sample already loaded? */
250
251 static int reverb_mode = 4;             /* reverb mode */
252 static int chorus_mode = 2;             /* chorus mode */
253 static short init_atten = AWE_DEFAULT_ATTENUATION; /* 12dB below */
254
255 static int awe_present = FALSE;         /* awe device present? */
256 static int awe_busy = FALSE;            /* awe device opened? */
257
258 #define DEFAULT_DRUM_FLAGS      ((1 << 9) | (1 << 25))
259 #define IS_DRUM_CHANNEL(c)      (drum_flags & (1 << (c)))
260 #define DRUM_CHANNEL_ON(c)      (drum_flags |= (1 << (c)))
261 #define DRUM_CHANNEL_OFF(c)     (drum_flags &= ~(1 << (c)))
262 static unsigned int drum_flags = DEFAULT_DRUM_FLAGS; /* channel flags */
263
264 static int playing_mode = AWE_PLAY_INDIRECT;
265 #define SINGLE_LAYER_MODE()     (playing_mode == AWE_PLAY_INDIRECT || playing_mode == AWE_PLAY_DIRECT)
266 #define MULTI_LAYER_MODE()      (playing_mode == AWE_PLAY_MULTI || playing_mode == AWE_PLAY_MULTI2)
267
268 static int current_alloc_time = 0;      /* voice allocation index for channel mode */
269
270 static struct MiscModeDef {
271         int value;
272         int init_each_time;
273 } misc_modes_default[AWE_MD_END] = {
274         {0,0}, {0,0}, /* <-- not used */
275         {AWE_VERSION_NUMBER, FALSE},
276         {TRUE, TRUE}, /* exclusive */
277         {TRUE, TRUE}, /* realpan */
278         {AWE_DEFAULT_BANK, TRUE}, /* gusbank */
279         {FALSE, TRUE}, /* keep effect */
280         {AWE_DEFAULT_ATTENUATION, FALSE}, /* zero_atten */
281         {FALSE, TRUE}, /* chn_prior */
282         {AWE_DEFAULT_MOD_SENSE, TRUE}, /* modwheel sense */
283         {AWE_DEFAULT_PRESET, TRUE}, /* def_preset */
284         {AWE_DEFAULT_BANK, TRUE}, /* def_bank */
285         {AWE_DEFAULT_DRUM, TRUE}, /* def_drum */
286         {FALSE, TRUE}, /* toggle_drum_bank */
287 };
288
289 static int misc_modes[AWE_MD_END];
290
291 static int awe_bass_level = 5;
292 static int awe_treble_level = 9;
293
294
295 static struct synth_info awe_info = {
296         "AWE32 Synth",          /* name */
297         0,                      /* device */
298         SYNTH_TYPE_SAMPLE,      /* synth_type */
299         SAMPLE_TYPE_AWE32,      /* synth_subtype */
300         0,                      /* perc_mode (obsolete) */
301         AWE_MAX_VOICES,         /* nr_voices */
302         0,                      /* nr_drums (obsolete) */
303         AWE_MAX_INFOS           /* instr_bank_size */
304 };
305
306
307 static struct voice_alloc_info *voice_alloc;    /* set at initialization */
308
309
310 /*----------------------------------------------------------------
311  * function prototypes
312  *----------------------------------------------------------------*/
313
314 #if defined(linux) && !defined(AWE_OBSOLETE_VOXWARE)
315 static int awe_check_port(void);
316 static void awe_request_region(void);
317 static void awe_release_region(void);
318 #endif
319
320 static void awe_reset_samples(void);
321 /* emu8000 chip i/o access */
322 static void awe_poke(unsigned short cmd, unsigned short port, unsigned short data);
323 static void awe_poke_dw(unsigned short cmd, unsigned short port, unsigned int data);
324 static unsigned short awe_peek(unsigned short cmd, unsigned short port);
325 static unsigned int awe_peek_dw(unsigned short cmd, unsigned short port);
326 static void awe_wait(unsigned short delay);
327
328 /* initialize emu8000 chip */
329 static void awe_initialize(void);
330
331 /* set voice parameters */
332 static void awe_init_misc_modes(int init_all);
333 static void awe_init_voice_info(awe_voice_info *vp);
334 static void awe_init_voice_parm(awe_voice_parm *pp);
335 #ifdef AWE_HAS_GUS_COMPATIBILITY
336 static int freq_to_note(int freq);
337 static int calc_rate_offset(int Hz);
338 /*static int calc_parm_delay(int msec);*/
339 static int calc_parm_hold(int msec);
340 static int calc_parm_attack(int msec);
341 static int calc_parm_decay(int msec);
342 static int calc_parm_search(int msec, short *table);
343 #endif
344
345 /* turn on/off note */
346 static void awe_note_on(int voice);
347 static void awe_note_off(int voice);
348 static void awe_terminate(int voice);
349 static void awe_exclusive_off(int voice);
350 static void awe_note_off_all(int do_sustain);
351
352 /* calculate voice parameters */
353 typedef void (*fx_affect_func)(int voice, int forced);
354 static void awe_set_pitch(int voice, int forced);
355 static void awe_set_voice_pitch(int voice, int forced);
356 static void awe_set_volume(int voice, int forced);
357 static void awe_set_voice_vol(int voice, int forced);
358 static void awe_set_pan(int voice, int forced);
359 static void awe_fx_fmmod(int voice, int forced);
360 static void awe_fx_tremfrq(int voice, int forced);
361 static void awe_fx_fm2frq2(int voice, int forced);
362 static void awe_fx_filterQ(int voice, int forced);
363 static void awe_calc_pitch(int voice);
364 #ifdef AWE_HAS_GUS_COMPATIBILITY
365 static void awe_calc_pitch_from_freq(int voice, int freq);
366 #endif
367 static void awe_calc_volume(int voice);
368 static void awe_voice_init(int voice, int init_all);
369 static void awe_channel_init(int ch, int init_all);
370 static void awe_fx_init(int ch);
371
372 /* sequencer interface */
373 static int awe_open(int dev, int mode);
374 static void awe_close(int dev);
375 static int awe_ioctl(int dev, unsigned int cmd, caddr_t arg);
376 static int awe_kill_note(int dev, int voice, int note, int velocity);
377 static int awe_start_note(int dev, int v, int note_num, int volume);
378 static int awe_set_instr(int dev, int voice, int instr_no);
379 static int awe_set_instr_2(int dev, int voice, int instr_no);
380 static void awe_reset(int dev);
381 static void awe_hw_control(int dev, unsigned char *event);
382 static int awe_load_patch(int dev, int format, const char *addr,
383                           int offs, int count, int pmgr_flag);
384 static void awe_aftertouch(int dev, int voice, int pressure);
385 static void awe_controller(int dev, int voice, int ctrl_num, int value);
386 static void awe_panning(int dev, int voice, int value);
387 static void awe_volume_method(int dev, int mode);
388 #ifndef AWE_NO_PATCHMGR
389 static int awe_patchmgr(int dev, struct patmgr_info *rec);
390 #endif
391 static void awe_bender(int dev, int voice, int value);
392 static int awe_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc);
393 static void awe_setup_voice(int dev, int voice, int chn);
394
395 /* hardware controls */
396 #ifdef AWE_HAS_GUS_COMPATIBILITY
397 static void awe_hw_gus_control(int dev, int cmd, unsigned char *event);
398 #endif
399 static void awe_hw_awe_control(int dev, int cmd, unsigned char *event);
400 static void awe_voice_change(int voice, fx_affect_func func);
401 static void awe_sostenuto_on(int voice, int forced);
402 static void awe_sustain_off(int voice, int forced);
403 static void awe_terminate_and_init(int voice, int forced);
404
405 /* voice search */
406 static int awe_search_instr(int bank, int preset);
407 static int awe_search_multi_voices(int rec, int note, int velocity, awe_voice_info **vlist);
408 static void awe_alloc_multi_voices(int ch, int note, int velocity, int key);
409 static void awe_alloc_one_voice(int voice, int note, int velocity);
410 static int awe_clear_voice(void);
411
412 /* load / remove patches */
413 static int awe_open_patch(awe_patch_info *patch, const char *addr, int count);
414 static int awe_close_patch(awe_patch_info *patch, const char *addr, int count);
415 static int awe_unload_patch(awe_patch_info *patch, const char *addr, int count);
416 static int awe_load_info(awe_patch_info *patch, const char *addr, int count);
417 static int awe_load_data(awe_patch_info *patch, const char *addr, int count);
418 static int awe_replace_data(awe_patch_info *patch, const char *addr, int count);
419 static int awe_load_map(awe_patch_info *patch, const char *addr, int count);
420 #ifdef AWE_HAS_GUS_COMPATIBILITY
421 static int awe_load_guspatch(const char *addr, int offs, int size, int pmgr_flag);
422 #endif
423 static int check_patch_opened(int type, char *name);
424 static int awe_write_wave_data(const char *addr, int offset, awe_sample_info *sp, int channels);
425 static void add_sf_info(int rec);
426 static void add_sf_sample(int rec);
427 static void purge_old_list(int rec, int next);
428 static void add_info_list(int rec);
429 static void awe_remove_samples(int sf_id);
430 static void rebuild_preset_list(void);
431 static short awe_set_sample(awe_voice_info *vp);
432
433 /* lowlevel functions */
434 static void awe_init_audio(void);
435 static void awe_init_dma(void);
436 static void awe_init_array(void);
437 static void awe_send_array(unsigned short *data);
438 static void awe_tweak_voice(int voice);
439 static void awe_tweak(void);
440 static void awe_init_fm(void);
441 static int awe_open_dram_for_write(int offset, int channels);
442 static void awe_open_dram_for_check(void);
443 static void awe_close_dram(void);
444 static void awe_write_dram(unsigned short c);
445 static int awe_detect_base(int addr);
446 static int awe_detect(void);
447 static int awe_check_dram(void);
448 static int awe_load_chorus_fx(awe_patch_info *patch, const char *addr, int count);
449 static void awe_set_chorus_mode(int mode);
450 static int awe_load_reverb_fx(awe_patch_info *patch, const char *addr, int count);
451 static void awe_set_reverb_mode(int mode);
452 static void awe_equalizer(int bass, int treble);
453 #ifdef CONFIG_AWE32_MIXER
454 static int awe_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg);
455 #endif
456
457 /* define macros for compatibility */
458 #if defined(__DragonFly__) || defined(__FreeBSD__)
459 #  include "awe_compat.h"
460 #else
461 #  include "awe_compat.h"
462 #endif
463
464 /*----------------------------------------------------------------
465  * synth operation table
466  *----------------------------------------------------------------*/
467
468 static struct synth_operations awe_operations =
469 {
470 #ifdef AWE_OSS38
471         "EMU8K",
472 #endif
473         &awe_info,
474         0,
475         SYNTH_TYPE_SAMPLE,
476         SAMPLE_TYPE_AWE32,
477         awe_open,
478         awe_close,
479         awe_ioctl,
480         awe_kill_note,
481         awe_start_note,
482         awe_set_instr_2,
483         awe_reset,
484         awe_hw_control,
485         awe_load_patch,
486         awe_aftertouch,
487         awe_controller,
488         awe_panning,
489         awe_volume_method,
490 #ifndef AWE_NO_PATCHMGR
491         awe_patchmgr,
492 #endif
493         awe_bender,
494         awe_alloc,
495         awe_setup_voice
496 };
497
498 #ifdef CONFIG_AWE32_MIXER
499 static struct mixer_operations awe_mixer_operations = {
500 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
501         "AWE32",
502 #endif
503         "AWE32 Equalizer",
504         awe_mixer_ioctl,
505 };
506 #endif
507
508
509 /*================================================================
510  * attach / unload interface
511  *================================================================*/
512
513 #ifdef AWE_OBSOLETE_VOXWARE
514 #define ATTACH_DECL     static
515 #else
516 #define ATTACH_DECL     /**/
517 #endif
518
519 #if (defined(__DragonFly__) || defined(__FreeBSD__)) && !defined(AWE_OBSOLETE_VOXWARE)
520 #  define ATTACH_RET
521 void attach_awe(struct address_info *hw_config)
522 #else
523 #  define ATTACH_RET ret
524 ATTACH_DECL
525 int attach_awe(void)
526 #endif
527 {
528     int ret = 0;
529
530         /* check presence of AWE32 card */
531         if (! awe_detect()) {
532                 printk("AWE32: not detected\n");
533                 return ATTACH_RET;
534         }
535
536         /* check AWE32 ports are available */
537         if (awe_check_port()) {
538                 printk("AWE32: I/O area already used.\n");
539                 return ATTACH_RET;
540         }
541
542         /* set buffers to NULL */
543         voices = NULL;
544         channels = NULL;
545         sflists = NULL;
546         samples = NULL;
547         infos = NULL;
548
549         /* voice & channel info */
550         voices = (voice_info*)my_malloc(AWE_MAX_VOICES * sizeof(voice_info));
551         channels = (awe_chan_info*)my_malloc(AWE_MAX_CHANNELS * sizeof(awe_chan_info));
552
553         if (voices == NULL || channels == NULL) {
554                 my_free(voices);
555                 my_free(channels);
556                 printk("AWE32: can't allocate sample tables\n");
557                 return ATTACH_RET;
558         }
559
560         /* allocate sample tables */
561         INIT_TABLE(sflists, max_sfs, AWE_MAX_SF_LISTS, sf_list);
562         INIT_TABLE(samples, max_samples, AWE_MAX_SAMPLES, awe_sample_list);
563         INIT_TABLE(infos, max_infos, AWE_MAX_INFOS, awe_voice_list);
564
565         if (num_synths >= MAX_SYNTH_DEV)
566                 printk("AWE32 Error: too many synthesizers\n");
567         else {
568                 voice_alloc = &awe_operations.alloc;
569                 voice_alloc->max_voice = awe_max_voices;
570                 synth_devs[num_synths++] = &awe_operations;
571         }
572
573 #ifdef CONFIG_AWE32_MIXER
574         if (num_mixers < MAX_MIXER_DEV) {
575                 mixer_devs[num_mixers++] = &awe_mixer_operations;
576         }
577 #endif
578
579         /* reserve I/O ports for awedrv */
580         awe_request_region();
581
582         /* clear all samples */
583         awe_reset_samples();
584
585         /* intialize AWE32 hardware */
586         awe_initialize();
587
588         snprintf(awe_info.name, sizeof(awe_info.name), "AWE32-%s (RAM%dk)",
589                 AWEDRV_VERSION, awe_mem_size/1024);
590 #if defined(__DragonFly__) || defined(__FreeBSD__)
591         printk("awe0: <SoundBlaster EMU8000 MIDI (RAM%dk)>", awe_mem_size/1024);
592 #elif defined(AWE_DEBUG_ON)
593         printk("%s\n", awe_info.name);
594 #endif
595
596         /* set default values */
597         awe_init_misc_modes(TRUE);
598
599         /* set reverb & chorus modes */
600         awe_set_reverb_mode(reverb_mode);
601         awe_set_chorus_mode(chorus_mode);
602
603         awe_present = TRUE;
604
605     ret = 1;
606     return ATTACH_RET;
607 }
608
609
610 #ifdef AWE_DYNAMIC_BUFFER
611 static void free_tables(void)
612 {
613         my_free(sflists);
614         sflists = NULL; max_sfs = 0;
615         my_free(samples);
616         samples = NULL; max_samples = 0;
617         my_free(infos);
618         infos = NULL; max_infos = 0;
619 }
620 #else
621 #define free_buffers() /**/
622 #endif
623
624
625 #ifdef linux
626 ATTACH_DECL
627 void unload_awe(void)
628 {
629         if (awe_present) {
630                 awe_reset_samples();
631                 awe_release_region();
632                 my_free(voices);
633                 my_free(channels);
634                 free_tables();
635                 awe_present = FALSE;
636         }
637 }
638 #endif
639
640
641 /*----------------------------------------------------------------
642  * old type interface
643  *----------------------------------------------------------------*/
644
645 #ifdef AWE_OBSOLETE_VOXWARE
646
647 #if defined(__DragonFly__) || defined(__FreeBSD__)
648 long attach_awe_obsolete(long mem_start, struct address_info *hw_config)
649 #else
650 int attach_awe_obsolete(int mem_start, struct address_info *hw_config)
651 #endif
652 {
653         my_malloc_init(mem_start);
654         if (! attach_awe())
655                 return 0;
656         return my_malloc_memptr();
657 }
658
659 int probe_awe_obsolete(struct address_info *hw_config)
660 {
661         return 1;
662         /*return awe_detect();*/
663 }
664
665 #else
666 #if defined(__DragonFly__) || defined(__FreeBSD__ )
667 int probe_awe(struct address_info *hw_config)
668 {
669         return 1;
670 }
671 #endif
672 #endif /* AWE_OBSOLETE_VOXWARE */
673
674
675 /*================================================================
676  * clear sample tables 
677  *================================================================*/
678
679 static void
680 awe_reset_samples(void)
681 {
682         int i;
683
684         /* free all bank tables */
685         for (i = 0; i < AWE_MAX_PRESETS; i++)
686                 preset_table[i] = -1;
687
688         free_tables();
689
690         current_sf_id = 0;
691         locked_sf_id = 0;
692         patch_opened = 0;
693 }
694
695
696 /*================================================================
697  * EMU register access
698  *================================================================*/
699
700 /* select a given AWE32 pointer */
701 static int awe_cur_cmd = -1;
702 #define awe_set_cmd(cmd) \
703 if (awe_cur_cmd != cmd) { OUTW(cmd, awe_base + 0x802); awe_cur_cmd = cmd; }
704 #define awe_port(port)          (awe_base - 0x620 + port)
705
706 /* write 16bit data */
707 INLINE static void
708 awe_poke(unsigned short cmd, unsigned short port, unsigned short data)
709 {
710         awe_set_cmd(cmd);
711         OUTW(data, awe_port(port));
712 }
713
714 /* write 32bit data */
715 INLINE static void
716 awe_poke_dw(unsigned short cmd, unsigned short port, unsigned int data)
717 {
718         awe_set_cmd(cmd);
719         OUTW(data, awe_port(port));             /* write lower 16 bits */
720         OUTW(data >> 16, awe_port(port)+2);     /* write higher 16 bits */
721 }
722
723 /* read 16bit data */
724 INLINE static unsigned short
725 awe_peek(unsigned short cmd, unsigned short port)
726 {
727         unsigned short k;
728         awe_set_cmd(cmd);
729         k = inw(awe_port(port));
730         return k;
731 }
732
733 /* read 32bit data */
734 INLINE static unsigned int
735 awe_peek_dw(unsigned short cmd, unsigned short port)
736 {
737         unsigned int k1, k2;
738         awe_set_cmd(cmd);
739         k1 = inw(awe_port(port));
740         k2 = inw(awe_port(port)+2);
741         k1 |= k2 << 16;
742         return k1;
743 }
744
745 /* wait delay number of AWE32 44100Hz clocks */
746 static void
747 awe_wait(unsigned short delay)
748 {
749         unsigned short clock, target;
750         unsigned short port = awe_port(AWE_WC_Port);
751         int counter;
752   
753         /* sample counter */
754         awe_set_cmd(AWE_WC_Cmd);
755         clock = (unsigned short)inw(port);
756         target = clock + delay;
757         counter = 0;
758         if (target < clock) {
759                 for (; (unsigned short)inw(port) > target; counter++)
760                         if (counter > 65536)
761                                 break;
762         }
763         for (; (unsigned short)inw(port) < target; counter++)
764                 if (counter > 65536)
765                         break;
766 }
767
768 /* write a word data */
769 INLINE static void
770 awe_write_dram(unsigned short c)
771 {
772         awe_poke(AWE_SMLD, c);
773 }
774
775
776 #if defined(linux) && !defined(AWE_OBSOLETE_VOXWARE)
777
778 /*================================================================
779  * port check / request
780  *  0x620-622, 0xA20-A22, 0xE20-E22
781  *================================================================*/
782
783 static int
784 awe_check_port(void)
785 {
786         return (check_region(awe_port(Data0), 4) ||
787                 check_region(awe_port(Data1), 4) ||
788                 check_region(awe_port(Data3), 4));
789 }
790
791 static void
792 awe_request_region(void)
793 {
794         request_region(awe_port(Data0), 4, "sound driver (AWE32)");
795         request_region(awe_port(Data1), 4, "sound driver (AWE32)");
796         request_region(awe_port(Data3), 4, "sound driver (AWE32)");
797 }
798
799 static void
800 awe_release_region(void)
801 {
802         release_region(awe_port(Data0), 4);
803         release_region(awe_port(Data1), 4);
804         release_region(awe_port(Data3), 4);
805 }
806
807 #endif /* !AWE_OBSOLETE_VOXWARE */
808
809
810 /*================================================================
811  * AWE32 initialization
812  *================================================================*/
813 static void
814 awe_initialize(void)
815 {
816         AWE_DEBUG(0,printk("AWE32: initializing..\n"));
817
818         /* initialize hardware configuration */
819         awe_poke(AWE_HWCF1, 0x0059);
820         awe_poke(AWE_HWCF2, 0x0020);
821
822         /* disable audio; this seems to reduce a clicking noise a bit.. */
823         awe_poke(AWE_HWCF3, 0);
824
825         /* initialize audio channels */
826         awe_init_audio();
827
828         /* initialize DMA */
829         awe_init_dma();
830
831         /* initialize init array */
832         awe_init_array();
833
834         /* check DRAM memory size */
835         awe_mem_size = awe_check_dram();
836
837         /* initialize the FM section of the AWE32 */
838         awe_init_fm();
839
840         /* set up voice envelopes */
841         awe_tweak();
842
843         /* enable audio */
844         awe_poke(AWE_HWCF3, 0x0004);
845
846         /* set equalizer */
847         awe_equalizer(5, 9);
848 }
849
850
851 /*================================================================
852  * AWE32 voice parameters
853  *================================================================*/
854
855 /* initialize voice_info record */
856 static void
857 awe_init_voice_info(awe_voice_info *vp)
858 {
859         vp->sf_id = 0; /* normal mode */
860         vp->sample = 0;
861         vp->rate_offset = 0;
862
863         vp->start = 0;
864         vp->end = 0;
865         vp->loopstart = 0;
866         vp->loopend = 0;
867         vp->mode = 0;
868         vp->root = 60;
869         vp->tune = 0;
870         vp->low = 0;
871         vp->high = 127;
872         vp->vellow = 0;
873         vp->velhigh = 127;
874
875         vp->fixkey = -1;
876         vp->fixvel = -1;
877         vp->fixpan = -1;
878         vp->pan = -1;
879
880         vp->exclusiveClass = 0;
881         vp->amplitude = 127;
882         vp->attenuation = 0;
883         vp->scaleTuning = 100;
884
885         awe_init_voice_parm(&vp->parm);
886 }
887
888 /* initialize voice_parm record:
889  * Env1/2: delay=0, attack=0, hold=0, sustain=0, decay=0, release=0.
890  * Vibrato and Tremolo effects are zero.
891  * Cutoff is maximum.
892  * Chorus and Reverb effects are zero.
893  */
894 static void
895 awe_init_voice_parm(awe_voice_parm *pp)
896 {
897         pp->moddelay = 0x8000;
898         pp->modatkhld = 0x7f7f;
899         pp->moddcysus = 0x7f7f;
900         pp->modrelease = 0x807f;
901         pp->modkeyhold = 0;
902         pp->modkeydecay = 0;
903
904         pp->voldelay = 0x8000;
905         pp->volatkhld = 0x7f7f;
906         pp->voldcysus = 0x7f7f;
907         pp->volrelease = 0x807f;
908         pp->volkeyhold = 0;
909         pp->volkeydecay = 0;
910
911         pp->lfo1delay = 0x8000;
912         pp->lfo2delay = 0x8000;
913         pp->pefe = 0;
914
915         pp->fmmod = 0;
916         pp->tremfrq = 0;
917         pp->fm2frq2 = 0;
918
919         pp->cutoff = 0xff;
920         pp->filterQ = 0;
921
922         pp->chorus = 0;
923         pp->reverb = 0;
924 }       
925
926
927 #ifdef AWE_HAS_GUS_COMPATIBILITY
928
929 /* convert frequency mHz to abstract cents (= midi key * 100) */
930 static int
931 freq_to_note(int mHz)
932 {
933         /* abscents = log(mHz/8176) / log(2) * 1200 */
934         unsigned int max_val = (unsigned int)0xffffffff / 10000;
935         int i, times;
936         unsigned int base;
937         unsigned int freq;
938         int note, tune;
939
940         if (mHz == 0)
941                 return 0;
942         if (mHz < 0)
943                 return 12799; /* maximum */
944
945         freq = mHz;
946         note = 0;
947         for (base = 8176 * 2; freq >= base; base *= 2) {
948                 note += 12;
949                 if (note >= 128) /* over maximum */
950                         return 12799;
951         }
952         base /= 2;
953
954         /* to avoid overflow... */
955         times = 10000;
956         while (freq > max_val) {
957                 max_val *= 10;
958                 times /= 10;
959                 base /= 10;
960         }
961
962         freq = freq * times / base;
963         for (i = 0; i < 12; i++) {
964                 if (freq < semitone_tuning[i+1])
965                         break;
966                 note++;
967         }
968
969         tune = 0;
970         freq = freq * 10000 / semitone_tuning[i];
971         for (i = 0; i < 100; i++) {
972                 if (freq < cent_tuning[i+1])
973                         break;
974                 tune++;
975         }
976
977         return note * 100 + tune;
978 }
979
980
981 /* convert Hz to AWE32 rate offset:
982  * sample pitch offset for the specified sample rate
983  * rate=44100 is no offset, each 4096 is 1 octave (twice).
984  * eg, when rate is 22050, this offset becomes -4096.
985  */
986 static int
987 calc_rate_offset(int Hz)
988 {
989         /* offset = log(Hz / 44100) / log(2) * 4096 */
990         int freq, base, i;
991
992         /* maybe smaller than max (44100Hz) */
993         if (Hz <= 0 || Hz >= 44100) return 0;
994
995         base = 0;
996         for (freq = Hz * 2; freq < 44100; freq *= 2)
997                 base++;
998         base *= 1200;
999
1000         freq = 44100 * 10000 / (freq/2);
1001         for (i = 0; i < 12; i++) {
1002                 if (freq < semitone_tuning[i+1])
1003                         break;
1004                 base += 100;
1005         }
1006         freq = freq * 10000 / semitone_tuning[i];
1007         for (i = 0; i < 100; i++) {
1008                 if (freq < cent_tuning[i+1])
1009                         break;
1010                 base++;
1011         }
1012         return -base * 4096 / 1200;
1013 }
1014
1015
1016 /*----------------------------------------------------------------
1017  * convert envelope time parameter to AWE32 raw parameter
1018  *----------------------------------------------------------------*/
1019
1020 /* attack & decay/release time table (msec) */
1021 static short attack_time_tbl[128] = {
1022 32767, 11878, 5939, 3959, 2969, 2375, 1979, 1696, 1484, 1319, 1187, 1079, 989, 913, 848, 791, 742,
1023  698, 659, 625, 593, 565, 539, 516, 494, 475, 456, 439, 424, 409, 395, 383, 371,
1024  359, 344, 330, 316, 302, 290, 277, 266, 255, 244, 233, 224, 214, 205, 196, 188,
1025  180, 173, 165, 158, 152, 145, 139, 133, 127, 122, 117, 112, 107, 103, 98, 94,
1026  90, 86, 83, 79, 76, 73, 69, 67, 64, 61, 58, 56, 54, 51, 49, 47,
1027  45, 43, 41, 39, 38, 36, 35, 33, 32, 30, 29, 28, 27, 25, 24, 23,
1028  22, 21, 20, 20, 19, 18, 17, 16, 16, 15, 14, 14, 13, 13, 12, 11,
1029  11, 10, 10, 10, 9, 9, 8, 8, 8, 7, 7, 7, 6, 6, 0,
1030 };
1031
1032 static short decay_time_tbl[128] = {
1033 32767, 32766, 4589, 4400, 4219, 4045, 3879, 3719, 3566, 3419, 3279, 3144, 3014, 2890, 2771, 2657,
1034  2548, 2443, 2343, 2246, 2154, 2065, 1980, 1899, 1820, 1746, 1674, 1605, 1539, 1475, 1415, 1356,
1035  1301, 1247, 1196, 1146, 1099, 1054, 1011, 969, 929, 891, 854, 819, 785, 753, 722, 692,
1036  664, 636, 610, 585, 561, 538, 516, 494, 474, 455, 436, 418, 401, 384, 368, 353,
1037  339, 325, 311, 298, 286, 274, 263, 252, 242, 232, 222, 213, 204, 196, 188, 180,
1038  173, 166, 159, 152, 146, 140, 134, 129, 123, 118, 113, 109, 104, 100, 96, 92,
1039  88, 84, 81, 77, 74, 71, 68, 65, 63, 60, 58, 55, 53, 51, 49, 47,
1040  45, 43, 41, 39, 38, 36, 35, 33, 32, 30, 29, 28, 27, 26, 25, 24,
1041 };
1042
1043 /*
1044 static int
1045 calc_parm_delay(int msec)
1046 {
1047         return (0x8000 - msec * 1000 / 725);
1048 }
1049 */
1050
1051 /* delay time = 0x8000 - msec/92 */
1052 static int
1053 calc_parm_hold(int msec)
1054 {
1055         int val = (0x7f * 92 - msec) / 92;
1056         if (val < 1) val = 1;
1057         if (val > 127) val = 127;
1058         return val;
1059 }
1060
1061 /* attack time: search from time table */
1062 static int
1063 calc_parm_attack(int msec)
1064 {
1065         return calc_parm_search(msec, attack_time_tbl);
1066 }
1067
1068 /* decay/release time: search from time table */
1069 static int
1070 calc_parm_decay(int msec)
1071 {
1072         return calc_parm_search(msec, decay_time_tbl);
1073 }
1074
1075 /* search an index for specified time from given time table */
1076 static int
1077 calc_parm_search(int msec, short *table)
1078 {
1079         int left = 1, right = 127, mid;
1080         while (left < right) {
1081                 mid = (left + right) / 2;
1082                 if (msec < (int)table[mid])
1083                         left = mid + 1;
1084                 else
1085                         right = mid;
1086         }
1087         return left;
1088 }
1089 #endif /* AWE_HAS_GUS_COMPATIBILITY */
1090
1091
1092 /*================================================================
1093  * effects table
1094  *================================================================*/
1095
1096 /* set an effect value */
1097 #define FX_FLAG_OFF     0
1098 #define FX_FLAG_SET     1
1099 #define FX_FLAG_ADD     2
1100
1101 #define FX_SET(rec,type,value) \
1102         ((rec)->flags[type] = FX_FLAG_SET, (rec)->val[type] = (value))
1103 #define FX_ADD(rec,type,value) \
1104         ((rec)->flags[type] = FX_FLAG_ADD, (rec)->val[type] = (value))
1105 #define FX_UNSET(rec,type) \
1106         ((rec)->flags[type] = FX_FLAG_OFF, (rec)->val[type] = 0)
1107
1108 /* check the effect value is set */
1109 #define FX_ON(rec,type) ((rec)->flags[type])
1110
1111 #define PARM_BYTE       0
1112 #define PARM_WORD       1
1113
1114 static struct PARM_DEFS {
1115         int type;       /* byte or word */
1116         int low, high;  /* value range */
1117         fx_affect_func realtime;        /* realtime paramater change */
1118 } parm_defs[] = {
1119         {PARM_WORD, 0, 0x8000, NULL},   /* env1 delay */
1120         {PARM_BYTE, 1, 0x7f, NULL},     /* env1 attack */
1121         {PARM_BYTE, 0, 0x7e, NULL},     /* env1 hold */
1122         {PARM_BYTE, 1, 0x7f, NULL},     /* env1 decay */
1123         {PARM_BYTE, 1, 0x7f, NULL},     /* env1 release */
1124         {PARM_BYTE, 0, 0x7f, NULL},     /* env1 sustain */
1125         {PARM_BYTE, 0, 0xff, NULL},     /* env1 pitch */
1126         {PARM_BYTE, 0, 0xff, NULL},     /* env1 cutoff */
1127
1128         {PARM_WORD, 0, 0x8000, NULL},   /* env2 delay */
1129         {PARM_BYTE, 1, 0x7f, NULL},     /* env2 attack */
1130         {PARM_BYTE, 0, 0x7e, NULL},     /* env2 hold */
1131         {PARM_BYTE, 1, 0x7f, NULL},     /* env2 decay */
1132         {PARM_BYTE, 1, 0x7f, NULL},     /* env2 release */
1133         {PARM_BYTE, 0, 0x7f, NULL},     /* env2 sustain */
1134
1135         {PARM_WORD, 0, 0x8000, NULL},   /* lfo1 delay */
1136         {PARM_BYTE, 0, 0xff, awe_fx_tremfrq},   /* lfo1 freq */
1137         {PARM_BYTE, 0, 0x7f, awe_fx_tremfrq},   /* lfo1 volume (positive only)*/
1138         {PARM_BYTE, 0, 0x7f, awe_fx_fmmod},     /* lfo1 pitch (positive only)*/
1139         {PARM_BYTE, 0, 0xff, awe_fx_fmmod},     /* lfo1 cutoff (positive only)*/
1140
1141         {PARM_WORD, 0, 0x8000, NULL},   /* lfo2 delay */
1142         {PARM_BYTE, 0, 0xff, awe_fx_fm2frq2},   /* lfo2 freq */
1143         {PARM_BYTE, 0, 0x7f, awe_fx_fm2frq2},   /* lfo2 pitch (positive only)*/
1144
1145         {PARM_WORD, 0, 0xffff, awe_set_voice_pitch},    /* initial pitch */
1146         {PARM_BYTE, 0, 0xff, NULL},     /* chorus */
1147         {PARM_BYTE, 0, 0xff, NULL},     /* reverb */
1148         {PARM_BYTE, 0, 0xff, awe_set_volume},   /* initial cutoff */
1149         {PARM_BYTE, 0, 15, awe_fx_filterQ},     /* initial resonance */
1150
1151         {PARM_WORD, 0, 0xffff, NULL},   /* sample start */
1152         {PARM_WORD, 0, 0xffff, NULL},   /* loop start */
1153         {PARM_WORD, 0, 0xffff, NULL},   /* loop end */
1154         {PARM_WORD, 0, 0xffff, NULL},   /* coarse sample start */
1155         {PARM_WORD, 0, 0xffff, NULL},   /* coarse loop start */
1156         {PARM_WORD, 0, 0xffff, NULL},   /* coarse loop end */
1157         {PARM_BYTE, 0, 0xff, awe_set_volume},   /* initial attenuation */
1158 };
1159
1160
1161 static unsigned char
1162 FX_BYTE(FX_Rec *rec, FX_Rec *lay, int type, unsigned char value)
1163 {
1164         int effect = 0;
1165         int on = 0;
1166         if (lay && (on = FX_ON(lay, type)) != 0)
1167                 effect = lay->val[type];
1168         if (!on && (on = FX_ON(rec, type)) != 0)
1169                 effect = rec->val[type];
1170         if (on == FX_FLAG_ADD)
1171                 effect += (int)value;
1172         if (on) {
1173                 if (effect < parm_defs[type].low)
1174                         effect = parm_defs[type].low;
1175                 else if (effect > parm_defs[type].high)
1176                         effect = parm_defs[type].high;
1177                 return (unsigned char)effect;
1178         }
1179         return value;
1180 }
1181
1182 /* get word effect value */
1183 static unsigned short
1184 FX_WORD(FX_Rec *rec, FX_Rec *lay, int type, unsigned short value)
1185 {
1186         int effect = 0;
1187         int on = 0;
1188         if (lay && (on = FX_ON(lay, type)) != 0)
1189                 effect = lay->val[type];
1190         if (!on && (on = FX_ON(rec, type)) != 0)
1191                 effect = rec->val[type];
1192         if (on == FX_FLAG_ADD)
1193                 effect += (int)value;
1194         if (on) {
1195                 if (effect < parm_defs[type].low)
1196                         effect = parm_defs[type].low;
1197                 else if (effect > parm_defs[type].high)
1198                         effect = parm_defs[type].high;
1199                 return (unsigned short)effect;
1200         }
1201         return value;
1202 }
1203
1204 /* get word (upper=type1/lower=type2) effect value */
1205 static unsigned short
1206 FX_COMB(FX_Rec *rec, FX_Rec *lay, int type1, int type2, unsigned short value)
1207 {
1208         unsigned short tmp;
1209         tmp = FX_BYTE(rec, lay, type1, (unsigned char)(value >> 8));
1210         tmp <<= 8;
1211         tmp |= FX_BYTE(rec, lay, type2, (unsigned char)(value & 0xff));
1212         return tmp;
1213 }
1214
1215 /* address offset */
1216 static int
1217 FX_OFFSET(FX_Rec *rec, FX_Rec *lay, int lo, int hi, int mode)
1218 {
1219         int addr = 0;
1220         if (lay && FX_ON(lay, hi))
1221                 addr = (short)lay->val[hi];
1222         else if (FX_ON(rec, hi))
1223                 addr = (short)rec->val[hi];
1224         addr = addr << 15;
1225         if (lay && FX_ON(lay, lo))
1226                 addr += (short)lay->val[lo];
1227         else if (FX_ON(rec, lo))
1228                 addr += (short)rec->val[lo];
1229         if (!(mode & AWE_SAMPLE_8BITS))
1230                 addr /= 2;
1231         return addr;
1232 }
1233
1234
1235 /*================================================================
1236  * turn on/off sample
1237  *================================================================*/
1238
1239 static void
1240 awe_note_on(int voice)
1241 {
1242         unsigned int temp;
1243         int addr;
1244         awe_voice_info *vp;
1245         FX_Rec *fx = &voices[voice].cinfo->fx;
1246         FX_Rec *fx_lay = NULL;
1247         if (voices[voice].layer < MAX_LAYERS)
1248                 fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1249
1250         /* A voice sample must assigned before calling */
1251         if ((vp = voices[voice].sample) == NULL || vp->index < 0)
1252                 return;
1253
1254         /* channel to be silent and idle */
1255         awe_poke(AWE_DCYSUSV(voice), 0x0080);
1256         awe_poke(AWE_VTFT(voice), 0);
1257         awe_poke(AWE_CVCF(voice), 0);
1258         awe_poke(AWE_PTRX(voice), 0);
1259         awe_poke(AWE_CPF(voice), 0);
1260
1261         /* modulation & volume envelope */
1262         awe_poke(AWE_ENVVAL(voice),
1263                  FX_WORD(fx, fx_lay, AWE_FX_ENV1_DELAY, vp->parm.moddelay));
1264         awe_poke(AWE_ATKHLD(voice),
1265                  FX_COMB(fx, fx_lay, AWE_FX_ENV1_HOLD, AWE_FX_ENV1_ATTACK,
1266                          vp->parm.modatkhld));
1267         awe_poke(AWE_DCYSUS(voice),
1268                  FX_COMB(fx, fx_lay, AWE_FX_ENV1_SUSTAIN, AWE_FX_ENV1_DECAY,
1269                           vp->parm.moddcysus));
1270         awe_poke(AWE_ENVVOL(voice),
1271                  FX_WORD(fx, fx_lay, AWE_FX_ENV2_DELAY, vp->parm.voldelay));
1272         awe_poke(AWE_ATKHLDV(voice),
1273                  FX_COMB(fx, fx_lay, AWE_FX_ENV2_HOLD, AWE_FX_ENV2_ATTACK,
1274                          vp->parm.volatkhld));
1275         /* decay/sustain parameter for volume envelope must be set at last */
1276
1277         /* pitch offset */
1278         awe_set_pitch(voice, TRUE);
1279
1280         /* cutoff and volume */
1281         awe_set_volume(voice, TRUE);
1282
1283         /* modulation envelope heights */
1284         awe_poke(AWE_PEFE(voice),
1285                  FX_COMB(fx, fx_lay, AWE_FX_ENV1_PITCH, AWE_FX_ENV1_CUTOFF,
1286                          vp->parm.pefe));
1287
1288         /* lfo1/2 delay */
1289         awe_poke(AWE_LFO1VAL(voice),
1290                  FX_WORD(fx, fx_lay, AWE_FX_LFO1_DELAY, vp->parm.lfo1delay));
1291         awe_poke(AWE_LFO2VAL(voice),
1292                  FX_WORD(fx, fx_lay, AWE_FX_LFO2_DELAY, vp->parm.lfo2delay));
1293
1294         /* lfo1 pitch & cutoff shift */
1295         awe_fx_fmmod(voice, TRUE);
1296         /* lfo1 volume & freq */
1297         awe_fx_tremfrq(voice, TRUE);
1298         /* lfo2 pitch & freq */
1299         awe_fx_fm2frq2(voice, TRUE);
1300         /* pan & loop start */
1301         awe_set_pan(voice, TRUE);
1302
1303         /* chorus & loop end (chorus 8bit, MSB) */
1304         addr = vp->loopend - 1;
1305         addr += FX_OFFSET(fx, fx_lay, AWE_FX_LOOP_END,
1306                           AWE_FX_COARSE_LOOP_END, vp->mode);
1307         temp = FX_BYTE(fx, fx_lay, AWE_FX_CHORUS, vp->parm.chorus);
1308         temp = (temp <<24) | (unsigned int)addr;
1309         awe_poke_dw(AWE_CSL(voice), temp);
1310         AWE_DEBUG(4,printk("AWE32: [-- loopend=%x/%x]\n", vp->loopend, addr));
1311
1312         /* Q & current address (Q 4bit value, MSB) */
1313         addr = vp->start - 1;
1314         addr += FX_OFFSET(fx, fx_lay, AWE_FX_SAMPLE_START,
1315                           AWE_FX_COARSE_SAMPLE_START, vp->mode);
1316         temp = FX_BYTE(fx, fx_lay, AWE_FX_FILTERQ, vp->parm.filterQ);
1317         temp = (temp<<28) | (unsigned int)addr;
1318         awe_poke_dw(AWE_CCCA(voice), temp);
1319         AWE_DEBUG(4,printk("AWE32: [-- startaddr=%x/%x]\n", vp->start, addr));
1320
1321         /* reset volume */
1322         awe_poke_dw(AWE_VTFT(voice), 0x0000FFFF);
1323         awe_poke_dw(AWE_CVCF(voice), 0x0000FFFF);
1324
1325         /* turn on envelope */
1326         awe_poke(AWE_DCYSUSV(voice),
1327                  FX_COMB(fx, fx_lay, AWE_FX_ENV2_SUSTAIN, AWE_FX_ENV2_DECAY,
1328                           vp->parm.voldcysus));
1329         /* set reverb */
1330         temp = FX_BYTE(fx, fx_lay, AWE_FX_REVERB, vp->parm.reverb);
1331         temp = (awe_peek_dw(AWE_PTRX(voice)) & 0xffff0000) | (temp<<8);
1332         awe_poke_dw(AWE_PTRX(voice), temp);
1333         awe_poke_dw(AWE_CPF(voice), 0x40000000);
1334
1335         voices[voice].state = AWE_ST_ON;
1336
1337         /* clear voice position for the next note on this channel */
1338         if (SINGLE_LAYER_MODE()) {
1339                 FX_UNSET(fx, AWE_FX_SAMPLE_START);
1340                 FX_UNSET(fx, AWE_FX_COARSE_SAMPLE_START);
1341         }
1342 }
1343
1344
1345 /* turn off the voice */
1346 static void
1347 awe_note_off(int voice)
1348 {
1349         awe_voice_info *vp;
1350         unsigned short tmp;
1351         FX_Rec *fx = &voices[voice].cinfo->fx;
1352         FX_Rec *fx_lay = NULL;
1353         if (voices[voice].layer < MAX_LAYERS)
1354                 fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1355
1356         if ((vp = voices[voice].sample) == NULL) {
1357                 voices[voice].state = AWE_ST_OFF;
1358                 return;
1359         }
1360
1361         tmp = 0x8000 | FX_BYTE(fx, fx_lay, AWE_FX_ENV1_RELEASE,
1362                                (unsigned char)vp->parm.modrelease);
1363         awe_poke(AWE_DCYSUS(voice), tmp);
1364         tmp = 0x8000 | FX_BYTE(fx, fx_lay, AWE_FX_ENV2_RELEASE,
1365                                (unsigned char)vp->parm.volrelease);
1366         awe_poke(AWE_DCYSUSV(voice), tmp);
1367         voices[voice].state = AWE_ST_RELEASED;
1368 }
1369
1370 /* force to terminate the voice (no releasing echo) */
1371 static void
1372 awe_terminate(int voice)
1373 {
1374         awe_poke(AWE_DCYSUSV(voice), 0x807F);
1375         awe_tweak_voice(voice);
1376         voices[voice].state = AWE_ST_OFF;
1377 }
1378
1379 /* turn off other voices with the same exclusive class (for drums) */
1380 static void
1381 awe_exclusive_off(int voice)
1382 {
1383         int i, exclass;
1384
1385         if (voices[voice].sample == NULL)
1386                 return;
1387         if ((exclass = voices[voice].sample->exclusiveClass) == 0)
1388                 return; /* not exclusive */
1389
1390         /* turn off voices with the same class */
1391         for (i = 0; i < awe_max_voices; i++) {
1392                 if (i != voice && IS_PLAYING(i) &&
1393                     voices[i].sample && voices[i].ch == voices[voice].ch &&
1394                     voices[i].sample->exclusiveClass == exclass) {
1395                         AWE_DEBUG(4,printk("AWE32: [exoff(%d)]\n", i));
1396                         awe_terminate(i);
1397                         awe_voice_init(i, TRUE);
1398                 }
1399         }
1400 }
1401
1402
1403 /*================================================================
1404  * change the parameters of an audible voice
1405  *================================================================*/
1406
1407 /* change pitch */
1408 static void
1409 awe_set_pitch(int voice, int forced)
1410 {
1411         if (IS_NO_EFFECT(voice) && !forced) return;
1412         awe_poke(AWE_IP(voice), voices[voice].apitch);
1413         AWE_DEBUG(3,printk("AWE32: [-- pitch=%x]\n", voices[voice].apitch));
1414 }
1415
1416 /* calculate & change pitch */
1417 static void
1418 awe_set_voice_pitch(int voice, int forced)
1419 {
1420         awe_calc_pitch(voice);
1421         awe_set_pitch(voice, forced);
1422 }
1423
1424 /* change volume & cutoff */
1425 static void
1426 awe_set_volume(int voice, int forced)
1427 {
1428         awe_voice_info *vp;
1429         unsigned short tmp2;
1430         FX_Rec *fx = &voices[voice].cinfo->fx;
1431         FX_Rec *fx_lay = NULL;
1432         if (voices[voice].layer < MAX_LAYERS)
1433                 fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1434
1435         if (!IS_PLAYING(voice) && !forced) return;
1436         if ((vp = voices[voice].sample) == NULL || vp->index < 0)
1437                 return;
1438
1439         tmp2 = FX_BYTE(fx, fx_lay, AWE_FX_CUTOFF, vp->parm.cutoff);
1440         tmp2 = (tmp2 << 8);
1441         tmp2 |= FX_BYTE(fx, fx_lay, AWE_FX_ATTEN,
1442                         (unsigned char)voices[voice].avol);
1443         awe_poke(AWE_IFATN(voice), tmp2);
1444 }
1445
1446 /* calculate & change volume */
1447 static void
1448 awe_set_voice_vol(int voice, int forced)
1449 {
1450         if (IS_EMPTY(voice))
1451                 return;
1452         awe_calc_volume(voice);
1453         awe_set_volume(voice, forced);
1454 }
1455
1456
1457 /* change pan; this could make a click noise.. */
1458 static void
1459 awe_set_pan(int voice, int forced)
1460 {
1461         unsigned int temp;
1462         int addr;
1463         awe_voice_info *vp;
1464         FX_Rec *fx = &voices[voice].cinfo->fx;
1465         FX_Rec *fx_lay = NULL;
1466         if (voices[voice].layer < MAX_LAYERS)
1467                 fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1468
1469         if (IS_NO_EFFECT(voice) && !forced) return;
1470         if ((vp = voices[voice].sample) == NULL || vp->index < 0)
1471                 return;
1472
1473         /* pan & loop start (pan 8bit, MSB, 0:right, 0xff:left) */
1474         if (vp->fixpan > 0)     /* 0-127 */
1475                 temp = 255 - (int)vp->fixpan * 2;
1476         else {
1477                 int pos = 0;
1478                 if (vp->pan >= 0) /* 0-127 */
1479                         pos = (int)vp->pan * 2 - 128;
1480                 pos += voices[voice].cinfo->panning; /* -128 - 127 */
1481                 pos = 127 - pos;
1482                 if (pos < 0)
1483                         temp = 0;
1484                 else if (pos > 255)
1485                         temp = 255;
1486                 else
1487                         temp = pos;
1488         }
1489         if (forced || temp != voices[voice].apan) {
1490                 addr = vp->loopstart - 1;
1491                 addr += FX_OFFSET(fx, fx_lay, AWE_FX_LOOP_START,
1492                                   AWE_FX_COARSE_LOOP_START, vp->mode);
1493                 temp = (temp<<24) | (unsigned int)addr;
1494                 awe_poke_dw(AWE_PSST(voice), temp);
1495                 voices[voice].apan = temp;
1496                 AWE_DEBUG(4,printk("AWE32: [-- loopstart=%x/%x]\n", vp->loopstart, addr));
1497         }
1498 }
1499
1500 /* effects change during playing */
1501 static void
1502 awe_fx_fmmod(int voice, int forced)
1503 {
1504         awe_voice_info *vp;
1505         FX_Rec *fx = &voices[voice].cinfo->fx;
1506         FX_Rec *fx_lay = NULL;
1507         if (voices[voice].layer < MAX_LAYERS)
1508                 fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1509
1510         if (IS_NO_EFFECT(voice) && !forced) return;
1511         if ((vp = voices[voice].sample) == NULL || vp->index < 0)
1512                 return;
1513         awe_poke(AWE_FMMOD(voice),
1514                  FX_COMB(fx, fx_lay, AWE_FX_LFO1_PITCH, AWE_FX_LFO1_CUTOFF,
1515                          vp->parm.fmmod));
1516 }
1517
1518 /* set tremolo (lfo1) volume & frequency */
1519 static void
1520 awe_fx_tremfrq(int voice, int forced)
1521 {
1522         awe_voice_info *vp;
1523         FX_Rec *fx = &voices[voice].cinfo->fx;
1524         FX_Rec *fx_lay = NULL;
1525         if (voices[voice].layer < MAX_LAYERS)
1526                 fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1527
1528         if (IS_NO_EFFECT(voice) && !forced) return;
1529         if ((vp = voices[voice].sample) == NULL || vp->index < 0)
1530                 return;
1531         awe_poke(AWE_TREMFRQ(voice),
1532                  FX_COMB(fx, fx_lay, AWE_FX_LFO1_VOLUME, AWE_FX_LFO1_FREQ,
1533                          vp->parm.tremfrq));
1534 }
1535
1536 /* set lfo2 pitch & frequency */
1537 static void
1538 awe_fx_fm2frq2(int voice, int forced)
1539 {
1540         awe_voice_info *vp;
1541         FX_Rec *fx = &voices[voice].cinfo->fx;
1542         FX_Rec *fx_lay = NULL;
1543         if (voices[voice].layer < MAX_LAYERS)
1544                 fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1545
1546         if (IS_NO_EFFECT(voice) && !forced) return;
1547         if ((vp = voices[voice].sample) == NULL || vp->index < 0)
1548                 return;
1549         awe_poke(AWE_FM2FRQ2(voice),
1550                  FX_COMB(fx, fx_lay, AWE_FX_LFO2_PITCH, AWE_FX_LFO2_FREQ,
1551                          vp->parm.fm2frq2));
1552 }
1553
1554
1555 /* Q & current address (Q 4bit value, MSB) */
1556 static void
1557 awe_fx_filterQ(int voice, int forced)
1558 {
1559         unsigned int addr;
1560         awe_voice_info *vp;
1561         FX_Rec *fx = &voices[voice].cinfo->fx;
1562         FX_Rec *fx_lay = NULL;
1563         if (voices[voice].layer < MAX_LAYERS)
1564                 fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1565
1566         if (IS_NO_EFFECT(voice) && !forced) return;
1567         if ((vp = voices[voice].sample) == NULL || vp->index < 0)
1568                 return;
1569
1570         addr = awe_peek_dw(AWE_CCCA(voice)) & 0xffffff;
1571         addr |= (FX_BYTE(fx, fx_lay, AWE_FX_FILTERQ, vp->parm.filterQ) << 28);
1572         awe_poke_dw(AWE_CCCA(voice), addr);
1573 }
1574
1575 /*================================================================
1576  * calculate pitch offset
1577  *----------------------------------------------------------------
1578  * 0xE000 is no pitch offset at 44100Hz sample.
1579  * Every 4096 is one octave.
1580  *================================================================*/
1581
1582 static void
1583 awe_calc_pitch(int voice)
1584 {
1585         voice_info *vp = &voices[voice];
1586         awe_voice_info *ap;
1587         awe_chan_info *cp = voices[voice].cinfo;
1588         int offset;
1589
1590         /* search voice information */
1591         if ((ap = vp->sample) == NULL)
1592                         return;
1593         if (ap->index < 0) {
1594                 AWE_DEBUG(3,printk("AWE32: set sample (%d)\n", ap->sample));
1595                 if (awe_set_sample(ap) < 0)
1596                         return;
1597         }
1598
1599         /* calculate offset */
1600         if (ap->fixkey >= 0) {
1601                 AWE_DEBUG(3,printk("AWE32: p-> fixkey(%d) tune(%d)\n", ap->fixkey, ap->tune));
1602                 offset = (ap->fixkey - ap->root) * 4096 / 12;
1603         } else {
1604                 AWE_DEBUG(3,printk("AWE32: p(%d)-> root(%d) tune(%d)\n", vp->note, ap->root, ap->tune));
1605                 offset = (vp->note - ap->root) * 4096 / 12;
1606                 AWE_DEBUG(4,printk("AWE32: p-> ofs=%d\n", offset));
1607         }
1608         offset = (offset * ap->scaleTuning) / 100;
1609         AWE_DEBUG(4,printk("AWE32: p-> scale* ofs=%d\n", offset));
1610         offset += ap->tune * 4096 / 1200;
1611         AWE_DEBUG(4,printk("AWE32: p-> tune+ ofs=%d\n", offset));
1612         if (cp->bender != 0) {
1613                 AWE_DEBUG(3,printk("AWE32: p-> bend(%d) %d\n", voice, cp->bender));
1614                 /* (819200: 1 semitone) ==> (4096: 12 semitones) */
1615                 offset += cp->bender * cp->bender_range / 2400;
1616         }
1617
1618         /* add initial pitch correction */
1619         if (FX_ON(&cp->fx_layer[vp->layer], AWE_FX_INIT_PITCH))
1620                 offset += cp->fx_layer[vp->layer].val[AWE_FX_INIT_PITCH];
1621         else if (FX_ON(&cp->fx, AWE_FX_INIT_PITCH))
1622                 offset += cp->fx.val[AWE_FX_INIT_PITCH];
1623
1624         /* 0xe000: root pitch */
1625         vp->apitch = 0xe000 + ap->rate_offset + offset;
1626         AWE_DEBUG(4,printk("AWE32: p-> sum aofs=%x, rate_ofs=%d\n", vp->apitch, ap->rate_offset));
1627         if (vp->apitch > 0xffff)
1628                 vp->apitch = 0xffff;
1629         if (vp->apitch < 0)
1630                 vp->apitch = 0;
1631 }
1632
1633
1634 #ifdef AWE_HAS_GUS_COMPATIBILITY
1635 /* calculate MIDI key and semitone from the specified frequency */
1636 static void
1637 awe_calc_pitch_from_freq(int voice, int freq)
1638 {
1639         voice_info *vp = &voices[voice];
1640         awe_voice_info *ap;
1641         FX_Rec *fx = &voices[voice].cinfo->fx;
1642         FX_Rec *fx_lay = NULL;
1643         int offset;
1644         int note;
1645
1646         if (voices[voice].layer < MAX_LAYERS)
1647                 fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1648
1649         /* search voice information */
1650         if ((ap = vp->sample) == NULL)
1651                 return;
1652         if (ap->index < 0) {
1653                 AWE_DEBUG(3,printk("AWE32: set sample (%d)\n", ap->sample));
1654                 if (awe_set_sample(ap) < 0)
1655                         return;
1656         }
1657         note = freq_to_note(freq);
1658         offset = (note - ap->root * 100 + ap->tune) * 4096 / 1200;
1659         offset = (offset * ap->scaleTuning) / 100;
1660         if (fx_lay && FX_ON(fx_lay, AWE_FX_INIT_PITCH))
1661                 offset += fx_lay->val[AWE_FX_INIT_PITCH];
1662         else if (FX_ON(fx, AWE_FX_INIT_PITCH))
1663                 offset += fx->val[AWE_FX_INIT_PITCH];
1664         vp->apitch = 0xe000 + ap->rate_offset + offset;
1665         if (vp->apitch > 0xffff)
1666                 vp->apitch = 0xffff;
1667         if (vp->apitch < 0)
1668                 vp->apitch = 0;
1669 }
1670 #endif /* AWE_HAS_GUS_COMPATIBILITY */
1671
1672
1673 /*================================================================
1674  * calculate volume attenuation
1675  *----------------------------------------------------------------
1676  * Voice volume is controlled by volume attenuation parameter.
1677  * So volume becomes maximum when avol is 0 (no attenuation), and
1678  * minimum when 255 (-96dB or silence).
1679  *================================================================*/
1680
1681 static int vol_table[128] = {
1682         255,111,95,86,79,74,70,66,63,61,58,56,54,52,50,49,
1683         47,46,45,43,42,41,40,39,38,37,36,35,34,34,33,32,
1684         31,31,30,29,29,28,27,27,26,26,25,24,24,23,23,22,
1685         22,21,21,21,20,20,19,19,18,18,18,17,17,16,16,16,
1686         15,15,15,14,14,14,13,13,13,12,12,12,11,11,11,10,
1687         10,10,10,9,9,9,8,8,8,8,7,7,7,7,6,6,
1688         6,6,5,5,5,5,5,4,4,4,4,3,3,3,3,3,
1689         2,2,2,2,2,1,1,1,1,1,0,0,0,0,0,0,
1690 };
1691
1692 static void
1693 awe_calc_volume(int voice)
1694 {
1695         voice_info *vp = &voices[voice];
1696         awe_voice_info *ap;
1697         awe_chan_info *cp = voices[voice].cinfo;
1698         int vol;
1699
1700         /* search voice information */
1701         if ((ap = vp->sample) == NULL)
1702                 return;
1703
1704         ap = vp->sample;
1705         if (ap->index < 0) {
1706                 AWE_DEBUG(3,printk("AWE32: set sample (%d)\n", ap->sample));
1707                 if (awe_set_sample(ap) < 0)
1708                         return;
1709         }
1710         
1711         /* 0 - 127 */
1712         vol = (vp->velocity * cp->main_vol * cp->expression_vol) / (127*127);
1713         vol = vol * ap->amplitude / 127;
1714
1715         if (vol < 0) vol = 0;
1716         if (vol > 127) vol = 127;
1717
1718         /* calc to attenuation */
1719         vol = vol_table[vol];
1720         vol = vol + (int)ap->attenuation + init_atten;
1721         if (vol > 255) vol = 255;
1722
1723         vp->avol = vol;
1724         AWE_DEBUG(3,printk("AWE32: [-- voice(%d) vol=%x]\n", voice, vol));
1725 }
1726
1727
1728 /* set sostenuto on */
1729 static void awe_sostenuto_on(int voice, int forced)
1730 {
1731         if (IS_NO_EFFECT(voice) && !forced) return;
1732         voices[voice].sostenuto = 127;
1733 }
1734
1735
1736 /* drop sustain */
1737 static void awe_sustain_off(int voice, int forced)
1738 {
1739         if (voices[voice].state == AWE_ST_SUSTAINED) {
1740                 awe_note_off(voice);
1741                 awe_fx_init(voices[voice].ch);
1742                 awe_voice_init(voice, FALSE);
1743         }
1744 }
1745
1746
1747 /* terminate and initialize voice */
1748 static void awe_terminate_and_init(int voice, int forced)
1749 {
1750         awe_terminate(voice);
1751         awe_fx_init(voices[voice].ch);
1752         awe_voice_init(voice, TRUE);
1753 }
1754
1755
1756 /*================================================================
1757  * synth operation routines
1758  *================================================================*/
1759
1760 #define AWE_VOICE_KEY(v)        (0x8000 | (v))
1761 #define AWE_CHAN_KEY(c,n)       (((c) << 8) | ((n) + 1))
1762 #define KEY_CHAN_MATCH(key,c)   (((key) >> 8) == (c))
1763
1764 /* initialize the voice */
1765 static void
1766 awe_voice_init(int voice, int init_all)
1767 {
1768         voice_info *vp = &voices[voice];
1769
1770         /* reset voice search key */
1771         if (playing_mode == AWE_PLAY_DIRECT)
1772                 vp->key = AWE_VOICE_KEY(voice);
1773         else
1774                 vp->key = 0;
1775
1776         /* clear voice mapping */
1777         voice_alloc->map[voice] = 0;
1778
1779         /* touch the timing flag */
1780         vp->time = current_alloc_time;
1781
1782         /* initialize other parameters if necessary */
1783         if (init_all) {
1784                 vp->note = -1;
1785                 vp->velocity = 0;
1786                 vp->sostenuto = 0;
1787
1788                 vp->sample = NULL;
1789                 vp->cinfo = &channels[voice];
1790                 vp->ch = voice;
1791                 vp->state = AWE_ST_OFF;
1792
1793                 /* emu8000 parameters */
1794                 vp->apitch = 0;
1795                 vp->avol = 255;
1796                 vp->apan = -1;
1797         }
1798 }
1799
1800 /* clear effects */
1801 static void awe_fx_init(int ch)
1802 {
1803         if (SINGLE_LAYER_MODE() && !misc_modes[AWE_MD_KEEP_EFFECT]) {
1804                 BZERO(&channels[ch].fx, sizeof(channels[ch].fx));
1805                 BZERO(&channels[ch].fx_layer, sizeof(&channels[ch].fx_layer));
1806         }
1807 }
1808
1809 /* initialize channel info */
1810 static void awe_channel_init(int ch, int init_all)
1811 {
1812         awe_chan_info *cp = &channels[ch];
1813         cp->channel = ch;
1814         if (init_all) {
1815                 cp->panning = 0; /* zero center */
1816                 cp->bender_range = 200; /* sense * 100 */
1817                 cp->main_vol = 127;
1818                 if (MULTI_LAYER_MODE() && IS_DRUM_CHANNEL(ch)) {
1819                         cp->instr = misc_modes[AWE_MD_DEF_DRUM];
1820                         cp->bank = AWE_DRUM_BANK;
1821                 } else {
1822                         cp->instr = misc_modes[AWE_MD_DEF_PRESET];
1823                         cp->bank = misc_modes[AWE_MD_DEF_BANK];
1824                 }
1825                 cp->vrec = -1;
1826                 cp->def_vrec = -1;
1827         }
1828
1829         cp->bender = 0; /* zero tune skew */
1830         cp->expression_vol = 127;
1831         cp->chan_press = 0;
1832         cp->sustained = 0;
1833
1834         if (! misc_modes[AWE_MD_KEEP_EFFECT]) {
1835                 BZERO(&cp->fx, sizeof(cp->fx));
1836                 BZERO(&cp->fx_layer, sizeof(cp->fx_layer));
1837         }
1838 }
1839
1840
1841 /* change the voice parameters; voice = channel */
1842 static void awe_voice_change(int voice, fx_affect_func func)
1843 {
1844         int i; 
1845         switch (playing_mode) {
1846         case AWE_PLAY_DIRECT:
1847                 func(voice, FALSE);
1848                 break;
1849         case AWE_PLAY_INDIRECT:
1850                 for (i = 0; i < awe_max_voices; i++)
1851                         if (voices[i].key == AWE_VOICE_KEY(voice))
1852                                 func(i, FALSE);
1853                 break;
1854         default:
1855                 for (i = 0; i < awe_max_voices; i++)
1856                         if (KEY_CHAN_MATCH(voices[i].key, voice))
1857                                 func(i, FALSE);
1858                 break;
1859         }
1860 }
1861
1862
1863 /*----------------------------------------------------------------
1864  * device open / close
1865  *----------------------------------------------------------------*/
1866
1867 /* open device:
1868  *   reset status of all voices, and clear sample position flag
1869  */
1870 static int
1871 awe_open(int dev, int mode)
1872 {
1873         if (awe_busy)
1874                 return RET_ERROR(EBUSY);
1875
1876         awe_busy = TRUE;
1877
1878         /* set default mode */
1879         awe_init_misc_modes(FALSE);
1880         init_atten = misc_modes[AWE_MD_ZERO_ATTEN];
1881         drum_flags = DEFAULT_DRUM_FLAGS;
1882         playing_mode = AWE_PLAY_INDIRECT;
1883
1884         /* reset voices & channels */
1885         awe_reset(dev);
1886
1887         patch_opened = 0;
1888
1889         return 0;
1890 }
1891
1892
1893 /* close device:
1894  *   reset all voices again (terminate sounds)
1895  */
1896 static void
1897 awe_close(int dev)
1898 {
1899         awe_reset(dev);
1900         awe_busy = FALSE;
1901 }
1902
1903
1904 /* set miscellaneous mode parameters
1905  */
1906 static void
1907 awe_init_misc_modes(int init_all)
1908 {
1909         int i;
1910         for (i = 0; i < AWE_MD_END; i++) {
1911                 if (init_all || misc_modes_default[i].init_each_time)
1912                         misc_modes[i] = misc_modes_default[i].value;
1913         }
1914 }
1915
1916
1917 /* sequencer I/O control:
1918  */
1919 static int
1920 awe_ioctl(int dev, unsigned int cmd, caddr_t arg)
1921 {
1922         switch (cmd) {
1923         case SNDCTL_SYNTH_INFO:
1924                 if (playing_mode == AWE_PLAY_DIRECT)
1925                         awe_info.nr_voices = awe_max_voices;
1926                 else
1927                         awe_info.nr_voices = AWE_MAX_CHANNELS;
1928                 IOCTL_TO_USER((char*)arg, 0, &awe_info, sizeof(awe_info));
1929                 return 0;
1930                 break;
1931
1932         case SNDCTL_SEQ_RESETSAMPLES:
1933                 awe_reset_samples();
1934                 awe_reset(dev);
1935                 return 0;
1936                 break;
1937
1938         case SNDCTL_SEQ_PERCMODE:
1939                 /* what's this? */
1940                 return 0;
1941                 break;
1942
1943         case SNDCTL_SYNTH_MEMAVL:
1944                 return awe_mem_size - awe_free_mem_ptr() * 2;
1945
1946         default:
1947                 printk("AWE32: unsupported ioctl %d\n", cmd);
1948                 return RET_ERROR(EINVAL);
1949         }
1950 }
1951
1952
1953 static int voice_in_range(int voice)
1954 {
1955         if (playing_mode == AWE_PLAY_DIRECT) {
1956                 if (voice < 0 || voice >= awe_max_voices)
1957                         return FALSE;
1958         } else {
1959                 if (voice < 0 || voice >= AWE_MAX_CHANNELS)
1960                         return FALSE;
1961         }
1962         return TRUE;
1963 }
1964
1965 static void release_voice(int voice, int do_sustain)
1966 {
1967         if (IS_NO_SOUND(voice))
1968                 return;
1969         if (do_sustain && (voices[voice].cinfo->sustained == 127 ||
1970                             voices[voice].sostenuto == 127))
1971                 voices[voice].state = AWE_ST_SUSTAINED;
1972         else {
1973                 awe_note_off(voice);
1974                 awe_fx_init(voices[voice].ch);
1975                 awe_voice_init(voice, FALSE);
1976         }
1977 }
1978
1979 /* release all notes */
1980 static void awe_note_off_all(int do_sustain)
1981 {
1982         int i;
1983         for (i = 0; i < awe_max_voices; i++)
1984                 release_voice(i, do_sustain);
1985 }
1986
1987 /* kill a voice:
1988  *   not terminate, just release the voice.
1989  */
1990 static int
1991 awe_kill_note(int dev, int voice, int note, int velocity)
1992 {
1993         int i, v2, key;
1994
1995         AWE_DEBUG(2,printk("AWE32: [off(%d) nt=%d vl=%d]\n", voice, note, velocity));
1996         if (! voice_in_range(voice))
1997                 return RET_ERROR(EINVAL);
1998
1999         switch (playing_mode) {
2000         case AWE_PLAY_DIRECT:
2001         case AWE_PLAY_INDIRECT:
2002                 key = AWE_VOICE_KEY(voice);
2003                 break;
2004
2005         case AWE_PLAY_MULTI2:
2006                 v2 = voice_alloc->map[voice] >> 8;
2007                 voice_alloc->map[voice] = 0;
2008                 voice = v2;
2009                 if (voice < 0 || voice >= AWE_MAX_CHANNELS)
2010                         return RET_ERROR(EINVAL);
2011                 /* continue to below */
2012         default:
2013                 key = AWE_CHAN_KEY(voice, note);
2014                 break;
2015         }
2016
2017         for (i = 0; i < awe_max_voices; i++) {
2018                 if (voices[i].key == key)
2019                         release_voice(i, TRUE);
2020         }
2021         return 0;
2022 }
2023
2024
2025 static void start_or_volume_change(int voice, int velocity)
2026 {
2027         voices[voice].velocity = velocity;
2028         awe_calc_volume(voice);
2029         if (voices[voice].state == AWE_ST_STANDBY)
2030                 awe_note_on(voice);
2031         else if (voices[voice].state == AWE_ST_ON)
2032                 awe_set_volume(voice, FALSE);
2033 }
2034
2035 static void set_and_start_voice(int voice, int state)
2036 {
2037         /* calculate pitch & volume parameters */
2038         voices[voice].state = state;
2039         awe_calc_pitch(voice);
2040         awe_calc_volume(voice);
2041         if (state == AWE_ST_ON)
2042                 awe_note_on(voice);
2043 }
2044
2045 /* start a voice:
2046  *   if note is 255, identical with aftertouch function.
2047  *   Otherwise, start a voice with specified not and volume.
2048  */
2049 static int
2050 awe_start_note(int dev, int voice, int note, int velocity)
2051 {
2052         int i, key, state, volonly;
2053
2054         AWE_DEBUG(2,printk("AWE32: [on(%d) nt=%d vl=%d]\n", voice, note, velocity));
2055         if (! voice_in_range(voice))
2056                 return RET_ERROR(EINVAL);
2057             
2058         if (velocity == 0)
2059                 state = AWE_ST_STANDBY; /* stand by for playing */
2060         else
2061                 state = AWE_ST_ON;      /* really play */
2062         volonly = FALSE;
2063
2064         switch (playing_mode) {
2065         case AWE_PLAY_DIRECT:
2066         case AWE_PLAY_INDIRECT:
2067                 key = AWE_VOICE_KEY(voice);
2068                 if (note == 255)
2069                         volonly = TRUE;
2070                 break;
2071
2072         case AWE_PLAY_MULTI2:
2073                 voice = voice_alloc->map[voice] >> 8;
2074                 if (voice < 0 || voice >= AWE_MAX_CHANNELS)
2075                         return RET_ERROR(EINVAL);
2076                 /* continue to below */
2077         default:
2078                 if (note >= 128) { /* key volume mode */
2079                         note -= 128;
2080                         volonly = TRUE;
2081                 }
2082                 key = AWE_CHAN_KEY(voice, note);
2083                 break;
2084         }
2085
2086         /* dynamic volume change */
2087         if (volonly) {
2088                 for (i = 0; i < awe_max_voices; i++) {
2089                         if (voices[i].key == key)
2090                                 start_or_volume_change(i, velocity);
2091                 }
2092                 return 0;
2093         }
2094
2095         /* if the same note still playing, stop it */
2096         for (i = 0; i < awe_max_voices; i++)
2097                 if (voices[i].key == key) {
2098                         if (voices[i].state == AWE_ST_ON) {
2099                                 awe_note_off(i);
2100                                 awe_voice_init(i, FALSE);
2101                         } else if (voices[i].state == AWE_ST_STANDBY)
2102                                 awe_voice_init(i, TRUE);
2103                 }
2104
2105         /* allocate voices */
2106         if (playing_mode == AWE_PLAY_DIRECT)
2107                 awe_alloc_one_voice(voice, note, velocity);
2108         else
2109                 awe_alloc_multi_voices(voice, note, velocity, key);
2110
2111         /* turn off other voices exlusively (for drums) */
2112         for (i = 0; i < awe_max_voices; i++)
2113                 if (voices[i].key == key)
2114                         awe_exclusive_off(i);
2115
2116         /* set up pitch and volume parameters */
2117         for (i = 0; i < awe_max_voices; i++) {
2118                 if (voices[i].key == key && voices[i].state == AWE_ST_OFF)
2119                         set_and_start_voice(i, state);
2120         }
2121
2122         return 0;
2123 }
2124
2125
2126 /* search instrument from preset table with the specified bank */
2127 static int
2128 awe_search_instr(int bank, int preset)
2129 {
2130         int i;
2131
2132         for (i = preset_table[preset]; i >= 0; i = infos[i].next_bank) {
2133                 if (infos[i].bank == bank)
2134                         return i;
2135         }
2136         return -1;
2137 }
2138
2139
2140 /* assign the instrument to a voice */
2141 static int
2142 awe_set_instr_2(int dev, int voice, int instr_no)
2143 {
2144         if (playing_mode == AWE_PLAY_MULTI2) {
2145                 voice = voice_alloc->map[voice] >> 8;
2146                 if (voice < 0 || voice >= AWE_MAX_CHANNELS)
2147                         return RET_ERROR(EINVAL);
2148         }
2149         return awe_set_instr(dev, voice, instr_no);
2150 }
2151
2152 /* assign the instrument to a channel; voice is the channel number */
2153 static int
2154 awe_set_instr(int dev, int voice, int instr_no)
2155 {
2156         awe_chan_info *cinfo;
2157         int def_bank;
2158
2159         if (! voice_in_range(voice))
2160                 return RET_ERROR(EINVAL);
2161
2162         if (instr_no < 0 || instr_no >= AWE_MAX_PRESETS)
2163                 return RET_ERROR(EINVAL);
2164
2165         cinfo = &channels[voice];
2166
2167         if (MULTI_LAYER_MODE() && IS_DRUM_CHANNEL(voice))
2168                 def_bank = AWE_DRUM_BANK; /* always search drumset */
2169         else
2170                 def_bank = cinfo->bank;
2171
2172         cinfo->vrec = -1;
2173         cinfo->def_vrec = -1;
2174         cinfo->vrec = awe_search_instr(def_bank, instr_no);
2175         if (def_bank == AWE_DRUM_BANK)  /* search default drumset */
2176                 cinfo->def_vrec = awe_search_instr(def_bank, misc_modes[AWE_MD_DEF_DRUM]);
2177         else    /* search default preset */
2178                 cinfo->def_vrec = awe_search_instr(misc_modes[AWE_MD_DEF_BANK], instr_no);
2179
2180         if (cinfo->vrec < 0 && cinfo->def_vrec < 0) {
2181                 AWE_DEBUG(1,printk("AWE32 Warning: can't find instrument %d\n", instr_no));
2182         }
2183
2184         cinfo->instr = instr_no;
2185
2186         return 0;
2187 }
2188
2189
2190 /* reset all voices; terminate sounds and initialize parameters */
2191 static void
2192 awe_reset(int dev)
2193 {
2194         int i;
2195         current_alloc_time = 0;
2196         /* don't turn off voice 31 and 32.  they are used also for FM voices */
2197         for (i = 0; i < awe_max_voices; i++) {
2198                 awe_terminate(i);
2199                 awe_voice_init(i, TRUE);
2200         }
2201         for (i = 0; i < AWE_MAX_CHANNELS; i++)
2202                 awe_channel_init(i, TRUE);
2203         for (i = 0; i < 16; i++) {
2204                 awe_operations.chn_info[i].controllers[CTL_MAIN_VOLUME] = 127;
2205                 awe_operations.chn_info[i].controllers[CTL_EXPRESSION] = 127;
2206         }
2207         awe_init_fm();
2208         awe_tweak();
2209 }
2210
2211
2212 /* hardware specific control:
2213  *   GUS specific and AWE32 specific controls are available.
2214  */
2215 static void
2216 awe_hw_control(int dev, unsigned char *event)
2217 {
2218         int cmd = event[2];
2219         if (cmd & _AWE_MODE_FLAG)
2220                 awe_hw_awe_control(dev, cmd & _AWE_MODE_VALUE_MASK, event);
2221 #ifdef AWE_HAS_GUS_COMPATIBILITY
2222         else
2223                 awe_hw_gus_control(dev, cmd & _AWE_MODE_VALUE_MASK, event);
2224 #endif
2225 }
2226
2227
2228 #ifdef AWE_HAS_GUS_COMPATIBILITY
2229
2230 /* GUS compatible controls */
2231 static void
2232 awe_hw_gus_control(int dev, int cmd, unsigned char *event)
2233 {
2234         int voice, i, key;
2235         unsigned short p1;
2236         short p2;
2237         int plong;
2238
2239         if (MULTI_LAYER_MODE())
2240                 return;
2241         if (cmd == _GUS_NUMVOICES)
2242                 return;
2243
2244         voice = event[3];
2245         if (! voice_in_range(voice))
2246                 return;
2247
2248         p1 = *(unsigned short *) &event[4];
2249         p2 = *(short *) &event[6];
2250         plong = *(int*) &event[4];
2251
2252         switch (cmd) {
2253         case _GUS_VOICESAMPLE:
2254                 awe_set_instr(dev, voice, p1);
2255                 return;
2256
2257         case _GUS_VOICEBALA:
2258                 /* 0 to 15 --> -128 to 127 */
2259                 awe_panning(dev, voice, ((int)p1 << 4) - 128);
2260                 return;
2261
2262         case _GUS_VOICEVOL:
2263         case _GUS_VOICEVOL2:
2264                 /* not supported yet */
2265                 return;
2266
2267         case _GUS_RAMPRANGE:
2268         case _GUS_RAMPRATE:
2269         case _GUS_RAMPMODE:
2270         case _GUS_RAMPON:
2271         case _GUS_RAMPOFF:
2272                 /* volume ramping not supported */
2273                 return;
2274
2275         case _GUS_VOLUME_SCALE:
2276                 return;
2277
2278         case _GUS_VOICE_POS:
2279                 FX_SET(&channels[voice].fx, AWE_FX_SAMPLE_START,
2280                        (short)(plong & 0x7fff));
2281                 FX_SET(&channels[voice].fx, AWE_FX_COARSE_SAMPLE_START,
2282                        (plong >> 15) & 0xffff);
2283                 return;
2284         }
2285
2286         key = AWE_VOICE_KEY(voice);
2287         for (i = 0; i < awe_max_voices; i++) {
2288                 if (voices[i].key == key) {
2289                         switch (cmd) {
2290                         case _GUS_VOICEON:
2291                                 awe_note_on(i);
2292                                 break;
2293
2294                         case _GUS_VOICEOFF:
2295                                 awe_terminate(i);
2296                                 awe_fx_init(voices[i].ch);
2297                                 awe_voice_init(i, TRUE);
2298                                 break;
2299
2300                         case _GUS_VOICEFADE:
2301                                 awe_note_off(i);
2302                                 awe_fx_init(voices[i].ch);
2303                                 awe_voice_init(i, FALSE);
2304                                 break;
2305
2306                         case _GUS_VOICEFREQ:
2307                                 awe_calc_pitch_from_freq(i, plong);
2308                                 break;
2309                         }
2310                 }
2311         }
2312 }
2313
2314 #endif
2315
2316
2317 /* AWE32 specific controls */
2318 static void
2319 awe_hw_awe_control(int dev, int cmd, unsigned char *event)
2320 {
2321         int voice;
2322         unsigned short p1;
2323         short p2;
2324         awe_chan_info *cinfo;
2325         FX_Rec *fx;
2326         int i;
2327
2328         voice = event[3];
2329         if (! voice_in_range(voice))
2330                 return;
2331
2332         if (playing_mode == AWE_PLAY_MULTI2) {
2333                 voice = voice_alloc->map[voice] >> 8;
2334                 if (voice < 0 || voice >= AWE_MAX_CHANNELS)
2335                         return;
2336         }
2337
2338         p1 = *(unsigned short *) &event[4];
2339         p2 = *(short *) &event[6];
2340         cinfo = &channels[voice];
2341
2342         switch (cmd) {
2343         case _AWE_DEBUG_MODE:
2344                 debug_mode = p1;
2345                 printk("AWE32: debug mode = %d\n", debug_mode);
2346                 break;
2347         case _AWE_REVERB_MODE:
2348                 awe_set_reverb_mode(p1);
2349                 break;
2350
2351         case _AWE_CHORUS_MODE:
2352                 awe_set_chorus_mode(p1);
2353                 break;
2354                       
2355         case _AWE_REMOVE_LAST_SAMPLES:
2356                 AWE_DEBUG(0,printk("AWE32: remove last samples\n"));
2357                 if (locked_sf_id > 0)
2358                         awe_remove_samples(locked_sf_id);
2359                 break;
2360
2361         case _AWE_INITIALIZE_CHIP:
2362                 awe_initialize();
2363                 break;
2364
2365         case _AWE_SEND_EFFECT:
2366                 fx = &cinfo->fx;
2367                 i = FX_FLAG_SET;
2368                 if (p1 >= 0x100) {
2369                         int layer = (p1 >> 8);
2370                         if (layer >= 0 && layer < MAX_LAYERS)
2371                                 fx = &cinfo->fx_layer[layer];
2372                         p1 &= 0xff;
2373                 }
2374                 if (p1 & 0x40) i = FX_FLAG_OFF;
2375                 if (p1 & 0x80) i = FX_FLAG_ADD;
2376                 p1 &= 0x3f;
2377                 if (p1 < AWE_FX_END) {
2378                         AWE_DEBUG(0,printk("AWE32: effects (%d) %d %d\n", voice, p1, p2));
2379                         if (i == FX_FLAG_SET)
2380                                 FX_SET(fx, p1, p2);
2381                         else if (i == FX_FLAG_ADD)
2382                                 FX_ADD(fx, p1, p2);
2383                         else
2384                                 FX_UNSET(fx, p1);
2385                         if (i != FX_FLAG_OFF && parm_defs[p1].realtime) {
2386                                 AWE_DEBUG(0,printk("AWE32: fx_realtime (%d)\n", voice));
2387                                 awe_voice_change(voice, parm_defs[p1].realtime);
2388                         }
2389                 }
2390                 break;
2391
2392         case _AWE_RESET_CHANNEL:
2393                 awe_channel_init(voice, !p1);
2394                 break;
2395                 
2396         case _AWE_TERMINATE_ALL:
2397                 awe_reset(0);
2398                 break;
2399
2400         case _AWE_TERMINATE_CHANNEL:
2401                 awe_voice_change(voice, awe_terminate_and_init);
2402                 break;
2403
2404         case _AWE_RELEASE_ALL:
2405                 awe_note_off_all(FALSE);
2406                 break;
2407         case _AWE_NOTEOFF_ALL:
2408                 awe_note_off_all(TRUE);
2409                 break;
2410
2411         case _AWE_INITIAL_VOLUME:
2412                 AWE_DEBUG(0,printk("AWE32: init attenuation %d\n", p1));
2413                 if (p2 == 0) /* absolute value */
2414                         init_atten = (short)p1;
2415                 else /* relative value */
2416                         init_atten = misc_modes[AWE_MD_ZERO_ATTEN] + (short)p1;
2417                 if (init_atten < 0) init_atten = 0;
2418                 for (i = 0; i < awe_max_voices; i++)
2419                         awe_set_voice_vol(i, TRUE);
2420                 break;
2421
2422         case _AWE_CHN_PRESSURE:
2423                 cinfo->chan_press = p1;
2424                 p1 = p1 * misc_modes[AWE_MD_MOD_SENSE] / 1200;
2425                 FX_ADD(&cinfo->fx, AWE_FX_LFO1_PITCH, p1);
2426                 awe_voice_change(voice, awe_fx_fmmod);
2427                 FX_ADD(&cinfo->fx, AWE_FX_LFO2_PITCH, p1);
2428                 awe_voice_change(voice, awe_fx_fm2frq2);
2429                 break;
2430
2431         case _AWE_CHANNEL_MODE:
2432                 AWE_DEBUG(0,printk("AWE32: channel mode = %d\n", p1));
2433                 playing_mode = p1;
2434                 awe_reset(0);
2435                 break;
2436
2437         case _AWE_DRUM_CHANNELS:
2438                 AWE_DEBUG(0,printk("AWE32: drum flags = %x\n", p1));
2439                 drum_flags = *(unsigned int*)&event[4];
2440                 break;
2441
2442         case _AWE_MISC_MODE:
2443                 AWE_DEBUG(0,printk("AWE32: misc mode = %d %d\n", p1, p2));
2444                 if (p1 > AWE_MD_VERSION && p1 < AWE_MD_END)
2445                         misc_modes[p1] = p2;
2446                 break;
2447
2448         case _AWE_EQUALIZER:
2449                 awe_equalizer((int)p1, (int)p2);
2450                 break;
2451
2452         default:
2453                 AWE_DEBUG(0,printk("AWE32: hw control cmd=%d voice=%d\n", cmd, voice));
2454                 break;
2455         }
2456 }
2457
2458
2459 /* voice pressure change */
2460 static void
2461 awe_aftertouch(int dev, int voice, int pressure)
2462 {
2463         int note;
2464
2465         AWE_DEBUG(2,printk("AWE32: [after(%d) %d]\n", voice, pressure));
2466         if (! voice_in_range(voice))
2467                 return;
2468
2469         switch (playing_mode) {
2470         case AWE_PLAY_DIRECT:
2471         case AWE_PLAY_INDIRECT:
2472                 awe_start_note(dev, voice, 255, pressure);
2473                 break;
2474         case AWE_PLAY_MULTI2:
2475                 note = (voice_alloc->map[voice] & 0xff) - 1;
2476                 awe_start_note(dev, voice, note + 0x80, pressure);
2477                 break;
2478         }
2479 }
2480
2481
2482 /* voice control change */
2483 static void
2484 awe_controller(int dev, int voice, int ctrl_num, int value)
2485 {
2486         int i;
2487         awe_chan_info *cinfo;
2488
2489         if (! voice_in_range(voice))
2490                 return;
2491
2492         if (playing_mode == AWE_PLAY_MULTI2) {
2493                 voice = voice_alloc->map[voice] >> 8;
2494                 if (voice < 0 || voice >= AWE_MAX_CHANNELS)
2495                         return;
2496         }
2497
2498         cinfo = &channels[voice];
2499
2500         switch (ctrl_num) {
2501         case CTL_BANK_SELECT: /* MIDI control #0 */
2502                 AWE_DEBUG(2,printk("AWE32: [bank(%d) %d]\n", voice, value));
2503                 if (MULTI_LAYER_MODE() && IS_DRUM_CHANNEL(voice) &&
2504                     !misc_modes[AWE_MD_TOGGLE_DRUM_BANK])
2505                         break;
2506                 cinfo->bank = value;
2507                 if (cinfo->bank == AWE_DRUM_BANK)
2508                         DRUM_CHANNEL_ON(cinfo->channel);
2509                 else
2510                         DRUM_CHANNEL_OFF(cinfo->channel);
2511                 awe_set_instr(dev, voice, cinfo->instr);
2512                 break;
2513
2514         case CTL_MODWHEEL: /* MIDI control #1 */
2515                 AWE_DEBUG(2,printk("AWE32: [modwheel(%d) %d]\n", voice, value));
2516                 i = value * misc_modes[AWE_MD_MOD_SENSE] / 1200;
2517                 FX_ADD(&cinfo->fx, AWE_FX_LFO1_PITCH, i);
2518                 awe_voice_change(voice, awe_fx_fmmod);
2519                 FX_ADD(&cinfo->fx, AWE_FX_LFO2_PITCH, i);
2520                 awe_voice_change(voice, awe_fx_fm2frq2);
2521                 break;
2522
2523         case CTRL_PITCH_BENDER: /* SEQ1 V2 contorl */
2524                 AWE_DEBUG(2,printk("AWE32: [bend(%d) %d]\n", voice, value));
2525                 /* zero centered */
2526                 cinfo->bender = value;
2527                 awe_voice_change(voice, awe_set_voice_pitch);
2528                 break;
2529
2530         case CTRL_PITCH_BENDER_RANGE: /* SEQ1 V2 control */
2531                 AWE_DEBUG(2,printk("AWE32: [range(%d) %d]\n", voice, value));
2532                 /* value = sense x 100 */
2533                 cinfo->bender_range = value;
2534                 /* no audible pitch change yet.. */
2535                 break;
2536
2537         case CTL_EXPRESSION: /* MIDI control #11 */
2538                 if (SINGLE_LAYER_MODE())
2539                         value /= 128;
2540         case CTRL_EXPRESSION: /* SEQ1 V2 control */
2541                 AWE_DEBUG(2,printk("AWE32: [expr(%d) %d]\n", voice, value));
2542                 /* 0 - 127 */
2543                 cinfo->expression_vol = value;
2544                 awe_voice_change(voice, awe_set_voice_vol);
2545                 break;
2546
2547         case CTL_PAN:   /* MIDI control #10 */
2548                 AWE_DEBUG(2,printk("AWE32: [pan(%d) %d]\n", voice, value));
2549                 /* (0-127) -> signed 8bit */
2550                 cinfo->panning = value * 2 - 128;
2551                 if (misc_modes[AWE_MD_REALTIME_PAN])
2552                         awe_voice_change(voice, awe_set_pan);
2553                 break;
2554
2555         case CTL_MAIN_VOLUME:   /* MIDI control #7 */
2556                 if (SINGLE_LAYER_MODE())
2557                         value = (value * 100) / 16383;
2558         case CTRL_MAIN_VOLUME:  /* SEQ1 V2 control */
2559                 AWE_DEBUG(2,printk("AWE32: [mainvol(%d) %d]\n", voice, value));
2560                 /* 0 - 127 */
2561                 cinfo->main_vol = value;
2562                 awe_voice_change(voice, awe_set_voice_vol);
2563                 break;
2564
2565         case CTL_EXT_EFF_DEPTH: /* reverb effects: 0-127 */
2566                 AWE_DEBUG(2,printk("AWE32: [reverb(%d) %d]\n", voice, value));
2567                 FX_SET(&cinfo->fx, AWE_FX_REVERB, value * 2);
2568                 break;
2569
2570         case CTL_CHORUS_DEPTH: /* chorus effects: 0-127 */
2571                 AWE_DEBUG(2,printk("AWE32: [chorus(%d) %d]\n", voice, value));
2572                 FX_SET(&cinfo->fx, AWE_FX_CHORUS, value * 2);
2573                 break;
2574
2575 #ifdef AWE_ACCEPT_ALL_SOUNDS_CONTROLL
2576         case 120:  /* all sounds off */
2577                 awe_note_off_all(FALSE);
2578                 break;
2579         case 123:  /* all notes off */
2580                 awe_note_off_all(TRUE);
2581                 break;
2582 #endif
2583
2584         case CTL_SUSTAIN: /* MIDI control #64 */
2585                 cinfo->sustained = value;
2586                 if (value != 127)
2587                         awe_voice_change(voice, awe_sustain_off);
2588                 break;
2589
2590         case CTL_SOSTENUTO: /* MIDI control #66 */
2591                 if (value == 127)
2592                         awe_voice_change(voice, awe_sostenuto_on);
2593                 else
2594                         awe_voice_change(voice, awe_sustain_off);
2595                 break;
2596
2597         default:
2598                 AWE_DEBUG(0,printk("AWE32: [control(%d) ctrl=%d val=%d]\n",
2599                            voice, ctrl_num, value));
2600                 break;
2601         }
2602 }
2603
2604
2605 /* voice pan change (value = -128 - 127) */
2606 static void
2607 awe_panning(int dev, int voice, int value)
2608 {
2609         awe_chan_info *cinfo;
2610
2611         if (! voice_in_range(voice))
2612                 return;
2613
2614         if (playing_mode == AWE_PLAY_MULTI2) {
2615                 voice = voice_alloc->map[voice] >> 8;
2616                 if (voice < 0 || voice >= AWE_MAX_CHANNELS)
2617                         return;
2618         }
2619
2620         cinfo = &channels[voice];
2621         cinfo->panning = value;
2622         AWE_DEBUG(2,printk("AWE32: [pan(%d) %d]\n", voice, cinfo->panning));
2623         if (misc_modes[AWE_MD_REALTIME_PAN])
2624                 awe_voice_change(voice, awe_set_pan);
2625 }
2626
2627
2628 /* volume mode change */
2629 static void
2630 awe_volume_method(int dev, int mode)
2631 {
2632         /* not impremented */
2633         AWE_DEBUG(0,printk("AWE32: [volmethod mode=%d]\n", mode));
2634 }
2635
2636
2637 #ifndef AWE_NO_PATCHMGR
2638 /* patch manager */
2639 static int
2640 awe_patchmgr(int dev, struct patmgr_info *rec)
2641 {
2642         printk("AWE32 Warning: patch manager control not supported\n");
2643         return 0;
2644 }
2645 #endif
2646
2647
2648 /* pitch wheel change: 0-16384 */
2649 static void
2650 awe_bender(int dev, int voice, int value)
2651 {
2652         awe_chan_info *cinfo;
2653
2654         if (! voice_in_range(voice))
2655                 return;
2656
2657         if (playing_mode == AWE_PLAY_MULTI2) {
2658                 voice = voice_alloc->map[voice] >> 8;
2659                 if (voice < 0 || voice >= AWE_MAX_CHANNELS)
2660                         return;
2661         }
2662
2663         /* convert to zero centered value */
2664         cinfo = &channels[voice];
2665         cinfo->bender = value - 8192;
2666         AWE_DEBUG(2,printk("AWE32: [bend(%d) %d]\n", voice, cinfo->bender));
2667         awe_voice_change(voice, awe_set_voice_pitch);
2668 }
2669
2670
2671 /*----------------------------------------------------------------
2672  * load a sound patch:
2673  *   three types of patches are accepted: AWE, GUS, and SYSEX.
2674  *----------------------------------------------------------------*/
2675
2676 static int
2677 awe_load_patch(int dev, int format, const char *addr,
2678                int offs, int count, int pmgr_flag)
2679 {
2680         awe_patch_info patch;
2681         int rc = 0;
2682
2683 #ifdef AWE_HAS_GUS_COMPATIBILITY
2684         if (format == GUS_PATCH) {
2685                 return awe_load_guspatch(addr, offs, count, pmgr_flag);
2686         } else
2687 #endif
2688         if (format == SYSEX_PATCH) {
2689                 /* no system exclusive message supported yet */
2690                 return 0;
2691         } else if (format != AWE_PATCH) {
2692                 printk("AWE32 Error: Invalid patch format (key) 0x%x\n", format);
2693                 return RET_ERROR(EINVAL);
2694         }
2695         
2696         if (count < AWE_PATCH_INFO_SIZE) {
2697                 printk("AWE32 Error: Patch header too short\n");
2698                 return RET_ERROR(EINVAL);
2699         }
2700         COPY_FROM_USER(((char*)&patch) + offs, addr, offs, 
2701                        AWE_PATCH_INFO_SIZE - offs);
2702
2703         count -= AWE_PATCH_INFO_SIZE;
2704         if (count < patch.len) {
2705                 printk("AWE32: sample: Patch record too short (%d<%d)\n",
2706                        count, patch.len);
2707                 return RET_ERROR(EINVAL);
2708         }
2709         
2710         switch (patch.type) {
2711         case AWE_LOAD_INFO:
2712                 rc = awe_load_info(&patch, addr, count);
2713                 break;
2714         case AWE_LOAD_DATA:
2715                 rc = awe_load_data(&patch, addr, count);
2716                 break;
2717         case AWE_OPEN_PATCH:
2718                 rc = awe_open_patch(&patch, addr, count);
2719                 break;
2720         case AWE_CLOSE_PATCH:
2721                 rc = awe_close_patch(&patch, addr, count);
2722                 break;
2723         case AWE_UNLOAD_PATCH:
2724                 rc = awe_unload_patch(&patch, addr, count);
2725                 break;
2726         case AWE_REPLACE_DATA:
2727                 rc = awe_replace_data(&patch, addr, count);
2728                 break;
2729         case AWE_MAP_PRESET:
2730                 rc = awe_load_map(&patch, addr, count);
2731                 break;
2732         case AWE_LOAD_CHORUS_FX:
2733                 rc = awe_load_chorus_fx(&patch, addr, count);
2734                 break;
2735         case AWE_LOAD_REVERB_FX:
2736                 rc = awe_load_reverb_fx(&patch, addr, count);
2737                 break;
2738
2739         default:
2740                 printk("AWE32 Error: unknown patch format type %d\n",
2741                        patch.type);
2742                 rc = RET_ERROR(EINVAL);
2743         }
2744
2745         return rc;
2746 }
2747
2748
2749 /* create an sflist record */
2750 static int
2751 awe_create_sf(int type, char *name)
2752 {
2753         sf_list *rec;
2754
2755         /* terminate sounds */
2756         awe_reset(0);
2757         if (current_sf_id >= max_sfs) {
2758                 int newsize = max_sfs + AWE_MAX_SF_LISTS;
2759                 sf_list *newlist = my_realloc(sflists, sizeof(sf_list)*max_sfs,
2760                                               sizeof(sf_list)*newsize);
2761                 if (newlist == NULL)
2762                         return 1;
2763                 sflists = newlist;
2764                 max_sfs = newsize;
2765         }
2766         rec = &sflists[current_sf_id];
2767         rec->sf_id = current_sf_id + 1;
2768         rec->type = type;
2769         if (current_sf_id == 0 || (type & AWE_PAT_LOCKED) != 0)
2770                 locked_sf_id = current_sf_id + 1;
2771         /*
2772         if (name)
2773                 MEMCPY(rec->name, name, AWE_PATCH_NAME_LEN);
2774         else
2775                 BZERO(rec->name, AWE_PATCH_NAME_LEN);
2776          */
2777         rec->num_info = awe_free_info();
2778         rec->num_sample = awe_free_sample();
2779         rec->mem_ptr = awe_free_mem_ptr();
2780         rec->infos = -1;
2781         rec->samples = -1;
2782
2783         current_sf_id++;
2784         return 0;
2785 }
2786
2787
2788 /* open patch; create sf list and set opened flag */
2789 static int
2790 awe_open_patch(awe_patch_info *patch, const char *addr, int count)
2791 {
2792         awe_open_parm parm;
2793         COPY_FROM_USER(&parm, addr, AWE_PATCH_INFO_SIZE, sizeof(parm));
2794         if (awe_create_sf(parm.type, parm.name)) {
2795                 printk("AWE32: can't open: failed to alloc new list\n");
2796                 return RET_ERROR(ENOSPC);
2797         }
2798         patch_opened = TRUE;
2799         return current_sf_id;
2800 }
2801
2802 /* check if the patch is already opened */
2803 static int
2804 check_patch_opened(int type, char *name)
2805 {
2806         if (! patch_opened) {
2807                 if (awe_create_sf(type, name)) {
2808                         printk("AWE32: failed to alloc new list\n");
2809                         return RET_ERROR(ENOSPC);
2810                 }
2811                 patch_opened = TRUE;
2812                 return current_sf_id;
2813         }
2814         return current_sf_id;
2815 }
2816
2817 /* close the patch; if no voice is loaded, remove the patch */
2818 static int
2819 awe_close_patch(awe_patch_info *patch, const char *addr, int count)
2820 {
2821         if (patch_opened && current_sf_id > 0) {
2822                 /* if no voice is loaded, release the current patch */
2823                 if (sflists[current_sf_id-1].infos == -1)
2824                         awe_remove_samples(current_sf_id - 1);
2825         }
2826         patch_opened = 0;
2827         return 0;
2828 }
2829
2830
2831 /* remove the latest patch */
2832 static int
2833 awe_unload_patch(awe_patch_info *patch, const char *addr, int count)
2834 {
2835         if (current_sf_id > 0)
2836                 awe_remove_samples(current_sf_id - 1);
2837         return 0;
2838 }
2839
2840 /* allocate voice info list records */
2841 static int alloc_new_info(int nvoices)
2842 {
2843         int newsize, free_info;
2844         awe_voice_list *newlist;
2845         free_info = awe_free_info();
2846         if (free_info + nvoices >= max_infos) {
2847                 do {
2848                         newsize = max_infos + AWE_MAX_INFOS;
2849                 } while (free_info + nvoices >= newsize);
2850                 newlist = my_realloc(infos, sizeof(awe_voice_list)*max_infos,
2851                                      sizeof(awe_voice_list)*newsize);
2852                 if (newlist == NULL) {
2853                         printk("AWE32: can't alloc info table\n");
2854                         return RET_ERROR(ENOSPC);
2855                 }
2856                 infos = newlist;
2857                 max_infos = newsize;
2858         }
2859         return 0;
2860 }
2861
2862 /* allocate sample info list records */
2863 static int alloc_new_sample(void)
2864 {
2865         int newsize, free_sample;
2866         awe_sample_list *newlist;
2867         free_sample = awe_free_sample();
2868         if (free_sample >= max_samples) {
2869                 newsize = max_samples + AWE_MAX_SAMPLES;
2870                 newlist = my_realloc(samples,
2871                                      sizeof(awe_sample_list)*max_samples,
2872                                      sizeof(awe_sample_list)*newsize);
2873                 if (newlist == NULL) {
2874                         printk("AWE32: can't alloc sample table\n");
2875                         return RET_ERROR(ENOSPC);
2876                 }
2877                 samples = newlist;
2878                 max_samples = newsize;
2879         }
2880         return 0;
2881 }
2882
2883 /* load voice map */
2884 static int
2885 awe_load_map(awe_patch_info *patch, const char *addr, int count)
2886 {
2887         awe_voice_map map;
2888         awe_voice_list *rec;
2889         int free_info;
2890
2891         if (check_patch_opened(AWE_PAT_TYPE_MAP, NULL) < 0)
2892                 return RET_ERROR(ENOSPC);
2893         if (alloc_new_info(1) < 0)
2894                 return RET_ERROR(ENOSPC);
2895
2896         COPY_FROM_USER(&map, addr, AWE_PATCH_INFO_SIZE, sizeof(map));
2897         
2898         free_info = awe_free_info();
2899         rec = &infos[free_info];
2900         rec->bank = map.map_bank;
2901         rec->instr = map.map_instr;
2902         rec->type = V_ST_MAPPED;
2903         rec->disabled = FALSE;
2904         awe_init_voice_info(&rec->v);
2905         if (map.map_key >= 0) {
2906                 rec->v.low = map.map_key;
2907                 rec->v.high = map.map_key;
2908         }
2909         rec->v.start = map.src_instr;
2910         rec->v.end = map.src_bank;
2911         rec->v.fixkey = map.src_key;
2912         rec->v.sf_id = current_sf_id;
2913         add_info_list(free_info);
2914         add_sf_info(free_info);
2915
2916         return 0;
2917 }
2918
2919 /* load voice information data */
2920 static int
2921 awe_load_info(awe_patch_info *patch, const char *addr, int count)
2922 {
2923         int offset;
2924         awe_voice_rec_hdr hdr;
2925         int i;
2926         int total_size;
2927
2928         if (count < AWE_VOICE_REC_SIZE) {
2929                 printk("AWE32 Error: invalid patch info length\n");
2930                 return RET_ERROR(EINVAL);
2931         }
2932
2933         offset = AWE_PATCH_INFO_SIZE;
2934         COPY_FROM_USER((char*)&hdr, addr, offset, AWE_VOICE_REC_SIZE);
2935         offset += AWE_VOICE_REC_SIZE;
2936
2937         if (hdr.nvoices <= 0 || hdr.nvoices >= 100) {
2938                 printk("AWE32 Error: Illegal voice number %d\n", hdr.nvoices);
2939                 return RET_ERROR(EINVAL);
2940         }
2941         total_size = AWE_VOICE_REC_SIZE + AWE_VOICE_INFO_SIZE * hdr.nvoices;
2942         if (count < total_size) {
2943                 printk("AWE32 Error: patch length(%d) is smaller than nvoices(%d)\n",
2944                        count, hdr.nvoices);
2945                 return RET_ERROR(EINVAL);
2946         }
2947
2948         if (check_patch_opened(AWE_PAT_TYPE_MISC, NULL) < 0)
2949                 return RET_ERROR(ENOSPC);
2950
2951 #if 0 /* it looks like not so useful.. */
2952         /* check if the same preset already exists in the info list */
2953         for (i = sflists[current_sf_id-1].infos; i >= 0; i = infos[i].next) {
2954                 if (infos[i].disabled) continue;
2955                 if (infos[i].bank == hdr.bank && infos[i].instr == hdr.instr) {
2956                         /* in exclusive mode, do skip loading this */
2957                         if (hdr.write_mode == AWE_WR_EXCLUSIVE)
2958                                 return 0;
2959                         /* in replace mode, disable the old data */
2960                         else if (hdr.write_mode == AWE_WR_REPLACE)
2961                                 infos[i].disabled = TRUE;
2962                 }
2963         }
2964         if (hdr.write_mode == AWE_WR_REPLACE)
2965                 rebuild_preset_list();
2966 #endif
2967
2968         if (alloc_new_info(hdr.nvoices) < 0)
2969                 return RET_ERROR(ENOSPC);
2970
2971         for (i = 0; i < hdr.nvoices; i++) {
2972                 int rec = awe_free_info();
2973
2974                 infos[rec].bank = hdr.bank;
2975                 infos[rec].instr = hdr.instr;
2976                 infos[rec].type = V_ST_NORMAL;
2977                 infos[rec].disabled = FALSE;
2978
2979                 /* copy awe_voice_info parameters */
2980                 COPY_FROM_USER(&infos[rec].v, addr, offset, AWE_VOICE_INFO_SIZE);
2981                 offset += AWE_VOICE_INFO_SIZE;
2982                 infos[rec].v.sf_id = current_sf_id;
2983                 if (infos[rec].v.mode & AWE_MODE_INIT_PARM)
2984                         awe_init_voice_parm(&infos[rec].v.parm);
2985                 awe_set_sample(&infos[rec].v);
2986                 add_info_list(rec);
2987                 add_sf_info(rec);
2988         }
2989
2990         return 0;
2991 }
2992
2993 /* load wave sample data */
2994 static int
2995 awe_load_data(awe_patch_info *patch, const char *addr, int count)
2996 {
2997         int offset, size;
2998         int rc, free_sample;
2999         awe_sample_info *rec;
3000
3001         if (check_patch_opened(AWE_PAT_TYPE_MISC, NULL) < 0)
3002                 return RET_ERROR(ENOSPC);
3003
3004         if (alloc_new_sample() < 0)
3005                 return RET_ERROR(ENOSPC);
3006
3007         free_sample = awe_free_sample();
3008         rec = &samples[free_sample].v;
3009
3010         size = (count - AWE_SAMPLE_INFO_SIZE) / 2;
3011         offset = AWE_PATCH_INFO_SIZE;
3012         COPY_FROM_USER(rec, addr, offset, AWE_SAMPLE_INFO_SIZE);
3013         offset += AWE_SAMPLE_INFO_SIZE;
3014         if (size != rec->size) {
3015                 printk("AWE32: load: sample size differed (%d != %d)\n",
3016                        rec->size, size);
3017                 return RET_ERROR(EINVAL);
3018         }
3019         if (rec->size > 0)
3020                 if ((rc = awe_write_wave_data(addr, offset, rec, -1)) != 0)
3021                         return rc;
3022
3023         rec->sf_id = current_sf_id;
3024
3025         add_sf_sample(free_sample);
3026
3027         return 0;
3028 }
3029
3030
3031 /* replace wave sample data */
3032 static int
3033 awe_replace_data(awe_patch_info *patch, const char *addr, int count)
3034 {
3035         int offset;
3036         int size;
3037         int rc, i;
3038         int channels;
3039         awe_sample_info cursmp;
3040         int save_mem_ptr;
3041
3042         if (! patch_opened) {
3043                 printk("AWE32: replace: patch not opened\n");
3044                 return RET_ERROR(EINVAL);
3045         }
3046
3047         size = (count - AWE_SAMPLE_INFO_SIZE) / 2;
3048         offset = AWE_PATCH_INFO_SIZE;
3049         COPY_FROM_USER(&cursmp, addr, offset, AWE_SAMPLE_INFO_SIZE);
3050         offset += AWE_SAMPLE_INFO_SIZE;
3051         if (cursmp.size == 0 || size != cursmp.size) {
3052                 printk("AWE32: replace: illegal sample size (%d!=%d)\n",
3053                        cursmp.size, size);
3054                 return RET_ERROR(EINVAL);
3055         }
3056         channels = patch->optarg;
3057         if (channels <= 0 || channels > AWE_NORMAL_VOICES) {
3058                 printk("AWE32: replace: illegal channels %d\n", channels);
3059                 return RET_ERROR(EINVAL);
3060         }
3061
3062         for (i = sflists[current_sf_id-1].samples;
3063              i >= 0; i = samples[i].next) {
3064                 if (samples[i].v.sample == cursmp.sample)
3065                         break;
3066         }
3067         if (i < 0) {
3068                 printk("AWE32: replace: cannot find existing sample data %d\n",
3069                        cursmp.sample);
3070                 return RET_ERROR(EINVAL);
3071         }
3072                 
3073         if (samples[i].v.size != cursmp.size) {
3074                 printk("AWE32: replace: exiting size differed (%d!=%d)\n",
3075                        samples[i].v.size, cursmp.size);
3076                 return RET_ERROR(EINVAL);
3077         }
3078
3079         save_mem_ptr = awe_free_mem_ptr();
3080         sflists[current_sf_id-1].mem_ptr = samples[i].v.start - awe_mem_start;
3081         MEMCPY(&samples[i].v, &cursmp, sizeof(cursmp));
3082         if ((rc = awe_write_wave_data(addr, offset, &samples[i].v, channels)) != 0)
3083                 return rc;
3084         sflists[current_sf_id-1].mem_ptr = save_mem_ptr;
3085         samples[i].v.sf_id = current_sf_id;
3086
3087         return 0;
3088 }
3089
3090
3091 /*----------------------------------------------------------------*/
3092
3093 static const char *readbuf_addr;
3094 static int readbuf_offs;
3095 static int readbuf_flags;
3096 #if defined(__DragonFly__) || defined(__FreeBSD__)
3097 static unsigned short *readbuf_loop;
3098 static int readbuf_loopstart, readbuf_loopend;
3099 #endif
3100
3101 /* initialize read buffer */
3102 static int
3103 readbuf_init(const char *addr, int offset, awe_sample_info *sp)
3104 {
3105 #if defined(__DragonFly__) || defined(__FreeBSD__)
3106         readbuf_loop = NULL;
3107         readbuf_loopstart = sp->loopstart;
3108         readbuf_loopend = sp->loopend;
3109         if (sp->mode_flags & (AWE_SAMPLE_BIDIR_LOOP|AWE_SAMPLE_REVERSE_LOOP)) {
3110                 int looplen = sp->loopend - sp->loopstart;
3111                 readbuf_loop = my_malloc(looplen * 2);
3112                 if (readbuf_loop == NULL) {
3113                         printk("AWE32: can't malloc temp buffer\n");
3114                         return RET_ERROR(ENOSPC);
3115                 }
3116         }
3117 #endif
3118         readbuf_addr = addr;
3119         readbuf_offs = offset;
3120         readbuf_flags = sp->mode_flags;
3121         return 0;
3122 }
3123
3124 /* read directly from user buffer */
3125 static unsigned short
3126 readbuf_word(int pos)
3127 {
3128         unsigned short c;
3129         /* read from user buffer */
3130         if (readbuf_flags & AWE_SAMPLE_8BITS) {
3131                 unsigned char cc;
3132                 GET_BYTE_FROM_USER(cc, readbuf_addr, readbuf_offs + pos);
3133                 c = cc << 8; /* convert 8bit -> 16bit */
3134         } else {
3135                 GET_SHORT_FROM_USER(c, readbuf_addr, readbuf_offs + pos * 2);
3136         }
3137         if (readbuf_flags & AWE_SAMPLE_UNSIGNED)
3138                 c ^= 0x8000; /* unsigned -> signed */
3139 #if defined(__DragonFly__) || defined(__FreeBSD__)
3140         /* write on cache for reverse loop */
3141         if (readbuf_flags & (AWE_SAMPLE_BIDIR_LOOP|AWE_SAMPLE_REVERSE_LOOP)) {
3142                 if (pos >= readbuf_loopstart && pos < readbuf_loopend)
3143                         readbuf_loop[pos - readbuf_loopstart] = c;
3144         }
3145 #endif
3146         return c;
3147 }
3148
3149 #if defined(__DragonFly__) || defined(__FreeBSD__)
3150 /* read from cache */
3151 static unsigned short
3152 readbuf_word_cache(int pos)
3153 {
3154         if (pos >= readbuf_loopstart && pos < readbuf_loopend)
3155                 return readbuf_loop[pos - readbuf_loopstart];
3156         return 0;
3157 }
3158
3159 static void
3160 readbuf_end(void)
3161 {
3162         if (readbuf_loop) {
3163                 my_free(readbuf_loop);
3164         }
3165         readbuf_loop = NULL;
3166 }
3167
3168 #else
3169
3170 #define readbuf_word_cache      readbuf_word
3171 #define readbuf_end()           /**/
3172
3173 #endif
3174
3175 /*----------------------------------------------------------------*/
3176
3177 #define BLANK_LOOP_START        8
3178 #define BLANK_LOOP_END          40
3179 #define BLANK_LOOP_SIZE         48
3180
3181 /* loading onto memory */
3182 static int 
3183 awe_write_wave_data(const char *addr, int offset, awe_sample_info *sp, int channels)
3184 {
3185         int i, truesize, dram_offset;
3186         int rc;
3187
3188         /* be sure loop points start < end */
3189         if (sp->loopstart > sp->loopend) {
3190                 int tmp = sp->loopstart;
3191                 sp->loopstart = sp->loopend;
3192                 sp->loopend = tmp;
3193         }
3194
3195         /* compute true data size to be loaded */
3196         truesize = sp->size;
3197         if (sp->mode_flags & AWE_SAMPLE_BIDIR_LOOP)
3198                 truesize += sp->loopend - sp->loopstart;
3199         if (sp->mode_flags & AWE_SAMPLE_NO_BLANK)
3200                 truesize += BLANK_LOOP_SIZE;
3201         if (awe_free_mem_ptr() + truesize >= awe_mem_size/2) {
3202                 printk("AWE32 Error: Sample memory full\n");
3203                 return RET_ERROR(ENOSPC);
3204         }
3205
3206         /* recalculate address offset */
3207         sp->end -= sp->start;
3208         sp->loopstart -= sp->start;
3209         sp->loopend -= sp->start;
3210
3211         dram_offset = awe_free_mem_ptr() + awe_mem_start;
3212         sp->start = dram_offset;
3213         sp->end += dram_offset;
3214         sp->loopstart += dram_offset;
3215         sp->loopend += dram_offset;
3216
3217         /* set the total size (store onto obsolete checksum value) */
3218         if (sp->size == 0)
3219                 sp->checksum = 0;
3220         else
3221                 sp->checksum = truesize;
3222
3223         if ((rc = awe_open_dram_for_write(dram_offset, channels)) != 0)
3224                 return rc;
3225
3226         if (readbuf_init(addr, offset, sp) < 0)
3227                 return RET_ERROR(ENOSPC);
3228
3229         for (i = 0; i < sp->size; i++) {
3230                 unsigned short c;
3231                 c = readbuf_word(i);
3232                 awe_write_dram(c);
3233                 if (i == sp->loopend &&
3234                     (sp->mode_flags & (AWE_SAMPLE_BIDIR_LOOP|AWE_SAMPLE_REVERSE_LOOP))) {
3235                         int looplen = sp->loopend - sp->loopstart;
3236                         /* copy reverse loop */
3237                         int k;
3238                         for (k = 1; k <= looplen; k++) {
3239                                 c = readbuf_word_cache(i - k);
3240                                 awe_write_dram(c);
3241                         }
3242                         if (sp->mode_flags & AWE_SAMPLE_BIDIR_LOOP) {
3243                                 sp->end += looplen;
3244                         } else {
3245                                 sp->start += looplen;
3246                                 sp->end += looplen;
3247                         }
3248                 }
3249         }
3250         readbuf_end();
3251
3252         /* if no blank loop is attached in the sample, add it */
3253         if (sp->mode_flags & AWE_SAMPLE_NO_BLANK) {
3254                 for (i = 0; i < BLANK_LOOP_SIZE; i++)
3255                         awe_write_dram(0);
3256                 if (sp->mode_flags & AWE_SAMPLE_SINGLESHOT) {
3257                         sp->loopstart = sp->end + BLANK_LOOP_START;
3258                         sp->loopend = sp->end + BLANK_LOOP_END;
3259                 }
3260         }
3261
3262         sflists[current_sf_id-1].mem_ptr += truesize;
3263         awe_close_dram();
3264
3265         /* initialize FM */
3266         awe_init_fm();
3267
3268         return 0;
3269 }
3270
3271
3272 /*----------------------------------------------------------------*/
3273
3274 #ifdef AWE_HAS_GUS_COMPATIBILITY
3275
3276 /* calculate GUS envelope time:
3277  * is this correct?  i have no idea..
3278  */
3279 static int
3280 calc_gus_envelope_time(int rate, int start, int end)
3281 {
3282         int r, p, t;
3283         r = (3 - ((rate >> 6) & 3)) * 3;
3284         p = rate & 0x3f;
3285         t = end - start;
3286         if (t < 0) t = -t;
3287         if (13 > r)
3288                 t = t << (13 - r);
3289         else
3290                 t = t >> (r - 13);
3291         return (t * 10) / (p * 441);
3292 }
3293
3294 #define calc_gus_sustain(val)  (0x7f - vol_table[(val)/2])
3295 #define calc_gus_attenuation(val)       vol_table[(val)/2]
3296
3297 /* load GUS patch */
3298 static int
3299 awe_load_guspatch(const char *addr, int offs, int size, int pmgr_flag)
3300 {
3301         struct patch_info patch;
3302         awe_voice_info *rec;
3303         awe_sample_info *smp;
3304         int sizeof_patch;
3305         int note, free_sample, free_info;
3306         int rc;
3307
3308         sizeof_patch = offsetof(struct patch_info, data); /* header size */
3309         if (size < sizeof_patch) {
3310                 printk("AWE32 Error: Patch header too short\n");
3311                 return RET_ERROR(EINVAL);
3312         }
3313         COPY_FROM_USER(((char*)&patch) + offs, addr, offs, sizeof_patch - offs);
3314         size -= sizeof_patch;
3315         if (size < patch.len) {
3316                 printk("AWE32 Warning: Patch record too short (%d<%ld)\n",
3317                        size, patch.len);
3318                 return RET_ERROR(EINVAL);
3319         }
3320         if (check_patch_opened(AWE_PAT_TYPE_GUS, NULL) < 0)
3321                 return RET_ERROR(ENOSPC);
3322         if (alloc_new_sample() < 0)
3323                 return RET_ERROR(ENOSPC);
3324         if (alloc_new_info(1))
3325                 return RET_ERROR(ENOSPC);
3326
3327         free_sample = awe_free_sample();
3328         smp = &samples[free_sample].v;
3329
3330         smp->sample = free_sample;
3331         smp->start = 0;
3332         smp->end = patch.len;
3333         smp->loopstart = patch.loop_start;
3334         smp->loopend = patch.loop_end;
3335         smp->size = patch.len;
3336
3337         /* set up mode flags */
3338         smp->mode_flags = 0;
3339         if (!(patch.mode & WAVE_16_BITS))
3340                 smp->mode_flags |= AWE_SAMPLE_8BITS;
3341         if (patch.mode & WAVE_UNSIGNED)
3342                 smp->mode_flags |= AWE_SAMPLE_UNSIGNED;
3343         smp->mode_flags |= AWE_SAMPLE_NO_BLANK;
3344         if (!(patch.mode & (WAVE_LOOPING|WAVE_BIDIR_LOOP|WAVE_LOOP_BACK)))
3345                 smp->mode_flags |= AWE_SAMPLE_SINGLESHOT;
3346         if (patch.mode & WAVE_BIDIR_LOOP)
3347                 smp->mode_flags |= AWE_SAMPLE_BIDIR_LOOP;
3348         if (patch.mode & WAVE_LOOP_BACK)
3349                 smp->mode_flags |= AWE_SAMPLE_REVERSE_LOOP;
3350
3351         AWE_DEBUG(0,printk("AWE32: [sample %d mode %x]\n", patch.instr_no, smp->mode_flags));
3352         if (patch.mode & WAVE_16_BITS) {
3353                 /* convert to word offsets */
3354                 smp->size /= 2;
3355                 smp->end /= 2;
3356                 smp->loopstart /= 2;
3357                 smp->loopend /= 2;
3358         }
3359         smp->checksum_flag = 0;
3360         smp->checksum = 0;
3361
3362         if ((rc = awe_write_wave_data(addr, sizeof_patch, smp, -1)) != 0)
3363                 return rc;
3364
3365         smp->sf_id = current_sf_id;
3366         add_sf_sample(free_sample);
3367
3368         /* set up voice info */
3369         free_info = awe_free_info();
3370         rec = &infos[free_info].v;
3371         awe_init_voice_info(rec);
3372         rec->sample = free_sample; /* the last sample */
3373         rec->rate_offset = calc_rate_offset(patch.base_freq);
3374         note = freq_to_note(patch.base_note);
3375         rec->root = note / 100;
3376         rec->tune = -(note % 100);
3377         rec->low = freq_to_note(patch.low_note) / 100;
3378         rec->high = freq_to_note(patch.high_note) / 100;
3379         AWE_DEBUG(1,printk("AWE32: [gus base offset=%d, note=%d, range=%d-%d(%lu-%lu)]\n",
3380                        rec->rate_offset, note,
3381                        rec->low, rec->high,
3382               patch.low_note, patch.high_note));
3383         /* panning position; -128 - 127 => 0-127 */
3384         rec->pan = (patch.panning + 128) / 2;
3385
3386         /* detuning is ignored */
3387         /* 6points volume envelope */
3388         if (patch.mode & WAVE_ENVELOPES) {
3389                 int attack, hold, decay, release;
3390                 attack = calc_gus_envelope_time
3391                         (patch.env_rate[0], 0, patch.env_offset[0]);
3392                 hold = calc_gus_envelope_time
3393                         (patch.env_rate[1], patch.env_offset[0],
3394                          patch.env_offset[1]);
3395                 decay = calc_gus_envelope_time
3396                         (patch.env_rate[2], patch.env_offset[1],
3397                          patch.env_offset[2]);
3398                 release = calc_gus_envelope_time
3399                         (patch.env_rate[3], patch.env_offset[1],
3400                          patch.env_offset[4]);
3401                 release += calc_gus_envelope_time
3402                         (patch.env_rate[4], patch.env_offset[3],
3403                          patch.env_offset[4]);
3404                 release += calc_gus_envelope_time
3405                         (patch.env_rate[5], patch.env_offset[4],
3406                          patch.env_offset[5]);
3407                 rec->parm.volatkhld = (calc_parm_attack(attack) << 8) |
3408                         calc_parm_hold(hold);
3409                 rec->parm.voldcysus = (calc_gus_sustain(patch.env_offset[2]) << 8) |
3410                         calc_parm_decay(decay);
3411                 rec->parm.volrelease = 0x8000 | calc_parm_decay(release);
3412                 AWE_DEBUG(2,printk("AWE32: [gusenv atk=%d, hld=%d, dcy=%d, rel=%d]\n", attack, hold, decay, release));
3413                 rec->attenuation = calc_gus_attenuation(patch.env_offset[0]);
3414         }
3415
3416         /* tremolo effect */
3417         if (patch.mode & WAVE_TREMOLO) {
3418                 int rate = (patch.tremolo_rate * 1000 / 38) / 42;
3419                 rec->parm.tremfrq = ((patch.tremolo_depth / 2) << 8) | rate;
3420                 AWE_DEBUG(2,printk("AWE32: [gusenv tremolo rate=%d, dep=%d, tremfrq=%x]\n",
3421                                patch.tremolo_rate, patch.tremolo_depth,
3422                                rec->parm.tremfrq));
3423         }
3424         /* vibrato effect */
3425         if (patch.mode & WAVE_VIBRATO) {
3426                 int rate = (patch.vibrato_rate * 1000 / 38) / 42;
3427                 rec->parm.fm2frq2 = ((patch.vibrato_depth / 6) << 8) | rate;
3428                 AWE_DEBUG(2,printk("AWE32: [gusenv vibrato rate=%d, dep=%d, tremfrq=%x]\n",
3429                                patch.tremolo_rate, patch.tremolo_depth,
3430                                rec->parm.tremfrq));
3431         }
3432         
3433         /* scale_freq, scale_factor, volume, and fractions not implemented */
3434
3435         /* append to the tail of the list */
3436         infos[free_info].bank = misc_modes[AWE_MD_GUS_BANK];
3437         infos[free_info].instr = patch.instr_no;
3438         infos[free_info].disabled = FALSE;
3439         infos[free_info].type = V_ST_NORMAL;
3440         infos[free_info].v.sf_id = current_sf_id;
3441
3442         add_info_list(free_info);
3443         add_sf_info(free_info);
3444
3445         /* set the voice index */
3446         awe_set_sample(rec);
3447
3448         return 0;
3449 }
3450
3451 #endif  /* AWE_HAS_GUS_COMPATIBILITY */
3452
3453 /*----------------------------------------------------------------
3454  * sample and voice list handlers
3455  *----------------------------------------------------------------*/
3456
3457 /* append this to the sf list */
3458 static void add_sf_info(int rec)
3459 {
3460         int sf_id = infos[rec].v.sf_id;
3461         if (sf_id == 0) return;
3462         sf_id--;
3463         if (sflists[sf_id].infos < 0)
3464                 sflists[sf_id].infos = rec;
3465         else {
3466                 int i, prev;
3467                 prev = sflists[sf_id].infos;
3468                 while ((i = infos[prev].next) >= 0)
3469                         prev = i;
3470                 infos[prev].next = rec;
3471         }
3472         infos[rec].next = -1;
3473         sflists[sf_id].num_info++;
3474 }
3475
3476 /* prepend this sample to sf list */
3477 static void add_sf_sample(int rec)
3478 {
3479         int sf_id = samples[rec].v.sf_id;
3480         if (sf_id == 0) return;
3481         sf_id--;
3482         samples[rec].next = sflists[sf_id].samples;
3483         sflists[sf_id].samples = rec;
3484         sflists[sf_id].num_sample++;
3485 }
3486
3487 /* purge the old records which don't belong with the same file id */
3488 static void purge_old_list(int rec, int next)
3489 {
3490         infos[rec].next_instr = next;
3491         if (infos[rec].bank == AWE_DRUM_BANK) {
3492                 /* remove samples with the same note range */
3493                 int cur, *prevp = &infos[rec].next_instr;
3494                 int low = infos[rec].v.low;
3495                 int high = infos[rec].v.high;
3496                 for (cur = next; cur >= 0; cur = infos[cur].next_instr) {
3497                         if (infos[cur].v.low == low &&
3498                             infos[cur].v.high == high &&
3499                             infos[cur].v.sf_id != infos[rec].v.sf_id)
3500                                 *prevp = infos[cur].next_instr;
3501                         prevp = &infos[cur].next_instr;
3502                 }
3503         } else {
3504                 if (infos[next].v.sf_id != infos[rec].v.sf_id)
3505                         infos[rec].next_instr = -1;
3506         }
3507 }
3508
3509 /* prepend to top of the preset table */
3510 static void add_info_list(int rec)
3511 {
3512         int *prevp, cur;
3513         int instr = infos[rec].instr;
3514         int bank = infos[rec].bank;
3515
3516         if (infos[rec].disabled)
3517                 return;
3518
3519         prevp = &preset_table[instr];
3520         cur = *prevp;
3521         while (cur >= 0) {
3522                 /* search the first record with the same bank number */
3523                 if (infos[cur].bank == bank) {
3524                         /* replace the list with the new record */
3525                         infos[rec].next_bank = infos[cur].next_bank;
3526                         *prevp = rec;
3527                         purge_old_list(rec, cur);
3528                         return;
3529                 }
3530                 prevp = &infos[cur].next_bank;
3531                 cur = infos[cur].next_bank;
3532         }
3533
3534         /* this is the first bank record.. just add this */
3535         infos[rec].next_instr = -1;
3536         infos[rec].next_bank = preset_table[instr];
3537         preset_table[instr] = rec;
3538 }
3539
3540 /* remove samples later than the specified sf_id */
3541 static void
3542 awe_remove_samples(int sf_id)
3543 {
3544         if (sf_id <= 0) {
3545                 awe_reset_samples();
3546                 return;
3547         }
3548         /* already removed? */
3549         if (current_sf_id <= sf_id)
3550                 return;
3551
3552         current_sf_id = sf_id;
3553         if (locked_sf_id > sf_id)
3554                 locked_sf_id = sf_id;
3555
3556         rebuild_preset_list();
3557 }
3558
3559 /* rebuild preset search list */
3560 static void rebuild_preset_list(void)
3561 {
3562         int i, j;
3563
3564         for (i = 0; i < AWE_MAX_PRESETS; i++)
3565                 preset_table[i] = -1;
3566
3567         for (i = 0; i < current_sf_id; i++) {
3568                 for (j = sflists[i].infos; j >= 0; j = infos[j].next)
3569                         add_info_list(j);
3570         }
3571 }
3572
3573 /* search the specified sample */
3574 static short
3575 awe_set_sample(awe_voice_info *vp)
3576 {
3577         int i;
3578         vp->index = -1;
3579         for (i = sflists[vp->sf_id-1].samples; i >= 0; i = samples[i].next) {
3580                 if (samples[i].v.sample == vp->sample) {
3581                         /* set the actual sample offsets */
3582                         vp->start += samples[i].v.start;
3583                         vp->end += samples[i].v.end;
3584                         vp->loopstart += samples[i].v.loopstart;
3585                         vp->loopend += samples[i].v.loopend;
3586                         /* copy mode flags */
3587                         vp->mode = samples[i].v.mode_flags;
3588                         /* set index */
3589                         vp->index = i;
3590                         return i;
3591                 }
3592         }
3593         return -1;
3594 }
3595
3596
3597 /*----------------------------------------------------------------
3598  * voice allocation
3599  *----------------------------------------------------------------*/
3600
3601 /* look for all voices associated with the specified note & velocity */
3602 static int
3603 awe_search_multi_voices(int rec, int note, int velocity, awe_voice_info **vlist)
3604 {
3605         int nvoices;
3606
3607         nvoices = 0;
3608         for (; rec >= 0; rec = infos[rec].next_instr) {
3609                 if (note >= infos[rec].v.low &&
3610                     note <= infos[rec].v.high &&
3611                     velocity >= infos[rec].v.vellow &&
3612                     velocity <= infos[rec].v.velhigh) {
3613                         vlist[nvoices] = &infos[rec].v;
3614                         if (infos[rec].type == V_ST_MAPPED) /* mapper */
3615                                 return -1;
3616                         nvoices++;
3617                         if (nvoices >= AWE_MAX_VOICES)
3618                                 break;
3619                 }
3620         }
3621         return nvoices; 
3622 }
3623
3624 /* store the voice list from the specified note and velocity.
3625    if the preset is mapped, seek for the destination preset, and rewrite
3626    the note number if necessary.
3627    */
3628 static int
3629 really_alloc_voices(int vrec, int def_vrec, int *note, int velocity, awe_voice_info **vlist, int level)
3630 {
3631         int nvoices;
3632
3633         nvoices = awe_search_multi_voices(vrec, *note, velocity, vlist);
3634         if (nvoices == 0)
3635                 nvoices = awe_search_multi_voices(def_vrec, *note, velocity, vlist);
3636         if (nvoices < 0) { /* mapping */
3637                 int preset = vlist[0]->start;
3638                 int bank = vlist[0]->end;
3639                 int key = vlist[0]->fixkey;
3640                 if (level > 5) {
3641                         printk("AWE32: too deep mapping level\n");
3642                         return 0;
3643                 }
3644                 vrec = awe_search_instr(bank, preset);
3645                 if (bank == AWE_DRUM_BANK)
3646                         def_vrec = awe_search_instr(bank, 0);
3647                 else
3648                         def_vrec = awe_search_instr(0, preset);
3649                 if (key >= 0)
3650                         *note = key;
3651                 return really_alloc_voices(vrec, def_vrec, note, velocity, vlist, level+1);
3652         }
3653
3654         return nvoices;
3655 }
3656
3657 /* allocate voices corresponding note and velocity; supports multiple insts. */
3658 static void
3659 awe_alloc_multi_voices(int ch, int note, int velocity, int key)
3660 {
3661         int i, v, nvoices;
3662         awe_voice_info *vlist[AWE_MAX_VOICES];
3663
3664         if (channels[ch].vrec < 0 && channels[ch].def_vrec < 0)
3665                 awe_set_instr(0, ch, channels[ch].instr);
3666
3667         /* check the possible voices; note may be changeable if mapped */
3668         nvoices = really_alloc_voices(channels[ch].vrec, channels[ch].def_vrec,
3669                                       &note, velocity, vlist, 0);
3670
3671         /* set the voices */
3672         current_alloc_time++;
3673         for (i = 0; i < nvoices; i++) {
3674                 v = awe_clear_voice();
3675                 voices[v].key = key;
3676                 voices[v].ch = ch;
3677                 voices[v].note = note;
3678                 voices[v].velocity = velocity;
3679                 voices[v].time = current_alloc_time;
3680                 voices[v].cinfo = &channels[ch];
3681                 voices[v].sample = vlist[i];
3682                 voices[v].state = AWE_ST_MARK;
3683                 voices[v].layer = nvoices - i - 1;  /* in reverse order */
3684         }
3685
3686         /* clear the mark in allocated voices */
3687         for (i = 0; i < awe_max_voices; i++) {
3688                 if (voices[i].state == AWE_ST_MARK)
3689                         voices[i].state = AWE_ST_OFF;
3690                         
3691         }
3692 }
3693
3694
3695 /* search the best voice from the specified status condition */
3696 static int
3697 search_best_voice(int condition)
3698 {
3699         int i, time, best;
3700         best = -1;
3701         time = current_alloc_time + 1;
3702         for (i = 0; i < awe_max_voices; i++) {
3703                 if ((voices[i].state & condition) &&
3704                     (best < 0 || voices[i].time < time)) {
3705                         best = i;
3706                         time = voices[i].time;
3707                 }
3708         }
3709         /* clear voice */
3710         if (best >= 0) {
3711                 if (voices[best].state != AWE_ST_OFF)
3712                         awe_terminate(best);
3713                 awe_voice_init(best, TRUE);
3714         }
3715
3716         return best;
3717 }
3718
3719 /* search an empty voice.
3720    if no empty voice is found, at least terminate a voice
3721    */
3722 static int
3723 awe_clear_voice(void)
3724 {
3725         int best;
3726
3727         /* looking for the oldest empty voice */
3728         if ((best = search_best_voice(AWE_ST_OFF)) >= 0)
3729                 return best;
3730         if ((best = search_best_voice(AWE_ST_RELEASED)) >= 0)
3731                 return best;
3732         /* looking for the oldest sustained voice */
3733         if ((best = search_best_voice(AWE_ST_SUSTAINED)) >= 0)
3734                 return best;
3735
3736 #ifdef AWE_LOOKUP_MIDI_PRIORITY
3737         if (MULTI_LAYER_MODE() && misc_modes[AWE_MD_CHN_PRIOR]) {
3738                 int ch = -1;
3739                 int time = current_alloc_time + 1;
3740                 int i;
3741                 /* looking for the voices from high channel (except drum ch) */
3742                 for (i = 0; i < awe_max_voices; i++) {
3743                         if (IS_DRUM_CHANNEL(voices[i].ch)) continue;
3744                         if (voices[i].ch < ch) continue;
3745                         if (voices[i].state != AWE_ST_MARK &&
3746                             (voices[i].ch > ch || voices[i].time < time)) {
3747                                 best = i;
3748                                 time = voices[i].time;
3749                                 ch = voices[i].ch;
3750                         }
3751                 }
3752         }
3753 #endif
3754         if (best < 0)
3755                 best = search_best_voice(~AWE_ST_MARK);
3756
3757         if (best >= 0)
3758                 return best;
3759
3760         return 0;
3761 }
3762
3763
3764 /* search sample for the specified note & velocity and set it on the voice;
3765  * note that voice is the voice index (not channel index)
3766  */
3767 static void
3768 awe_alloc_one_voice(int voice, int note, int velocity)
3769 {
3770         int ch, nvoices;
3771         awe_voice_info *vlist[AWE_MAX_VOICES];
3772
3773         ch = voices[voice].ch;
3774         if (channels[ch].vrec < 0 && channels[ch].def_vrec < 0)
3775                 awe_set_instr(0, ch, channels[ch].instr);
3776
3777         nvoices = really_alloc_voices(voices[voice].cinfo->vrec,
3778                                       voices[voice].cinfo->def_vrec,
3779                                       &note, velocity, vlist, 0);
3780         if (nvoices > 0) {
3781                 voices[voice].time = ++current_alloc_time;
3782                 voices[voice].sample = vlist[0]; /* use the first one */
3783                 voices[voice].layer = 0;
3784                 voices[voice].note = note;
3785                 voices[voice].velocity = velocity;
3786         }
3787 }
3788
3789
3790 /*----------------------------------------------------------------
3791  * sequencer2 functions
3792  *----------------------------------------------------------------*/
3793
3794 /* search an empty voice; used by sequencer2 */
3795 static int
3796 awe_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc)
3797 {
3798         playing_mode = AWE_PLAY_MULTI2;
3799         awe_info.nr_voices = AWE_MAX_CHANNELS;
3800         return awe_clear_voice();
3801 }
3802
3803
3804 /* set up voice; used by sequencer2 */
3805 static void
3806 awe_setup_voice(int dev, int voice, int chn)
3807 {
3808         struct channel_info *info;
3809         if (synth_devs[dev] == NULL ||
3810             (info = &synth_devs[dev]->chn_info[chn]) == NULL)
3811                 return;
3812
3813         if (voice < 0 || voice >= awe_max_voices)
3814                 return;
3815
3816         AWE_DEBUG(2,printk("AWE32: [setup(%d) ch=%d]\n", voice, chn));
3817         channels[chn].expression_vol = info->controllers[CTL_EXPRESSION];
3818         channels[chn].main_vol = info->controllers[CTL_MAIN_VOLUME];
3819         channels[chn].panning =
3820                 info->controllers[CTL_PAN] * 2 - 128; /* signed 8bit */
3821         channels[chn].bender = info->bender_value; /* zero center */
3822         channels[chn].bank = info->controllers[CTL_BANK_SELECT];
3823         channels[chn].sustained = info->controllers[CTL_SUSTAIN];
3824         if (info->controllers[CTL_EXT_EFF_DEPTH]) {
3825                 FX_SET(&channels[chn].fx, AWE_FX_REVERB,
3826                        info->controllers[CTL_EXT_EFF_DEPTH] * 2);
3827         }
3828         if (info->controllers[CTL_CHORUS_DEPTH]) {
3829                 FX_SET(&channels[chn].fx, AWE_FX_CHORUS,
3830                        info->controllers[CTL_CHORUS_DEPTH] * 2);
3831         }
3832         awe_set_instr(dev, chn, info->pgm_num);
3833 }
3834
3835
3836 #ifdef CONFIG_AWE32_MIXER
3837 /*================================================================
3838  * AWE32 mixer device control
3839  *================================================================*/
3840
3841 static int
3842 awe_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg)
3843 {
3844         int i, level;
3845
3846         if (((cmd >> 8) & 0xff) != 'M')
3847                 return RET_ERROR(EINVAL);
3848
3849         level = (int)IOCTL_IN(arg);
3850         level = ((level & 0xff) + (level >> 8)) / 2;
3851         AWE_DEBUG(0,printk("AWEMix: cmd=%x val=%d\n", cmd & 0xff, level));
3852
3853         if (IO_WRITE_CHECK(cmd)) {
3854                 switch (cmd & 0xff) {
3855                 case SOUND_MIXER_BASS:
3856                         awe_bass_level = level * 12 / 100;
3857                         if (awe_bass_level >= 12)
3858                                 awe_bass_level = 11;
3859                         awe_equalizer(awe_bass_level, awe_treble_level);
3860                         break;
3861                 case SOUND_MIXER_TREBLE:
3862                         awe_treble_level = level * 12 / 100;
3863                         if (awe_treble_level >= 12)
3864                                 awe_treble_level = 11;
3865                         awe_equalizer(awe_bass_level, awe_treble_level);
3866                         break;
3867                 case SOUND_MIXER_VOLUME:
3868                         level = level * 127 / 100;
3869                         if (level >= 128) level = 127;
3870                         init_atten = vol_table[level];
3871                         for (i = 0; i < awe_max_voices; i++)
3872                                 awe_set_voice_vol(i, TRUE);
3873                         break;
3874                 }
3875         }
3876         switch (cmd & 0xff) {
3877         case SOUND_MIXER_BASS:
3878                 level = awe_bass_level * 100 / 24;
3879                 level = (level << 8) | level;
3880                 break;
3881         case SOUND_MIXER_TREBLE:
3882                 level = awe_treble_level * 100 / 24;
3883                 level = (level << 8) | level;
3884                 break;
3885         case SOUND_MIXER_VOLUME:
3886                 for (i = 127; i > 0; i--) {
3887                         if (init_atten <= vol_table[i])
3888                                 break;
3889                 }
3890                 level = i * 100 / 127;
3891                 level = (level << 8) | level;
3892                 break;
3893         case SOUND_MIXER_DEVMASK:
3894                 level = SOUND_MASK_BASS|SOUND_MASK_TREBLE|SOUND_MASK_VOLUME;
3895                 break;
3896         default:
3897                 level = 0;
3898                 break;
3899         }
3900         return IOCTL_OUT(arg, level);
3901 }
3902 #endif /* CONFIG_AWE32_MIXER */
3903
3904
3905 /*================================================================
3906  * initialization of AWE32
3907  *================================================================*/
3908
3909 /* intiailize audio channels */
3910 static void
3911 awe_init_audio(void)
3912 {
3913         int ch;
3914
3915         /* turn off envelope engines */
3916         for (ch = 0; ch < AWE_MAX_VOICES; ch++) {
3917                 awe_poke(AWE_DCYSUSV(ch), 0x80);
3918         }
3919   
3920         /* reset all other parameters to zero */
3921         for (ch = 0; ch < AWE_MAX_VOICES; ch++) {
3922                 awe_poke(AWE_ENVVOL(ch), 0);
3923                 awe_poke(AWE_ENVVAL(ch), 0);
3924                 awe_poke(AWE_DCYSUS(ch), 0);
3925                 awe_poke(AWE_ATKHLDV(ch), 0);
3926                 awe_poke(AWE_LFO1VAL(ch), 0);
3927                 awe_poke(AWE_ATKHLD(ch), 0);
3928                 awe_poke(AWE_LFO2VAL(ch), 0);
3929                 awe_poke(AWE_IP(ch), 0);
3930                 awe_poke(AWE_IFATN(ch), 0);
3931                 awe_poke(AWE_PEFE(ch), 0);
3932                 awe_poke(AWE_FMMOD(ch), 0);
3933                 awe_poke(AWE_TREMFRQ(ch), 0);
3934                 awe_poke(AWE_FM2FRQ2(ch), 0);
3935                 awe_poke_dw(AWE_PTRX(ch), 0);
3936                 awe_poke_dw(AWE_VTFT(ch), 0);
3937                 awe_poke_dw(AWE_PSST(ch), 0);
3938                 awe_poke_dw(AWE_CSL(ch), 0);
3939                 awe_poke_dw(AWE_CCCA(ch), 0);
3940         }
3941
3942         for (ch = 0; ch < AWE_MAX_VOICES; ch++) {
3943                 awe_poke_dw(AWE_CPF(ch), 0);
3944                 awe_poke_dw(AWE_CVCF(ch), 0);
3945         }
3946 }
3947
3948
3949 /* initialize DMA address */
3950 static void
3951 awe_init_dma(void)
3952 {
3953         awe_poke_dw(AWE_SMALR, 0);
3954         awe_poke_dw(AWE_SMARR, 0);
3955         awe_poke_dw(AWE_SMALW, 0);
3956         awe_poke_dw(AWE_SMARW, 0);
3957 }
3958
3959
3960 /* initialization arrays; from ADIP */
3961
3962 static unsigned short init1[128] = {
3963         0x03ff, 0x0030,  0x07ff, 0x0130, 0x0bff, 0x0230,  0x0fff, 0x0330,
3964         0x13ff, 0x0430,  0x17ff, 0x0530, 0x1bff, 0x0630,  0x1fff, 0x0730,
3965         0x23ff, 0x0830,  0x27ff, 0x0930, 0x2bff, 0x0a30,  0x2fff, 0x0b30,
3966         0x33ff, 0x0c30,  0x37ff, 0x0d30, 0x3bff, 0x0e30,  0x3fff, 0x0f30,
3967
3968         0x43ff, 0x0030,  0x47ff, 0x0130, 0x4bff, 0x0230,  0x4fff, 0x0330,
3969         0x53ff, 0x0430,  0x57ff, 0x0530, 0x5bff, 0x0630,  0x5fff, 0x0730,
3970         0x63ff, 0x0830,  0x67ff, 0x0930, 0x6bff, 0x0a30,  0x6fff, 0x0b30,
3971         0x73ff, 0x0c30,  0x77ff, 0x0d30, 0x7bff, 0x0e30,  0x7fff, 0x0f30,
3972
3973         0x83ff, 0x0030,  0x87ff, 0x0130, 0x8bff, 0x0230,  0x8fff, 0x0330,
3974         0x93ff, 0x0430,  0x97ff, 0x0530, 0x9bff, 0x0630,  0x9fff, 0x0730,
3975         0xa3ff, 0x0830,  0xa7ff, 0x0930, 0xabff, 0x0a30,  0xafff, 0x0b30,
3976         0xb3ff, 0x0c30,  0xb7ff, 0x0d30, 0xbbff, 0x0e30,  0xbfff, 0x0f30,
3977
3978         0xc3ff, 0x0030,  0xc7ff, 0x0130, 0xcbff, 0x0230,  0xcfff, 0x0330,
3979         0xd3ff, 0x0430,  0xd7ff, 0x0530, 0xdbff, 0x0630,  0xdfff, 0x0730,
3980         0xe3ff, 0x0830,  0xe7ff, 0x0930, 0xebff, 0x0a30,  0xefff, 0x0b30,
3981         0xf3ff, 0x0c30,  0xf7ff, 0x0d30, 0xfbff, 0x0e30,  0xffff, 0x0f30,
3982 };
3983
3984 static unsigned short init2[128] = {
3985         0x03ff, 0x8030, 0x07ff, 0x8130, 0x0bff, 0x8230, 0x0fff, 0x8330,
3986         0x13ff, 0x8430, 0x17ff, 0x8530, 0x1bff, 0x8630, 0x1fff, 0x8730,
3987         0x23ff, 0x8830, 0x27ff, 0x8930, 0x2bff, 0x8a30, 0x2fff, 0x8b30,
3988         0x33ff, 0x8c30, 0x37ff, 0x8d30, 0x3bff, 0x8e30, 0x3fff, 0x8f30,
3989
3990         0x43ff, 0x8030, 0x47ff, 0x8130, 0x4bff, 0x8230, 0x4fff, 0x8330,
3991         0x53ff, 0x8430, 0x57ff, 0x8530, 0x5bff, 0x8630, 0x5fff, 0x8730,
3992         0x63ff, 0x8830, 0x67ff, 0x8930, 0x6bff, 0x8a30, 0x6fff, 0x8b30,
3993         0x73ff, 0x8c30, 0x77ff, 0x8d30, 0x7bff, 0x8e30, 0x7fff, 0x8f30,
3994
3995         0x83ff, 0x8030, 0x87ff, 0x8130, 0x8bff, 0x8230, 0x8fff, 0x8330,
3996         0x93ff, 0x8430, 0x97ff, 0x8530, 0x9bff, 0x8630, 0x9fff, 0x8730,
3997         0xa3ff, 0x8830, 0xa7ff, 0x8930, 0xabff, 0x8a30, 0xafff, 0x8b30,
3998         0xb3ff, 0x8c30, 0xb7ff, 0x8d30, 0xbbff, 0x8e30, 0xbfff, 0x8f30,
3999
4000         0xc3ff, 0x8030, 0xc7ff, 0x8130, 0xcbff, 0x8230, 0xcfff, 0x8330,
4001         0xd3ff, 0x8430, 0xd7ff, 0x8530, 0xdbff, 0x8630, 0xdfff, 0x8730,
4002         0xe3ff, 0x8830, 0xe7ff, 0x8930, 0xebff, 0x8a30, 0xefff, 0x8b30,
4003         0xf3ff, 0x8c30, 0xf7ff, 0x8d30, 0xfbff, 0x8e30, 0xffff, 0x8f30,
4004 };
4005
4006 static unsigned short init3[128] = {
4007         0x0C10, 0x8470, 0x14FE, 0xB488, 0x167F, 0xA470, 0x18E7, 0x84B5,
4008         0x1B6E, 0x842A, 0x1F1D, 0x852A, 0x0DA3, 0x8F7C, 0x167E, 0xF254,
4009         0x0000, 0x842A, 0x0001, 0x852A, 0x18E6, 0x8BAA, 0x1B6D, 0xF234,
4010         0x229F, 0x8429, 0x2746, 0x8529, 0x1F1C, 0x86E7, 0x229E, 0xF224,
4011
4012         0x0DA4, 0x8429, 0x2C29, 0x8529, 0x2745, 0x87F6, 0x2C28, 0xF254,
4013         0x383B, 0x8428, 0x320F, 0x8528, 0x320E, 0x8F02, 0x1341, 0xF264,
4014         0x3EB6, 0x8428, 0x3EB9, 0x8528, 0x383A, 0x8FA9, 0x3EB5, 0xF294,
4015         0x3EB7, 0x8474, 0x3EBA, 0x8575, 0x3EB8, 0xC4C3, 0x3EBB, 0xC5C3,
4016
4017         0x0000, 0xA404, 0x0001, 0xA504, 0x141F, 0x8671, 0x14FD, 0x8287,
4018         0x3EBC, 0xE610, 0x3EC8, 0x8C7B, 0x031A, 0x87E6, 0x3EC8, 0x86F7,
4019         0x3EC0, 0x821E, 0x3EBE, 0xD208, 0x3EBD, 0x821F, 0x3ECA, 0x8386,
4020         0x3EC1, 0x8C03, 0x3EC9, 0x831E, 0x3ECA, 0x8C4C, 0x3EBF, 0x8C55,
4021
4022         0x3EC9, 0xC208, 0x3EC4, 0xBC84, 0x3EC8, 0x8EAD, 0x3EC8, 0xD308,
4023         0x3EC2, 0x8F7E, 0x3ECB, 0x8219, 0x3ECB, 0xD26E, 0x3EC5, 0x831F,
4024         0x3EC6, 0xC308, 0x3EC3, 0xB2FF, 0x3EC9, 0x8265, 0x3EC9, 0x8319,
4025         0x1342, 0xD36E, 0x3EC7, 0xB3FF, 0x0000, 0x8365, 0x1420, 0x9570,
4026 };
4027
4028 static unsigned short init4[128] = {
4029         0x0C10, 0x8470, 0x14FE, 0xB488, 0x167F, 0xA470, 0x18E7, 0x84B5,
4030         0x1B6E, 0x842A, 0x1F1D, 0x852A, 0x0DA3, 0x0F7C, 0x167E, 0x7254,
4031         0x0000, 0x842A, 0x0001, 0x852A, 0x18E6, 0x0BAA, 0x1B6D, 0x7234,
4032         0x229F, 0x8429, 0x2746, 0x8529, 0x1F1C, 0x06E7, 0x229E, 0x7224,
4033
4034         0x0DA4, 0x8429, 0x2C29, 0x8529, 0x2745, 0x07F6, 0x2C28, 0x7254,
4035         0x383B, 0x8428, 0x320F, 0x8528, 0x320E, 0x0F02, 0x1341, 0x7264,
4036         0x3EB6, 0x8428, 0x3EB9, 0x8528, 0x383A, 0x0FA9, 0x3EB5, 0x7294,
4037         0x3EB7, 0x8474, 0x3EBA, 0x8575, 0x3EB8, 0x44C3, 0x3EBB, 0x45C3,
4038
4039         0x0000, 0xA404, 0x0001, 0xA504, 0x141F, 0x0671, 0x14FD, 0x0287,
4040         0x3EBC, 0xE610, 0x3EC8, 0x0C7B, 0x031A, 0x07E6, 0x3EC8, 0x86F7,
4041         0x3EC0, 0x821E, 0x3EBE, 0xD208, 0x3EBD, 0x021F, 0x3ECA, 0x0386,
4042         0x3EC1, 0x0C03, 0x3EC9, 0x031E, 0x3ECA, 0x8C4C, 0x3EBF, 0x0C55,
4043
4044         0x3EC9, 0xC208, 0x3EC4, 0xBC84, 0x3EC8, 0x0EAD, 0x3EC8, 0xD308,
4045         0x3EC2, 0x8F7E, 0x3ECB, 0x0219, 0x3ECB, 0xD26E, 0x3EC5, 0x031F,
4046         0x3EC6, 0xC308, 0x3EC3, 0x32FF, 0x3EC9, 0x0265, 0x3EC9, 0x8319,
4047         0x1342, 0xD36E, 0x3EC7, 0x33FF, 0x0000, 0x8365, 0x1420, 0x9570,
4048 };
4049
4050
4051 /* send initialization arrays to start up */
4052 static void
4053 awe_init_array(void)
4054 {
4055         awe_send_array(init1);
4056         awe_wait(1024);
4057         awe_send_array(init2);
4058         awe_send_array(init3);
4059         awe_poke_dw(AWE_HWCF4, 0);
4060         awe_poke_dw(AWE_HWCF5, 0x83);
4061         awe_poke_dw(AWE_HWCF6, 0x8000);
4062         awe_send_array(init4);
4063 }
4064
4065 /* send an initialization array */
4066 static void
4067 awe_send_array(unsigned short *data)
4068 {
4069         int i;
4070         unsigned short *p;
4071
4072         p = data;
4073         for (i = 0; i < AWE_MAX_VOICES; i++, p++)
4074                 awe_poke(AWE_INIT1(i), *p);
4075         for (i = 0; i < AWE_MAX_VOICES; i++, p++)
4076                 awe_poke(AWE_INIT2(i), *p);
4077         for (i = 0; i < AWE_MAX_VOICES; i++, p++)
4078                 awe_poke(AWE_INIT3(i), *p);
4079         for (i = 0; i < AWE_MAX_VOICES; i++, p++)
4080                 awe_poke(AWE_INIT4(i), *p);
4081 }
4082
4083
4084 /*
4085  * set up awe32 channels to some known state.
4086  */
4087
4088 /* set the envelope & LFO parameters to the default values; see ADIP */
4089 static void
4090 awe_tweak_voice(int i)
4091 {
4092         /* set all mod/vol envelope shape to minimum */
4093         awe_poke(AWE_ENVVOL(i), 0x8000);
4094         awe_poke(AWE_ENVVAL(i), 0x8000);
4095         awe_poke(AWE_DCYSUS(i), 0x7F7F);
4096         awe_poke(AWE_ATKHLDV(i), 0x7F7F);
4097         awe_poke(AWE_ATKHLD(i), 0x7F7F);
4098         awe_poke(AWE_PEFE(i), 0);  /* mod envelope height to zero */
4099         awe_poke(AWE_LFO1VAL(i), 0x8000); /* no delay for LFO1 */
4100         awe_poke(AWE_LFO2VAL(i), 0x8000);
4101         awe_poke(AWE_IP(i), 0xE000);    /* no pitch shift */
4102         awe_poke(AWE_IFATN(i), 0xFF00); /* volume to minimum */
4103         awe_poke(AWE_FMMOD(i), 0);
4104         awe_poke(AWE_TREMFRQ(i), 0);
4105         awe_poke(AWE_FM2FRQ2(i), 0);
4106 }
4107
4108 static void
4109 awe_tweak(void)
4110 {
4111         int i;
4112         /* reset all channels */
4113         for (i = 0; i < awe_max_voices; i++)
4114                 awe_tweak_voice(i);
4115 }
4116
4117
4118 /*
4119  *  initializes the FM section of AWE32;
4120  *   see Vince Vu's unofficial AWE32 programming guide
4121  */
4122
4123 static void
4124 awe_init_fm(void)
4125 {
4126 #ifndef AWE_ALWAYS_INIT_FM
4127         /* if no extended memory is on board.. */
4128         if (awe_mem_size <= 0)
4129                 return;
4130 #endif
4131         AWE_DEBUG(3,printk("AWE32: initializing FM\n"));
4132
4133         /* Initialize the last two channels for DRAM refresh and producing
4134            the reverb and chorus effects for Yamaha OPL-3 synthesizer */
4135
4136         /* 31: FM left channel, 0xffffe0-0xffffe8 */
4137         awe_poke(AWE_DCYSUSV(30), 0x80);
4138         awe_poke_dw(AWE_PSST(30), 0xFFFFFFE0); /* full left */
4139         awe_poke_dw(AWE_CSL(30), 0x00FFFFE8 |
4140                     (DEF_FM_CHORUS_DEPTH << 24));
4141         awe_poke_dw(AWE_PTRX(30), (DEF_FM_REVERB_DEPTH << 8));
4142         awe_poke_dw(AWE_CPF(30), 0);
4143         awe_poke_dw(AWE_CCCA(30), 0x00FFFFE3);
4144
4145         /* 32: FM right channel, 0xfffff0-0xfffff8 */
4146         awe_poke(AWE_DCYSUSV(31), 0x80);
4147         awe_poke_dw(AWE_PSST(31), 0x00FFFFF0); /* full right */
4148         awe_poke_dw(AWE_CSL(31), 0x00FFFFF8 |
4149                     (DEF_FM_CHORUS_DEPTH << 24));
4150         awe_poke_dw(AWE_PTRX(31), (DEF_FM_REVERB_DEPTH << 8));
4151         awe_poke_dw(AWE_CPF(31), 0x8000);
4152         awe_poke_dw(AWE_CCCA(31), 0x00FFFFF3);
4153
4154         /* skew volume & cutoff */
4155         awe_poke_dw(AWE_VTFT(30), 0x8000FFFF);
4156         awe_poke_dw(AWE_VTFT(31), 0x8000FFFF);
4157
4158         voices[30].state = AWE_ST_FM;
4159         voices[31].state = AWE_ST_FM;
4160
4161         /* change maximum channels to 30 */
4162         awe_max_voices = AWE_NORMAL_VOICES;
4163         if (playing_mode == AWE_PLAY_DIRECT)
4164                 awe_info.nr_voices = awe_max_voices;
4165         else
4166                 awe_info.nr_voices = AWE_MAX_CHANNELS;
4167         voice_alloc->max_voice = awe_max_voices;
4168 }
4169
4170 /*
4171  *  AWE32 DRAM access routines
4172  */
4173
4174 /* open DRAM write accessing mode */
4175 static int
4176 awe_open_dram_for_write(int offset, int channels)
4177 {
4178         int vidx[AWE_NORMAL_VOICES];
4179         int i;
4180
4181         if (channels < 0 || channels >= AWE_NORMAL_VOICES) {
4182                 channels = AWE_NORMAL_VOICES;
4183                 for (i = 0; i < AWE_NORMAL_VOICES; i++)
4184                         vidx[i] = i;
4185         } else {
4186                 for (i = 0; i < channels; i++)
4187                         vidx[i] = awe_clear_voice();
4188         }
4189
4190         /* use all channels for DMA transfer */
4191         for (i = 0; i < channels; i++) {
4192                 if (vidx[i] < 0) continue;
4193                 awe_poke(AWE_DCYSUSV(vidx[i]), 0x80);
4194                 awe_poke_dw(AWE_VTFT(vidx[i]), 0);
4195                 awe_poke_dw(AWE_CVCF(vidx[i]), 0);
4196                 awe_poke_dw(AWE_PTRX(vidx[i]), 0x40000000);
4197                 awe_poke_dw(AWE_CPF(vidx[i]), 0x40000000);
4198                 awe_poke_dw(AWE_PSST(vidx[i]), 0);
4199                 awe_poke_dw(AWE_CSL(vidx[i]), 0);
4200                 awe_poke_dw(AWE_CCCA(vidx[i]), 0x06000000);
4201                 voices[vidx[i]].state = AWE_ST_DRAM;
4202         }
4203         /* point channels 31 & 32 to ROM samples for DRAM refresh */
4204         awe_poke_dw(AWE_VTFT(30), 0);
4205         awe_poke_dw(AWE_PSST(30), 0x1d8);
4206         awe_poke_dw(AWE_CSL(30), 0x1e0);
4207         awe_poke_dw(AWE_CCCA(30), 0x1d8);
4208         awe_poke_dw(AWE_VTFT(31), 0);
4209         awe_poke_dw(AWE_PSST(31), 0x1d8);
4210         awe_poke_dw(AWE_CSL(31), 0x1e0);
4211         awe_poke_dw(AWE_CCCA(31), 0x1d8);
4212         voices[30].state = AWE_ST_FM;
4213         voices[31].state = AWE_ST_FM;
4214
4215         /* if full bit is on, not ready to write on */
4216         if (awe_peek_dw(AWE_SMALW) & 0x80000000) {
4217                 for (i = 0; i < channels; i++) {
4218                         awe_poke_dw(AWE_CCCA(vidx[i]), 0);
4219                         voices[i].state = AWE_ST_OFF;
4220                 }
4221                 return RET_ERROR(ENOSPC);
4222         }
4223
4224         /* set address to write */
4225         awe_poke_dw(AWE_SMALW, offset);
4226
4227         return 0;
4228 }
4229
4230 /* open DRAM for RAM size detection */
4231 static void
4232 awe_open_dram_for_check(void)
4233 {
4234         int i;
4235         for (i = 0; i < AWE_NORMAL_VOICES; i++) {
4236                 awe_poke(AWE_DCYSUSV(i), 0x80);
4237                 awe_poke_dw(AWE_VTFT(i), 0);
4238                 awe_poke_dw(AWE_CVCF(i), 0);
4239                 awe_poke_dw(AWE_PTRX(i), 0x40000000);
4240                 awe_poke_dw(AWE_CPF(i), 0x40000000);
4241                 awe_poke_dw(AWE_PSST(i), 0);
4242                 awe_poke_dw(AWE_CSL(i), 0);
4243                 if (i & 1) /* DMA write */
4244                         awe_poke_dw(AWE_CCCA(i), 0x06000000);
4245                 else       /* DMA read */
4246                         awe_poke_dw(AWE_CCCA(i), 0x04000000);
4247                 voices[i].state = AWE_ST_DRAM;
4248         }
4249 }
4250
4251
4252 /* close dram access */
4253 static void
4254 awe_close_dram(void)
4255 {
4256         int i;
4257         /* wait until FULL bit in SMAxW register be false */
4258         for (i = 0; i < 10000; i++) {
4259                 if (!(awe_peek_dw(AWE_SMALW) & 0x80000000))
4260                         break;
4261                 awe_wait(10);
4262         }
4263
4264         for (i = 0; i < AWE_NORMAL_VOICES; i++) {
4265                 if (voices[i].state == AWE_ST_DRAM) {
4266                         awe_poke_dw(AWE_CCCA(i), 0);
4267                         awe_poke(AWE_DCYSUSV(i), 0x807F);
4268                         voices[i].state = AWE_ST_OFF;
4269                 }
4270         }
4271 }
4272
4273
4274 /*================================================================
4275  * detect presence of AWE32 and check memory size
4276  *================================================================*/
4277
4278 /* detect emu8000 chip on the specified address; from VV's guide */
4279
4280 static int
4281 awe_detect_base(int addr)
4282 {
4283         awe_base = addr;
4284         if ((awe_peek(AWE_U1) & 0x000F) != 0x000C)
4285                 return 0;
4286         if ((awe_peek(AWE_HWCF1) & 0x007E) != 0x0058)
4287                 return 0;
4288         if ((awe_peek(AWE_HWCF2) & 0x0003) != 0x0003)
4289                 return 0;
4290         AWE_DEBUG(0,printk("AWE32 found at %x\n", awe_base));
4291         return 1;
4292 }
4293         
4294 static int
4295 awe_detect(void)
4296 {
4297         int base;
4298         if (awe_base == 0) {
4299                 for (base = 0x620; base <= 0x680; base += 0x20)
4300                         if (awe_detect_base(base))
4301                                 return 1;
4302                 AWE_DEBUG(0,printk("AWE32 not found\n"));
4303                 return 0;
4304         }
4305         return 1;
4306 }
4307
4308
4309 /*================================================================
4310  * check dram size on AWE board
4311  *================================================================*/
4312
4313 /* any three numbers you like */
4314 #define UNIQUE_ID1      0x1234
4315 #define UNIQUE_ID2      0x4321
4316 #define UNIQUE_ID3      0xFFFF
4317
4318 static int
4319 awe_check_dram(void)
4320 {
4321         if (awe_mem_size > 0) {
4322                 awe_mem_size *= 1024; /* convert to Kbytes */
4323                 return awe_mem_size;
4324         }
4325
4326         awe_open_dram_for_check();
4327
4328         awe_mem_size = 0;
4329
4330         /* set up unique two id numbers */
4331         awe_poke_dw(AWE_SMALW, AWE_DRAM_OFFSET);
4332         awe_poke(AWE_SMLD, UNIQUE_ID1);
4333         awe_poke(AWE_SMLD, UNIQUE_ID2);
4334
4335         while (awe_mem_size < AWE_MAX_DRAM_SIZE) {
4336                 awe_wait(2);
4337                 /* read a data on the DRAM start address */
4338                 awe_poke_dw(AWE_SMALR, AWE_DRAM_OFFSET);
4339                 awe_peek(AWE_SMLD); /* discard stale data  */
4340                 if (awe_peek(AWE_SMLD) != UNIQUE_ID1)
4341                         break;
4342                 if (awe_peek(AWE_SMLD) != UNIQUE_ID2)
4343                         break;
4344                 awe_mem_size += 32;  /* increment 32 Kbytes */
4345                 /* Write a unique data on the test address;
4346                  * if the address is out of range, the data is written on
4347                  * 0x200000(=AWE_DRAM_OFFSET).  Then the two id words are
4348                  * broken by this data.
4349                  */
4350                 awe_poke_dw(AWE_SMALW, AWE_DRAM_OFFSET + awe_mem_size*512L);
4351                 awe_poke(AWE_SMLD, UNIQUE_ID3);
4352                 awe_wait(2);
4353                 /* read a data on the just written DRAM address */
4354                 awe_poke_dw(AWE_SMALR, AWE_DRAM_OFFSET + awe_mem_size*512L);
4355                 awe_peek(AWE_SMLD); /* discard stale data  */
4356                 if (awe_peek(AWE_SMLD) != UNIQUE_ID3)
4357                         break;
4358         }
4359         awe_close_dram();
4360
4361         AWE_DEBUG(0,printk("AWE32: %d Kbytes memory detected\n", awe_mem_size));
4362
4363         /* convert to Kbytes */
4364         awe_mem_size *= 1024;
4365         return awe_mem_size;
4366 }
4367
4368
4369 /*================================================================
4370  * chorus and reverb controls; from VV's guide
4371  *================================================================*/
4372
4373 /* 5 parameters for each chorus mode; 3 x 16bit, 2 x 32bit */
4374 static char chorus_defined[AWE_CHORUS_NUMBERS];
4375 static awe_chorus_fx_rec chorus_parm[AWE_CHORUS_NUMBERS] = {
4376         {0xE600, 0x03F6, 0xBC2C ,0x00000000, 0x0000006D}, /* chorus 1 */
4377         {0xE608, 0x031A, 0xBC6E, 0x00000000, 0x0000017C}, /* chorus 2 */
4378         {0xE610, 0x031A, 0xBC84, 0x00000000, 0x00000083}, /* chorus 3 */
4379         {0xE620, 0x0269, 0xBC6E, 0x00000000, 0x0000017C}, /* chorus 4 */
4380         {0xE680, 0x04D3, 0xBCA6, 0x00000000, 0x0000005B}, /* feedback */
4381         {0xE6E0, 0x044E, 0xBC37, 0x00000000, 0x00000026}, /* flanger */
4382         {0xE600, 0x0B06, 0xBC00, 0x0000E000, 0x00000083}, /* short delay */
4383         {0xE6C0, 0x0B06, 0xBC00, 0x0000E000, 0x00000083}, /* short delay + feedback */
4384 };
4385
4386 static int
4387 awe_load_chorus_fx(awe_patch_info *patch, const char *addr, int count)
4388 {
4389         if (patch->optarg < AWE_CHORUS_PREDEFINED || patch->optarg >= AWE_CHORUS_NUMBERS) {
4390                 printk("AWE32 Error: illegal chorus mode %d for uploading\n", patch->optarg);
4391                 return RET_ERROR(EINVAL);
4392         }
4393         if (count < sizeof(awe_chorus_fx_rec)) {
4394                 printk("AWE32 Error: too short chorus fx parameters\n");
4395                 return RET_ERROR(EINVAL);
4396         }
4397         COPY_FROM_USER(&chorus_parm[patch->optarg], addr, AWE_PATCH_INFO_SIZE,
4398                        sizeof(awe_chorus_fx_rec));
4399         chorus_defined[patch->optarg] = TRUE;
4400         return 0;
4401 }
4402
4403 static void
4404 awe_set_chorus_mode(int effect)
4405 {
4406         if (effect < 0 || effect >= AWE_CHORUS_NUMBERS ||
4407             (effect >= AWE_CHORUS_PREDEFINED && !chorus_defined[effect]))
4408                 return;
4409         awe_poke(AWE_INIT3(9), chorus_parm[effect].feedback);
4410         awe_poke(AWE_INIT3(12), chorus_parm[effect].delay_offset);
4411         awe_poke(AWE_INIT4(3), chorus_parm[effect].lfo_depth);
4412         awe_poke_dw(AWE_HWCF4, chorus_parm[effect].delay);
4413         awe_poke_dw(AWE_HWCF5, chorus_parm[effect].lfo_freq);
4414         awe_poke_dw(AWE_HWCF6, 0x8000);
4415         awe_poke_dw(AWE_HWCF7, 0x0000);
4416         chorus_mode = effect;
4417 }
4418
4419 /*----------------------------------------------------------------*/
4420
4421 /* reverb mode settings; write the following 28 data of 16 bit length
4422  *   on the corresponding ports in the reverb_cmds array
4423  */
4424 static char reverb_defined[AWE_CHORUS_NUMBERS];
4425 static awe_reverb_fx_rec reverb_parm[AWE_REVERB_NUMBERS] = {
4426 {{  /* room 1 */
4427         0xB488, 0xA450, 0x9550, 0x84B5, 0x383A, 0x3EB5, 0x72F4,
4428         0x72A4, 0x7254, 0x7204, 0x7204, 0x7204, 0x4416, 0x4516,
4429         0xA490, 0xA590, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
4430         0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
4431 }},
4432 {{  /* room 2 */
4433         0xB488, 0xA458, 0x9558, 0x84B5, 0x383A, 0x3EB5, 0x7284,
4434         0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4448, 0x4548,
4435         0xA440, 0xA540, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
4436         0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
4437 }},
4438 {{  /* room 3 */
4439         0xB488, 0xA460, 0x9560, 0x84B5, 0x383A, 0x3EB5, 0x7284,
4440         0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4416, 0x4516,
4441         0xA490, 0xA590, 0x842C, 0x852C, 0x842C, 0x852C, 0x842B,
4442         0x852B, 0x842B, 0x852B, 0x842A, 0x852A, 0x842A, 0x852A,
4443 }},
4444 {{  /* hall 1 */
4445         0xB488, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7284,
4446         0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4448, 0x4548,
4447         0xA440, 0xA540, 0x842B, 0x852B, 0x842B, 0x852B, 0x842A,
4448         0x852A, 0x842A, 0x852A, 0x8429, 0x8529, 0x8429, 0x8529,
4449 }},
4450 {{  /* hall 2 */
4451         0xB488, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7254,
4452         0x7234, 0x7224, 0x7254, 0x7264, 0x7294, 0x44C3, 0x45C3,
4453         0xA404, 0xA504, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
4454         0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
4455 }},
4456 {{  /* plate */
4457         0xB4FF, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7234,
4458         0x7234, 0x7234, 0x7234, 0x7234, 0x7234, 0x4448, 0x4548,
4459         0xA440, 0xA540, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
4460         0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
4461 }},
4462 {{  /* delay */
4463         0xB4FF, 0xA470, 0x9500, 0x84B5, 0x333A, 0x39B5, 0x7204,
4464         0x7204, 0x7204, 0x7204, 0x7204, 0x72F4, 0x4400, 0x4500,
4465         0xA4FF, 0xA5FF, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420,
4466         0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520,
4467 }},
4468 {{  /* panning delay */
4469         0xB4FF, 0xA490, 0x9590, 0x8474, 0x333A, 0x39B5, 0x7204,
4470         0x7204, 0x7204, 0x7204, 0x7204, 0x72F4, 0x4400, 0x4500,
4471         0xA4FF, 0xA5FF, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420,
4472         0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520,
4473 }},
4474 };
4475
4476 static struct ReverbCmdPair {
4477         unsigned short cmd, port;
4478 } reverb_cmds[28] = {
4479   {AWE_INIT1(0x03)}, {AWE_INIT1(0x05)}, {AWE_INIT4(0x1F)}, {AWE_INIT1(0x07)},
4480   {AWE_INIT2(0x14)}, {AWE_INIT2(0x16)}, {AWE_INIT1(0x0F)}, {AWE_INIT1(0x17)},
4481   {AWE_INIT1(0x1F)}, {AWE_INIT2(0x07)}, {AWE_INIT2(0x0F)}, {AWE_INIT2(0x17)},
4482   {AWE_INIT2(0x1D)}, {AWE_INIT2(0x1F)}, {AWE_INIT3(0x01)}, {AWE_INIT3(0x03)},
4483   {AWE_INIT1(0x09)}, {AWE_INIT1(0x0B)}, {AWE_INIT1(0x11)}, {AWE_INIT1(0x13)},
4484   {AWE_INIT1(0x19)}, {AWE_INIT1(0x1B)}, {AWE_INIT2(0x01)}, {AWE_INIT2(0x03)},
4485   {AWE_INIT2(0x09)}, {AWE_INIT2(0x0B)}, {AWE_INIT2(0x11)}, {AWE_INIT2(0x13)},
4486 };
4487
4488 static int
4489 awe_load_reverb_fx(awe_patch_info *patch, const char *addr, int count)
4490 {
4491         if (patch->optarg < AWE_REVERB_PREDEFINED || patch->optarg >= AWE_REVERB_NUMBERS) {
4492                 printk("AWE32 Error: illegal reverb mode %d for uploading\n", patch->optarg);
4493                 return RET_ERROR(EINVAL);
4494         }
4495         if (count < sizeof(awe_reverb_fx_rec)) {
4496                 printk("AWE32 Error: too short reverb fx parameters\n");
4497                 return RET_ERROR(EINVAL);
4498         }
4499         COPY_FROM_USER(&reverb_parm[patch->optarg], addr, AWE_PATCH_INFO_SIZE,
4500                        sizeof(awe_reverb_fx_rec));
4501         reverb_defined[patch->optarg] = TRUE;
4502         return 0;
4503 }
4504
4505 static void
4506 awe_set_reverb_mode(int effect)
4507 {
4508         int i;
4509         if (effect < 0 || effect >= AWE_REVERB_NUMBERS ||
4510             (effect >= AWE_REVERB_PREDEFINED && !reverb_defined[effect]))
4511                 return;
4512         for (i = 0; i < 28; i++)
4513                 awe_poke(reverb_cmds[i].cmd, reverb_cmds[i].port,
4514                          reverb_parm[effect].parms[i]);
4515         reverb_mode = effect;
4516 }
4517
4518 /*================================================================
4519  * treble/bass equalizer control
4520  *================================================================*/
4521
4522 static unsigned short bass_parm[12][3] = {
4523         {0xD26A, 0xD36A, 0x0000}, /* -12 dB */
4524         {0xD25B, 0xD35B, 0x0000}, /*  -8 */
4525         {0xD24C, 0xD34C, 0x0000}, /*  -6 */
4526         {0xD23D, 0xD33D, 0x0000}, /*  -4 */
4527         {0xD21F, 0xD31F, 0x0000}, /*  -2 */
4528         {0xC208, 0xC308, 0x0001}, /*   0 (HW default) */
4529         {0xC219, 0xC319, 0x0001}, /*  +2 */
4530         {0xC22A, 0xC32A, 0x0001}, /*  +4 */
4531         {0xC24C, 0xC34C, 0x0001}, /*  +6 */
4532         {0xC26E, 0xC36E, 0x0001}, /*  +8 */
4533         {0xC248, 0xC348, 0x0002}, /* +10 */
4534         {0xC26A, 0xC36A, 0x0002}, /* +12 dB */
4535 };
4536
4537 static unsigned short treble_parm[12][9] = {
4538         {0x821E, 0xC26A, 0x031E, 0xC36A, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001}, /* -12 dB */
4539         {0x821E, 0xC25B, 0x031E, 0xC35B, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
4540         {0x821E, 0xC24C, 0x031E, 0xC34C, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
4541         {0x821E, 0xC23D, 0x031E, 0xC33D, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
4542         {0x821E, 0xC21F, 0x031E, 0xC31F, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
4543         {0x821E, 0xD208, 0x031E, 0xD308, 0x021E, 0xD208, 0x831E, 0xD308, 0x0002},
4544         {0x821E, 0xD208, 0x031E, 0xD308, 0x021D, 0xD219, 0x831D, 0xD319, 0x0002},
4545         {0x821E, 0xD208, 0x031E, 0xD308, 0x021C, 0xD22A, 0x831C, 0xD32A, 0x0002},
4546         {0x821E, 0xD208, 0x031E, 0xD308, 0x021A, 0xD24C, 0x831A, 0xD34C, 0x0002},
4547         {0x821E, 0xD208, 0x031E, 0xD308, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002}, /* +8 (HW default) */
4548         {0x821D, 0xD219, 0x031D, 0xD319, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002},
4549         {0x821C, 0xD22A, 0x031C, 0xD32A, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002}, /* +12 dB */
4550 };
4551
4552
4553 /*
4554  * set Emu8000 digital equalizer; from 0 to 11 [-12dB - 12dB]
4555  */
4556 static void
4557 awe_equalizer(int bass, int treble)
4558 {
4559         unsigned short w;
4560
4561         if (bass < 0 || bass > 11 || treble < 0 || treble > 11)
4562                 return;
4563         awe_bass_level = bass;
4564         awe_treble_level = treble;
4565         awe_poke(AWE_INIT4(0x01), bass_parm[bass][0]);
4566         awe_poke(AWE_INIT4(0x11), bass_parm[bass][1]);
4567         awe_poke(AWE_INIT3(0x11), treble_parm[treble][0]);
4568         awe_poke(AWE_INIT3(0x13), treble_parm[treble][1]);
4569         awe_poke(AWE_INIT3(0x1B), treble_parm[treble][2]);
4570         awe_poke(AWE_INIT4(0x07), treble_parm[treble][3]);
4571         awe_poke(AWE_INIT4(0x0B), treble_parm[treble][4]);
4572         awe_poke(AWE_INIT4(0x0D), treble_parm[treble][5]);
4573         awe_poke(AWE_INIT4(0x17), treble_parm[treble][6]);
4574         awe_poke(AWE_INIT4(0x19), treble_parm[treble][7]);
4575         w = bass_parm[bass][2] + treble_parm[treble][8];
4576         awe_poke(AWE_INIT4(0x15), (unsigned short)(w + 0x0262));
4577         awe_poke(AWE_INIT4(0x1D), (unsigned short)(w + 0x8362));
4578 }
4579
4580
4581 #endif /* CONFIG_AWE32_SYNTH */