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