1 /*
2  * sound/awe_wave.c
3  *
4  * The low level driver for the AWE32/SB32/AWE64 wave table synth.
5  *   version 0.4.4; Jan. 4, 2000
6  *
7  * Copyright (C) 1996-2000 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 
24 #include <linux/awe_voice.h>
25 #include <linux/config.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/string.h>
29 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
30 #include <linux/isapnp.h>
31 #endif
32 
33 #include "sound_config.h"
34 
35 #include "awe_wave.h"
36 #include "awe_hw.h"
37 
38 #ifdef AWE_HAS_GUS_COMPATIBILITY
39 #include "tuning.h"
40 #include <linux/ultrasound.h>
41 #endif
42 
43 /*
44  * debug message
45  */
46 
47 #ifdef AWE_DEBUG_ON
48 #define DEBUG(LVL,XXX)	{if (ctrls[AWE_MD_DEBUG_MODE] > LVL) { XXX; }}
49 #define ERRMSG(XXX)	{if (ctrls[AWE_MD_DEBUG_MODE]) { XXX; }}
50 #define FATALERR(XXX)	XXX
51 #else
52 #define DEBUG(LVL,XXX) /**/
53 #define ERRMSG(XXX)	XXX
54 #define FATALERR(XXX)	XXX
55 #endif
56 
57 /*
58  * bank and voice record
59  */
60 
61 typedef struct _sf_list sf_list;
62 typedef struct _awe_voice_list awe_voice_list;
63 typedef struct _awe_sample_list awe_sample_list;
64 
65 /* soundfont record */
66 struct _sf_list {
67 	unsigned short sf_id;	/* id number */
68 	unsigned short type;	/* lock & shared flags */
69 	int num_info;		/* current info table index */
70 	int num_sample;		/* current sample table index */
71 	int mem_ptr;		/* current word byte pointer */
72 	awe_voice_list *infos, *last_infos;	/* instruments */
73 	awe_sample_list *samples, *last_samples;	/* samples */
74 #ifdef AWE_ALLOW_SAMPLE_SHARING
75 	sf_list *shared;	/* shared list */
76 	unsigned char name[AWE_PATCH_NAME_LEN];	/* sharing id */
77 #endif
78 	sf_list *next, *prev;
79 };
80 
81 /* instrument list */
82 struct _awe_voice_list {
83 	awe_voice_info v;	/* instrument information */
84 	sf_list *holder;	/* parent sf_list of this record */
85 	unsigned char bank, instr;	/* preset number information */
86 	char type, disabled;	/* type=normal/mapped, disabled=boolean */
87 	awe_voice_list *next;	/* linked list with same sf_id */
88 	awe_voice_list *next_instr;	/* instrument list */
89 	awe_voice_list *next_bank;	/* hash table list */
90 };
91 
92 /* voice list type */
93 #define V_ST_NORMAL	0
94 #define V_ST_MAPPED	1
95 
96 /* sample list */
97 struct _awe_sample_list {
98 	awe_sample_info v;	/* sample information */
99 	sf_list *holder;	/* parent sf_list of this record */
100 	awe_sample_list *next;	/* linked list with same sf_id */
101 };
102 
103 /* sample and information table */
104 static int current_sf_id = 0;	/* current number of fonts */
105 static int locked_sf_id = 0;	/* locked position */
106 static sf_list *sfhead = NULL, *sftail = NULL;	/* linked-lists */
107 
108 #define awe_free_mem_ptr() (sftail ? sftail->mem_ptr : 0)
109 #define awe_free_info() (sftail ? sftail->num_info : 0)
110 #define awe_free_sample() (sftail ? sftail->num_sample : 0)
111 
112 #define AWE_MAX_PRESETS		256
113 #define AWE_DEFAULT_PRESET	0
114 #define AWE_DEFAULT_BANK	0
115 #define AWE_DEFAULT_DRUM	0
116 #define AWE_DRUM_BANK		128
117 
118 #define MAX_LAYERS	AWE_MAX_VOICES
119 
120 /* preset table index */
121 static awe_voice_list *preset_table[AWE_MAX_PRESETS];
122 
123 /*
124  * voice table
125  */
126 
127 /* effects table */
128 typedef	struct FX_Rec { /* channel effects */
129 	unsigned char flags[AWE_FX_END];
130 	short val[AWE_FX_END];
131 } FX_Rec;
132 
133 
134 /* channel parameters */
135 typedef struct _awe_chan_info {
136 	int channel;		/* channel number */
137 	int bank;		/* current tone bank */
138 	int instr;		/* current program */
139 	int bender;		/* midi pitchbend (-8192 - 8192) */
140 	int bender_range;	/* midi bender range (x100) */
141 	int panning;		/* panning (0-127) */
142 	int main_vol;		/* channel volume (0-127) */
143 	int expression_vol;	/* midi expression (0-127) */
144 	int chan_press;		/* channel pressure */
145 	int sustained;		/* sustain status in MIDI */
146 	FX_Rec fx;		/* effects */
147 	FX_Rec fx_layer[MAX_LAYERS]; /* layer effects */
148 } awe_chan_info;
149 
150 /* voice parameters */
151 typedef struct _voice_info {
152 	int state;
153 #define AWE_ST_OFF		(1<<0)	/* no sound */
154 #define AWE_ST_ON		(1<<1)	/* playing */
155 #define AWE_ST_STANDBY		(1<<2)	/* stand by for playing */
156 #define AWE_ST_SUSTAINED	(1<<3)	/* sustained */
157 #define AWE_ST_MARK		(1<<4)	/* marked for allocation */
158 #define AWE_ST_DRAM		(1<<5)	/* DRAM read/write */
159 #define AWE_ST_FM		(1<<6)	/* reserved for FM */
160 #define AWE_ST_RELEASED		(1<<7)	/* released */
161 
162 	int ch;			/* midi channel */
163 	int key;		/* internal key for search */
164 	int layer;		/* layer number (for channel mode only) */
165 	int time;		/* allocated time */
166 	awe_chan_info	*cinfo;	/* channel info */
167 
168 	int note;		/* midi key (0-127) */
169 	int velocity;		/* midi velocity (0-127) */
170 	int sostenuto;		/* sostenuto on/off */
171 	awe_voice_info *sample;	/* assigned voice */
172 
173 	/* EMU8000 parameters */
174 	int apitch;		/* pitch parameter */
175 	int avol;		/* volume parameter */
176 	int apan;		/* panning parameter */
177 	int acutoff;		/* cutoff parameter */
178 	short aaux;		/* aux word */
179 } voice_info;
180 
181 /* voice information */
182 static voice_info voices[AWE_MAX_VOICES];
183 
184 #define IS_NO_SOUND(v)	(voices[v].state & (AWE_ST_OFF|AWE_ST_RELEASED|AWE_ST_STANDBY|AWE_ST_SUSTAINED))
185 #define IS_NO_EFFECT(v)	(voices[v].state != AWE_ST_ON)
186 #define IS_PLAYING(v)	(voices[v].state & (AWE_ST_ON|AWE_ST_SUSTAINED|AWE_ST_RELEASED))
187 #define IS_EMPTY(v)	(voices[v].state & (AWE_ST_OFF|AWE_ST_MARK|AWE_ST_DRAM|AWE_ST_FM))
188 
189 
190 /* MIDI channel effects information (for hw control) */
191 static awe_chan_info channels[AWE_MAX_CHANNELS];
192 
193 
194 /*
195  * global variables
196  */
197 
198 #ifndef AWE_DEFAULT_BASE_ADDR
199 #define AWE_DEFAULT_BASE_ADDR	0	/* autodetect */
200 #endif
201 
202 #ifndef AWE_DEFAULT_MEM_SIZE
203 #define AWE_DEFAULT_MEM_SIZE	-1	/* autodetect */
204 #endif
205 
206 int io = AWE_DEFAULT_BASE_ADDR; /* Emu8000 base address */
207 int memsize = AWE_DEFAULT_MEM_SIZE; /* memory size in Kbytes */
208 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
209 static int isapnp = -1;
210 #else
211 static int isapnp = 0;
212 #endif
213 
214 MODULE_AUTHOR("Takashi Iwai <iwai@ww.uni-erlangen.de>");
215 MODULE_DESCRIPTION("SB AWE32/64 WaveTable driver");
216 MODULE_LICENSE("GPL");
217 
218 MODULE_PARM(io, "i");
219 MODULE_PARM_DESC(io, "base i/o port of Emu8000");
220 MODULE_PARM(memsize, "i");
221 MODULE_PARM_DESC(memsize, "onboard DRAM size in Kbytes");
222 MODULE_PARM(isapnp, "i");
223 MODULE_PARM_DESC(isapnp, "use ISAPnP detection");
224 EXPORT_NO_SYMBOLS;
225 
226 /* DRAM start offset */
227 static int awe_mem_start = AWE_DRAM_OFFSET;
228 
229 /* maximum channels for playing */
230 static int awe_max_voices = AWE_MAX_VOICES;
231 
232 static int patch_opened = 0;		/* sample already loaded? */
233 
234 static char atten_relative = FALSE;
235 static short atten_offset = 0;
236 
237 static int awe_present = FALSE;		/* awe device present? */
238 static int awe_busy = FALSE;		/* awe device opened? */
239 
240 static int my_dev = -1;
241 
242 #define DEFAULT_DRUM_FLAGS	((1 << 9) | (1 << 25))
243 #define IS_DRUM_CHANNEL(c)	(drum_flags & (1 << (c)))
244 #define DRUM_CHANNEL_ON(c)	(drum_flags |= (1 << (c)))
245 #define DRUM_CHANNEL_OFF(c)	(drum_flags &= ~(1 << (c)))
246 static unsigned int drum_flags = DEFAULT_DRUM_FLAGS; /* channel flags */
247 
248 static int playing_mode = AWE_PLAY_INDIRECT;
249 #define SINGLE_LAYER_MODE()	(playing_mode == AWE_PLAY_INDIRECT || playing_mode == AWE_PLAY_DIRECT)
250 #define MULTI_LAYER_MODE()	(playing_mode == AWE_PLAY_MULTI || playing_mode == AWE_PLAY_MULTI2)
251 
252 static int current_alloc_time = 0;	/* voice allocation index for channel mode */
253 
254 static struct synth_info awe_info = {
255 	"AWE32 Synth",		/* name */
256 	0,			/* device */
257 	SYNTH_TYPE_SAMPLE,	/* synth_type */
258 	SAMPLE_TYPE_AWE32,	/* synth_subtype */
259 	0,			/* perc_mode (obsolete) */
260 	AWE_MAX_VOICES,		/* nr_voices */
261 	0,			/* nr_drums (obsolete) */
262 	400			/* instr_bank_size */
263 };
264 
265 
266 static struct voice_alloc_info *voice_alloc;	/* set at initialization */
267 
268 
269 /*
270  * function prototypes
271  */
272 
273 static int awe_check_port(void);
274 static void awe_request_region(void);
275 static void awe_release_region(void);
276 
277 static void awe_reset_samples(void);
278 /* emu8000 chip i/o access */
279 static void setup_ports(int p1, int p2, int p3);
280 static void awe_poke(unsigned short cmd, unsigned short port, unsigned short data);
281 static void awe_poke_dw(unsigned short cmd, unsigned short port, unsigned int data);
282 static unsigned short awe_peek(unsigned short cmd, unsigned short port);
283 static unsigned int awe_peek_dw(unsigned short cmd, unsigned short port);
284 static void awe_wait(unsigned short delay);
285 
286 /* initialize emu8000 chip */
287 static int _attach_awe(void);
288 static void _unload_awe(void);
289 static void awe_initialize(void);
290 
291 /* set voice parameters */
292 static void awe_init_ctrl_parms(int init_all);
293 static void awe_init_voice_info(awe_voice_info *vp);
294 static void awe_init_voice_parm(awe_voice_parm *pp);
295 #ifdef AWE_HAS_GUS_COMPATIBILITY
296 static int freq_to_note(int freq);
297 static int calc_rate_offset(int Hz);
298 /*static int calc_parm_delay(int msec);*/
299 static int calc_parm_hold(int msec);
300 static int calc_parm_attack(int msec);
301 static int calc_parm_decay(int msec);
302 static int calc_parm_search(int msec, short *table);
303 #endif /* gus compat */
304 
305 /* turn on/off note */
306 static void awe_note_on(int voice);
307 static void awe_note_off(int voice);
308 static void awe_terminate(int voice);
309 static void awe_exclusive_off(int voice);
310 static void awe_note_off_all(int do_sustain);
311 
312 /* calculate voice parameters */
313 typedef void (*fx_affect_func)(int voice, int forced);
314 static void awe_set_pitch(int voice, int forced);
315 static void awe_set_voice_pitch(int voice, int forced);
316 static void awe_set_volume(int voice, int forced);
317 static void awe_set_voice_vol(int voice, int forced);
318 static void awe_set_pan(int voice, int forced);
319 static void awe_fx_fmmod(int voice, int forced);
320 static void awe_fx_tremfrq(int voice, int forced);
321 static void awe_fx_fm2frq2(int voice, int forced);
322 static void awe_fx_filterQ(int voice, int forced);
323 static void awe_calc_pitch(int voice);
324 #ifdef AWE_HAS_GUS_COMPATIBILITY
325 static void awe_calc_pitch_from_freq(int voice, int freq);
326 #endif
327 static void awe_calc_volume(int voice);
328 static void awe_update_volume(void);
329 static void awe_change_master_volume(short val);
330 static void awe_voice_init(int voice, int init_all);
331 static void awe_channel_init(int ch, int init_all);
332 static void awe_fx_init(int ch);
333 static void awe_send_effect(int voice, int layer, int type, int val);
334 static void awe_modwheel_change(int voice, int value);
335 
336 /* sequencer interface */
337 static int awe_open(int dev, int mode);
338 static void awe_close(int dev);
339 static int awe_ioctl(int dev, unsigned int cmd, caddr_t arg);
340 static int awe_kill_note(int dev, int voice, int note, int velocity);
341 static int awe_start_note(int dev, int v, int note_num, int volume);
342 static int awe_set_instr(int dev, int voice, int instr_no);
343 static int awe_set_instr_2(int dev, int voice, int instr_no);
344 static void awe_reset(int dev);
345 static void awe_hw_control(int dev, unsigned char *event);
346 static int awe_load_patch(int dev, int format, const char *addr,
347 			  int offs, int count, int pmgr_flag);
348 static void awe_aftertouch(int dev, int voice, int pressure);
349 static void awe_controller(int dev, int voice, int ctrl_num, int value);
350 static void awe_panning(int dev, int voice, int value);
351 static void awe_volume_method(int dev, int mode);
352 static void awe_bender(int dev, int voice, int value);
353 static int awe_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc);
354 static void awe_setup_voice(int dev, int voice, int chn);
355 
356 #define awe_key_pressure(dev,voice,key,press) awe_start_note(dev,voice,(key)+128,press)
357 
358 /* hardware controls */
359 #ifdef AWE_HAS_GUS_COMPATIBILITY
360 static void awe_hw_gus_control(int dev, int cmd, unsigned char *event);
361 #endif
362 static void awe_hw_awe_control(int dev, int cmd, unsigned char *event);
363 static void awe_voice_change(int voice, fx_affect_func func);
364 static void awe_sostenuto_on(int voice, int forced);
365 static void awe_sustain_off(int voice, int forced);
366 static void awe_terminate_and_init(int voice, int forced);
367 
368 /* voice search */
369 static int awe_search_key(int bank, int preset, int note);
370 static awe_voice_list *awe_search_instr(int bank, int preset, int note);
371 static int awe_search_multi_voices(awe_voice_list *rec, int note, int velocity, awe_voice_info **vlist);
372 static void awe_alloc_multi_voices(int ch, int note, int velocity, int key);
373 static void awe_alloc_one_voice(int voice, int note, int velocity);
374 static int awe_clear_voice(void);
375 
376 /* load / remove patches */
377 static int awe_open_patch(awe_patch_info *patch, const char *addr, int count);
378 static int awe_close_patch(awe_patch_info *patch, const char *addr, int count);
379 static int awe_unload_patch(awe_patch_info *patch, const char *addr, int count);
380 static int awe_load_info(awe_patch_info *patch, const char *addr, int count);
381 static int awe_remove_info(awe_patch_info *patch, const char *addr, int count);
382 static int awe_load_data(awe_patch_info *patch, const char *addr, int count);
383 static int awe_replace_data(awe_patch_info *patch, const char *addr, int count);
384 static int awe_load_map(awe_patch_info *patch, const char *addr, int count);
385 #ifdef AWE_HAS_GUS_COMPATIBILITY
386 static int awe_load_guspatch(const char *addr, int offs, int size, int pmgr_flag);
387 #endif
388 /*static int awe_probe_info(awe_patch_info *patch, const char *addr, int count);*/
389 static int awe_probe_data(awe_patch_info *patch, const char *addr, int count);
390 static sf_list *check_patch_opened(int type, char *name);
391 static int awe_write_wave_data(const char *addr, int offset, awe_sample_list *sp, int channels);
392 static int awe_create_sf(int type, char *name);
393 static void awe_free_sf(sf_list *sf);
394 static void add_sf_info(sf_list *sf, awe_voice_list *rec);
395 static void add_sf_sample(sf_list *sf, awe_sample_list *smp);
396 static void purge_old_list(awe_voice_list *rec, awe_voice_list *next);
397 static void add_info_list(awe_voice_list *rec);
398 static void awe_remove_samples(int sf_id);
399 static void rebuild_preset_list(void);
400 static short awe_set_sample(awe_voice_list *rec);
401 static awe_sample_list *search_sample_index(sf_list *sf, int sample);
402 
403 static int is_identical_holder(sf_list *sf1, sf_list *sf2);
404 #ifdef AWE_ALLOW_SAMPLE_SHARING
405 static int is_identical_name(unsigned char *name, sf_list *p);
406 static int is_shared_sf(unsigned char *name);
407 static int info_duplicated(sf_list *sf, awe_voice_list *rec);
408 #endif /* allow sharing */
409 
410 /* lowlevel functions */
411 static void awe_init_audio(void);
412 static void awe_init_dma(void);
413 static void awe_init_array(void);
414 static void awe_send_array(unsigned short *data);
415 static void awe_tweak_voice(int voice);
416 static void awe_tweak(void);
417 static void awe_init_fm(void);
418 static int awe_open_dram_for_write(int offset, int channels);
419 static void awe_open_dram_for_check(void);
420 static void awe_close_dram(void);
421 /*static void awe_write_dram(unsigned short c);*/
422 static int awe_detect_base(int addr);
423 static int awe_detect(void);
424 static void awe_check_dram(void);
425 static int awe_load_chorus_fx(awe_patch_info *patch, const char *addr, int count);
426 static void awe_set_chorus_mode(int mode);
427 static void awe_update_chorus_mode(void);
428 static int awe_load_reverb_fx(awe_patch_info *patch, const char *addr, int count);
429 static void awe_set_reverb_mode(int mode);
430 static void awe_update_reverb_mode(void);
431 static void awe_equalizer(int bass, int treble);
432 static void awe_update_equalizer(void);
433 
434 #ifdef CONFIG_AWE32_MIXER
435 static void attach_mixer(void);
436 static void unload_mixer(void);
437 #endif
438 
439 #ifdef CONFIG_AWE32_MIDIEMU
440 static void attach_midiemu(void);
441 static void unload_midiemu(void);
442 #endif
443 
444 #define limitvalue(x, a, b) if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b)
445 
446 /*
447  * control parameters
448  */
449 
450 
451 #ifdef AWE_USE_NEW_VOLUME_CALC
452 #define DEF_VOLUME_CALC	TRUE
453 #else
454 #define DEF_VOLUME_CALC	FALSE
455 #endif /* new volume */
456 
457 #define DEF_ZERO_ATTEN		32	/* 12dB below */
458 #define DEF_MOD_SENSE		18
459 #define DEF_CHORUS_MODE		2
460 #define DEF_REVERB_MODE		4
461 #define DEF_BASS_LEVEL		5
462 #define DEF_TREBLE_LEVEL	9
463 
464 static struct CtrlParmsDef {
465 	int value;
466 	int init_each_time;
467 	void (*update)(void);
468 } ctrl_parms[AWE_MD_END] = {
469 	{0,0, NULL}, {0,0, NULL}, /* <-- not used */
470 	{AWE_VERSION_NUMBER, FALSE, NULL},
471 	{TRUE, FALSE, NULL}, /* exclusive */
472 	{TRUE, FALSE, NULL}, /* realpan */
473 	{AWE_DEFAULT_BANK, FALSE, NULL}, /* gusbank */
474 	{FALSE, TRUE, NULL}, /* keep effect */
475 	{DEF_ZERO_ATTEN, FALSE, awe_update_volume}, /* zero_atten */
476 	{FALSE, FALSE, NULL}, /* chn_prior */
477 	{DEF_MOD_SENSE, FALSE, NULL}, /* modwheel sense */
478 	{AWE_DEFAULT_PRESET, FALSE, NULL}, /* def_preset */
479 	{AWE_DEFAULT_BANK, FALSE, NULL}, /* def_bank */
480 	{AWE_DEFAULT_DRUM, FALSE, NULL}, /* def_drum */
481 	{FALSE, FALSE, NULL}, /* toggle_drum_bank */
482 	{DEF_VOLUME_CALC, FALSE, awe_update_volume}, /* new_volume_calc */
483 	{DEF_CHORUS_MODE, FALSE, awe_update_chorus_mode}, /* chorus mode */
484 	{DEF_REVERB_MODE, FALSE, awe_update_reverb_mode}, /* reverb mode */
485 	{DEF_BASS_LEVEL, FALSE, awe_update_equalizer}, /* bass level */
486 	{DEF_TREBLE_LEVEL, FALSE, awe_update_equalizer}, /* treble level */
487 	{0, FALSE, NULL},	/* debug mode */
488 	{FALSE, FALSE, NULL}, /* pan exchange */
489 };
490 
491 static int ctrls[AWE_MD_END];
492 
493 
494 /*
495  * synth operation table
496  */
497 
498 static struct synth_operations awe_operations =
499 {
500 	owner:		THIS_MODULE,
501 	id:		"EMU8K",
502 	info:		&awe_info,
503 	midi_dev:	0,
504 	synth_type:	SYNTH_TYPE_SAMPLE,
505 	synth_subtype:	SAMPLE_TYPE_AWE32,
506 	open:		awe_open,
507 	close:		awe_close,
508 	ioctl:		awe_ioctl,
509 	kill_note:	awe_kill_note,
510 	start_note:	awe_start_note,
511 	set_instr:	awe_set_instr_2,
512 	reset:		awe_reset,
513 	hw_control:	awe_hw_control,
514 	load_patch:	awe_load_patch,
515 	aftertouch:	awe_aftertouch,
516 	controller:	awe_controller,
517 	panning:	awe_panning,
518 	volume_method:	awe_volume_method,
519 	bender:		awe_bender,
520 	alloc_voice:	awe_alloc,
521 	setup_voice:	awe_setup_voice
522 };
523 
524 
525 /*
526  * General attach / unload interface
527  */
528 
_attach_awe(void)529 static int __init _attach_awe(void)
530 {
531 	if (awe_present) return 0; /* for OSS38.. called twice? */
532 
533 	/* check presence of AWE32 card */
534 	if (! awe_detect()) {
535 		printk(KERN_ERR "AWE32: not detected\n");
536 		return 0;
537 	}
538 
539 	/* check AWE32 ports are available */
540 	if (awe_check_port()) {
541 		printk(KERN_ERR "AWE32: I/O area already used.\n");
542 		return 0;
543 	}
544 
545 	/* set buffers to NULL */
546 	sfhead = sftail = NULL;
547 
548 	my_dev = sound_alloc_synthdev();
549 	if (my_dev == -1) {
550 		printk(KERN_ERR "AWE32 Error: too many synthesizers\n");
551 		return 0;
552 	}
553 
554 	voice_alloc = &awe_operations.alloc;
555 	voice_alloc->max_voice = awe_max_voices;
556 	synth_devs[my_dev] = &awe_operations;
557 
558 #ifdef CONFIG_AWE32_MIXER
559 	attach_mixer();
560 #endif
561 #ifdef CONFIG_AWE32_MIDIEMU
562 	attach_midiemu();
563 #endif
564 
565 	/* reserve I/O ports for awedrv */
566 	awe_request_region();
567 
568 	/* clear all samples */
569 	awe_reset_samples();
570 
571 	/* intialize AWE32 hardware */
572 	awe_initialize();
573 
574 	sprintf(awe_info.name, "AWE32-%s (RAM%dk)",
575 		AWEDRV_VERSION, memsize/1024);
576 	printk(KERN_INFO "<SoundBlaster EMU8000 (RAM%dk)>\n", memsize/1024);
577 
578 	awe_present = TRUE;
579 
580 	return 1;
581 }
582 
583 
free_tables(void)584 static void free_tables(void)
585 {
586 	if (sftail) {
587 		sf_list *p, *prev;
588 		for (p = sftail; p; p = prev) {
589 			prev = p->prev;
590 			awe_free_sf(p);
591 		}
592 	}
593 	sfhead = sftail = NULL;
594 }
595 
596 
_unload_awe(void)597 static void __exit _unload_awe(void)
598 {
599 	if (awe_present) {
600 		awe_reset_samples();
601 		awe_release_region();
602 		free_tables();
603 #ifdef CONFIG_AWE32_MIXER
604 		unload_mixer();
605 #endif
606 #ifdef CONFIG_AWE32_MIDIEMU
607 		unload_midiemu();
608 #endif
609 		sound_unload_synthdev(my_dev);
610 		awe_present = FALSE;
611 	}
612 }
613 
614 
615 /*
616  * clear sample tables
617  */
618 
619 static void
awe_reset_samples(void)620 awe_reset_samples(void)
621 {
622 	/* free all bank tables */
623 	memset(preset_table, 0, sizeof(preset_table));
624 	free_tables();
625 
626 	current_sf_id = 0;
627 	locked_sf_id = 0;
628 	patch_opened = 0;
629 }
630 
631 
632 /*
633  * EMU register access
634  */
635 
636 /* select a given AWE32 pointer */
637 static int awe_ports[5];
638 static int port_setuped = FALSE;
639 static int awe_cur_cmd = -1;
640 #define awe_set_cmd(cmd) \
641 if (awe_cur_cmd != cmd) { outw(cmd, awe_ports[Pointer]); awe_cur_cmd = cmd; }
642 
643 /* store values to i/o port array */
setup_ports(int port1,int port2,int port3)644 static void setup_ports(int port1, int port2, int port3)
645 {
646 	awe_ports[0] = port1;
647 	if (port2 == 0)
648 		port2 = port1 + 0x400;
649 	awe_ports[1] = port2;
650 	awe_ports[2] = port2 + 2;
651 	if (port3 == 0)
652 		port3 = port1 + 0x800;
653 	awe_ports[3] = port3;
654 	awe_ports[4] = port3 + 2;
655 
656 	port_setuped = TRUE;
657 }
658 
659 /* write 16bit data */
660 static void
awe_poke(unsigned short cmd,unsigned short port,unsigned short data)661 awe_poke(unsigned short cmd, unsigned short port, unsigned short data)
662 {
663 	awe_set_cmd(cmd);
664 	outw(data, awe_ports[port]);
665 }
666 
667 /* write 32bit data */
668 static void
awe_poke_dw(unsigned short cmd,unsigned short port,unsigned int data)669 awe_poke_dw(unsigned short cmd, unsigned short port, unsigned int data)
670 {
671 	unsigned short addr = awe_ports[port];
672 	awe_set_cmd(cmd);
673 	outw(data, addr);		/* write lower 16 bits */
674 	outw(data >> 16, addr + 2);	/* write higher 16 bits */
675 }
676 
677 /* read 16bit data */
678 static unsigned short
awe_peek(unsigned short cmd,unsigned short port)679 awe_peek(unsigned short cmd, unsigned short port)
680 {
681 	unsigned short k;
682 	awe_set_cmd(cmd);
683 	k = inw(awe_ports[port]);
684 	return k;
685 }
686 
687 /* read 32bit data */
688 static unsigned int
awe_peek_dw(unsigned short cmd,unsigned short port)689 awe_peek_dw(unsigned short cmd, unsigned short port)
690 {
691 	unsigned int k1, k2;
692 	unsigned short addr = awe_ports[port];
693 	awe_set_cmd(cmd);
694 	k1 = inw(addr);
695 	k2 = inw(addr + 2);
696 	k1 |= k2 << 16;
697 	return k1;
698 }
699 
700 /* wait delay number of AWE32 44100Hz clocks */
701 #ifdef WAIT_BY_LOOP /* wait by loop -- that's not good.. */
702 static void
awe_wait(unsigned short delay)703 awe_wait(unsigned short delay)
704 {
705 	unsigned short clock, target;
706 	unsigned short port = awe_ports[AWE_WC_Port];
707 	int counter;
708 
709 	/* sample counter */
710 	awe_set_cmd(AWE_WC_Cmd);
711 	clock = (unsigned short)inw(port);
712 	target = clock + delay;
713 	counter = 0;
714 	if (target < clock) {
715 		for (; (unsigned short)inw(port) > target; counter++)
716 			if (counter > 65536)
717 				break;
718 	}
719 	for (; (unsigned short)inw(port) < target; counter++)
720 		if (counter > 65536)
721 			break;
722 }
723 #else
724 
awe_wait(unsigned short delay)725 static void awe_wait(unsigned short delay)
726 {
727 	current->state = TASK_INTERRUPTIBLE;
728 	schedule_timeout((HZ*(unsigned long)delay + 44099)/44100);
729 }
730 /*
731 static void awe_wait(unsigned short delay)
732 {
733 	udelay(((unsigned long)delay * 1000000L + 44099) / 44100);
734 }
735 */
736 #endif /* wait by loop */
737 
738 /* write a word data */
739 #define awe_write_dram(c)	awe_poke(AWE_SMLD, c)
740 
741 
742 /*
743  * port check / request
744  *  0x620-623, 0xA20-A23, 0xE20-E23
745  */
746 
747 static int __init
awe_check_port(void)748 awe_check_port(void)
749 {
750 	if (! port_setuped) return 0;
751 	return (check_region(awe_ports[0], 4) ||
752 		check_region(awe_ports[1], 4) ||
753 		check_region(awe_ports[3], 4));
754 }
755 
756 static void __init
awe_request_region(void)757 awe_request_region(void)
758 {
759 	if (! port_setuped) return;
760 	request_region(awe_ports[0], 4, "sound driver (AWE32)");
761 	request_region(awe_ports[1], 4, "sound driver (AWE32)");
762 	request_region(awe_ports[3], 4, "sound driver (AWE32)");
763 }
764 
765 static void __exit
awe_release_region(void)766 awe_release_region(void)
767 {
768 	if (! port_setuped) return;
769 	release_region(awe_ports[0], 4);
770 	release_region(awe_ports[1], 4);
771 	release_region(awe_ports[3], 4);
772 }
773 
774 
775 /*
776  * initialization of AWE driver
777  */
778 
779 static void
awe_initialize(void)780 awe_initialize(void)
781 {
782 	DEBUG(0,printk("AWE32: initializing..\n"));
783 
784 	/* initialize hardware configuration */
785 	awe_poke(AWE_HWCF1, 0x0059);
786 	awe_poke(AWE_HWCF2, 0x0020);
787 
788 	/* disable audio; this seems to reduce a clicking noise a bit.. */
789 	awe_poke(AWE_HWCF3, 0);
790 
791 	/* initialize audio channels */
792 	awe_init_audio();
793 
794 	/* initialize DMA */
795 	awe_init_dma();
796 
797 	/* initialize init array */
798 	awe_init_array();
799 
800 	/* check DRAM memory size */
801 	awe_check_dram();
802 
803 	/* initialize the FM section of the AWE32 */
804 	awe_init_fm();
805 
806 	/* set up voice envelopes */
807 	awe_tweak();
808 
809 	/* enable audio */
810 	awe_poke(AWE_HWCF3, 0x0004);
811 
812 	/* set default values */
813 	awe_init_ctrl_parms(TRUE);
814 
815 	/* set equalizer */
816 	awe_update_equalizer();
817 
818 	/* set reverb & chorus modes */
819 	awe_update_reverb_mode();
820 	awe_update_chorus_mode();
821 }
822 
823 
824 /*
825  * AWE32 voice parameters
826  */
827 
828 /* initialize voice_info record */
829 static void
awe_init_voice_info(awe_voice_info * vp)830 awe_init_voice_info(awe_voice_info *vp)
831 {
832 	vp->sample = 0;
833 	vp->rate_offset = 0;
834 
835 	vp->start = 0;
836 	vp->end = 0;
837 	vp->loopstart = 0;
838 	vp->loopend = 0;
839 	vp->mode = 0;
840 	vp->root = 60;
841 	vp->tune = 0;
842 	vp->low = 0;
843 	vp->high = 127;
844 	vp->vellow = 0;
845 	vp->velhigh = 127;
846 
847 	vp->fixkey = -1;
848 	vp->fixvel = -1;
849 	vp->fixpan = -1;
850 	vp->pan = -1;
851 
852 	vp->exclusiveClass = 0;
853 	vp->amplitude = 127;
854 	vp->attenuation = 0;
855 	vp->scaleTuning = 100;
856 
857 	awe_init_voice_parm(&vp->parm);
858 }
859 
860 /* initialize voice_parm record:
861  * Env1/2: delay=0, attack=0, hold=0, sustain=0, decay=0, release=0.
862  * Vibrato and Tremolo effects are zero.
863  * Cutoff is maximum.
864  * Chorus and Reverb effects are zero.
865  */
866 static void
awe_init_voice_parm(awe_voice_parm * pp)867 awe_init_voice_parm(awe_voice_parm *pp)
868 {
869 	pp->moddelay = 0x8000;
870 	pp->modatkhld = 0x7f7f;
871 	pp->moddcysus = 0x7f7f;
872 	pp->modrelease = 0x807f;
873 	pp->modkeyhold = 0;
874 	pp->modkeydecay = 0;
875 
876 	pp->voldelay = 0x8000;
877 	pp->volatkhld = 0x7f7f;
878 	pp->voldcysus = 0x7f7f;
879 	pp->volrelease = 0x807f;
880 	pp->volkeyhold = 0;
881 	pp->volkeydecay = 0;
882 
883 	pp->lfo1delay = 0x8000;
884 	pp->lfo2delay = 0x8000;
885 	pp->pefe = 0;
886 
887 	pp->fmmod = 0;
888 	pp->tremfrq = 0;
889 	pp->fm2frq2 = 0;
890 
891 	pp->cutoff = 0xff;
892 	pp->filterQ = 0;
893 
894 	pp->chorus = 0;
895 	pp->reverb = 0;
896 }
897 
898 
899 #ifdef AWE_HAS_GUS_COMPATIBILITY
900 
901 /* convert frequency mHz to abstract cents (= midi key * 100) */
902 static int
freq_to_note(int mHz)903 freq_to_note(int mHz)
904 {
905 	/* abscents = log(mHz/8176) / log(2) * 1200 */
906 	unsigned int max_val = (unsigned int)0xffffffff / 10000;
907 	int i, times;
908 	unsigned int base;
909 	unsigned int freq;
910 	int note, tune;
911 
912 	if (mHz == 0)
913 		return 0;
914 	if (mHz < 0)
915 		return 12799; /* maximum */
916 
917 	freq = mHz;
918 	note = 0;
919 	for (base = 8176 * 2; freq >= base; base *= 2) {
920 		note += 12;
921 		if (note >= 128) /* over maximum */
922 			return 12799;
923 	}
924 	base /= 2;
925 
926 	/* to avoid overflow... */
927 	times = 10000;
928 	while (freq > max_val) {
929 		max_val *= 10;
930 		times /= 10;
931 		base /= 10;
932 	}
933 
934 	freq = freq * times / base;
935 	for (i = 0; i < 12; i++) {
936 		if (freq < semitone_tuning[i+1])
937 			break;
938 		note++;
939 	}
940 
941 	tune = 0;
942 	freq = freq * 10000 / semitone_tuning[i];
943 	for (i = 0; i < 100; i++) {
944 		if (freq < cent_tuning[i+1])
945 			break;
946 		tune++;
947 	}
948 
949 	return note * 100 + tune;
950 }
951 
952 
953 /* convert Hz to AWE32 rate offset:
954  * sample pitch offset for the specified sample rate
955  * rate=44100 is no offset, each 4096 is 1 octave (twice).
956  * eg, when rate is 22050, this offset becomes -4096.
957  */
958 static int
calc_rate_offset(int Hz)959 calc_rate_offset(int Hz)
960 {
961 	/* offset = log(Hz / 44100) / log(2) * 4096 */
962 	int freq, base, i;
963 
964 	/* maybe smaller than max (44100Hz) */
965 	if (Hz <= 0 || Hz >= 44100) return 0;
966 
967 	base = 0;
968 	for (freq = Hz * 2; freq < 44100; freq *= 2)
969 		base++;
970 	base *= 1200;
971 
972 	freq = 44100 * 10000 / (freq/2);
973 	for (i = 0; i < 12; i++) {
974 		if (freq < semitone_tuning[i+1])
975 			break;
976 		base += 100;
977 	}
978 	freq = freq * 10000 / semitone_tuning[i];
979 	for (i = 0; i < 100; i++) {
980 		if (freq < cent_tuning[i+1])
981 			break;
982 		base++;
983 	}
984 	return -base * 4096 / 1200;
985 }
986 
987 
988 /*
989  * convert envelope time parameter to AWE32 raw parameter
990  */
991 
992 /* attack & decay/release time table (msec) */
993 static short attack_time_tbl[128] = {
994 32767, 32767, 5989, 4235, 2994, 2518, 2117, 1780, 1497, 1373, 1259, 1154, 1058, 970, 890, 816,
995 707, 691, 662, 634, 607, 581, 557, 533, 510, 489, 468, 448, 429, 411, 393, 377,
996 361, 345, 331, 317, 303, 290, 278, 266, 255, 244, 234, 224, 214, 205, 196, 188,
997 180, 172, 165, 158, 151, 145, 139, 133, 127, 122, 117, 112, 107, 102, 98, 94,
998 90, 86, 82, 79, 75, 72, 69, 66, 63, 61, 58, 56, 53, 51, 49, 47,
999 45, 43, 41, 39, 37, 36, 34, 33, 31, 30, 29, 28, 26, 25, 24, 23,
1000 22, 21, 20, 19, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12,
1001 11, 11, 10, 10, 10, 9, 9, 8, 8, 8, 8, 7, 7, 7, 6, 0,
1002 };
1003 
1004 static short decay_time_tbl[128] = {
1005 32767, 32767, 22614, 15990, 11307, 9508, 7995, 6723, 5653, 5184, 4754, 4359, 3997, 3665, 3361, 3082,
1006 2828, 2765, 2648, 2535, 2428, 2325, 2226, 2132, 2042, 1955, 1872, 1793, 1717, 1644, 1574, 1507,
1007 1443, 1382, 1324, 1267, 1214, 1162, 1113, 1066, 978, 936, 897, 859, 822, 787, 754, 722,
1008 691, 662, 634, 607, 581, 557, 533, 510, 489, 468, 448, 429, 411, 393, 377, 361,
1009 345, 331, 317, 303, 290, 278, 266, 255, 244, 234, 224, 214, 205, 196, 188, 180,
1010 172, 165, 158, 151, 145, 139, 133, 127, 122, 117, 112, 107, 102, 98, 94, 90,
1011 86, 82, 79, 75, 72, 69, 66, 63, 61, 58, 56, 53, 51, 49, 47, 45,
1012 43, 41, 39, 37, 36, 34, 33, 31, 30, 29, 28, 26, 25, 24, 23, 22,
1013 };
1014 
1015 #define calc_parm_delay(msec) (0x8000 - (msec) * 1000 / 725);
1016 
1017 /* delay time = 0x8000 - msec/92 */
1018 static int
calc_parm_hold(int msec)1019 calc_parm_hold(int msec)
1020 {
1021 	int val = (0x7f * 92 - msec) / 92;
1022 	if (val < 1) val = 1;
1023 	if (val > 127) val = 127;
1024 	return val;
1025 }
1026 
1027 /* attack time: search from time table */
1028 static int
calc_parm_attack(int msec)1029 calc_parm_attack(int msec)
1030 {
1031 	return calc_parm_search(msec, attack_time_tbl);
1032 }
1033 
1034 /* decay/release time: search from time table */
1035 static int
calc_parm_decay(int msec)1036 calc_parm_decay(int msec)
1037 {
1038 	return calc_parm_search(msec, decay_time_tbl);
1039 }
1040 
1041 /* search an index for specified time from given time table */
1042 static int
calc_parm_search(int msec,short * table)1043 calc_parm_search(int msec, short *table)
1044 {
1045 	int left = 1, right = 127, mid;
1046 	while (left < right) {
1047 		mid = (left + right) / 2;
1048 		if (msec < (int)table[mid])
1049 			left = mid + 1;
1050 		else
1051 			right = mid;
1052 	}
1053 	return left;
1054 }
1055 #endif /* AWE_HAS_GUS_COMPATIBILITY */
1056 
1057 
1058 /*
1059  * effects table
1060  */
1061 
1062 /* set an effect value */
1063 #define FX_FLAG_OFF	0
1064 #define FX_FLAG_SET	1
1065 #define FX_FLAG_ADD	2
1066 
1067 #define FX_SET(rec,type,value) \
1068 	((rec)->flags[type] = FX_FLAG_SET, (rec)->val[type] = (value))
1069 #define FX_ADD(rec,type,value) \
1070 	((rec)->flags[type] = FX_FLAG_ADD, (rec)->val[type] = (value))
1071 #define FX_UNSET(rec,type) \
1072 	((rec)->flags[type] = FX_FLAG_OFF, (rec)->val[type] = 0)
1073 
1074 /* check the effect value is set */
1075 #define FX_ON(rec,type)	((rec)->flags[type])
1076 
1077 #define PARM_BYTE	0
1078 #define PARM_WORD	1
1079 #define PARM_SIGN	2
1080 
1081 static struct PARM_DEFS {
1082 	int type;	/* byte or word */
1083 	int low, high;	/* value range */
1084 	fx_affect_func realtime;	/* realtime paramater change */
1085 } parm_defs[] = {
1086 	{PARM_WORD, 0, 0x8000, NULL},	/* env1 delay */
1087 	{PARM_BYTE, 1, 0x7f, NULL},	/* env1 attack */
1088 	{PARM_BYTE, 0, 0x7e, NULL},	/* env1 hold */
1089 	{PARM_BYTE, 1, 0x7f, NULL},	/* env1 decay */
1090 	{PARM_BYTE, 1, 0x7f, NULL},	/* env1 release */
1091 	{PARM_BYTE, 0, 0x7f, NULL},	/* env1 sustain */
1092 	{PARM_BYTE, 0, 0xff, NULL},	/* env1 pitch */
1093 	{PARM_BYTE, 0, 0xff, NULL},	/* env1 cutoff */
1094 
1095 	{PARM_WORD, 0, 0x8000, NULL},	/* env2 delay */
1096 	{PARM_BYTE, 1, 0x7f, NULL},	/* env2 attack */
1097 	{PARM_BYTE, 0, 0x7e, NULL},	/* env2 hold */
1098 	{PARM_BYTE, 1, 0x7f, NULL},	/* env2 decay */
1099 	{PARM_BYTE, 1, 0x7f, NULL},	/* env2 release */
1100 	{PARM_BYTE, 0, 0x7f, NULL},	/* env2 sustain */
1101 
1102 	{PARM_WORD, 0, 0x8000, NULL},	/* lfo1 delay */
1103 	{PARM_BYTE, 0, 0xff, awe_fx_tremfrq},	/* lfo1 freq */
1104 	{PARM_SIGN, -128, 127, awe_fx_tremfrq},	/* lfo1 volume */
1105 	{PARM_SIGN, -128, 127, awe_fx_fmmod},	/* lfo1 pitch */
1106 	{PARM_BYTE, 0, 0xff, awe_fx_fmmod},	/* lfo1 cutoff */
1107 
1108 	{PARM_WORD, 0, 0x8000, NULL},	/* lfo2 delay */
1109 	{PARM_BYTE, 0, 0xff, awe_fx_fm2frq2},	/* lfo2 freq */
1110 	{PARM_SIGN, -128, 127, awe_fx_fm2frq2},	/* lfo2 pitch */
1111 
1112 	{PARM_WORD, 0, 0xffff, awe_set_voice_pitch},	/* initial pitch */
1113 	{PARM_BYTE, 0, 0xff, NULL},	/* chorus */
1114 	{PARM_BYTE, 0, 0xff, NULL},	/* reverb */
1115 	{PARM_BYTE, 0, 0xff, awe_set_volume},	/* initial cutoff */
1116 	{PARM_BYTE, 0, 15, awe_fx_filterQ},	/* initial resonance */
1117 
1118 	{PARM_WORD, 0, 0xffff, NULL},	/* sample start */
1119 	{PARM_WORD, 0, 0xffff, NULL},	/* loop start */
1120 	{PARM_WORD, 0, 0xffff, NULL},	/* loop end */
1121 	{PARM_WORD, 0, 0xffff, NULL},	/* coarse sample start */
1122 	{PARM_WORD, 0, 0xffff, NULL},	/* coarse loop start */
1123 	{PARM_WORD, 0, 0xffff, NULL},	/* coarse loop end */
1124 	{PARM_BYTE, 0, 0xff, awe_set_volume},	/* initial attenuation */
1125 };
1126 
1127 
1128 static unsigned char
FX_BYTE(FX_Rec * rec,FX_Rec * lay,int type,unsigned char value)1129 FX_BYTE(FX_Rec *rec, FX_Rec *lay, int type, unsigned char value)
1130 {
1131 	int effect = 0;
1132 	int on = 0;
1133 	if (lay && (on = FX_ON(lay, type)) != 0)
1134 		effect = lay->val[type];
1135 	if (!on && (on = FX_ON(rec, type)) != 0)
1136 		effect = rec->val[type];
1137 	if (on == FX_FLAG_ADD) {
1138 		if (parm_defs[type].type == PARM_SIGN) {
1139 			if (value > 0x7f)
1140 				effect += (int)value - 0x100;
1141 			else
1142 				effect += (int)value;
1143 		} else {
1144 			effect += (int)value;
1145 		}
1146 	}
1147 	if (on) {
1148 		if (effect < parm_defs[type].low)
1149 			effect = parm_defs[type].low;
1150 		else if (effect > parm_defs[type].high)
1151 			effect = parm_defs[type].high;
1152 		return (unsigned char)effect;
1153 	}
1154 	return value;
1155 }
1156 
1157 /* get word effect value */
1158 static unsigned short
FX_WORD(FX_Rec * rec,FX_Rec * lay,int type,unsigned short value)1159 FX_WORD(FX_Rec *rec, FX_Rec *lay, int type, unsigned short value)
1160 {
1161 	int effect = 0;
1162 	int on = 0;
1163 	if (lay && (on = FX_ON(lay, type)) != 0)
1164 		effect = lay->val[type];
1165 	if (!on && (on = FX_ON(rec, type)) != 0)
1166 		effect = rec->val[type];
1167 	if (on == FX_FLAG_ADD)
1168 		effect += (int)value;
1169 	if (on) {
1170 		if (effect < parm_defs[type].low)
1171 			effect = parm_defs[type].low;
1172 		else if (effect > parm_defs[type].high)
1173 			effect = parm_defs[type].high;
1174 		return (unsigned short)effect;
1175 	}
1176 	return value;
1177 }
1178 
1179 /* get word (upper=type1/lower=type2) effect value */
1180 static unsigned short
FX_COMB(FX_Rec * rec,FX_Rec * lay,int type1,int type2,unsigned short value)1181 FX_COMB(FX_Rec *rec, FX_Rec *lay, int type1, int type2, unsigned short value)
1182 {
1183 	unsigned short tmp;
1184 	tmp = FX_BYTE(rec, lay, type1, (unsigned char)(value >> 8));
1185 	tmp <<= 8;
1186 	tmp |= FX_BYTE(rec, lay, type2, (unsigned char)(value & 0xff));
1187 	return tmp;
1188 }
1189 
1190 /* address offset */
1191 static int
FX_OFFSET(FX_Rec * rec,FX_Rec * lay,int lo,int hi,int mode)1192 FX_OFFSET(FX_Rec *rec, FX_Rec *lay, int lo, int hi, int mode)
1193 {
1194 	int addr = 0;
1195 	if (lay && FX_ON(lay, hi))
1196 		addr = (short)lay->val[hi];
1197 	else if (FX_ON(rec, hi))
1198 		addr = (short)rec->val[hi];
1199 	addr = addr << 15;
1200 	if (lay && FX_ON(lay, lo))
1201 		addr += (short)lay->val[lo];
1202 	else if (FX_ON(rec, lo))
1203 		addr += (short)rec->val[lo];
1204 	if (!(mode & AWE_SAMPLE_8BITS))
1205 		addr /= 2;
1206 	return addr;
1207 }
1208 
1209 
1210 /*
1211  * turn on/off sample
1212  */
1213 
1214 /* table for volume target calculation */
1215 static unsigned short voltarget[16] = {
1216    0xEAC0, 0XE0C8, 0XD740, 0XCE20, 0XC560, 0XBD08, 0XB500, 0XAD58,
1217    0XA5F8, 0X9EF0, 0X9830, 0X91C0, 0X8B90, 0X85A8, 0X8000, 0X7A90
1218 };
1219 
1220 static void
awe_note_on(int voice)1221 awe_note_on(int voice)
1222 {
1223 	unsigned int temp;
1224 	int addr;
1225 	int vtarget, ftarget, ptarget, pitch;
1226 	awe_voice_info *vp;
1227 	awe_voice_parm_block *parm;
1228 	FX_Rec *fx = &voices[voice].cinfo->fx;
1229 	FX_Rec *fx_lay = NULL;
1230 	if (voices[voice].layer < MAX_LAYERS)
1231 		fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1232 
1233 	/* A voice sample must assigned before calling */
1234 	if ((vp = voices[voice].sample) == NULL || vp->index == 0)
1235 		return;
1236 
1237 	parm = (awe_voice_parm_block*)&vp->parm;
1238 
1239 	/* channel to be silent and idle */
1240 	awe_poke(AWE_DCYSUSV(voice), 0x0080);
1241 	awe_poke(AWE_VTFT(voice), 0x0000FFFF);
1242 	awe_poke(AWE_CVCF(voice), 0x0000FFFF);
1243 	awe_poke(AWE_PTRX(voice), 0);
1244 	awe_poke(AWE_CPF(voice), 0);
1245 
1246 	/* set pitch offset */
1247 	awe_set_pitch(voice, TRUE);
1248 
1249 	/* modulation & volume envelope */
1250 	if (parm->modatk >= 0x80 && parm->moddelay >= 0x8000) {
1251 		awe_poke(AWE_ENVVAL(voice), 0xBFFF);
1252 		pitch = (parm->env1pit<<4) + voices[voice].apitch;
1253 		if (pitch > 0xffff) pitch = 0xffff;
1254 		/* calculate filter target */
1255 		ftarget = parm->cutoff + parm->env1fc;
1256 		limitvalue(ftarget, 0, 255);
1257 		ftarget <<= 8;
1258 	} else {
1259 		awe_poke(AWE_ENVVAL(voice),
1260 			 FX_WORD(fx, fx_lay, AWE_FX_ENV1_DELAY, parm->moddelay));
1261 		ftarget = parm->cutoff;
1262 		ftarget <<= 8;
1263 		pitch = voices[voice].apitch;
1264 	}
1265 
1266 	/* calcualte pitch target */
1267 	if (pitch != 0xffff) {
1268 		ptarget = 1 << (pitch >> 12);
1269 		if (pitch & 0x800) ptarget += (ptarget*0x102e)/0x2710;
1270 		if (pitch & 0x400) ptarget += (ptarget*0x764)/0x2710;
1271 		if (pitch & 0x200) ptarget += (ptarget*0x389)/0x2710;
1272 		ptarget += (ptarget>>1);
1273 		if (ptarget > 0xffff) ptarget = 0xffff;
1274 
1275 	} else ptarget = 0xffff;
1276 	if (parm->modatk >= 0x80)
1277 		awe_poke(AWE_ATKHLD(voice),
1278 			 FX_BYTE(fx, fx_lay, AWE_FX_ENV1_HOLD, parm->modhld) << 8 | 0x7f);
1279 	else
1280 		awe_poke(AWE_ATKHLD(voice),
1281 			 FX_COMB(fx, fx_lay, AWE_FX_ENV1_HOLD, AWE_FX_ENV1_ATTACK,
1282 				 vp->parm.modatkhld));
1283 	awe_poke(AWE_DCYSUS(voice),
1284 		 FX_COMB(fx, fx_lay, AWE_FX_ENV1_SUSTAIN, AWE_FX_ENV1_DECAY,
1285 			  vp->parm.moddcysus));
1286 
1287 	if (parm->volatk >= 0x80 && parm->voldelay >= 0x8000) {
1288 		awe_poke(AWE_ENVVOL(voice), 0xBFFF);
1289 		vtarget = voltarget[voices[voice].avol%0x10]>>(voices[voice].avol>>4);
1290 	} else {
1291 		awe_poke(AWE_ENVVOL(voice),
1292 			 FX_WORD(fx, fx_lay, AWE_FX_ENV2_DELAY, vp->parm.voldelay));
1293 		vtarget = 0;
1294 	}
1295 	if (parm->volatk >= 0x80)
1296 		awe_poke(AWE_ATKHLDV(voice),
1297 			 FX_BYTE(fx, fx_lay, AWE_FX_ENV2_HOLD, parm->volhld) << 8 | 0x7f);
1298 	else
1299 		awe_poke(AWE_ATKHLDV(voice),
1300 			 FX_COMB(fx, fx_lay, AWE_FX_ENV2_HOLD, AWE_FX_ENV2_ATTACK,
1301 			 vp->parm.volatkhld));
1302 	/* decay/sustain parameter for volume envelope must be set at last */
1303 
1304 	/* cutoff and volume */
1305 	awe_set_volume(voice, TRUE);
1306 
1307 	/* modulation envelope heights */
1308 	awe_poke(AWE_PEFE(voice),
1309 		 FX_COMB(fx, fx_lay, AWE_FX_ENV1_PITCH, AWE_FX_ENV1_CUTOFF,
1310 			 vp->parm.pefe));
1311 
1312 	/* lfo1/2 delay */
1313 	awe_poke(AWE_LFO1VAL(voice),
1314 		 FX_WORD(fx, fx_lay, AWE_FX_LFO1_DELAY, vp->parm.lfo1delay));
1315 	awe_poke(AWE_LFO2VAL(voice),
1316 		 FX_WORD(fx, fx_lay, AWE_FX_LFO2_DELAY, vp->parm.lfo2delay));
1317 
1318 	/* lfo1 pitch & cutoff shift */
1319 	awe_fx_fmmod(voice, TRUE);
1320 	/* lfo1 volume & freq */
1321 	awe_fx_tremfrq(voice, TRUE);
1322 	/* lfo2 pitch & freq */
1323 	awe_fx_fm2frq2(voice, TRUE);
1324 	/* pan & loop start */
1325 	awe_set_pan(voice, TRUE);
1326 
1327 	/* chorus & loop end (chorus 8bit, MSB) */
1328 	addr = vp->loopend - 1;
1329 	addr += FX_OFFSET(fx, fx_lay, AWE_FX_LOOP_END,
1330 			  AWE_FX_COARSE_LOOP_END, vp->mode);
1331 	temp = FX_BYTE(fx, fx_lay, AWE_FX_CHORUS, vp->parm.chorus);
1332 	temp = (temp <<24) | (unsigned int)addr;
1333 	awe_poke_dw(AWE_CSL(voice), temp);
1334 	DEBUG(4,printk("AWE32: [-- loopend=%x/%x]\n", vp->loopend, addr));
1335 
1336 	/* Q & current address (Q 4bit value, MSB) */
1337 	addr = vp->start - 1;
1338 	addr += FX_OFFSET(fx, fx_lay, AWE_FX_SAMPLE_START,
1339 			  AWE_FX_COARSE_SAMPLE_START, vp->mode);
1340 	temp = FX_BYTE(fx, fx_lay, AWE_FX_FILTERQ, vp->parm.filterQ);
1341 	temp = (temp<<28) | (unsigned int)addr;
1342 	awe_poke_dw(AWE_CCCA(voice), temp);
1343 	DEBUG(4,printk("AWE32: [-- startaddr=%x/%x]\n", vp->start, addr));
1344 
1345 	/* clear unknown registers */
1346 	awe_poke_dw(AWE_00A0(voice), 0);
1347 	awe_poke_dw(AWE_0080(voice), 0);
1348 
1349 	/* reset volume */
1350 	awe_poke_dw(AWE_VTFT(voice), (vtarget<<16)|ftarget);
1351 	awe_poke_dw(AWE_CVCF(voice), (vtarget<<16)|ftarget);
1352 
1353 	/* set reverb */
1354 	temp = FX_BYTE(fx, fx_lay, AWE_FX_REVERB, vp->parm.reverb);
1355 	temp = (temp << 8) | (ptarget << 16) | voices[voice].aaux;
1356 	awe_poke_dw(AWE_PTRX(voice), temp);
1357 	awe_poke_dw(AWE_CPF(voice), ptarget << 16);
1358 	/* turn on envelope */
1359 	awe_poke(AWE_DCYSUSV(voice),
1360 		 FX_COMB(fx, fx_lay, AWE_FX_ENV2_SUSTAIN, AWE_FX_ENV2_DECAY,
1361 			  vp->parm.voldcysus));
1362 
1363 	voices[voice].state = AWE_ST_ON;
1364 
1365 	/* clear voice position for the next note on this channel */
1366 	if (SINGLE_LAYER_MODE()) {
1367 		FX_UNSET(fx, AWE_FX_SAMPLE_START);
1368 		FX_UNSET(fx, AWE_FX_COARSE_SAMPLE_START);
1369 	}
1370 }
1371 
1372 
1373 /* turn off the voice */
1374 static void
awe_note_off(int voice)1375 awe_note_off(int voice)
1376 {
1377 	awe_voice_info *vp;
1378 	unsigned short tmp;
1379 	FX_Rec *fx = &voices[voice].cinfo->fx;
1380 	FX_Rec *fx_lay = NULL;
1381 	if (voices[voice].layer < MAX_LAYERS)
1382 		fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1383 
1384 	if ((vp = voices[voice].sample) == NULL) {
1385 		voices[voice].state = AWE_ST_OFF;
1386 		return;
1387 	}
1388 
1389 	tmp = 0x8000 | FX_BYTE(fx, fx_lay, AWE_FX_ENV1_RELEASE,
1390 			       (unsigned char)vp->parm.modrelease);
1391 	awe_poke(AWE_DCYSUS(voice), tmp);
1392 	tmp = 0x8000 | FX_BYTE(fx, fx_lay, AWE_FX_ENV2_RELEASE,
1393 			       (unsigned char)vp->parm.volrelease);
1394 	awe_poke(AWE_DCYSUSV(voice), tmp);
1395 	voices[voice].state = AWE_ST_RELEASED;
1396 }
1397 
1398 /* force to terminate the voice (no releasing echo) */
1399 static void
awe_terminate(int voice)1400 awe_terminate(int voice)
1401 {
1402 	awe_poke(AWE_DCYSUSV(voice), 0x807F);
1403 	awe_tweak_voice(voice);
1404 	voices[voice].state = AWE_ST_OFF;
1405 }
1406 
1407 /* turn off other voices with the same exclusive class (for drums) */
1408 static void
awe_exclusive_off(int voice)1409 awe_exclusive_off(int voice)
1410 {
1411 	int i, exclass;
1412 
1413 	if (voices[voice].sample == NULL)
1414 		return;
1415 	if ((exclass = voices[voice].sample->exclusiveClass) == 0)
1416 		return;	/* not exclusive */
1417 
1418 	/* turn off voices with the same class */
1419 	for (i = 0; i < awe_max_voices; i++) {
1420 		if (i != voice && IS_PLAYING(i) &&
1421 		    voices[i].sample && voices[i].ch == voices[voice].ch &&
1422 		    voices[i].sample->exclusiveClass == exclass) {
1423 			DEBUG(4,printk("AWE32: [exoff(%d)]\n", i));
1424 			awe_terminate(i);
1425 			awe_voice_init(i, TRUE);
1426 		}
1427 	}
1428 }
1429 
1430 
1431 /*
1432  * change the parameters of an audible voice
1433  */
1434 
1435 /* change pitch */
1436 static void
awe_set_pitch(int voice,int forced)1437 awe_set_pitch(int voice, int forced)
1438 {
1439 	if (IS_NO_EFFECT(voice) && !forced) return;
1440 	awe_poke(AWE_IP(voice), voices[voice].apitch);
1441 	DEBUG(3,printk("AWE32: [-- pitch=%x]\n", voices[voice].apitch));
1442 }
1443 
1444 /* calculate & change pitch */
1445 static void
awe_set_voice_pitch(int voice,int forced)1446 awe_set_voice_pitch(int voice, int forced)
1447 {
1448 	awe_calc_pitch(voice);
1449 	awe_set_pitch(voice, forced);
1450 }
1451 
1452 /* change volume & cutoff */
1453 static void
awe_set_volume(int voice,int forced)1454 awe_set_volume(int voice, int forced)
1455 {
1456 	awe_voice_info *vp;
1457 	unsigned short tmp2;
1458 	FX_Rec *fx = &voices[voice].cinfo->fx;
1459 	FX_Rec *fx_lay = NULL;
1460 	if (voices[voice].layer < MAX_LAYERS)
1461 		fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1462 
1463 	if (!IS_PLAYING(voice) && !forced) return;
1464 	if ((vp = voices[voice].sample) == NULL || vp->index == 0)
1465 		return;
1466 
1467 	tmp2 = FX_BYTE(fx, fx_lay, AWE_FX_CUTOFF,
1468 		       (unsigned char)voices[voice].acutoff);
1469 	tmp2 = (tmp2 << 8);
1470 	tmp2 |= FX_BYTE(fx, fx_lay, AWE_FX_ATTEN,
1471 			(unsigned char)voices[voice].avol);
1472 	awe_poke(AWE_IFATN(voice), tmp2);
1473 }
1474 
1475 /* calculate & change volume */
1476 static void
awe_set_voice_vol(int voice,int forced)1477 awe_set_voice_vol(int voice, int forced)
1478 {
1479 	if (IS_EMPTY(voice))
1480 		return;
1481 	awe_calc_volume(voice);
1482 	awe_set_volume(voice, forced);
1483 }
1484 
1485 
1486 /* change pan; this could make a click noise.. */
1487 static void
awe_set_pan(int voice,int forced)1488 awe_set_pan(int voice, int forced)
1489 {
1490 	unsigned int temp;
1491 	int addr;
1492 	awe_voice_info *vp;
1493 	FX_Rec *fx = &voices[voice].cinfo->fx;
1494 	FX_Rec *fx_lay = NULL;
1495 	if (voices[voice].layer < MAX_LAYERS)
1496 		fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1497 
1498 	if (IS_NO_EFFECT(voice) && !forced) return;
1499 	if ((vp = voices[voice].sample) == NULL || vp->index == 0)
1500 		return;
1501 
1502 	/* pan & loop start (pan 8bit, MSB, 0:right, 0xff:left) */
1503 	if (vp->fixpan > 0)	/* 0-127 */
1504 		temp = 255 - (int)vp->fixpan * 2;
1505 	else {
1506 		int pos = 0;
1507 		if (vp->pan >= 0) /* 0-127 */
1508 			pos = (int)vp->pan * 2 - 128;
1509 		pos += voices[voice].cinfo->panning; /* -128 - 127 */
1510 		temp = 127 - pos;
1511 	}
1512 	limitvalue(temp, 0, 255);
1513 	if (ctrls[AWE_MD_PAN_EXCHANGE]) {
1514 		temp = 255 - temp;
1515 	}
1516 	if (forced || temp != voices[voice].apan) {
1517 		voices[voice].apan = temp;
1518 		if (temp == 0)
1519 			voices[voice].aaux = 0xff;
1520 		else
1521 			voices[voice].aaux = (-temp) & 0xff;
1522 		addr = vp->loopstart - 1;
1523 		addr += FX_OFFSET(fx, fx_lay, AWE_FX_LOOP_START,
1524 				  AWE_FX_COARSE_LOOP_START, vp->mode);
1525 		temp = (temp<<24) | (unsigned int)addr;
1526 		awe_poke_dw(AWE_PSST(voice), temp);
1527 		DEBUG(4,printk("AWE32: [-- loopstart=%x/%x]\n", vp->loopstart, addr));
1528 	}
1529 }
1530 
1531 /* effects change during playing */
1532 static void
awe_fx_fmmod(int voice,int forced)1533 awe_fx_fmmod(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_FMMOD(voice),
1545 		 FX_COMB(fx, fx_lay, AWE_FX_LFO1_PITCH, AWE_FX_LFO1_CUTOFF,
1546 			 vp->parm.fmmod));
1547 }
1548 
1549 /* set tremolo (lfo1) volume & frequency */
1550 static void
awe_fx_tremfrq(int voice,int forced)1551 awe_fx_tremfrq(int voice, int forced)
1552 {
1553 	awe_voice_info *vp;
1554 	FX_Rec *fx = &voices[voice].cinfo->fx;
1555 	FX_Rec *fx_lay = NULL;
1556 	if (voices[voice].layer < MAX_LAYERS)
1557 		fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1558 
1559 	if (IS_NO_EFFECT(voice) && !forced) return;
1560 	if ((vp = voices[voice].sample) == NULL || vp->index == 0)
1561 		return;
1562 	awe_poke(AWE_TREMFRQ(voice),
1563 		 FX_COMB(fx, fx_lay, AWE_FX_LFO1_VOLUME, AWE_FX_LFO1_FREQ,
1564 			 vp->parm.tremfrq));
1565 }
1566 
1567 /* set lfo2 pitch & frequency */
1568 static void
awe_fx_fm2frq2(int voice,int forced)1569 awe_fx_fm2frq2(int voice, int forced)
1570 {
1571 	awe_voice_info *vp;
1572 	FX_Rec *fx = &voices[voice].cinfo->fx;
1573 	FX_Rec *fx_lay = NULL;
1574 	if (voices[voice].layer < MAX_LAYERS)
1575 		fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1576 
1577 	if (IS_NO_EFFECT(voice) && !forced) return;
1578 	if ((vp = voices[voice].sample) == NULL || vp->index == 0)
1579 		return;
1580 	awe_poke(AWE_FM2FRQ2(voice),
1581 		 FX_COMB(fx, fx_lay, AWE_FX_LFO2_PITCH, AWE_FX_LFO2_FREQ,
1582 			 vp->parm.fm2frq2));
1583 }
1584 
1585 
1586 /* Q & current address (Q 4bit value, MSB) */
1587 static void
awe_fx_filterQ(int voice,int forced)1588 awe_fx_filterQ(int voice, int forced)
1589 {
1590 	unsigned int addr;
1591 	awe_voice_info *vp;
1592 	FX_Rec *fx = &voices[voice].cinfo->fx;
1593 	FX_Rec *fx_lay = NULL;
1594 	if (voices[voice].layer < MAX_LAYERS)
1595 		fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1596 
1597 	if (IS_NO_EFFECT(voice) && !forced) return;
1598 	if ((vp = voices[voice].sample) == NULL || vp->index == 0)
1599 		return;
1600 
1601 	addr = awe_peek_dw(AWE_CCCA(voice)) & 0xffffff;
1602 	addr |= (FX_BYTE(fx, fx_lay, AWE_FX_FILTERQ, vp->parm.filterQ) << 28);
1603 	awe_poke_dw(AWE_CCCA(voice), addr);
1604 }
1605 
1606 /*
1607  * calculate pitch offset
1608  *
1609  * 0xE000 is no pitch offset at 44100Hz sample.
1610  * Every 4096 is one octave.
1611  */
1612 
1613 static void
awe_calc_pitch(int voice)1614 awe_calc_pitch(int voice)
1615 {
1616 	voice_info *vp = &voices[voice];
1617 	awe_voice_info *ap;
1618 	awe_chan_info *cp = voices[voice].cinfo;
1619 	int offset;
1620 
1621 	/* search voice information */
1622 	if ((ap = vp->sample) == NULL)
1623 			return;
1624 	if (ap->index == 0) {
1625 		DEBUG(3,printk("AWE32: set sample (%d)\n", ap->sample));
1626 		if (awe_set_sample((awe_voice_list*)ap) == 0)
1627 			return;
1628 	}
1629 
1630 	/* calculate offset */
1631 	if (ap->fixkey >= 0) {
1632 		DEBUG(3,printk("AWE32: p-> fixkey(%d) tune(%d)\n", ap->fixkey, ap->tune));
1633 		offset = (ap->fixkey - ap->root) * 4096 / 12;
1634 	} else {
1635 		DEBUG(3,printk("AWE32: p(%d)-> root(%d) tune(%d)\n", vp->note, ap->root, ap->tune));
1636 		offset = (vp->note - ap->root) * 4096 / 12;
1637 		DEBUG(4,printk("AWE32: p-> ofs=%d\n", offset));
1638 	}
1639 	offset = (offset * ap->scaleTuning) / 100;
1640 	DEBUG(4,printk("AWE32: p-> scale* ofs=%d\n", offset));
1641 	offset += ap->tune * 4096 / 1200;
1642 	DEBUG(4,printk("AWE32: p-> tune+ ofs=%d\n", offset));
1643 	if (cp->bender != 0) {
1644 		DEBUG(3,printk("AWE32: p-> bend(%d) %d\n", voice, cp->bender));
1645 		/* (819200: 1 semitone) ==> (4096: 12 semitones) */
1646 		offset += cp->bender * cp->bender_range / 2400;
1647 	}
1648 
1649 	/* add initial pitch correction */
1650 	if (FX_ON(&cp->fx_layer[vp->layer], AWE_FX_INIT_PITCH))
1651 		offset += cp->fx_layer[vp->layer].val[AWE_FX_INIT_PITCH];
1652 	else if (FX_ON(&cp->fx, AWE_FX_INIT_PITCH))
1653 		offset += cp->fx.val[AWE_FX_INIT_PITCH];
1654 
1655 	/* 0xe000: root pitch */
1656 	vp->apitch = 0xe000 + ap->rate_offset + offset;
1657 	DEBUG(4,printk("AWE32: p-> sum aofs=%x, rate_ofs=%d\n", vp->apitch, ap->rate_offset));
1658 	if (vp->apitch > 0xffff)
1659 		vp->apitch = 0xffff;
1660 	if (vp->apitch < 0)
1661 		vp->apitch = 0;
1662 }
1663 
1664 
1665 #ifdef AWE_HAS_GUS_COMPATIBILITY
1666 /* calculate MIDI key and semitone from the specified frequency */
1667 static void
awe_calc_pitch_from_freq(int voice,int freq)1668 awe_calc_pitch_from_freq(int voice, int freq)
1669 {
1670 	voice_info *vp = &voices[voice];
1671 	awe_voice_info *ap;
1672 	FX_Rec *fx = &voices[voice].cinfo->fx;
1673 	FX_Rec *fx_lay = NULL;
1674 	int offset;
1675 	int note;
1676 
1677 	if (voices[voice].layer < MAX_LAYERS)
1678 		fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
1679 
1680 	/* search voice information */
1681 	if ((ap = vp->sample) == NULL)
1682 		return;
1683 	if (ap->index == 0) {
1684 		DEBUG(3,printk("AWE32: set sample (%d)\n", ap->sample));
1685 		if (awe_set_sample((awe_voice_list*)ap) == 0)
1686 			return;
1687 	}
1688 	note = freq_to_note(freq);
1689 	offset = (note - ap->root * 100 + ap->tune) * 4096 / 1200;
1690 	offset = (offset * ap->scaleTuning) / 100;
1691 	if (fx_lay && FX_ON(fx_lay, AWE_FX_INIT_PITCH))
1692 		offset += fx_lay->val[AWE_FX_INIT_PITCH];
1693 	else if (FX_ON(fx, AWE_FX_INIT_PITCH))
1694 		offset += fx->val[AWE_FX_INIT_PITCH];
1695 	vp->apitch = 0xe000 + ap->rate_offset + offset;
1696 	if (vp->apitch > 0xffff)
1697 		vp->apitch = 0xffff;
1698 	if (vp->apitch < 0)
1699 		vp->apitch = 0;
1700 }
1701 #endif /* AWE_HAS_GUS_COMPATIBILITY */
1702 
1703 
1704 /*
1705  * calculate volume attenuation
1706  *
1707  * Voice volume is controlled by volume attenuation parameter.
1708  * So volume becomes maximum when avol is 0 (no attenuation), and
1709  * minimum when 255 (-96dB or silence).
1710  */
1711 
1712 static int vol_table[128] = {
1713 	255,111,95,86,79,74,70,66,63,61,58,56,54,52,50,49,
1714 	47,46,45,43,42,41,40,39,38,37,36,35,34,34,33,32,
1715 	31,31,30,29,29,28,27,27,26,26,25,24,24,23,23,22,
1716 	22,21,21,21,20,20,19,19,18,18,18,17,17,16,16,16,
1717 	15,15,15,14,14,14,13,13,13,12,12,12,11,11,11,10,
1718 	10,10,10,9,9,9,8,8,8,8,7,7,7,7,6,6,
1719 	6,6,5,5,5,5,5,4,4,4,4,3,3,3,3,3,
1720 	2,2,2,2,2,1,1,1,1,1,0,0,0,0,0,0,
1721 };
1722 
1723 /* tables for volume->attenuation calculation */
1724 static unsigned char voltab1[128] = {
1725    0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1726    0x63, 0x2b, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22,
1727    0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a,
1728    0x19, 0x19, 0x18, 0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x14,
1729    0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10,
1730    0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0d,
1731    0x0d, 0x0d, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b,
1732    0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09,
1733    0x08, 0x08, 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06,
1734    0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x04,
1735    0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02,
1736    0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01,
1737    0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1738 };
1739 
1740 static unsigned char voltab2[128] = {
1741    0x32, 0x31, 0x30, 0x2f, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x2a,
1742    0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x24, 0x23, 0x22, 0x21,
1743    0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1c, 0x1b, 0x1a,
1744    0x1a, 0x19, 0x19, 0x18, 0x18, 0x17, 0x16, 0x16, 0x15, 0x15,
1745    0x14, 0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x10,
1746    0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d,
1747    0x0d, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a,
1748    0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08,
1749    0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
1750    0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
1751    0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03,
1752    0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01,
1753    0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
1754 };
1755 
1756 static unsigned char expressiontab[128] = {
1757    0x7f, 0x6c, 0x62, 0x5a, 0x54, 0x50, 0x4b, 0x48, 0x45, 0x42,
1758    0x40, 0x3d, 0x3b, 0x39, 0x38, 0x36, 0x34, 0x33, 0x31, 0x30,
1759    0x2f, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26, 0x25,
1760    0x24, 0x24, 0x23, 0x22, 0x21, 0x21, 0x20, 0x1f, 0x1e, 0x1e,
1761    0x1d, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a, 0x1a, 0x19, 0x18, 0x18,
1762    0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x15, 0x14, 0x14, 0x13,
1763    0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10, 0x10, 0x0f, 0x0f,
1764    0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0c, 0x0c, 0x0c,
1765    0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09,
1766    0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
1767    0x06, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03,
1768    0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
1769    0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1770 };
1771 
1772 static void
awe_calc_volume(int voice)1773 awe_calc_volume(int voice)
1774 {
1775 	voice_info *vp = &voices[voice];
1776 	awe_voice_info *ap;
1777 	awe_chan_info *cp = voices[voice].cinfo;
1778 	int vol;
1779 
1780 	/* search voice information */
1781 	if ((ap = vp->sample) == NULL)
1782 		return;
1783 
1784 	ap = vp->sample;
1785 	if (ap->index == 0) {
1786 		DEBUG(3,printk("AWE32: set sample (%d)\n", ap->sample));
1787 		if (awe_set_sample((awe_voice_list*)ap) == 0)
1788 			return;
1789 	}
1790 
1791 	if (ctrls[AWE_MD_NEW_VOLUME_CALC]) {
1792 		int main_vol = cp->main_vol * ap->amplitude / 127;
1793 		limitvalue(vp->velocity, 0, 127);
1794 		limitvalue(main_vol, 0, 127);
1795 		limitvalue(cp->expression_vol, 0, 127);
1796 
1797 		vol = voltab1[main_vol] + voltab2[vp->velocity];
1798 		vol = (vol * 8) / 3;
1799 		vol += ap->attenuation;
1800 		if (cp->expression_vol < 127)
1801 			vol += ((0x100 - vol) * expressiontab[cp->expression_vol])/128;
1802 		vol += atten_offset;
1803 		if (atten_relative)
1804 			vol += ctrls[AWE_MD_ZERO_ATTEN];
1805 		limitvalue(vol, 0, 255);
1806 		vp->avol = vol;
1807 
1808 	} else {
1809 		/* 0 - 127 */
1810 		vol = (vp->velocity * cp->main_vol * cp->expression_vol) / (127*127);
1811 		vol = vol * ap->amplitude / 127;
1812 
1813 		if (vol < 0) vol = 0;
1814 		if (vol > 127) vol = 127;
1815 
1816 		/* calc to attenuation */
1817 		vol = vol_table[vol];
1818 		vol += (int)ap->attenuation;
1819 		vol += atten_offset;
1820 		if (atten_relative)
1821 			vol += ctrls[AWE_MD_ZERO_ATTEN];
1822 		if (vol > 255) vol = 255;
1823 
1824 		vp->avol = vol;
1825 	}
1826 	if (cp->bank !=  AWE_DRUM_BANK && ((awe_voice_parm_block*)(&ap->parm))->volatk < 0x7d) {
1827 		int atten;
1828 		if (vp->velocity < 70) atten = 70;
1829 		else atten = vp->velocity;
1830 		vp->acutoff = (atten * ap->parm.cutoff + 0xa0) >> 7;
1831 	} else {
1832 		vp->acutoff = ap->parm.cutoff;
1833 	}
1834 	DEBUG(3,printk("AWE32: [-- voice(%d) vol=%x]\n", voice, vol));
1835 }
1836 
1837 /* change master volume */
1838 static void
awe_change_master_volume(short val)1839 awe_change_master_volume(short val)
1840 {
1841 	limitvalue(val, 0, 127);
1842 	atten_offset = vol_table[val];
1843 	atten_relative = TRUE;
1844 	awe_update_volume();
1845 }
1846 
1847 /* update volumes of all available channels */
awe_update_volume(void)1848 static void awe_update_volume(void)
1849 {
1850 	int i;
1851 	for (i = 0; i < awe_max_voices; i++)
1852 		awe_set_voice_vol(i, TRUE);
1853 }
1854 
1855 /* set sostenuto on */
awe_sostenuto_on(int voice,int forced)1856 static void awe_sostenuto_on(int voice, int forced)
1857 {
1858 	if (IS_NO_EFFECT(voice) && !forced) return;
1859 	voices[voice].sostenuto = 127;
1860 }
1861 
1862 
1863 /* drop sustain */
awe_sustain_off(int voice,int forced)1864 static void awe_sustain_off(int voice, int forced)
1865 {
1866 	if (voices[voice].state == AWE_ST_SUSTAINED) {
1867 		awe_note_off(voice);
1868 		awe_fx_init(voices[voice].ch);
1869 		awe_voice_init(voice, FALSE);
1870 	}
1871 }
1872 
1873 
1874 /* terminate and initialize voice */
awe_terminate_and_init(int voice,int forced)1875 static void awe_terminate_and_init(int voice, int forced)
1876 {
1877 	awe_terminate(voice);
1878 	awe_fx_init(voices[voice].ch);
1879 	awe_voice_init(voice, TRUE);
1880 }
1881 
1882 
1883 /*
1884  * synth operation routines
1885  */
1886 
1887 #define AWE_VOICE_KEY(v)	(0x8000 | (v))
1888 #define AWE_CHAN_KEY(c,n)	(((c) << 8) | ((n) + 1))
1889 #define KEY_CHAN_MATCH(key,c)	(((key) >> 8) == (c))
1890 
1891 /* initialize the voice */
1892 static void
awe_voice_init(int voice,int init_all)1893 awe_voice_init(int voice, int init_all)
1894 {
1895 	voice_info *vp = &voices[voice];
1896 
1897 	/* reset voice search key */
1898 	if (playing_mode == AWE_PLAY_DIRECT)
1899 		vp->key = AWE_VOICE_KEY(voice);
1900 	else
1901 		vp->key = 0;
1902 
1903 	/* clear voice mapping */
1904 	voice_alloc->map[voice] = 0;
1905 
1906 	/* touch the timing flag */
1907 	vp->time = current_alloc_time;
1908 
1909 	/* initialize other parameters if necessary */
1910 	if (init_all) {
1911 		vp->note = -1;
1912 		vp->velocity = 0;
1913 		vp->sostenuto = 0;
1914 
1915 		vp->sample = NULL;
1916 		vp->cinfo = &channels[voice];
1917 		vp->ch = voice;
1918 		vp->state = AWE_ST_OFF;
1919 
1920 		/* emu8000 parameters */
1921 		vp->apitch = 0;
1922 		vp->avol = 255;
1923 		vp->apan = -1;
1924 	}
1925 }
1926 
1927 /* clear effects */
awe_fx_init(int ch)1928 static void awe_fx_init(int ch)
1929 {
1930 	if (SINGLE_LAYER_MODE() && !ctrls[AWE_MD_KEEP_EFFECT]) {
1931 		memset(&channels[ch].fx, 0, sizeof(channels[ch].fx));
1932 		memset(&channels[ch].fx_layer, 0, sizeof(&channels[ch].fx_layer));
1933 	}
1934 }
1935 
1936 /* initialize channel info */
awe_channel_init(int ch,int init_all)1937 static void awe_channel_init(int ch, int init_all)
1938 {
1939 	awe_chan_info *cp = &channels[ch];
1940 	cp->channel = ch;
1941 	if (init_all) {
1942 		cp->panning = 0; /* zero center */
1943 		cp->bender_range = 200; /* sense * 100 */
1944 		cp->main_vol = 127;
1945 		if (MULTI_LAYER_MODE() && IS_DRUM_CHANNEL(ch)) {
1946 			cp->instr = ctrls[AWE_MD_DEF_DRUM];
1947 			cp->bank = AWE_DRUM_BANK;
1948 		} else {
1949 			cp->instr = ctrls[AWE_MD_DEF_PRESET];
1950 			cp->bank = ctrls[AWE_MD_DEF_BANK];
1951 		}
1952 	}
1953 
1954 	cp->bender = 0; /* zero tune skew */
1955 	cp->expression_vol = 127;
1956 	cp->chan_press = 0;
1957 	cp->sustained = 0;
1958 
1959 	if (! ctrls[AWE_MD_KEEP_EFFECT]) {
1960 		memset(&cp->fx, 0, sizeof(cp->fx));
1961 		memset(&cp->fx_layer, 0, sizeof(cp->fx_layer));
1962 	}
1963 }
1964 
1965 
1966 /* change the voice parameters; voice = channel */
awe_voice_change(int voice,fx_affect_func func)1967 static void awe_voice_change(int voice, fx_affect_func func)
1968 {
1969 	int i;
1970 	switch (playing_mode) {
1971 	case AWE_PLAY_DIRECT:
1972 		func(voice, FALSE);
1973 		break;
1974 	case AWE_PLAY_INDIRECT:
1975 		for (i = 0; i < awe_max_voices; i++)
1976 			if (voices[i].key == AWE_VOICE_KEY(voice))
1977 				func(i, FALSE);
1978 		break;
1979 	default:
1980 		for (i = 0; i < awe_max_voices; i++)
1981 			if (KEY_CHAN_MATCH(voices[i].key, voice))
1982 				func(i, FALSE);
1983 		break;
1984 	}
1985 }
1986 
1987 
1988 /*
1989  * device open / close
1990  */
1991 
1992 /* open device:
1993  *   reset status of all voices, and clear sample position flag
1994  */
1995 static int
awe_open(int dev,int mode)1996 awe_open(int dev, int mode)
1997 {
1998 	if (awe_busy)
1999 		return -EBUSY;
2000 
2001 	awe_busy = TRUE;
2002 
2003 	/* set default mode */
2004 	awe_init_ctrl_parms(FALSE);
2005 	atten_relative = TRUE;
2006 	atten_offset = 0;
2007 	drum_flags = DEFAULT_DRUM_FLAGS;
2008 	playing_mode = AWE_PLAY_INDIRECT;
2009 
2010 	/* reset voices & channels */
2011 	awe_reset(dev);
2012 
2013 	patch_opened = 0;
2014 
2015 	return 0;
2016 }
2017 
2018 
2019 /* close device:
2020  *   reset all voices again (terminate sounds)
2021  */
2022 static void
awe_close(int dev)2023 awe_close(int dev)
2024 {
2025 	awe_reset(dev);
2026 	awe_busy = FALSE;
2027 }
2028 
2029 
2030 /* set miscellaneous mode parameters
2031  */
2032 static void
awe_init_ctrl_parms(int init_all)2033 awe_init_ctrl_parms(int init_all)
2034 {
2035 	int i;
2036 	for (i = 0; i < AWE_MD_END; i++) {
2037 		if (init_all || ctrl_parms[i].init_each_time)
2038 			ctrls[i] = ctrl_parms[i].value;
2039 	}
2040 }
2041 
2042 
2043 /* sequencer I/O control:
2044  */
2045 static int
awe_ioctl(int dev,unsigned int cmd,caddr_t arg)2046 awe_ioctl(int dev, unsigned int cmd, caddr_t arg)
2047 {
2048 	switch (cmd) {
2049 	case SNDCTL_SYNTH_INFO:
2050 		if (playing_mode == AWE_PLAY_DIRECT)
2051 			awe_info.nr_voices = awe_max_voices;
2052 		else
2053 			awe_info.nr_voices = AWE_MAX_CHANNELS;
2054 		if(copy_to_user(arg, &awe_info, sizeof(awe_info)))
2055 			return -EFAULT;
2056 		return 0;
2057 
2058 	case SNDCTL_SEQ_RESETSAMPLES:
2059 		awe_reset(dev);
2060 		awe_reset_samples();
2061 		return 0;
2062 
2063 	case SNDCTL_SEQ_PERCMODE:
2064 		/* what's this? */
2065 		return 0;
2066 
2067 	case SNDCTL_SYNTH_MEMAVL:
2068 		return memsize - awe_free_mem_ptr() * 2;
2069 
2070 	default:
2071 		return -EINVAL;
2072 	}
2073 }
2074 
2075 
voice_in_range(int voice)2076 static int voice_in_range(int voice)
2077 {
2078 	if (playing_mode == AWE_PLAY_DIRECT) {
2079 		if (voice < 0 || voice >= awe_max_voices)
2080 			return FALSE;
2081 	} else {
2082 		if (voice < 0 || voice >= AWE_MAX_CHANNELS)
2083 			return FALSE;
2084 	}
2085 	return TRUE;
2086 }
2087 
release_voice(int voice,int do_sustain)2088 static void release_voice(int voice, int do_sustain)
2089 {
2090 	if (IS_NO_SOUND(voice))
2091 		return;
2092 	if (do_sustain && (voices[voice].cinfo->sustained == 127 ||
2093 			    voices[voice].sostenuto == 127))
2094 		voices[voice].state = AWE_ST_SUSTAINED;
2095 	else {
2096 		awe_note_off(voice);
2097 		awe_fx_init(voices[voice].ch);
2098 		awe_voice_init(voice, FALSE);
2099 	}
2100 }
2101 
2102 /* release all notes */
awe_note_off_all(int do_sustain)2103 static void awe_note_off_all(int do_sustain)
2104 {
2105 	int i;
2106 	for (i = 0; i < awe_max_voices; i++)
2107 		release_voice(i, do_sustain);
2108 }
2109 
2110 /* kill a voice:
2111  *   not terminate, just release the voice.
2112  */
2113 static int
awe_kill_note(int dev,int voice,int note,int velocity)2114 awe_kill_note(int dev, int voice, int note, int velocity)
2115 {
2116 	int i, v2, key;
2117 
2118 	DEBUG(2,printk("AWE32: [off(%d) nt=%d vl=%d]\n", voice, note, velocity));
2119 	if (! voice_in_range(voice))
2120 		return -EINVAL;
2121 
2122 	switch (playing_mode) {
2123 	case AWE_PLAY_DIRECT:
2124 	case AWE_PLAY_INDIRECT:
2125 		key = AWE_VOICE_KEY(voice);
2126 		break;
2127 
2128 	case AWE_PLAY_MULTI2:
2129 		v2 = voice_alloc->map[voice] >> 8;
2130 		voice_alloc->map[voice] = 0;
2131 		voice = v2;
2132 		if (voice < 0 || voice >= AWE_MAX_CHANNELS)
2133 			return -EINVAL;
2134 		/* continue to below */
2135 	default:
2136 		key = AWE_CHAN_KEY(voice, note);
2137 		break;
2138 	}
2139 
2140 	for (i = 0; i < awe_max_voices; i++) {
2141 		if (voices[i].key == key)
2142 			release_voice(i, TRUE);
2143 	}
2144 	return 0;
2145 }
2146 
2147 
start_or_volume_change(int voice,int velocity)2148 static void start_or_volume_change(int voice, int velocity)
2149 {
2150 	voices[voice].velocity = velocity;
2151 	awe_calc_volume(voice);
2152 	if (voices[voice].state == AWE_ST_STANDBY)
2153 		awe_note_on(voice);
2154 	else if (voices[voice].state == AWE_ST_ON)
2155 		awe_set_volume(voice, FALSE);
2156 }
2157 
set_and_start_voice(int voice,int state)2158 static void set_and_start_voice(int voice, int state)
2159 {
2160 	/* calculate pitch & volume parameters */
2161 	voices[voice].state = state;
2162 	awe_calc_pitch(voice);
2163 	awe_calc_volume(voice);
2164 	if (state == AWE_ST_ON)
2165 		awe_note_on(voice);
2166 }
2167 
2168 /* start a voice:
2169  *   if note is 255, identical with aftertouch function.
2170  *   Otherwise, start a voice with specified not and volume.
2171  */
2172 static int
awe_start_note(int dev,int voice,int note,int velocity)2173 awe_start_note(int dev, int voice, int note, int velocity)
2174 {
2175 	int i, key, state, volonly;
2176 
2177 	DEBUG(2,printk("AWE32: [on(%d) nt=%d vl=%d]\n", voice, note, velocity));
2178 	if (! voice_in_range(voice))
2179 		return -EINVAL;
2180 
2181 	if (velocity == 0)
2182 		state = AWE_ST_STANDBY; /* stand by for playing */
2183 	else
2184 		state = AWE_ST_ON;	/* really play */
2185 	volonly = FALSE;
2186 
2187 	switch (playing_mode) {
2188 	case AWE_PLAY_DIRECT:
2189 	case AWE_PLAY_INDIRECT:
2190 		key = AWE_VOICE_KEY(voice);
2191 		if (note == 255)
2192 			volonly = TRUE;
2193 		break;
2194 
2195 	case AWE_PLAY_MULTI2:
2196 		voice = voice_alloc->map[voice] >> 8;
2197 		if (voice < 0 || voice >= AWE_MAX_CHANNELS)
2198 			return -EINVAL;
2199 		/* continue to below */
2200 	default:
2201 		if (note >= 128) { /* key volume mode */
2202 			note -= 128;
2203 			volonly = TRUE;
2204 		}
2205 		key = AWE_CHAN_KEY(voice, note);
2206 		break;
2207 	}
2208 
2209 	/* dynamic volume change */
2210 	if (volonly) {
2211 		for (i = 0; i < awe_max_voices; i++) {
2212 			if (voices[i].key == key)
2213 				start_or_volume_change(i, velocity);
2214 		}
2215 		return 0;
2216 	}
2217 
2218 	/* if the same note still playing, stop it */
2219 	if (playing_mode != AWE_PLAY_DIRECT || ctrls[AWE_MD_EXCLUSIVE_SOUND]) {
2220 		for (i = 0; i < awe_max_voices; i++)
2221 			if (voices[i].key == key) {
2222 				if (voices[i].state == AWE_ST_ON) {
2223 					awe_note_off(i);
2224 					awe_voice_init(i, FALSE);
2225 				} else if (voices[i].state == AWE_ST_STANDBY)
2226 					awe_voice_init(i, TRUE);
2227 			}
2228 	}
2229 
2230 	/* allocate voices */
2231 	if (playing_mode == AWE_PLAY_DIRECT)
2232 		awe_alloc_one_voice(voice, note, velocity);
2233 	else
2234 		awe_alloc_multi_voices(voice, note, velocity, key);
2235 
2236 	/* turn off other voices exlusively (for drums) */
2237 	for (i = 0; i < awe_max_voices; i++)
2238 		if (voices[i].key == key)
2239 			awe_exclusive_off(i);
2240 
2241 	/* set up pitch and volume parameters */
2242 	for (i = 0; i < awe_max_voices; i++) {
2243 		if (voices[i].key == key && voices[i].state == AWE_ST_OFF)
2244 			set_and_start_voice(i, state);
2245 	}
2246 
2247 	return 0;
2248 }
2249 
2250 
2251 /* calculate hash key */
2252 static int
awe_search_key(int bank,int preset,int note)2253 awe_search_key(int bank, int preset, int note)
2254 {
2255 	unsigned int key;
2256 
2257 #if 1 /* new hash table */
2258 	if (bank == AWE_DRUM_BANK)
2259 		key = preset + note + 128;
2260 	else
2261 		key = bank + preset;
2262 #else
2263 	key = preset;
2264 #endif
2265 	key %= AWE_MAX_PRESETS;
2266 
2267 	return (int)key;
2268 }
2269 
2270 
2271 /* search instrument from hash table */
2272 static awe_voice_list *
awe_search_instr(int bank,int preset,int note)2273 awe_search_instr(int bank, int preset, int note)
2274 {
2275 	awe_voice_list *p;
2276 	int key, key2;
2277 
2278 	key = awe_search_key(bank, preset, note);
2279 	for (p = preset_table[key]; p; p = p->next_bank) {
2280 		if (p->instr == preset && p->bank == bank)
2281 			return p;
2282 	}
2283 	key2 = awe_search_key(bank, preset, 0); /* search default */
2284 	if (key == key2)
2285 		return NULL;
2286 	for (p = preset_table[key2]; p; p = p->next_bank) {
2287 		if (p->instr == preset && p->bank == bank)
2288 			return p;
2289 	}
2290 	return NULL;
2291 }
2292 
2293 
2294 /* assign the instrument to a voice */
2295 static int
awe_set_instr_2(int dev,int voice,int instr_no)2296 awe_set_instr_2(int dev, int voice, int instr_no)
2297 {
2298 	if (playing_mode == AWE_PLAY_MULTI2) {
2299 		voice = voice_alloc->map[voice] >> 8;
2300 		if (voice < 0 || voice >= AWE_MAX_CHANNELS)
2301 			return -EINVAL;
2302 	}
2303 	return awe_set_instr(dev, voice, instr_no);
2304 }
2305 
2306 /* assign the instrument to a channel; voice is the channel number */
2307 static int
awe_set_instr(int dev,int voice,int instr_no)2308 awe_set_instr(int dev, int voice, int instr_no)
2309 {
2310 	awe_chan_info *cinfo;
2311 
2312 	if (! voice_in_range(voice))
2313 		return -EINVAL;
2314 
2315 	if (instr_no < 0 || instr_no >= AWE_MAX_PRESETS)
2316 		return -EINVAL;
2317 
2318 	cinfo = &channels[voice];
2319 	cinfo->instr = instr_no;
2320 	DEBUG(2,printk("AWE32: [program(%d) %d]\n", voice, instr_no));
2321 
2322 	return 0;
2323 }
2324 
2325 
2326 /* reset all voices; terminate sounds and initialize parameters */
2327 static void
awe_reset(int dev)2328 awe_reset(int dev)
2329 {
2330 	int i;
2331 	current_alloc_time = 0;
2332 	/* don't turn off voice 31 and 32.  they are used also for FM voices */
2333 	for (i = 0; i < awe_max_voices; i++) {
2334 		awe_terminate(i);
2335 		awe_voice_init(i, TRUE);
2336 	}
2337 	for (i = 0; i < AWE_MAX_CHANNELS; i++)
2338 		awe_channel_init(i, TRUE);
2339 	for (i = 0; i < 16; i++) {
2340 		awe_operations.chn_info[i].controllers[CTL_MAIN_VOLUME] = 127;
2341 		awe_operations.chn_info[i].controllers[CTL_EXPRESSION] = 127;
2342 	}
2343 	awe_init_fm();
2344 	awe_tweak();
2345 }
2346 
2347 
2348 /* hardware specific control:
2349  *   GUS specific and AWE32 specific controls are available.
2350  */
2351 static void
awe_hw_control(int dev,unsigned char * event)2352 awe_hw_control(int dev, unsigned char *event)
2353 {
2354 	int cmd = event[2];
2355 	if (cmd & _AWE_MODE_FLAG)
2356 		awe_hw_awe_control(dev, cmd & _AWE_MODE_VALUE_MASK, event);
2357 #ifdef AWE_HAS_GUS_COMPATIBILITY
2358 	else
2359 		awe_hw_gus_control(dev, cmd & _AWE_MODE_VALUE_MASK, event);
2360 #endif
2361 }
2362 
2363 
2364 #ifdef AWE_HAS_GUS_COMPATIBILITY
2365 
2366 /* GUS compatible controls */
2367 static void
awe_hw_gus_control(int dev,int cmd,unsigned char * event)2368 awe_hw_gus_control(int dev, int cmd, unsigned char *event)
2369 {
2370 	int voice, i, key;
2371 	unsigned short p1;
2372 	short p2;
2373 	int plong;
2374 
2375 	if (MULTI_LAYER_MODE())
2376 		return;
2377 	if (cmd == _GUS_NUMVOICES)
2378 		return;
2379 
2380 	voice = event[3];
2381 	if (! voice_in_range(voice))
2382 		return;
2383 
2384 	p1 = *(unsigned short *) &event[4];
2385 	p2 = *(short *) &event[6];
2386 	plong = *(int*) &event[4];
2387 
2388 	switch (cmd) {
2389 	case _GUS_VOICESAMPLE:
2390 		awe_set_instr(dev, voice, p1);
2391 		return;
2392 
2393 	case _GUS_VOICEBALA:
2394 		/* 0 to 15 --> -128 to 127 */
2395 		awe_panning(dev, voice, ((int)p1 << 4) - 128);
2396 		return;
2397 
2398 	case _GUS_VOICEVOL:
2399 	case _GUS_VOICEVOL2:
2400 		/* not supported yet */
2401 		return;
2402 
2403 	case _GUS_RAMPRANGE:
2404 	case _GUS_RAMPRATE:
2405 	case _GUS_RAMPMODE:
2406 	case _GUS_RAMPON:
2407 	case _GUS_RAMPOFF:
2408 		/* volume ramping not supported */
2409 		return;
2410 
2411 	case _GUS_VOLUME_SCALE:
2412 		return;
2413 
2414 	case _GUS_VOICE_POS:
2415 		FX_SET(&channels[voice].fx, AWE_FX_SAMPLE_START,
2416 		       (short)(plong & 0x7fff));
2417 		FX_SET(&channels[voice].fx, AWE_FX_COARSE_SAMPLE_START,
2418 		       (plong >> 15) & 0xffff);
2419 		return;
2420 	}
2421 
2422 	key = AWE_VOICE_KEY(voice);
2423 	for (i = 0; i < awe_max_voices; i++) {
2424 		if (voices[i].key == key) {
2425 			switch (cmd) {
2426 			case _GUS_VOICEON:
2427 				awe_note_on(i);
2428 				break;
2429 
2430 			case _GUS_VOICEOFF:
2431 				awe_terminate(i);
2432 				awe_fx_init(voices[i].ch);
2433 				awe_voice_init(i, TRUE);
2434 				break;
2435 
2436 			case _GUS_VOICEFADE:
2437 				awe_note_off(i);
2438 				awe_fx_init(voices[i].ch);
2439 				awe_voice_init(i, FALSE);
2440 				break;
2441 
2442 			case _GUS_VOICEFREQ:
2443 				awe_calc_pitch_from_freq(i, plong);
2444 				break;
2445 			}
2446 		}
2447 	}
2448 }
2449 
2450 #endif /* gus_compat */
2451 
2452 
2453 /* AWE32 specific controls */
2454 static void
awe_hw_awe_control(int dev,int cmd,unsigned char * event)2455 awe_hw_awe_control(int dev, int cmd, unsigned char *event)
2456 {
2457 	int voice;
2458 	unsigned short p1;
2459 	short p2;
2460 	int i;
2461 
2462 	voice = event[3];
2463 	if (! voice_in_range(voice))
2464 		return;
2465 
2466 	if (playing_mode == AWE_PLAY_MULTI2) {
2467 		voice = voice_alloc->map[voice] >> 8;
2468 		if (voice < 0 || voice >= AWE_MAX_CHANNELS)
2469 			return;
2470 	}
2471 
2472 	p1 = *(unsigned short *) &event[4];
2473 	p2 = *(short *) &event[6];
2474 
2475 	switch (cmd) {
2476 	case _AWE_DEBUG_MODE:
2477 		ctrls[AWE_MD_DEBUG_MODE] = p1;
2478 		printk(KERN_DEBUG "AWE32: debug mode = %d\n", ctrls[AWE_MD_DEBUG_MODE]);
2479 		break;
2480 	case _AWE_REVERB_MODE:
2481 		ctrls[AWE_MD_REVERB_MODE] = p1;
2482 		awe_update_reverb_mode();
2483 		break;
2484 
2485 	case _AWE_CHORUS_MODE:
2486 		ctrls[AWE_MD_CHORUS_MODE] = p1;
2487 		awe_update_chorus_mode();
2488 		break;
2489 
2490 	case _AWE_REMOVE_LAST_SAMPLES:
2491 		DEBUG(0,printk("AWE32: remove last samples\n"));
2492 		awe_reset(0);
2493 		if (locked_sf_id > 0)
2494 			awe_remove_samples(locked_sf_id);
2495 		break;
2496 
2497 	case _AWE_INITIALIZE_CHIP:
2498 		awe_initialize();
2499 		break;
2500 
2501 	case _AWE_SEND_EFFECT:
2502 		i = -1;
2503 		if (p1 >= 0x100) {
2504 			i = (p1 >> 8);
2505 			if (i < 0 || i >= MAX_LAYERS)
2506 				break;
2507 		}
2508 		awe_send_effect(voice, i, p1, p2);
2509 		break;
2510 
2511 	case _AWE_RESET_CHANNEL:
2512 		awe_channel_init(voice, !p1);
2513 		break;
2514 
2515 	case _AWE_TERMINATE_ALL:
2516 		awe_reset(0);
2517 		break;
2518 
2519 	case _AWE_TERMINATE_CHANNEL:
2520 		awe_voice_change(voice, awe_terminate_and_init);
2521 		break;
2522 
2523 	case _AWE_RELEASE_ALL:
2524 		awe_note_off_all(FALSE);
2525 		break;
2526 	case _AWE_NOTEOFF_ALL:
2527 		awe_note_off_all(TRUE);
2528 		break;
2529 
2530 	case _AWE_INITIAL_VOLUME:
2531 		DEBUG(0,printk("AWE32: init attenuation %d\n", p1));
2532 		atten_relative = (char)p2;
2533 		atten_offset = (short)p1;
2534 		awe_update_volume();
2535 		break;
2536 
2537 	case _AWE_CHN_PRESSURE:
2538 		channels[voice].chan_press = p1;
2539 		awe_modwheel_change(voice, p1);
2540 		break;
2541 
2542 	case _AWE_CHANNEL_MODE:
2543 		DEBUG(0,printk("AWE32: channel mode = %d\n", p1));
2544 		playing_mode = p1;
2545 		awe_reset(0);
2546 		break;
2547 
2548 	case _AWE_DRUM_CHANNELS:
2549 		DEBUG(0,printk("AWE32: drum flags = %x\n", p1));
2550 		drum_flags = *(unsigned int*)&event[4];
2551 		break;
2552 
2553 	case _AWE_MISC_MODE:
2554 		DEBUG(0,printk("AWE32: ctrl parms = %d %d\n", p1, p2));
2555 		if (p1 > AWE_MD_VERSION && p1 < AWE_MD_END) {
2556 			ctrls[p1] = p2;
2557 			if (ctrl_parms[p1].update)
2558 				ctrl_parms[p1].update();
2559 		}
2560 		break;
2561 
2562 	case _AWE_EQUALIZER:
2563 		ctrls[AWE_MD_BASS_LEVEL] = p1;
2564 		ctrls[AWE_MD_TREBLE_LEVEL] = p2;
2565 		awe_update_equalizer();
2566 		break;
2567 
2568 	default:
2569 		DEBUG(0,printk("AWE32: hw control cmd=%d voice=%d\n", cmd, voice));
2570 		break;
2571 	}
2572 }
2573 
2574 
2575 /* change effects */
2576 static void
awe_send_effect(int voice,int layer,int type,int val)2577 awe_send_effect(int voice, int layer, int type, int val)
2578 {
2579 	awe_chan_info *cinfo;
2580 	FX_Rec *fx;
2581 	int mode;
2582 
2583 	cinfo = &channels[voice];
2584 	if (layer >= 0 && layer < MAX_LAYERS)
2585 		fx = &cinfo->fx_layer[layer];
2586 	else
2587 		fx = &cinfo->fx;
2588 
2589 	if (type & 0x40)
2590 		mode = FX_FLAG_OFF;
2591 	else if (type & 0x80)
2592 		mode = FX_FLAG_ADD;
2593 	else
2594 		mode = FX_FLAG_SET;
2595 	type &= 0x3f;
2596 
2597 	if (type >= 0 && type < AWE_FX_END) {
2598 		DEBUG(2,printk("AWE32: effects (%d) %d %d\n", voice, type, val));
2599 		if (mode == FX_FLAG_SET)
2600 			FX_SET(fx, type, val);
2601 		else if (mode == FX_FLAG_ADD)
2602 			FX_ADD(fx, type, val);
2603 		else
2604 			FX_UNSET(fx, type);
2605 		if (mode != FX_FLAG_OFF && parm_defs[type].realtime) {
2606 			DEBUG(2,printk("AWE32: fx_realtime (%d)\n", voice));
2607 			awe_voice_change(voice, parm_defs[type].realtime);
2608 		}
2609 	}
2610 }
2611 
2612 
2613 /* change modulation wheel; voice is already mapped on multi2 mode */
2614 static void
awe_modwheel_change(int voice,int value)2615 awe_modwheel_change(int voice, int value)
2616 {
2617 	int i;
2618 	awe_chan_info *cinfo;
2619 
2620 	cinfo = &channels[voice];
2621 	i = value * ctrls[AWE_MD_MOD_SENSE] / 1200;
2622 	FX_ADD(&cinfo->fx, AWE_FX_LFO1_PITCH, i);
2623 	awe_voice_change(voice, awe_fx_fmmod);
2624 	FX_ADD(&cinfo->fx, AWE_FX_LFO2_PITCH, i);
2625 	awe_voice_change(voice, awe_fx_fm2frq2);
2626 }
2627 
2628 
2629 /* voice pressure change */
2630 static void
awe_aftertouch(int dev,int voice,int pressure)2631 awe_aftertouch(int dev, int voice, int pressure)
2632 {
2633 	int note;
2634 
2635 	DEBUG(2,printk("AWE32: [after(%d) %d]\n", voice, pressure));
2636 	if (! voice_in_range(voice))
2637 		return;
2638 
2639 	switch (playing_mode) {
2640 	case AWE_PLAY_DIRECT:
2641 	case AWE_PLAY_INDIRECT:
2642 		awe_start_note(dev, voice, 255, pressure);
2643 		break;
2644 	case AWE_PLAY_MULTI2:
2645 		note = (voice_alloc->map[voice] & 0xff) - 1;
2646 		awe_key_pressure(dev, voice, note + 0x80, pressure);
2647 		break;
2648 	}
2649 }
2650 
2651 
2652 /* voice control change */
2653 static void
awe_controller(int dev,int voice,int ctrl_num,int value)2654 awe_controller(int dev, int voice, int ctrl_num, int value)
2655 {
2656 	awe_chan_info *cinfo;
2657 
2658 	if (! voice_in_range(voice))
2659 		return;
2660 
2661 	if (playing_mode == AWE_PLAY_MULTI2) {
2662 		voice = voice_alloc->map[voice] >> 8;
2663 		if (voice < 0 || voice >= AWE_MAX_CHANNELS)
2664 			return;
2665 	}
2666 
2667 	cinfo = &channels[voice];
2668 
2669 	switch (ctrl_num) {
2670 	case CTL_BANK_SELECT: /* MIDI control #0 */
2671 		DEBUG(2,printk("AWE32: [bank(%d) %d]\n", voice, value));
2672 		if (MULTI_LAYER_MODE() && IS_DRUM_CHANNEL(voice) &&
2673 		    !ctrls[AWE_MD_TOGGLE_DRUM_BANK])
2674 			break;
2675 		if (value < 0 || value > 255)
2676 			break;
2677 		cinfo->bank = value;
2678 		if (cinfo->bank == AWE_DRUM_BANK)
2679 			DRUM_CHANNEL_ON(cinfo->channel);
2680 		else
2681 			DRUM_CHANNEL_OFF(cinfo->channel);
2682 		awe_set_instr(dev, voice, cinfo->instr);
2683 		break;
2684 
2685 	case CTL_MODWHEEL: /* MIDI control #1 */
2686 		DEBUG(2,printk("AWE32: [modwheel(%d) %d]\n", voice, value));
2687 		awe_modwheel_change(voice, value);
2688 		break;
2689 
2690 	case CTRL_PITCH_BENDER: /* SEQ1 V2 contorl */
2691 		DEBUG(2,printk("AWE32: [bend(%d) %d]\n", voice, value));
2692 		/* zero centered */
2693 		cinfo->bender = value;
2694 		awe_voice_change(voice, awe_set_voice_pitch);
2695 		break;
2696 
2697 	case CTRL_PITCH_BENDER_RANGE: /* SEQ1 V2 control */
2698 		DEBUG(2,printk("AWE32: [range(%d) %d]\n", voice, value));
2699 		/* value = sense x 100 */
2700 		cinfo->bender_range = value;
2701 		/* no audible pitch change yet.. */
2702 		break;
2703 
2704 	case CTL_EXPRESSION: /* MIDI control #11 */
2705 		if (SINGLE_LAYER_MODE())
2706 			value /= 128;
2707 	case CTRL_EXPRESSION: /* SEQ1 V2 control */
2708 		DEBUG(2,printk("AWE32: [expr(%d) %d]\n", voice, value));
2709 		/* 0 - 127 */
2710 		cinfo->expression_vol = value;
2711 		awe_voice_change(voice, awe_set_voice_vol);
2712 		break;
2713 
2714 	case CTL_PAN:	/* MIDI control #10 */
2715 		DEBUG(2,printk("AWE32: [pan(%d) %d]\n", voice, value));
2716 		/* (0-127) -> signed 8bit */
2717 		cinfo->panning = value * 2 - 128;
2718 		if (ctrls[AWE_MD_REALTIME_PAN])
2719 			awe_voice_change(voice, awe_set_pan);
2720 		break;
2721 
2722 	case CTL_MAIN_VOLUME:	/* MIDI control #7 */
2723 		if (SINGLE_LAYER_MODE())
2724 			value = (value * 100) / 16383;
2725 	case CTRL_MAIN_VOLUME:	/* SEQ1 V2 control */
2726 		DEBUG(2,printk("AWE32: [mainvol(%d) %d]\n", voice, value));
2727 		/* 0 - 127 */
2728 		cinfo->main_vol = value;
2729 		awe_voice_change(voice, awe_set_voice_vol);
2730 		break;
2731 
2732 	case CTL_EXT_EFF_DEPTH: /* reverb effects: 0-127 */
2733 		DEBUG(2,printk("AWE32: [reverb(%d) %d]\n", voice, value));
2734 		FX_SET(&cinfo->fx, AWE_FX_REVERB, value * 2);
2735 		break;
2736 
2737 	case CTL_CHORUS_DEPTH: /* chorus effects: 0-127 */
2738 		DEBUG(2,printk("AWE32: [chorus(%d) %d]\n", voice, value));
2739 		FX_SET(&cinfo->fx, AWE_FX_CHORUS, value * 2);
2740 		break;
2741 
2742 	case 120:  /* all sounds off */
2743 		awe_note_off_all(FALSE);
2744 		break;
2745 	case 123:  /* all notes off */
2746 		awe_note_off_all(TRUE);
2747 		break;
2748 
2749 	case CTL_SUSTAIN: /* MIDI control #64 */
2750 		cinfo->sustained = value;
2751 		if (value != 127)
2752 			awe_voice_change(voice, awe_sustain_off);
2753 		break;
2754 
2755 	case CTL_SOSTENUTO: /* MIDI control #66 */
2756 		if (value == 127)
2757 			awe_voice_change(voice, awe_sostenuto_on);
2758 		else
2759 			awe_voice_change(voice, awe_sustain_off);
2760 		break;
2761 
2762 	default:
2763 		DEBUG(0,printk("AWE32: [control(%d) ctrl=%d val=%d]\n",
2764 			   voice, ctrl_num, value));
2765 		break;
2766 	}
2767 }
2768 
2769 
2770 /* voice pan change (value = -128 - 127) */
2771 static void
awe_panning(int dev,int voice,int value)2772 awe_panning(int dev, int voice, int value)
2773 {
2774 	awe_chan_info *cinfo;
2775 
2776 	if (! voice_in_range(voice))
2777 		return;
2778 
2779 	if (playing_mode == AWE_PLAY_MULTI2) {
2780 		voice = voice_alloc->map[voice] >> 8;
2781 		if (voice < 0 || voice >= AWE_MAX_CHANNELS)
2782 			return;
2783 	}
2784 
2785 	cinfo = &channels[voice];
2786 	cinfo->panning = value;
2787 	DEBUG(2,printk("AWE32: [pan(%d) %d]\n", voice, cinfo->panning));
2788 	if (ctrls[AWE_MD_REALTIME_PAN])
2789 		awe_voice_change(voice, awe_set_pan);
2790 }
2791 
2792 
2793 /* volume mode change */
2794 static void
awe_volume_method(int dev,int mode)2795 awe_volume_method(int dev, int mode)
2796 {
2797 	/* not impremented */
2798 	DEBUG(0,printk("AWE32: [volmethod mode=%d]\n", mode));
2799 }
2800 
2801 
2802 /* pitch wheel change: 0-16384 */
2803 static void
awe_bender(int dev,int voice,int value)2804 awe_bender(int dev, int voice, int value)
2805 {
2806 	awe_chan_info *cinfo;
2807 
2808 	if (! voice_in_range(voice))
2809 		return;
2810 
2811 	if (playing_mode == AWE_PLAY_MULTI2) {
2812 		voice = voice_alloc->map[voice] >> 8;
2813 		if (voice < 0 || voice >= AWE_MAX_CHANNELS)
2814 			return;
2815 	}
2816 
2817 	/* convert to zero centered value */
2818 	cinfo = &channels[voice];
2819 	cinfo->bender = value - 8192;
2820 	DEBUG(2,printk("AWE32: [bend(%d) %d]\n", voice, cinfo->bender));
2821 	awe_voice_change(voice, awe_set_voice_pitch);
2822 }
2823 
2824 
2825 /*
2826  * load a sound patch:
2827  *   three types of patches are accepted: AWE, GUS, and SYSEX.
2828  */
2829 
2830 static int
awe_load_patch(int dev,int format,const char * addr,int offs,int count,int pmgr_flag)2831 awe_load_patch(int dev, int format, const char *addr,
2832 	       int offs, int count, int pmgr_flag)
2833 {
2834 	awe_patch_info patch;
2835 	int rc = 0;
2836 
2837 #ifdef AWE_HAS_GUS_COMPATIBILITY
2838 	if (format == GUS_PATCH) {
2839 		return awe_load_guspatch(addr, offs, count, pmgr_flag);
2840 	} else
2841 #endif
2842 	if (format == SYSEX_PATCH) {
2843 		/* no system exclusive message supported yet */
2844 		return 0;
2845 	} else if (format != AWE_PATCH) {
2846 		printk(KERN_WARNING "AWE32 Error: Invalid patch format (key) 0x%x\n", format);
2847 		return -EINVAL;
2848 	}
2849 
2850 	if (count < AWE_PATCH_INFO_SIZE) {
2851 		printk(KERN_WARNING "AWE32 Error: Patch header too short\n");
2852 		return -EINVAL;
2853 	}
2854 	if (copy_from_user(((char*)&patch) + offs, addr + offs,
2855 			   AWE_PATCH_INFO_SIZE - offs))
2856 		return -EFAULT;
2857 
2858 	count -= AWE_PATCH_INFO_SIZE;
2859 	if (count < patch.len) {
2860 		printk(KERN_WARNING "AWE32: sample: Patch record too short (%d<%d)\n",
2861 		       count, patch.len);
2862 		return -EINVAL;
2863 	}
2864 
2865 	switch (patch.type) {
2866 	case AWE_LOAD_INFO:
2867 		rc = awe_load_info(&patch, addr, count);
2868 		break;
2869 	case AWE_LOAD_DATA:
2870 		rc = awe_load_data(&patch, addr, count);
2871 		break;
2872 	case AWE_OPEN_PATCH:
2873 		rc = awe_open_patch(&patch, addr, count);
2874 		break;
2875 	case AWE_CLOSE_PATCH:
2876 		rc = awe_close_patch(&patch, addr, count);
2877 		break;
2878 	case AWE_UNLOAD_PATCH:
2879 		rc = awe_unload_patch(&patch, addr, count);
2880 		break;
2881 	case AWE_REPLACE_DATA:
2882 		rc = awe_replace_data(&patch, addr, count);
2883 		break;
2884 	case AWE_MAP_PRESET:
2885 		rc = awe_load_map(&patch, addr, count);
2886 		break;
2887 	/* case AWE_PROBE_INFO:
2888 		rc = awe_probe_info(&patch, addr, count);
2889 		break;*/
2890 	case AWE_PROBE_DATA:
2891 		rc = awe_probe_data(&patch, addr, count);
2892 		break;
2893 	case AWE_REMOVE_INFO:
2894 		rc = awe_remove_info(&patch, addr, count);
2895 		break;
2896 	case AWE_LOAD_CHORUS_FX:
2897 		rc = awe_load_chorus_fx(&patch, addr, count);
2898 		break;
2899 	case AWE_LOAD_REVERB_FX:
2900 		rc = awe_load_reverb_fx(&patch, addr, count);
2901 		break;
2902 
2903 	default:
2904 		printk(KERN_WARNING "AWE32 Error: unknown patch format type %d\n",
2905 		       patch.type);
2906 		rc = -EINVAL;
2907 	}
2908 
2909 	return rc;
2910 }
2911 
2912 
2913 /* create an sf list record */
2914 static int
awe_create_sf(int type,char * name)2915 awe_create_sf(int type, char *name)
2916 {
2917 	sf_list *rec;
2918 
2919 	/* terminate sounds */
2920 	awe_reset(0);
2921 	rec = (sf_list *)kmalloc(sizeof(*rec), GFP_KERNEL);
2922 	if (rec == NULL)
2923 		return 1; /* no memory */
2924 	rec->sf_id = current_sf_id + 1;
2925 	rec->type = type;
2926 	if (/*current_sf_id == 0 ||*/ (type & AWE_PAT_LOCKED) != 0)
2927 		locked_sf_id = current_sf_id + 1;
2928 	rec->num_info = awe_free_info();
2929 	rec->num_sample = awe_free_sample();
2930 	rec->mem_ptr = awe_free_mem_ptr();
2931 	rec->infos = rec->last_infos = NULL;
2932 	rec->samples = rec->last_samples = NULL;
2933 
2934 	/* add to linked-list */
2935 	rec->next = NULL;
2936 	rec->prev = sftail;
2937 	if (sftail)
2938 		sftail->next = rec;
2939 	else
2940 		sfhead = rec;
2941 	sftail = rec;
2942 	current_sf_id++;
2943 
2944 #ifdef AWE_ALLOW_SAMPLE_SHARING
2945 	rec->shared = NULL;
2946 	if (name)
2947 		memcpy(rec->name, name, AWE_PATCH_NAME_LEN);
2948 	else
2949 		strcpy(rec->name, "*TEMPORARY*");
2950 	if (current_sf_id > 1 && name && (type & AWE_PAT_SHARED) != 0) {
2951 		/* is the current font really a shared font? */
2952 		if (is_shared_sf(rec->name)) {
2953 			/* check if the shared font is already installed */
2954 			sf_list *p;
2955 			for (p = rec->prev; p; p = p->prev) {
2956 				if (is_identical_name(rec->name, p)) {
2957 					rec->shared = p;
2958 					break;
2959 				}
2960 			}
2961 		}
2962 	}
2963 #endif /* allow sharing */
2964 
2965 	return 0;
2966 }
2967 
2968 
2969 #ifdef AWE_ALLOW_SAMPLE_SHARING
2970 
2971 /* check if the given name is a valid shared name */
2972 #define ASC_TO_KEY(c) ((c) - 'A' + 1)
is_shared_sf(unsigned char * name)2973 static int is_shared_sf(unsigned char *name)
2974 {
2975 	static unsigned char id_head[4] = {
2976 		ASC_TO_KEY('A'), ASC_TO_KEY('W'), ASC_TO_KEY('E'),
2977 		AWE_MAJOR_VERSION,
2978 	};
2979 	if (memcmp(name, id_head, 4) == 0)
2980 		return TRUE;
2981 	return FALSE;
2982 }
2983 
2984 /* check if the given name matches to the existing list */
is_identical_name(unsigned char * name,sf_list * p)2985 static int is_identical_name(unsigned char *name, sf_list *p)
2986 {
2987 	char *id = p->name;
2988 	if (is_shared_sf(id) && memcmp(id, name, AWE_PATCH_NAME_LEN) == 0)
2989 		return TRUE;
2990 	return FALSE;
2991 }
2992 
2993 /* check if the given voice info exists */
info_duplicated(sf_list * sf,awe_voice_list * rec)2994 static int info_duplicated(sf_list *sf, awe_voice_list *rec)
2995 {
2996 	/* search for all sharing lists */
2997 	for (; sf; sf = sf->shared) {
2998 		awe_voice_list *p;
2999 		for (p = sf->infos; p; p = p->next) {
3000 			if (p->type == V_ST_NORMAL &&
3001 			    p->bank == rec->bank &&
3002 			    p->instr == rec->instr &&
3003 			    p->v.low == rec->v.low &&
3004 			    p->v.high == rec->v.high &&
3005 			    p->v.sample == rec->v.sample)
3006 				return TRUE;
3007 		}
3008 	}
3009 	return FALSE;
3010 }
3011 
3012 #endif /* AWE_ALLOW_SAMPLE_SHARING */
3013 
3014 
3015 /* free sf_list record */
3016 /* linked-list in this function is not cared */
3017 static void
awe_free_sf(sf_list * sf)3018 awe_free_sf(sf_list *sf)
3019 {
3020 	if (sf->infos) {
3021 		awe_voice_list *p, *next;
3022 		for (p = sf->infos; p; p = next) {
3023 			next = p->next;
3024 			kfree(p);
3025 		}
3026 	}
3027 	if (sf->samples) {
3028 		awe_sample_list *p, *next;
3029 		for (p = sf->samples; p; p = next) {
3030 			next = p->next;
3031 			kfree(p);
3032 		}
3033 	}
3034 	kfree(sf);
3035 }
3036 
3037 
3038 /* open patch; create sf list and set opened flag */
3039 static int
awe_open_patch(awe_patch_info * patch,const char * addr,int count)3040 awe_open_patch(awe_patch_info *patch, const char *addr, int count)
3041 {
3042 	awe_open_parm parm;
3043 	int shared;
3044 
3045 	if (copy_from_user(&parm, addr + AWE_PATCH_INFO_SIZE, sizeof(parm)))
3046 		return -EFAULT;
3047 	shared = FALSE;
3048 
3049 #ifdef AWE_ALLOW_SAMPLE_SHARING
3050 	if (sftail && (parm.type & AWE_PAT_SHARED) != 0) {
3051 		/* is the previous font the same font? */
3052 		if (is_identical_name(parm.name, sftail)) {
3053 			/* then append to the previous */
3054 			shared = TRUE;
3055 			awe_reset(0);
3056 			if (parm.type & AWE_PAT_LOCKED)
3057 				locked_sf_id = current_sf_id;
3058 		}
3059 	}
3060 #endif /* allow sharing */
3061 	if (! shared) {
3062 		if (awe_create_sf(parm.type, parm.name)) {
3063 			printk(KERN_ERR "AWE32: can't open: failed to alloc new list\n");
3064 			return -ENOMEM;
3065 		}
3066 	}
3067 	patch_opened = TRUE;
3068 	return current_sf_id;
3069 }
3070 
3071 /* check if the patch is already opened */
3072 static sf_list *
check_patch_opened(int type,char * name)3073 check_patch_opened(int type, char *name)
3074 {
3075 	if (! patch_opened) {
3076 		if (awe_create_sf(type, name)) {
3077 			printk(KERN_ERR "AWE32: failed to alloc new list\n");
3078 			return NULL;
3079 		}
3080 		patch_opened = TRUE;
3081 		return sftail;
3082 	}
3083 	return sftail;
3084 }
3085 
3086 /* close the patch; if no voice is loaded, remove the patch */
3087 static int
awe_close_patch(awe_patch_info * patch,const char * addr,int count)3088 awe_close_patch(awe_patch_info *patch, const char *addr, int count)
3089 {
3090 	if (patch_opened && sftail) {
3091 		/* if no voice is loaded, release the current patch */
3092 		if (sftail->infos == NULL) {
3093 			awe_reset(0);
3094 			awe_remove_samples(current_sf_id - 1);
3095 		}
3096 	}
3097 	patch_opened = 0;
3098 	return 0;
3099 }
3100 
3101 
3102 /* remove the latest patch */
3103 static int
awe_unload_patch(awe_patch_info * patch,const char * addr,int count)3104 awe_unload_patch(awe_patch_info *patch, const char *addr, int count)
3105 {
3106 	if (current_sf_id > 0 && current_sf_id > locked_sf_id) {
3107 		awe_reset(0);
3108 		awe_remove_samples(current_sf_id - 1);
3109 	}
3110 	return 0;
3111 }
3112 
3113 /* allocate voice info list records */
3114 static awe_voice_list *
alloc_new_info(void)3115 alloc_new_info(void)
3116 {
3117 	awe_voice_list *newlist;
3118 
3119 	newlist = (awe_voice_list *)kmalloc(sizeof(*newlist), GFP_KERNEL);
3120 	if (newlist == NULL) {
3121 		printk(KERN_ERR "AWE32: can't alloc info table\n");
3122 		return NULL;
3123 	}
3124 	return newlist;
3125 }
3126 
3127 /* allocate sample info list records */
3128 static awe_sample_list *
alloc_new_sample(void)3129 alloc_new_sample(void)
3130 {
3131 	awe_sample_list *newlist;
3132 
3133 	newlist = (awe_sample_list *)kmalloc(sizeof(*newlist), GFP_KERNEL);
3134 	if (newlist == NULL) {
3135 		printk(KERN_ERR "AWE32: can't alloc sample table\n");
3136 		return NULL;
3137 	}
3138 	return newlist;
3139 }
3140 
3141 /* load voice map */
3142 static int
awe_load_map(awe_patch_info * patch,const char * addr,int count)3143 awe_load_map(awe_patch_info *patch, const char *addr, int count)
3144 {
3145 	awe_voice_map map;
3146 	awe_voice_list *rec, *p;
3147 	sf_list *sf;
3148 
3149 	/* get the link info */
3150 	if (count < sizeof(map)) {
3151 		printk(KERN_WARNING "AWE32 Error: invalid patch info length\n");
3152 		return -EINVAL;
3153 	}
3154 	if (copy_from_user(&map, addr + AWE_PATCH_INFO_SIZE, sizeof(map)))
3155 		return -EFAULT;
3156 
3157 	/* check if the identical mapping already exists */
3158 	p = awe_search_instr(map.map_bank, map.map_instr, map.map_key);
3159 	for (; p; p = p->next_instr) {
3160 		if (p->type == V_ST_MAPPED &&
3161 		    p->v.start == map.src_instr &&
3162 		    p->v.end == map.src_bank &&
3163 		    p->v.fixkey == map.src_key)
3164 			return 0; /* already present! */
3165 	}
3166 
3167 	if ((sf = check_patch_opened(AWE_PAT_TYPE_MAP, NULL)) == NULL)
3168 		return -ENOMEM;
3169 
3170 	if ((rec = alloc_new_info()) == NULL)
3171 		return -ENOMEM;
3172 
3173 	rec->bank = map.map_bank;
3174 	rec->instr = map.map_instr;
3175 	rec->type = V_ST_MAPPED;
3176 	rec->disabled = FALSE;
3177 	awe_init_voice_info(&rec->v);
3178 	if (map.map_key >= 0) {
3179 		rec->v.low = map.map_key;
3180 		rec->v.high = map.map_key;
3181 	}
3182 	rec->v.start = map.src_instr;
3183 	rec->v.end = map.src_bank;
3184 	rec->v.fixkey = map.src_key;
3185 	add_sf_info(sf, rec);
3186 	add_info_list(rec);
3187 
3188 	return 0;
3189 }
3190 
3191 #if 0
3192 /* probe preset in the current list -- nothing to be loaded */
3193 static int
3194 awe_probe_info(awe_patch_info *patch, const char *addr, int count)
3195 {
3196 #ifdef AWE_ALLOW_SAMPLE_SHARING
3197 	awe_voice_map map;
3198 	awe_voice_list *p;
3199 
3200 	if (! patch_opened)
3201 		return -EINVAL;
3202 
3203 	/* get the link info */
3204 	if (count < sizeof(map)) {
3205 		printk(KERN_WARNING "AWE32 Error: invalid patch info length\n");
3206 		return -EINVAL;
3207 	}
3208 	if (copy_from_user(&map, addr + AWE_PATCH_INFO_SIZE, sizeof(map)))
3209 		return -EFAULT;
3210 
3211 	/* check if the identical mapping already exists */
3212 	if (sftail == NULL)
3213 		return -EINVAL;
3214 	p = awe_search_instr(map.src_bank, map.src_instr, map.src_key);
3215 	for (; p; p = p->next_instr) {
3216 		if (p->type == V_ST_NORMAL &&
3217 		    is_identical_holder(p->holder, sftail) &&
3218 		    p->v.low <= map.src_key &&
3219 		    p->v.high >= map.src_key)
3220 			return 0; /* already present! */
3221 	}
3222 #endif /* allow sharing */
3223 	return -EINVAL;
3224 }
3225 #endif
3226 
3227 /* probe sample in the current list -- nothing to be loaded */
3228 static int
awe_probe_data(awe_patch_info * patch,const char * addr,int count)3229 awe_probe_data(awe_patch_info *patch, const char *addr, int count)
3230 {
3231 #ifdef AWE_ALLOW_SAMPLE_SHARING
3232 	if (! patch_opened)
3233 		return -EINVAL;
3234 
3235 	/* search the specified sample by optarg */
3236 	if (search_sample_index(sftail, patch->optarg) != NULL)
3237 		return 0;
3238 #endif /* allow sharing */
3239 	return -EINVAL;
3240 }
3241 
3242 
3243 /* remove the present instrument layers */
3244 static int
remove_info(sf_list * sf,int bank,int instr)3245 remove_info(sf_list *sf, int bank, int instr)
3246 {
3247 	awe_voice_list *prev, *next, *p;
3248 	int removed = 0;
3249 
3250 	prev = NULL;
3251 	for (p = sf->infos; p; p = next) {
3252 		next = p->next;
3253 		if (p->type == V_ST_NORMAL &&
3254 		    p->bank == bank && p->instr == instr) {
3255 			/* remove this layer */
3256 			if (prev)
3257 				prev->next = next;
3258 			else
3259 				sf->infos = next;
3260 			if (p == sf->last_infos)
3261 				sf->last_infos = prev;
3262 			sf->num_info--;
3263 			removed++;
3264 			kfree(p);
3265 		} else
3266 			prev = p;
3267 	}
3268 	if (removed)
3269 		rebuild_preset_list();
3270 	return removed;
3271 }
3272 
3273 /* load voice information data */
3274 static int
awe_load_info(awe_patch_info * patch,const char * addr,int count)3275 awe_load_info(awe_patch_info *patch, const char *addr, int count)
3276 {
3277 	int offset;
3278 	awe_voice_rec_hdr hdr;
3279 	int i;
3280 	int total_size;
3281 	sf_list *sf;
3282 	awe_voice_list *rec;
3283 
3284 	if (count < AWE_VOICE_REC_SIZE) {
3285 		printk(KERN_WARNING "AWE32 Error: invalid patch info length\n");
3286 		return -EINVAL;
3287 	}
3288 
3289 	offset = AWE_PATCH_INFO_SIZE;
3290 	if (copy_from_user((char*)&hdr, addr + offset, AWE_VOICE_REC_SIZE))
3291 		return -EFAULT;
3292 	offset += AWE_VOICE_REC_SIZE;
3293 
3294 	if (hdr.nvoices <= 0 || hdr.nvoices >= 100) {
3295 		printk(KERN_WARNING "AWE32 Error: Invalid voice number %d\n", hdr.nvoices);
3296 		return -EINVAL;
3297 	}
3298 	total_size = AWE_VOICE_REC_SIZE + AWE_VOICE_INFO_SIZE * hdr.nvoices;
3299 	if (count < total_size) {
3300 		printk(KERN_WARNING "AWE32 Error: patch length(%d) is smaller than nvoices(%d)\n",
3301 		       count, hdr.nvoices);
3302 		return -EINVAL;
3303 	}
3304 
3305 	if ((sf = check_patch_opened(AWE_PAT_TYPE_MISC, NULL)) == NULL)
3306 		return -ENOMEM;
3307 
3308 	switch (hdr.write_mode) {
3309 	case AWE_WR_EXCLUSIVE:
3310 		/* exclusive mode - if the instrument already exists,
3311 		   return error */
3312 		for (rec = sf->infos; rec; rec = rec->next) {
3313 			if (rec->type == V_ST_NORMAL &&
3314 			    rec->bank == hdr.bank &&
3315 			    rec->instr == hdr.instr)
3316 				return -EINVAL;
3317 		}
3318 		break;
3319 	case AWE_WR_REPLACE:
3320 		/* replace mode - remove the instrument if it already exists */
3321 		remove_info(sf, hdr.bank, hdr.instr);
3322 		break;
3323 	}
3324 
3325 	/* append new layers */
3326 	for (i = 0; i < hdr.nvoices; i++) {
3327 		rec = alloc_new_info();
3328 		if (rec == NULL)
3329 			return -ENOMEM;
3330 
3331 		rec->bank = hdr.bank;
3332 		rec->instr = hdr.instr;
3333 		rec->type = V_ST_NORMAL;
3334 		rec->disabled = FALSE;
3335 
3336 		/* copy awe_voice_info parameters */
3337 		if (copy_from_user(&rec->v, addr + offset, AWE_VOICE_INFO_SIZE)) {
3338 			kfree(rec);
3339 			return -EFAULT;
3340 		}
3341 		offset += AWE_VOICE_INFO_SIZE;
3342 #ifdef AWE_ALLOW_SAMPLE_SHARING
3343 		if (sf && sf->shared) {
3344 			if (info_duplicated(sf, rec)) {
3345 				kfree(rec);
3346 				continue;
3347 			}
3348 		}
3349 #endif /* allow sharing */
3350 		if (rec->v.mode & AWE_MODE_INIT_PARM)
3351 			awe_init_voice_parm(&rec->v.parm);
3352 		add_sf_info(sf, rec);
3353 		awe_set_sample(rec);
3354 		add_info_list(rec);
3355 	}
3356 
3357 	return 0;
3358 }
3359 
3360 
3361 /* remove instrument layers */
3362 static int
awe_remove_info(awe_patch_info * patch,const char * addr,int count)3363 awe_remove_info(awe_patch_info *patch, const char *addr, int count)
3364 {
3365 	unsigned char bank, instr;
3366 	sf_list *sf;
3367 
3368 	if (! patch_opened || (sf = sftail) == NULL) {
3369 		printk(KERN_WARNING "AWE32: remove_info: patch not opened\n");
3370 		return -EINVAL;
3371 	}
3372 
3373 	bank = ((unsigned short)patch->optarg >> 8) & 0xff;
3374 	instr = (unsigned short)patch->optarg & 0xff;
3375 	if (! remove_info(sf, bank, instr))
3376 		return -EINVAL;
3377 	return 0;
3378 }
3379 
3380 
3381 /* load wave sample data */
3382 static int
awe_load_data(awe_patch_info * patch,const char * addr,int count)3383 awe_load_data(awe_patch_info *patch, const char *addr, int count)
3384 {
3385 	int offset, size;
3386 	int rc;
3387 	awe_sample_info tmprec;
3388 	awe_sample_list *rec;
3389 	sf_list *sf;
3390 
3391 	if ((sf = check_patch_opened(AWE_PAT_TYPE_MISC, NULL)) == NULL)
3392 		return -ENOMEM;
3393 
3394 	size = (count - AWE_SAMPLE_INFO_SIZE) / 2;
3395 	offset = AWE_PATCH_INFO_SIZE;
3396 	if (copy_from_user(&tmprec, addr + offset, AWE_SAMPLE_INFO_SIZE))
3397 		return -EFAULT;
3398 	offset += AWE_SAMPLE_INFO_SIZE;
3399 	if (size != tmprec.size) {
3400 		printk(KERN_WARNING "AWE32: load: sample size differed (%d != %d)\n",
3401 		       tmprec.size, size);
3402 		return -EINVAL;
3403 	}
3404 
3405 	if (search_sample_index(sf, tmprec.sample) != NULL) {
3406 #ifdef AWE_ALLOW_SAMPLE_SHARING
3407 		/* if shared sample, skip this data */
3408 		if (sf->type & AWE_PAT_SHARED)
3409 			return 0;
3410 #endif /* allow sharing */
3411 		DEBUG(1,printk("AWE32: sample data %d already present\n", tmprec.sample));
3412 		return -EINVAL;
3413 	}
3414 
3415 	if ((rec = alloc_new_sample()) == NULL)
3416 		return -ENOMEM;
3417 
3418 	memcpy(&rec->v, &tmprec, sizeof(tmprec));
3419 
3420 	if (rec->v.size > 0) {
3421 		if ((rc = awe_write_wave_data(addr, offset, rec, -1)) < 0) {
3422 			kfree(rec);
3423 			return rc;
3424 		}
3425 		sf->mem_ptr += rc;
3426 	}
3427 
3428 	add_sf_sample(sf, rec);
3429 	return 0;
3430 }
3431 
3432 
3433 /* replace wave sample data */
3434 static int
awe_replace_data(awe_patch_info * patch,const char * addr,int count)3435 awe_replace_data(awe_patch_info *patch, const char *addr, int count)
3436 {
3437 	int offset;
3438 	int size;
3439 	int rc;
3440 	int channels;
3441 	awe_sample_info cursmp;
3442 	int save_mem_ptr;
3443 	sf_list *sf;
3444 	awe_sample_list *rec;
3445 
3446 	if (! patch_opened || (sf = sftail) == NULL) {
3447 		printk(KERN_WARNING "AWE32: replace: patch not opened\n");
3448 		return -EINVAL;
3449 	}
3450 
3451 	size = (count - AWE_SAMPLE_INFO_SIZE) / 2;
3452 	offset = AWE_PATCH_INFO_SIZE;
3453 	if (copy_from_user(&cursmp, addr + offset, AWE_SAMPLE_INFO_SIZE))
3454 		return -EFAULT;
3455 	offset += AWE_SAMPLE_INFO_SIZE;
3456 	if (cursmp.size == 0 || size != cursmp.size) {
3457 		printk(KERN_WARNING "AWE32: replace: invalid sample size (%d!=%d)\n",
3458 		       cursmp.size, size);
3459 		return -EINVAL;
3460 	}
3461 	channels = patch->optarg;
3462 	if (channels <= 0 || channels > AWE_NORMAL_VOICES) {
3463 		printk(KERN_WARNING "AWE32: replace: invalid channels %d\n", channels);
3464 		return -EINVAL;
3465 	}
3466 
3467 	for (rec = sf->samples; rec; rec = rec->next) {
3468 		if (rec->v.sample == cursmp.sample)
3469 			break;
3470 	}
3471 	if (rec == NULL) {
3472 		printk(KERN_WARNING "AWE32: replace: cannot find existing sample data %d\n",
3473 		       cursmp.sample);
3474 		return -EINVAL;
3475 	}
3476 
3477 	if (rec->v.size != cursmp.size) {
3478 		printk(KERN_WARNING "AWE32: replace: exiting size differed (%d!=%d)\n",
3479 		       rec->v.size, cursmp.size);
3480 		return -EINVAL;
3481 	}
3482 
3483 	save_mem_ptr = awe_free_mem_ptr();
3484 	sftail->mem_ptr = rec->v.start - awe_mem_start;
3485 	memcpy(&rec->v, &cursmp, sizeof(cursmp));
3486 	rec->v.sf_id = current_sf_id;
3487 	if ((rc = awe_write_wave_data(addr, offset, rec, channels)) < 0)
3488 		return rc;
3489 	sftail->mem_ptr = save_mem_ptr;
3490 
3491 	return 0;
3492 }
3493 
3494 
3495 /*----------------------------------------------------------------*/
3496 
3497 static const char *readbuf_addr;
3498 static int readbuf_offs;
3499 static int readbuf_flags;
3500 
3501 /* initialize read buffer */
3502 static int
readbuf_init(const char * addr,int offset,awe_sample_info * sp)3503 readbuf_init(const char *addr, int offset, awe_sample_info *sp)
3504 {
3505 	readbuf_addr = addr;
3506 	readbuf_offs = offset;
3507 	readbuf_flags = sp->mode_flags;
3508 	return 0;
3509 }
3510 
3511 /* read directly from user buffer */
3512 static unsigned short
readbuf_word(int pos)3513 readbuf_word(int pos)
3514 {
3515 	unsigned short c;
3516 	/* read from user buffer */
3517 	if (readbuf_flags & AWE_SAMPLE_8BITS) {
3518 		unsigned char cc;
3519 		get_user(cc, (unsigned char*)(readbuf_addr + readbuf_offs + pos));
3520 		c = (unsigned short)cc << 8; /* convert 8bit -> 16bit */
3521 	} else {
3522 		get_user(c, (unsigned short*)(readbuf_addr + readbuf_offs + pos * 2));
3523 	}
3524 	if (readbuf_flags & AWE_SAMPLE_UNSIGNED)
3525 		c ^= 0x8000; /* unsigned -> signed */
3526 	return c;
3527 }
3528 
3529 #define readbuf_word_cache	readbuf_word
3530 #define readbuf_end()		/**/
3531 
3532 /*----------------------------------------------------------------*/
3533 
3534 #define BLANK_LOOP_START	8
3535 #define BLANK_LOOP_END		40
3536 #define BLANK_LOOP_SIZE		48
3537 
3538 /* loading onto memory - return the actual written size */
3539 static int
awe_write_wave_data(const char * addr,int offset,awe_sample_list * list,int channels)3540 awe_write_wave_data(const char *addr, int offset, awe_sample_list *list, int channels)
3541 {
3542 	int i, truesize, dram_offset;
3543 	awe_sample_info *sp = &list->v;
3544 	int rc;
3545 
3546 	/* be sure loop points start < end */
3547 	if (sp->loopstart > sp->loopend) {
3548 		int tmp = sp->loopstart;
3549 		sp->loopstart = sp->loopend;
3550 		sp->loopend = tmp;
3551 	}
3552 
3553 	/* compute true data size to be loaded */
3554 	truesize = sp->size;
3555 	if (sp->mode_flags & (AWE_SAMPLE_BIDIR_LOOP|AWE_SAMPLE_REVERSE_LOOP))
3556 		truesize += sp->loopend - sp->loopstart;
3557 	if (sp->mode_flags & AWE_SAMPLE_NO_BLANK)
3558 		truesize += BLANK_LOOP_SIZE;
3559 	if (awe_free_mem_ptr() + truesize >= memsize/2) {
3560 		DEBUG(-1,printk("AWE32 Error: Sample memory full\n"));
3561 		return -ENOSPC;
3562 	}
3563 
3564 	/* recalculate address offset */
3565 	sp->end -= sp->start;
3566 	sp->loopstart -= sp->start;
3567 	sp->loopend -= sp->start;
3568 
3569 	dram_offset = awe_free_mem_ptr() + awe_mem_start;
3570 	sp->start = dram_offset;
3571 	sp->end += dram_offset;
3572 	sp->loopstart += dram_offset;
3573 	sp->loopend += dram_offset;
3574 
3575 	/* set the total size (store onto obsolete checksum value) */
3576 	if (sp->size == 0)
3577 		sp->checksum = 0;
3578 	else
3579 		sp->checksum = truesize;
3580 
3581 	if ((rc = awe_open_dram_for_write(dram_offset, channels)) != 0)
3582 		return rc;
3583 
3584 	if (readbuf_init(addr, offset, sp) < 0)
3585 		return -ENOSPC;
3586 
3587 	for (i = 0; i < sp->size; i++) {
3588 		unsigned short c;
3589 		c = readbuf_word(i);
3590 		awe_write_dram(c);
3591 		if (i == sp->loopend &&
3592 		    (sp->mode_flags & (AWE_SAMPLE_BIDIR_LOOP|AWE_SAMPLE_REVERSE_LOOP))) {
3593 			int looplen = sp->loopend - sp->loopstart;
3594 			/* copy reverse loop */
3595 			int k;
3596 			for (k = 1; k <= looplen; k++) {
3597 				c = readbuf_word_cache(i - k);
3598 				awe_write_dram(c);
3599 			}
3600 			if (sp->mode_flags & AWE_SAMPLE_BIDIR_LOOP) {
3601 				sp->end += looplen;
3602 			} else {
3603 				sp->start += looplen;
3604 				sp->end += looplen;
3605 			}
3606 		}
3607 	}
3608 	readbuf_end();
3609 
3610 	/* if no blank loop is attached in the sample, add it */
3611 	if (sp->mode_flags & AWE_SAMPLE_NO_BLANK) {
3612 		for (i = 0; i < BLANK_LOOP_SIZE; i++)
3613 			awe_write_dram(0);
3614 		if (sp->mode_flags & AWE_SAMPLE_SINGLESHOT) {
3615 			sp->loopstart = sp->end + BLANK_LOOP_START;
3616 			sp->loopend = sp->end + BLANK_LOOP_END;
3617 		}
3618 	}
3619 
3620 	awe_close_dram();
3621 
3622 	/* initialize FM */
3623 	awe_init_fm();
3624 
3625 	return truesize;
3626 }
3627 
3628 
3629 /*----------------------------------------------------------------*/
3630 
3631 #ifdef AWE_HAS_GUS_COMPATIBILITY
3632 
3633 /* calculate GUS envelope time:
3634  * is this correct?  i have no idea..
3635  */
3636 static int
calc_gus_envelope_time(int rate,int start,int end)3637 calc_gus_envelope_time(int rate, int start, int end)
3638 {
3639 	int r, p, t;
3640 	r = (3 - ((rate >> 6) & 3)) * 3;
3641 	p = rate & 0x3f;
3642 	t = end - start;
3643 	if (t < 0) t = -t;
3644 	if (13 > r)
3645 		t = t << (13 - r);
3646 	else
3647 		t = t >> (r - 13);
3648 	return (t * 10) / (p * 441);
3649 }
3650 
3651 #define calc_gus_sustain(val)  (0x7f - vol_table[(val)/2])
3652 #define calc_gus_attenuation(val)	vol_table[(val)/2]
3653 
3654 /* load GUS patch */
3655 static int
awe_load_guspatch(const char * addr,int offs,int size,int pmgr_flag)3656 awe_load_guspatch(const char *addr, int offs, int size, int pmgr_flag)
3657 {
3658 	struct patch_info patch;
3659 	awe_voice_info *rec;
3660 	awe_sample_info *smp;
3661 	awe_voice_list *vrec;
3662 	awe_sample_list *smprec;
3663 	int sizeof_patch;
3664 	int note, rc;
3665 	sf_list *sf;
3666 
3667 	sizeof_patch = (int)((long)&patch.data[0] - (long)&patch); /* header size */
3668 	if (size < sizeof_patch) {
3669 		printk(KERN_WARNING "AWE32 Error: Patch header too short\n");
3670 		return -EINVAL;
3671 	}
3672 	if (copy_from_user(((char*)&patch) + offs, addr + offs, sizeof_patch - offs))
3673 		return -EFAULT;
3674 	size -= sizeof_patch;
3675 	if (size < patch.len) {
3676 		printk(KERN_WARNING "AWE32 Error: Patch record too short (%d<%d)\n",
3677 		       size, patch.len);
3678 		return -EINVAL;
3679 	}
3680 	if ((sf = check_patch_opened(AWE_PAT_TYPE_GUS, NULL)) == NULL)
3681 		return -ENOMEM;
3682 	if ((smprec = alloc_new_sample()) == NULL)
3683 		return -ENOMEM;
3684 	if ((vrec = alloc_new_info()) == NULL) {
3685 		kfree(smprec);
3686 		return -ENOMEM;
3687 	}
3688 
3689 	smp = &smprec->v;
3690 	smp->sample = sf->num_sample;
3691 	smp->start = 0;
3692 	smp->end = patch.len;
3693 	smp->loopstart = patch.loop_start;
3694 	smp->loopend = patch.loop_end;
3695 	smp->size = patch.len;
3696 
3697 	/* set up mode flags */
3698 	smp->mode_flags = 0;
3699 	if (!(patch.mode & WAVE_16_BITS))
3700 		smp->mode_flags |= AWE_SAMPLE_8BITS;
3701 	if (patch.mode & WAVE_UNSIGNED)
3702 		smp->mode_flags |= AWE_SAMPLE_UNSIGNED;
3703 	smp->mode_flags |= AWE_SAMPLE_NO_BLANK;
3704 	if (!(patch.mode & (WAVE_LOOPING|WAVE_BIDIR_LOOP|WAVE_LOOP_BACK)))
3705 		smp->mode_flags |= AWE_SAMPLE_SINGLESHOT;
3706 	if (patch.mode & WAVE_BIDIR_LOOP)
3707 		smp->mode_flags |= AWE_SAMPLE_BIDIR_LOOP;
3708 	if (patch.mode & WAVE_LOOP_BACK)
3709 		smp->mode_flags |= AWE_SAMPLE_REVERSE_LOOP;
3710 
3711 	DEBUG(0,printk("AWE32: [sample %d mode %x]\n", patch.instr_no, smp->mode_flags));
3712 	if (patch.mode & WAVE_16_BITS) {
3713 		/* convert to word offsets */
3714 		smp->size /= 2;
3715 		smp->end /= 2;
3716 		smp->loopstart /= 2;
3717 		smp->loopend /= 2;
3718 	}
3719 	smp->checksum_flag = 0;
3720 	smp->checksum = 0;
3721 
3722 	if ((rc = awe_write_wave_data(addr, sizeof_patch, smprec, -1)) < 0)
3723 		return rc;
3724 	sf->mem_ptr += rc;
3725 	add_sf_sample(sf, smprec);
3726 
3727 	/* set up voice info */
3728 	rec = &vrec->v;
3729 	awe_init_voice_info(rec);
3730 	rec->sample = sf->num_info; /* the last sample */
3731 	rec->rate_offset = calc_rate_offset(patch.base_freq);
3732 	note = freq_to_note(patch.base_note);
3733 	rec->root = note / 100;
3734 	rec->tune = -(note % 100);
3735 	rec->low = freq_to_note(patch.low_note) / 100;
3736 	rec->high = freq_to_note(patch.high_note) / 100;
3737 	DEBUG(1,printk("AWE32: [gus base offset=%d, note=%d, range=%d-%d(%d-%d)]\n",
3738 		       rec->rate_offset, note,
3739 		       rec->low, rec->high,
3740 	      patch.low_note, patch.high_note));
3741 	/* panning position; -128 - 127 => 0-127 */
3742 	rec->pan = (patch.panning + 128) / 2;
3743 
3744 	/* detuning is ignored */
3745 	/* 6points volume envelope */
3746 	if (patch.mode & WAVE_ENVELOPES) {
3747 		int attack, hold, decay, release;
3748 		attack = calc_gus_envelope_time
3749 			(patch.env_rate[0], 0, patch.env_offset[0]);
3750 		hold = calc_gus_envelope_time
3751 			(patch.env_rate[1], patch.env_offset[0],
3752 			 patch.env_offset[1]);
3753 		decay = calc_gus_envelope_time
3754 			(patch.env_rate[2], patch.env_offset[1],
3755 			 patch.env_offset[2]);
3756 		release = calc_gus_envelope_time
3757 			(patch.env_rate[3], patch.env_offset[1],
3758 			 patch.env_offset[4]);
3759 		release += calc_gus_envelope_time
3760 			(patch.env_rate[4], patch.env_offset[3],
3761 			 patch.env_offset[4]);
3762 		release += calc_gus_envelope_time
3763 			(patch.env_rate[5], patch.env_offset[4],
3764 			 patch.env_offset[5]);
3765 		rec->parm.volatkhld = (calc_parm_hold(hold) << 8) |
3766 			calc_parm_attack(attack);
3767 		rec->parm.voldcysus = (calc_gus_sustain(patch.env_offset[2]) << 8) |
3768 			calc_parm_decay(decay);
3769 		rec->parm.volrelease = 0x8000 | calc_parm_decay(release);
3770 		DEBUG(2,printk("AWE32: [gusenv atk=%d, hld=%d, dcy=%d, rel=%d]\n", attack, hold, decay, release));
3771 		rec->attenuation = calc_gus_attenuation(patch.env_offset[0]);
3772 	}
3773 
3774 	/* tremolo effect */
3775 	if (patch.mode & WAVE_TREMOLO) {
3776 		int rate = (patch.tremolo_rate * 1000 / 38) / 42;
3777 		rec->parm.tremfrq = ((patch.tremolo_depth / 2) << 8) | rate;
3778 		DEBUG(2,printk("AWE32: [gusenv tremolo rate=%d, dep=%d, tremfrq=%x]\n",
3779 			       patch.tremolo_rate, patch.tremolo_depth,
3780 			       rec->parm.tremfrq));
3781 	}
3782 	/* vibrato effect */
3783 	if (patch.mode & WAVE_VIBRATO) {
3784 		int rate = (patch.vibrato_rate * 1000 / 38) / 42;
3785 		rec->parm.fm2frq2 = ((patch.vibrato_depth / 6) << 8) | rate;
3786 		DEBUG(2,printk("AWE32: [gusenv vibrato rate=%d, dep=%d, tremfrq=%x]\n",
3787 			       patch.tremolo_rate, patch.tremolo_depth,
3788 			       rec->parm.tremfrq));
3789 	}
3790 
3791 	/* scale_freq, scale_factor, volume, and fractions not implemented */
3792 
3793 	/* append to the tail of the list */
3794 	vrec->bank = ctrls[AWE_MD_GUS_BANK];
3795 	vrec->instr = patch.instr_no;
3796 	vrec->disabled = FALSE;
3797 	vrec->type = V_ST_NORMAL;
3798 
3799 	add_sf_info(sf, vrec);
3800 	add_info_list(vrec);
3801 
3802 	/* set the voice index */
3803 	awe_set_sample(vrec);
3804 
3805 	return 0;
3806 }
3807 
3808 #endif  /* AWE_HAS_GUS_COMPATIBILITY */
3809 
3810 /*
3811  * sample and voice list handlers
3812  */
3813 
3814 /* append this to the current sf list */
add_sf_info(sf_list * sf,awe_voice_list * rec)3815 static void add_sf_info(sf_list *sf, awe_voice_list *rec)
3816 {
3817 	if (sf == NULL)
3818 		return;
3819 	rec->holder = sf;
3820 	rec->v.sf_id = sf->sf_id;
3821 	if (sf->last_infos)
3822 		sf->last_infos->next = rec;
3823 	else
3824 		sf->infos = rec;
3825 	sf->last_infos = rec;
3826 	rec->next = NULL;
3827 	sf->num_info++;
3828 }
3829 
3830 /* prepend this sample to sf list */
add_sf_sample(sf_list * sf,awe_sample_list * rec)3831 static void add_sf_sample(sf_list *sf, awe_sample_list *rec)
3832 {
3833 	if (sf == NULL)
3834 		return;
3835 	rec->holder = sf;
3836 	rec->v.sf_id = sf->sf_id;
3837 	if (sf->last_samples)
3838 		sf->last_samples->next = rec;
3839 	else
3840 		sf->samples = rec;
3841 	sf->last_samples = rec;
3842 	rec->next = NULL;
3843 	sf->num_sample++;
3844 }
3845 
3846 /* purge the old records which don't belong with the same file id */
purge_old_list(awe_voice_list * rec,awe_voice_list * next)3847 static void purge_old_list(awe_voice_list *rec, awe_voice_list *next)
3848 {
3849 	rec->next_instr = next;
3850 	if (rec->bank == AWE_DRUM_BANK) {
3851 		/* remove samples with the same note range */
3852 		awe_voice_list *cur, *prev = rec;
3853 		int low = rec->v.low;
3854 		int high = rec->v.high;
3855 		for (cur = next; cur; cur = cur->next_instr) {
3856 			if (cur->v.low == low &&
3857 			    cur->v.high == high &&
3858 			    ! is_identical_holder(cur->holder, rec->holder))
3859 				prev->next_instr = cur->next_instr;
3860 			else
3861 				prev = cur;
3862 		}
3863 	} else {
3864 		if (! is_identical_holder(next->holder, rec->holder))
3865 			/* remove all samples */
3866 			rec->next_instr = NULL;
3867 	}
3868 }
3869 
3870 /* prepend to top of the preset table */
add_info_list(awe_voice_list * rec)3871 static void add_info_list(awe_voice_list *rec)
3872 {
3873 	awe_voice_list *prev, *cur;
3874 	int key;
3875 
3876 	if (rec->disabled)
3877 		return;
3878 
3879 	key = awe_search_key(rec->bank, rec->instr, rec->v.low);
3880 	prev = NULL;
3881 	for (cur = preset_table[key]; cur; cur = cur->next_bank) {
3882 		/* search the first record with the same bank number */
3883 		if (cur->instr == rec->instr && cur->bank == rec->bank) {
3884 			/* replace the list with the new record */
3885 			rec->next_bank = cur->next_bank;
3886 			if (prev)
3887 				prev->next_bank = rec;
3888 			else
3889 				preset_table[key] = rec;
3890 			purge_old_list(rec, cur);
3891 			return;
3892 		}
3893 		prev = cur;
3894 	}
3895 
3896 	/* this is the first bank record.. just add this */
3897 	rec->next_instr = NULL;
3898 	rec->next_bank = preset_table[key];
3899 	preset_table[key] = rec;
3900 }
3901 
3902 /* remove samples later than the specified sf_id */
3903 static void
awe_remove_samples(int sf_id)3904 awe_remove_samples(int sf_id)
3905 {
3906 	sf_list *p, *prev;
3907 
3908 	if (sf_id <= 0) {
3909 		awe_reset_samples();
3910 		return;
3911 	}
3912 	/* already removed? */
3913 	if (current_sf_id <= sf_id)
3914 		return;
3915 
3916 	for (p = sftail; p; p = prev) {
3917 		if (p->sf_id <= sf_id)
3918 			break;
3919 		prev = p->prev;
3920 		awe_free_sf(p);
3921 	}
3922 	sftail = p;
3923 	if (sftail) {
3924 		sf_id = sftail->sf_id;
3925 		sftail->next = NULL;
3926 	} else {
3927 		sf_id = 0;
3928 		sfhead = NULL;
3929 	}
3930 	current_sf_id = sf_id;
3931 	if (locked_sf_id > sf_id)
3932 		locked_sf_id = sf_id;
3933 
3934 	rebuild_preset_list();
3935 }
3936 
3937 /* rebuild preset search list */
rebuild_preset_list(void)3938 static void rebuild_preset_list(void)
3939 {
3940 	sf_list *p;
3941 	awe_voice_list *rec;
3942 
3943 	memset(preset_table, 0, sizeof(preset_table));
3944 
3945 	for (p = sfhead; p; p = p->next) {
3946 		for (rec = p->infos; rec; rec = rec->next)
3947 			add_info_list(rec);
3948 	}
3949 }
3950 
3951 /* compare the given sf_id pair */
is_identical_holder(sf_list * sf1,sf_list * sf2)3952 static int is_identical_holder(sf_list *sf1, sf_list *sf2)
3953 {
3954 	if (sf1 == NULL || sf2 == NULL)
3955 		return FALSE;
3956 	if (sf1 == sf2)
3957 		return TRUE;
3958 #ifdef AWE_ALLOW_SAMPLE_SHARING
3959 	{
3960 		/* compare with the sharing id */
3961 		sf_list *p;
3962 		int counter = 0;
3963 		if (sf1->sf_id < sf2->sf_id) { /* make sure id1 > id2 */
3964 			sf_list *tmp; tmp = sf1; sf1 = sf2; sf2 = tmp;
3965 		}
3966 		for (p = sf1->shared; p; p = p->shared) {
3967 			if (counter++ > current_sf_id)
3968 				break; /* strange sharing loop.. quit */
3969 			if (p == sf2)
3970 				return TRUE;
3971 		}
3972 	}
3973 #endif /* allow sharing */
3974 	return FALSE;
3975 }
3976 
3977 /* search the sample index matching with the given sample id */
3978 static awe_sample_list *
search_sample_index(sf_list * sf,int sample)3979 search_sample_index(sf_list *sf, int sample)
3980 {
3981 	awe_sample_list *p;
3982 #ifdef AWE_ALLOW_SAMPLE_SHARING
3983 	int counter = 0;
3984 	while (sf) {
3985 		for (p = sf->samples; p; p = p->next) {
3986 			if (p->v.sample == sample)
3987 				return p;
3988 		}
3989 		sf = sf->shared;
3990 		if (counter++ > current_sf_id)
3991 			break; /* strange sharing loop.. quit */
3992 	}
3993 #else
3994 	if (sf) {
3995 		for (p = sf->samples; p; p = p->next) {
3996 			if (p->v.sample == sample)
3997 				return p;
3998 		}
3999 	}
4000 #endif
4001 	return NULL;
4002 }
4003 
4004 /* search the specified sample */
4005 /* non-zero = found */
4006 static short
awe_set_sample(awe_voice_list * rec)4007 awe_set_sample(awe_voice_list *rec)
4008 {
4009 	awe_sample_list *smp;
4010 	awe_voice_info *vp = &rec->v;
4011 
4012 	vp->index = 0;
4013 	if ((smp = search_sample_index(rec->holder, vp->sample)) == NULL)
4014 		return 0;
4015 
4016 	/* set the actual sample offsets */
4017 	vp->start += smp->v.start;
4018 	vp->end += smp->v.end;
4019 	vp->loopstart += smp->v.loopstart;
4020 	vp->loopend += smp->v.loopend;
4021 	/* copy mode flags */
4022 	vp->mode = smp->v.mode_flags;
4023 	/* set flag */
4024 	vp->index = 1;
4025 
4026 	return 1;
4027 }
4028 
4029 
4030 /*
4031  * voice allocation
4032  */
4033 
4034 /* look for all voices associated with the specified note & velocity */
4035 static int
awe_search_multi_voices(awe_voice_list * rec,int note,int velocity,awe_voice_info ** vlist)4036 awe_search_multi_voices(awe_voice_list *rec, int note, int velocity,
4037 			awe_voice_info **vlist)
4038 {
4039 	int nvoices;
4040 
4041 	nvoices = 0;
4042 	for (; rec; rec = rec->next_instr) {
4043 		if (note >= rec->v.low &&
4044 		    note <= rec->v.high &&
4045 		    velocity >= rec->v.vellow &&
4046 		    velocity <= rec->v.velhigh) {
4047 			if (rec->type == V_ST_MAPPED) {
4048 				/* mapper */
4049 				vlist[0] = &rec->v;
4050 				return -1;
4051 			}
4052 			vlist[nvoices++] = &rec->v;
4053 			if (nvoices >= AWE_MAX_VOICES)
4054 				break;
4055 		}
4056 	}
4057 	return nvoices;
4058 }
4059 
4060 /* store the voice list from the specified note and velocity.
4061    if the preset is mapped, seek for the destination preset, and rewrite
4062    the note number if necessary.
4063    */
4064 static int
really_alloc_voices(int bank,int instr,int * note,int velocity,awe_voice_info ** vlist)4065 really_alloc_voices(int bank, int instr, int *note, int velocity, awe_voice_info **vlist)
4066 {
4067 	int nvoices;
4068 	awe_voice_list *vrec;
4069 	int level = 0;
4070 
4071 	for (;;) {
4072 		vrec = awe_search_instr(bank, instr, *note);
4073 		nvoices = awe_search_multi_voices(vrec, *note, velocity, vlist);
4074 		if (nvoices == 0) {
4075 			if (bank == AWE_DRUM_BANK)
4076 				/* search default drumset */
4077 				vrec = awe_search_instr(bank, ctrls[AWE_MD_DEF_DRUM], *note);
4078 			else
4079 				/* search default preset */
4080 				vrec = awe_search_instr(ctrls[AWE_MD_DEF_BANK], instr, *note);
4081 			nvoices = awe_search_multi_voices(vrec, *note, velocity, vlist);
4082 		}
4083 		if (nvoices == 0) {
4084 			if (bank == AWE_DRUM_BANK && ctrls[AWE_MD_DEF_DRUM] != 0)
4085 				/* search default drumset */
4086 				vrec = awe_search_instr(bank, 0, *note);
4087 			else if (bank != AWE_DRUM_BANK && ctrls[AWE_MD_DEF_BANK] != 0)
4088 				/* search default preset */
4089 				vrec = awe_search_instr(0, instr, *note);
4090 			nvoices = awe_search_multi_voices(vrec, *note, velocity, vlist);
4091 		}
4092 		if (nvoices < 0) { /* mapping */
4093 			int key = vlist[0]->fixkey;
4094 			instr = vlist[0]->start;
4095 			bank = vlist[0]->end;
4096 			if (level++ > 5) {
4097 				printk(KERN_ERR "AWE32: too deep mapping level\n");
4098 				return 0;
4099 			}
4100 			if (key >= 0)
4101 				*note = key;
4102 		} else
4103 			break;
4104 	}
4105 
4106 	return nvoices;
4107 }
4108 
4109 /* allocate voices corresponding note and velocity; supports multiple insts. */
4110 static void
awe_alloc_multi_voices(int ch,int note,int velocity,int key)4111 awe_alloc_multi_voices(int ch, int note, int velocity, int key)
4112 {
4113 	int i, v, nvoices, bank;
4114 	awe_voice_info *vlist[AWE_MAX_VOICES];
4115 
4116 	if (MULTI_LAYER_MODE() && IS_DRUM_CHANNEL(ch))
4117 		bank = AWE_DRUM_BANK; /* always search drumset */
4118 	else
4119 		bank = channels[ch].bank;
4120 
4121 	/* check the possible voices; note may be changeable if mapped */
4122 	nvoices = really_alloc_voices(bank, channels[ch].instr,
4123 				      &note, velocity, vlist);
4124 
4125 	/* set the voices */
4126 	current_alloc_time++;
4127 	for (i = 0; i < nvoices; i++) {
4128 		v = awe_clear_voice();
4129 		voices[v].key = key;
4130 		voices[v].ch = ch;
4131 		voices[v].note = note;
4132 		voices[v].velocity = velocity;
4133 		voices[v].time = current_alloc_time;
4134 		voices[v].cinfo = &channels[ch];
4135 		voices[v].sample = vlist[i];
4136 		voices[v].state = AWE_ST_MARK;
4137 		voices[v].layer = nvoices - i - 1;  /* in reverse order */
4138 	}
4139 
4140 	/* clear the mark in allocated voices */
4141 	for (i = 0; i < awe_max_voices; i++) {
4142 		if (voices[i].state == AWE_ST_MARK)
4143 			voices[i].state = AWE_ST_OFF;
4144 
4145 	}
4146 }
4147 
4148 
4149 /* search an empty voice.
4150    if no empty voice is found, at least terminate a voice
4151    */
4152 static int
awe_clear_voice(void)4153 awe_clear_voice(void)
4154 {
4155 	enum {
4156 		OFF=0, RELEASED, SUSTAINED, PLAYING, END
4157 	};
4158 	struct voice_candidate_t {
4159 		int best;
4160 		int time;
4161 		int vtarget;
4162 	} candidate[END];
4163 	int i, type, vtarget;
4164 
4165 	vtarget = 0xffff;
4166 	for (type = OFF; type < END; type++) {
4167 		candidate[type].best = -1;
4168 		candidate[type].time = current_alloc_time + 1;
4169 		candidate[type].vtarget = vtarget;
4170 	}
4171 
4172 	for (i = 0; i < awe_max_voices; i++) {
4173 		if (voices[i].state & AWE_ST_OFF)
4174 			type = OFF;
4175 		else if (voices[i].state & AWE_ST_RELEASED)
4176 			type = RELEASED;
4177 		else if (voices[i].state & AWE_ST_SUSTAINED)
4178 			type = SUSTAINED;
4179 		else if (voices[i].state & ~AWE_ST_MARK)
4180 			type = PLAYING;
4181 		else
4182 			continue;
4183 #ifdef AWE_CHECK_VTARGET
4184 		/* get current volume */
4185 		vtarget = (awe_peek_dw(AWE_VTFT(i)) >> 16) & 0xffff;
4186 #endif
4187 		if (candidate[type].best < 0 ||
4188 		    vtarget < candidate[type].vtarget ||
4189 		    (vtarget == candidate[type].vtarget &&
4190 		     voices[i].time < candidate[type].time)) {
4191 			candidate[type].best = i;
4192 			candidate[type].time = voices[i].time;
4193 			candidate[type].vtarget = vtarget;
4194 		}
4195 	}
4196 
4197 	for (type = OFF; type < END; type++) {
4198 		if ((i = candidate[type].best) >= 0) {
4199 			if (voices[i].state != AWE_ST_OFF)
4200 				awe_terminate(i);
4201 			awe_voice_init(i, TRUE);
4202 			return i;
4203 		}
4204 	}
4205 	return 0;
4206 }
4207 
4208 
4209 /* search sample for the specified note & velocity and set it on the voice;
4210  * note that voice is the voice index (not channel index)
4211  */
4212 static void
awe_alloc_one_voice(int voice,int note,int velocity)4213 awe_alloc_one_voice(int voice, int note, int velocity)
4214 {
4215 	int ch, nvoices, bank;
4216 	awe_voice_info *vlist[AWE_MAX_VOICES];
4217 
4218 	ch = voices[voice].ch;
4219 	if (MULTI_LAYER_MODE() && IS_DRUM_CHANNEL(voice))
4220 		bank = AWE_DRUM_BANK; /* always search drumset */
4221 	else
4222 		bank = voices[voice].cinfo->bank;
4223 
4224 	nvoices = really_alloc_voices(bank, voices[voice].cinfo->instr,
4225 				      &note, velocity, vlist);
4226 	if (nvoices > 0) {
4227 		voices[voice].time = ++current_alloc_time;
4228 		voices[voice].sample = vlist[0]; /* use the first one */
4229 		voices[voice].layer = 0;
4230 		voices[voice].note = note;
4231 		voices[voice].velocity = velocity;
4232 	}
4233 }
4234 
4235 
4236 /*
4237  * sequencer2 functions
4238  */
4239 
4240 /* search an empty voice; used by sequencer2 */
4241 static int
awe_alloc(int dev,int chn,int note,struct voice_alloc_info * alloc)4242 awe_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc)
4243 {
4244 	playing_mode = AWE_PLAY_MULTI2;
4245 	awe_info.nr_voices = AWE_MAX_CHANNELS;
4246 	return awe_clear_voice();
4247 }
4248 
4249 
4250 /* set up voice; used by sequencer2 */
4251 static void
awe_setup_voice(int dev,int voice,int chn)4252 awe_setup_voice(int dev, int voice, int chn)
4253 {
4254 	struct channel_info *info;
4255 	if (synth_devs[dev] == NULL ||
4256 	    (info = &synth_devs[dev]->chn_info[chn]) == NULL)
4257 		return;
4258 
4259 	if (voice < 0 || voice >= awe_max_voices)
4260 		return;
4261 
4262 	DEBUG(2,printk("AWE32: [setup(%d) ch=%d]\n", voice, chn));
4263 	channels[chn].expression_vol = info->controllers[CTL_EXPRESSION];
4264 	channels[chn].main_vol = info->controllers[CTL_MAIN_VOLUME];
4265 	channels[chn].panning =
4266 		info->controllers[CTL_PAN] * 2 - 128; /* signed 8bit */
4267 	channels[chn].bender = info->bender_value; /* zero center */
4268 	channels[chn].bank = info->controllers[CTL_BANK_SELECT];
4269 	channels[chn].sustained = info->controllers[CTL_SUSTAIN];
4270 	if (info->controllers[CTL_EXT_EFF_DEPTH]) {
4271 		FX_SET(&channels[chn].fx, AWE_FX_REVERB,
4272 		       info->controllers[CTL_EXT_EFF_DEPTH] * 2);
4273 	}
4274 	if (info->controllers[CTL_CHORUS_DEPTH]) {
4275 		FX_SET(&channels[chn].fx, AWE_FX_CHORUS,
4276 		       info->controllers[CTL_CHORUS_DEPTH] * 2);
4277 	}
4278 	awe_set_instr(dev, chn, info->pgm_num);
4279 }
4280 
4281 
4282 #ifdef CONFIG_AWE32_MIXER
4283 /*
4284  * AWE32 mixer device control
4285  */
4286 
4287 static int awe_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg);
4288 
4289 static int my_mixerdev = -1;
4290 
4291 static struct mixer_operations awe_mixer_operations = {
4292 	owner:	THIS_MODULE,
4293 	id:	"AWE",
4294 	name:	"AWE32 Equalizer",
4295 	ioctl:	awe_mixer_ioctl,
4296 };
4297 
attach_mixer(void)4298 static void __init attach_mixer(void)
4299 {
4300 	if ((my_mixerdev = sound_alloc_mixerdev()) >= 0) {
4301 		mixer_devs[my_mixerdev] = &awe_mixer_operations;
4302 	}
4303 }
4304 
unload_mixer(void)4305 static void __exit unload_mixer(void)
4306 {
4307 	if (my_mixerdev >= 0)
4308 		sound_unload_mixerdev(my_mixerdev);
4309 }
4310 
4311 static int
awe_mixer_ioctl(int dev,unsigned int cmd,caddr_t arg)4312 awe_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg)
4313 {
4314 	int i, level, value;
4315 
4316 	if (((cmd >> 8) & 0xff) != 'M')
4317 		return -EINVAL;
4318 
4319 	level = *(int*)arg;
4320 	level = ((level & 0xff) + (level >> 8)) / 2;
4321 	DEBUG(0,printk("AWEMix: cmd=%x val=%d\n", cmd & 0xff, level));
4322 
4323 	if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
4324 		switch (cmd & 0xff) {
4325 		case SOUND_MIXER_BASS:
4326 			value = level * 12 / 100;
4327 			if (value >= 12)
4328 				value = 11;
4329 			ctrls[AWE_MD_BASS_LEVEL] = value;
4330 			awe_update_equalizer();
4331 			break;
4332 		case SOUND_MIXER_TREBLE:
4333 			value = level * 12 / 100;
4334 			if (value >= 12)
4335 				value = 11;
4336 			ctrls[AWE_MD_TREBLE_LEVEL] = value;
4337 			awe_update_equalizer();
4338 			break;
4339 		case SOUND_MIXER_VOLUME:
4340 			level = level * 127 / 100;
4341 			if (level >= 128) level = 127;
4342 			atten_relative = FALSE;
4343 			atten_offset = vol_table[level];
4344 			awe_update_volume();
4345 			break;
4346 		}
4347 	}
4348 	switch (cmd & 0xff) {
4349 	case SOUND_MIXER_BASS:
4350 		level = ctrls[AWE_MD_BASS_LEVEL] * 100 / 24;
4351 		level = (level << 8) | level;
4352 		break;
4353 	case SOUND_MIXER_TREBLE:
4354 		level = ctrls[AWE_MD_TREBLE_LEVEL] * 100 / 24;
4355 		level = (level << 8) | level;
4356 		break;
4357 	case SOUND_MIXER_VOLUME:
4358 		value = atten_offset;
4359 		if (atten_relative)
4360 			value += ctrls[AWE_MD_ZERO_ATTEN];
4361 		for (i = 127; i > 0; i--) {
4362 			if (value <= vol_table[i])
4363 				break;
4364 		}
4365 		level = i * 100 / 127;
4366 		level = (level << 8) | level;
4367 		break;
4368 	case SOUND_MIXER_DEVMASK:
4369 		level = SOUND_MASK_BASS|SOUND_MASK_TREBLE|SOUND_MASK_VOLUME;
4370 		break;
4371 	default:
4372 		level = 0;
4373 		break;
4374 	}
4375 	return *(int*)arg = level;
4376 }
4377 #endif /* CONFIG_AWE32_MIXER */
4378 
4379 
4380 /*
4381  * initialization of Emu8000
4382  */
4383 
4384 /* intiailize audio channels */
4385 static void
awe_init_audio(void)4386 awe_init_audio(void)
4387 {
4388 	int ch;
4389 
4390 	/* turn off envelope engines */
4391 	for (ch = 0; ch < AWE_MAX_VOICES; ch++) {
4392 		awe_poke(AWE_DCYSUSV(ch), 0x80);
4393 	}
4394 
4395 	/* reset all other parameters to zero */
4396 	for (ch = 0; ch < AWE_MAX_VOICES; ch++) {
4397 		awe_poke(AWE_ENVVOL(ch), 0);
4398 		awe_poke(AWE_ENVVAL(ch), 0);
4399 		awe_poke(AWE_DCYSUS(ch), 0);
4400 		awe_poke(AWE_ATKHLDV(ch), 0);
4401 		awe_poke(AWE_LFO1VAL(ch), 0);
4402 		awe_poke(AWE_ATKHLD(ch), 0);
4403 		awe_poke(AWE_LFO2VAL(ch), 0);
4404 		awe_poke(AWE_IP(ch), 0);
4405 		awe_poke(AWE_IFATN(ch), 0);
4406 		awe_poke(AWE_PEFE(ch), 0);
4407 		awe_poke(AWE_FMMOD(ch), 0);
4408 		awe_poke(AWE_TREMFRQ(ch), 0);
4409 		awe_poke(AWE_FM2FRQ2(ch), 0);
4410 		awe_poke_dw(AWE_PTRX(ch), 0);
4411 		awe_poke_dw(AWE_VTFT(ch), 0);
4412 		awe_poke_dw(AWE_PSST(ch), 0);
4413 		awe_poke_dw(AWE_CSL(ch), 0);
4414 		awe_poke_dw(AWE_CCCA(ch), 0);
4415 	}
4416 
4417 	for (ch = 0; ch < AWE_MAX_VOICES; ch++) {
4418 		awe_poke_dw(AWE_CPF(ch), 0);
4419 		awe_poke_dw(AWE_CVCF(ch), 0);
4420 	}
4421 }
4422 
4423 
4424 /* initialize DMA address */
4425 static void
awe_init_dma(void)4426 awe_init_dma(void)
4427 {
4428 	awe_poke_dw(AWE_SMALR, 0);
4429 	awe_poke_dw(AWE_SMARR, 0);
4430 	awe_poke_dw(AWE_SMALW, 0);
4431 	awe_poke_dw(AWE_SMARW, 0);
4432 }
4433 
4434 
4435 /* initialization arrays; from ADIP */
4436 
4437 static unsigned short init1[128] = {
4438 	0x03ff, 0x0030,  0x07ff, 0x0130, 0x0bff, 0x0230,  0x0fff, 0x0330,
4439 	0x13ff, 0x0430,  0x17ff, 0x0530, 0x1bff, 0x0630,  0x1fff, 0x0730,
4440 	0x23ff, 0x0830,  0x27ff, 0x0930, 0x2bff, 0x0a30,  0x2fff, 0x0b30,
4441 	0x33ff, 0x0c30,  0x37ff, 0x0d30, 0x3bff, 0x0e30,  0x3fff, 0x0f30,
4442 
4443 	0x43ff, 0x0030,  0x47ff, 0x0130, 0x4bff, 0x0230,  0x4fff, 0x0330,
4444 	0x53ff, 0x0430,  0x57ff, 0x0530, 0x5bff, 0x0630,  0x5fff, 0x0730,
4445 	0x63ff, 0x0830,  0x67ff, 0x0930, 0x6bff, 0x0a30,  0x6fff, 0x0b30,
4446 	0x73ff, 0x0c30,  0x77ff, 0x0d30, 0x7bff, 0x0e30,  0x7fff, 0x0f30,
4447 
4448 	0x83ff, 0x0030,  0x87ff, 0x0130, 0x8bff, 0x0230,  0x8fff, 0x0330,
4449 	0x93ff, 0x0430,  0x97ff, 0x0530, 0x9bff, 0x0630,  0x9fff, 0x0730,
4450 	0xa3ff, 0x0830,  0xa7ff, 0x0930, 0xabff, 0x0a30,  0xafff, 0x0b30,
4451 	0xb3ff, 0x0c30,  0xb7ff, 0x0d30, 0xbbff, 0x0e30,  0xbfff, 0x0f30,
4452 
4453 	0xc3ff, 0x0030,  0xc7ff, 0x0130, 0xcbff, 0x0230,  0xcfff, 0x0330,
4454 	0xd3ff, 0x0430,  0xd7ff, 0x0530, 0xdbff, 0x0630,  0xdfff, 0x0730,
4455 	0xe3ff, 0x0830,  0xe7ff, 0x0930, 0xebff, 0x0a30,  0xefff, 0x0b30,
4456 	0xf3ff, 0x0c30,  0xf7ff, 0x0d30, 0xfbff, 0x0e30,  0xffff, 0x0f30,
4457 };
4458 
4459 static unsigned short init2[128] = {
4460 	0x03ff, 0x8030, 0x07ff, 0x8130, 0x0bff, 0x8230, 0x0fff, 0x8330,
4461 	0x13ff, 0x8430, 0x17ff, 0x8530, 0x1bff, 0x8630, 0x1fff, 0x8730,
4462 	0x23ff, 0x8830, 0x27ff, 0x8930, 0x2bff, 0x8a30, 0x2fff, 0x8b30,
4463 	0x33ff, 0x8c30, 0x37ff, 0x8d30, 0x3bff, 0x8e30, 0x3fff, 0x8f30,
4464 
4465 	0x43ff, 0x8030, 0x47ff, 0x8130, 0x4bff, 0x8230, 0x4fff, 0x8330,
4466 	0x53ff, 0x8430, 0x57ff, 0x8530, 0x5bff, 0x8630, 0x5fff, 0x8730,
4467 	0x63ff, 0x8830, 0x67ff, 0x8930, 0x6bff, 0x8a30, 0x6fff, 0x8b30,
4468 	0x73ff, 0x8c30, 0x77ff, 0x8d30, 0x7bff, 0x8e30, 0x7fff, 0x8f30,
4469 
4470 	0x83ff, 0x8030, 0x87ff, 0x8130, 0x8bff, 0x8230, 0x8fff, 0x8330,
4471 	0x93ff, 0x8430, 0x97ff, 0x8530, 0x9bff, 0x8630, 0x9fff, 0x8730,
4472 	0xa3ff, 0x8830, 0xa7ff, 0x8930, 0xabff, 0x8a30, 0xafff, 0x8b30,
4473 	0xb3ff, 0x8c30, 0xb7ff, 0x8d30, 0xbbff, 0x8e30, 0xbfff, 0x8f30,
4474 
4475 	0xc3ff, 0x8030, 0xc7ff, 0x8130, 0xcbff, 0x8230, 0xcfff, 0x8330,
4476 	0xd3ff, 0x8430, 0xd7ff, 0x8530, 0xdbff, 0x8630, 0xdfff, 0x8730,
4477 	0xe3ff, 0x8830, 0xe7ff, 0x8930, 0xebff, 0x8a30, 0xefff, 0x8b30,
4478 	0xf3ff, 0x8c30, 0xf7ff, 0x8d30, 0xfbff, 0x8e30, 0xffff, 0x8f30,
4479 };
4480 
4481 static unsigned short init3[128] = {
4482 	0x0C10, 0x8470, 0x14FE, 0xB488, 0x167F, 0xA470, 0x18E7, 0x84B5,
4483 	0x1B6E, 0x842A, 0x1F1D, 0x852A, 0x0DA3, 0x8F7C, 0x167E, 0xF254,
4484 	0x0000, 0x842A, 0x0001, 0x852A, 0x18E6, 0x8BAA, 0x1B6D, 0xF234,
4485 	0x229F, 0x8429, 0x2746, 0x8529, 0x1F1C, 0x86E7, 0x229E, 0xF224,
4486 
4487 	0x0DA4, 0x8429, 0x2C29, 0x8529, 0x2745, 0x87F6, 0x2C28, 0xF254,
4488 	0x383B, 0x8428, 0x320F, 0x8528, 0x320E, 0x8F02, 0x1341, 0xF264,
4489 	0x3EB6, 0x8428, 0x3EB9, 0x8528, 0x383A, 0x8FA9, 0x3EB5, 0xF294,
4490 	0x3EB7, 0x8474, 0x3EBA, 0x8575, 0x3EB8, 0xC4C3, 0x3EBB, 0xC5C3,
4491 
4492 	0x0000, 0xA404, 0x0001, 0xA504, 0x141F, 0x8671, 0x14FD, 0x8287,
4493 	0x3EBC, 0xE610, 0x3EC8, 0x8C7B, 0x031A, 0x87E6, 0x3EC8, 0x86F7,
4494 	0x3EC0, 0x821E, 0x3EBE, 0xD208, 0x3EBD, 0x821F, 0x3ECA, 0x8386,
4495 	0x3EC1, 0x8C03, 0x3EC9, 0x831E, 0x3ECA, 0x8C4C, 0x3EBF, 0x8C55,
4496 
4497 	0x3EC9, 0xC208, 0x3EC4, 0xBC84, 0x3EC8, 0x8EAD, 0x3EC8, 0xD308,
4498 	0x3EC2, 0x8F7E, 0x3ECB, 0x8219, 0x3ECB, 0xD26E, 0x3EC5, 0x831F,
4499 	0x3EC6, 0xC308, 0x3EC3, 0xB2FF, 0x3EC9, 0x8265, 0x3EC9, 0x8319,
4500 	0x1342, 0xD36E, 0x3EC7, 0xB3FF, 0x0000, 0x8365, 0x1420, 0x9570,
4501 };
4502 
4503 static unsigned short init4[128] = {
4504 	0x0C10, 0x8470, 0x14FE, 0xB488, 0x167F, 0xA470, 0x18E7, 0x84B5,
4505 	0x1B6E, 0x842A, 0x1F1D, 0x852A, 0x0DA3, 0x0F7C, 0x167E, 0x7254,
4506 	0x0000, 0x842A, 0x0001, 0x852A, 0x18E6, 0x0BAA, 0x1B6D, 0x7234,
4507 	0x229F, 0x8429, 0x2746, 0x8529, 0x1F1C, 0x06E7, 0x229E, 0x7224,
4508 
4509 	0x0DA4, 0x8429, 0x2C29, 0x8529, 0x2745, 0x07F6, 0x2C28, 0x7254,
4510 	0x383B, 0x8428, 0x320F, 0x8528, 0x320E, 0x0F02, 0x1341, 0x7264,
4511 	0x3EB6, 0x8428, 0x3EB9, 0x8528, 0x383A, 0x0FA9, 0x3EB5, 0x7294,
4512 	0x3EB7, 0x8474, 0x3EBA, 0x8575, 0x3EB8, 0x44C3, 0x3EBB, 0x45C3,
4513 
4514 	0x0000, 0xA404, 0x0001, 0xA504, 0x141F, 0x0671, 0x14FD, 0x0287,
4515 	0x3EBC, 0xE610, 0x3EC8, 0x0C7B, 0x031A, 0x07E6, 0x3EC8, 0x86F7,
4516 	0x3EC0, 0x821E, 0x3EBE, 0xD208, 0x3EBD, 0x021F, 0x3ECA, 0x0386,
4517 	0x3EC1, 0x0C03, 0x3EC9, 0x031E, 0x3ECA, 0x8C4C, 0x3EBF, 0x0C55,
4518 
4519 	0x3EC9, 0xC208, 0x3EC4, 0xBC84, 0x3EC8, 0x0EAD, 0x3EC8, 0xD308,
4520 	0x3EC2, 0x8F7E, 0x3ECB, 0x0219, 0x3ECB, 0xD26E, 0x3EC5, 0x031F,
4521 	0x3EC6, 0xC308, 0x3EC3, 0x32FF, 0x3EC9, 0x0265, 0x3EC9, 0x8319,
4522 	0x1342, 0xD36E, 0x3EC7, 0x33FF, 0x0000, 0x8365, 0x1420, 0x9570,
4523 };
4524 
4525 
4526 /* send initialization arrays to start up */
4527 static void
awe_init_array(void)4528 awe_init_array(void)
4529 {
4530 	awe_send_array(init1);
4531 	awe_wait(1024);
4532 	awe_send_array(init2);
4533 	awe_send_array(init3);
4534 	awe_poke_dw(AWE_HWCF4, 0);
4535 	awe_poke_dw(AWE_HWCF5, 0x83);
4536 	awe_poke_dw(AWE_HWCF6, 0x8000);
4537 	awe_send_array(init4);
4538 }
4539 
4540 /* send an initialization array */
4541 static void
awe_send_array(unsigned short * data)4542 awe_send_array(unsigned short *data)
4543 {
4544 	int i;
4545 	unsigned short *p;
4546 
4547 	p = data;
4548 	for (i = 0; i < AWE_MAX_VOICES; i++, p++)
4549 		awe_poke(AWE_INIT1(i), *p);
4550 	for (i = 0; i < AWE_MAX_VOICES; i++, p++)
4551 		awe_poke(AWE_INIT2(i), *p);
4552 	for (i = 0; i < AWE_MAX_VOICES; i++, p++)
4553 		awe_poke(AWE_INIT3(i), *p);
4554 	for (i = 0; i < AWE_MAX_VOICES; i++, p++)
4555 		awe_poke(AWE_INIT4(i), *p);
4556 }
4557 
4558 
4559 /*
4560  * set up awe32 channels to some known state.
4561  */
4562 
4563 /* set the envelope & LFO parameters to the default values; see ADIP */
4564 static void
awe_tweak_voice(int i)4565 awe_tweak_voice(int i)
4566 {
4567 	/* set all mod/vol envelope shape to minimum */
4568 	awe_poke(AWE_ENVVOL(i), 0x8000);
4569 	awe_poke(AWE_ENVVAL(i), 0x8000);
4570 	awe_poke(AWE_DCYSUS(i), 0x7F7F);
4571 	awe_poke(AWE_ATKHLDV(i), 0x7F7F);
4572 	awe_poke(AWE_ATKHLD(i), 0x7F7F);
4573 	awe_poke(AWE_PEFE(i), 0);  /* mod envelope height to zero */
4574 	awe_poke(AWE_LFO1VAL(i), 0x8000); /* no delay for LFO1 */
4575 	awe_poke(AWE_LFO2VAL(i), 0x8000);
4576 	awe_poke(AWE_IP(i), 0xE000);	/* no pitch shift */
4577 	awe_poke(AWE_IFATN(i), 0xFF00);	/* volume to minimum */
4578 	awe_poke(AWE_FMMOD(i), 0);
4579 	awe_poke(AWE_TREMFRQ(i), 0);
4580 	awe_poke(AWE_FM2FRQ2(i), 0);
4581 }
4582 
4583 static void
awe_tweak(void)4584 awe_tweak(void)
4585 {
4586 	int i;
4587 	/* reset all channels */
4588 	for (i = 0; i < awe_max_voices; i++)
4589 		awe_tweak_voice(i);
4590 }
4591 
4592 
4593 /*
4594  *  initializes the FM section of AWE32;
4595  *   see Vince Vu's unofficial AWE32 programming guide
4596  */
4597 
4598 static void
awe_init_fm(void)4599 awe_init_fm(void)
4600 {
4601 #ifndef AWE_ALWAYS_INIT_FM
4602 	/* if no extended memory is on board.. */
4603 	if (memsize <= 0)
4604 		return;
4605 #endif
4606 	DEBUG(3,printk("AWE32: initializing FM\n"));
4607 
4608 	/* Initialize the last two channels for DRAM refresh and producing
4609 	   the reverb and chorus effects for Yamaha OPL-3 synthesizer */
4610 
4611 	/* 31: FM left channel, 0xffffe0-0xffffe8 */
4612 	awe_poke(AWE_DCYSUSV(30), 0x80);
4613 	awe_poke_dw(AWE_PSST(30), 0xFFFFFFE0); /* full left */
4614 	awe_poke_dw(AWE_CSL(30), 0x00FFFFE8 |
4615 		    (DEF_FM_CHORUS_DEPTH << 24));
4616 	awe_poke_dw(AWE_PTRX(30), (DEF_FM_REVERB_DEPTH << 8));
4617 	awe_poke_dw(AWE_CPF(30), 0);
4618 	awe_poke_dw(AWE_CCCA(30), 0x00FFFFE3);
4619 
4620 	/* 32: FM right channel, 0xfffff0-0xfffff8 */
4621 	awe_poke(AWE_DCYSUSV(31), 0x80);
4622 	awe_poke_dw(AWE_PSST(31), 0x00FFFFF0); /* full right */
4623 	awe_poke_dw(AWE_CSL(31), 0x00FFFFF8 |
4624 		    (DEF_FM_CHORUS_DEPTH << 24));
4625 	awe_poke_dw(AWE_PTRX(31), (DEF_FM_REVERB_DEPTH << 8));
4626 	awe_poke_dw(AWE_CPF(31), 0x8000);
4627 	awe_poke_dw(AWE_CCCA(31), 0x00FFFFF3);
4628 
4629 	/* skew volume & cutoff */
4630 	awe_poke_dw(AWE_VTFT(30), 0x8000FFFF);
4631 	awe_poke_dw(AWE_VTFT(31), 0x8000FFFF);
4632 
4633 	voices[30].state = AWE_ST_FM;
4634 	voices[31].state = AWE_ST_FM;
4635 
4636 	/* change maximum channels to 30 */
4637 	awe_max_voices = AWE_NORMAL_VOICES;
4638 	if (playing_mode == AWE_PLAY_DIRECT)
4639 		awe_info.nr_voices = awe_max_voices;
4640 	else
4641 		awe_info.nr_voices = AWE_MAX_CHANNELS;
4642 	voice_alloc->max_voice = awe_max_voices;
4643 }
4644 
4645 /*
4646  *  AWE32 DRAM access routines
4647  */
4648 
4649 /* open DRAM write accessing mode */
4650 static int
awe_open_dram_for_write(int offset,int channels)4651 awe_open_dram_for_write(int offset, int channels)
4652 {
4653 	int vidx[AWE_NORMAL_VOICES];
4654 	int i;
4655 
4656 	if (channels < 0 || channels >= AWE_NORMAL_VOICES) {
4657 		channels = AWE_NORMAL_VOICES;
4658 		for (i = 0; i < AWE_NORMAL_VOICES; i++)
4659 			vidx[i] = i;
4660 	} else {
4661 		for (i = 0; i < channels; i++) {
4662 			vidx[i] = awe_clear_voice();
4663 			voices[vidx[i]].state = AWE_ST_MARK;
4664 		}
4665 	}
4666 
4667 	/* use all channels for DMA transfer */
4668 	for (i = 0; i < channels; i++) {
4669 		if (vidx[i] < 0) continue;
4670 		awe_poke(AWE_DCYSUSV(vidx[i]), 0x80);
4671 		awe_poke_dw(AWE_VTFT(vidx[i]), 0);
4672 		awe_poke_dw(AWE_CVCF(vidx[i]), 0);
4673 		awe_poke_dw(AWE_PTRX(vidx[i]), 0x40000000);
4674 		awe_poke_dw(AWE_CPF(vidx[i]), 0x40000000);
4675 		awe_poke_dw(AWE_PSST(vidx[i]), 0);
4676 		awe_poke_dw(AWE_CSL(vidx[i]), 0);
4677 		awe_poke_dw(AWE_CCCA(vidx[i]), 0x06000000);
4678 		voices[vidx[i]].state = AWE_ST_DRAM;
4679 	}
4680 	/* point channels 31 & 32 to ROM samples for DRAM refresh */
4681 	awe_poke_dw(AWE_VTFT(30), 0);
4682 	awe_poke_dw(AWE_PSST(30), 0x1d8);
4683 	awe_poke_dw(AWE_CSL(30), 0x1e0);
4684 	awe_poke_dw(AWE_CCCA(30), 0x1d8);
4685 	awe_poke_dw(AWE_VTFT(31), 0);
4686 	awe_poke_dw(AWE_PSST(31), 0x1d8);
4687 	awe_poke_dw(AWE_CSL(31), 0x1e0);
4688 	awe_poke_dw(AWE_CCCA(31), 0x1d8);
4689 	voices[30].state = AWE_ST_FM;
4690 	voices[31].state = AWE_ST_FM;
4691 
4692 	/* if full bit is on, not ready to write on */
4693 	if (awe_peek_dw(AWE_SMALW) & 0x80000000) {
4694 		for (i = 0; i < channels; i++) {
4695 			awe_poke_dw(AWE_CCCA(vidx[i]), 0);
4696 			voices[vidx[i]].state = AWE_ST_OFF;
4697 		}
4698 		printk("awe: not ready to write..\n");
4699 		return -EPERM;
4700 	}
4701 
4702 	/* set address to write */
4703 	awe_poke_dw(AWE_SMALW, offset);
4704 
4705 	return 0;
4706 }
4707 
4708 /* open DRAM for RAM size detection */
4709 static void
awe_open_dram_for_check(void)4710 awe_open_dram_for_check(void)
4711 {
4712 	int i;
4713 	for (i = 0; i < AWE_NORMAL_VOICES; i++) {
4714 		awe_poke(AWE_DCYSUSV(i), 0x80);
4715 		awe_poke_dw(AWE_VTFT(i), 0);
4716 		awe_poke_dw(AWE_CVCF(i), 0);
4717 		awe_poke_dw(AWE_PTRX(i), 0x40000000);
4718 		awe_poke_dw(AWE_CPF(i), 0x40000000);
4719 		awe_poke_dw(AWE_PSST(i), 0);
4720 		awe_poke_dw(AWE_CSL(i), 0);
4721 		if (i & 1) /* DMA write */
4722 			awe_poke_dw(AWE_CCCA(i), 0x06000000);
4723 		else	   /* DMA read */
4724 			awe_poke_dw(AWE_CCCA(i), 0x04000000);
4725 		voices[i].state = AWE_ST_DRAM;
4726 	}
4727 }
4728 
4729 
4730 /* close dram access */
4731 static void
awe_close_dram(void)4732 awe_close_dram(void)
4733 {
4734 	int i;
4735 	/* wait until FULL bit in SMAxW register be false */
4736 	for (i = 0; i < 10000; i++) {
4737 		if (!(awe_peek_dw(AWE_SMALW) & 0x80000000))
4738 			break;
4739 		awe_wait(10);
4740 	}
4741 
4742 	for (i = 0; i < AWE_NORMAL_VOICES; i++) {
4743 		if (voices[i].state == AWE_ST_DRAM) {
4744 			awe_poke_dw(AWE_CCCA(i), 0);
4745 			awe_poke(AWE_DCYSUSV(i), 0x807F);
4746 			voices[i].state = AWE_ST_OFF;
4747 		}
4748 	}
4749 }
4750 
4751 
4752 /*
4753  * detect presence of AWE32 and check memory size
4754  */
4755 
4756 /* detect emu8000 chip on the specified address; from VV's guide */
4757 
4758 static int __init
awe_detect_base(int addr)4759 awe_detect_base(int addr)
4760 {
4761 	setup_ports(addr, 0, 0);
4762 	if ((awe_peek(AWE_U1) & 0x000F) != 0x000C)
4763 		return 0;
4764 	if ((awe_peek(AWE_HWCF1) & 0x007E) != 0x0058)
4765 		return 0;
4766 	if ((awe_peek(AWE_HWCF2) & 0x0003) != 0x0003)
4767 		return 0;
4768         DEBUG(0,printk("AWE32 found at %x\n", addr));
4769 	return 1;
4770 }
4771 
4772 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
4773 static struct isapnp_device_id isapnp_awe_list[] __initdata = {
4774 	{	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
4775 		ISAPNP_VENDOR('C','T','L'), ISAPNP_FUNCTION(0x0021),
4776 		(unsigned long)"AWE32 WaveTable" },
4777 	{	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
4778 		ISAPNP_VENDOR('C','T','L'), ISAPNP_FUNCTION(0x0022),
4779 		(unsigned long)"AWE64 WaveTable" },
4780 	{	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
4781 		ISAPNP_VENDOR('C','T','L'), ISAPNP_FUNCTION(0x0023),
4782 		(unsigned long)"AWE64 Gold WaveTable" },
4783 	{0}
4784 };
4785 
4786 MODULE_DEVICE_TABLE(isapnp, isapnp_awe_list);
4787 
4788 static struct pci_dev *idev = NULL;
4789 
awe_probe_isapnp(int * port)4790 static int __init awe_probe_isapnp(int *port)
4791 {
4792 	int i;
4793 
4794 	for (i = 0; isapnp_awe_list[i].vendor != 0; i++) {
4795 		while ((idev = isapnp_find_dev(NULL,
4796 		                               isapnp_awe_list[i].vendor,
4797 		                               isapnp_awe_list[i].function,
4798 		                               idev))) {
4799 			if (idev->prepare(idev) < 0)
4800 				continue;
4801 			if (idev->activate(idev) < 0 ||
4802 			    !idev->resource[0].start) {
4803 				idev->deactivate(idev);
4804 				idev->deactivate(idev);
4805 				continue;
4806 			}
4807 			*port = idev->resource[0].start;
4808 			break;
4809 		}
4810 		if (!idev)
4811 			continue;
4812 		printk(KERN_INFO "ISAPnP reports %s at i/o %#x\n",
4813 		       (char*)isapnp_awe_list[i].driver_data, *port);
4814 		return 0;
4815 	}
4816 	return -ENODEV;
4817 }
4818 
awe_deactivate_isapnp(void)4819 static void __exit awe_deactivate_isapnp(void)
4820 {
4821 #if 1
4822 	if (idev) {
4823 		idev->deactivate(idev);
4824 		idev = NULL;
4825 	}
4826 #endif
4827 }
4828 
4829 #endif
4830 
4831 static int __init
awe_detect(void)4832 awe_detect(void)
4833 {
4834 	int base;
4835 
4836 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
4837 	if (isapnp) {
4838 		if (awe_probe_isapnp(&io) < 0) {
4839 			printk(KERN_ERR "AWE32: No ISAPnP cards found\n");
4840 			if (isapnp != -1)
4841 			  return 0;
4842 		} else {
4843 			setup_ports(io, 0, 0);
4844 			return 1;
4845 		}
4846 	}
4847 #endif /* isapnp */
4848 
4849 	if (io) /* use default i/o port value */
4850 		setup_ports(io, 0, 0);
4851 	else { /* probe it */
4852 		for (base = 0x620; base <= 0x680; base += 0x20)
4853 			if (awe_detect_base(base))
4854 				return 1;
4855 		DEBUG(0,printk("AWE32 not found\n"));
4856 		return 0;
4857 	}
4858 
4859 	return 1;
4860 }
4861 
4862 
4863 /*
4864  * check dram size on AWE board
4865  */
4866 
4867 /* any three numbers you like */
4868 #define UNIQUE_ID1	0x1234
4869 #define UNIQUE_ID2	0x4321
4870 #define UNIQUE_ID3	0xABCD
4871 
4872 static void __init
awe_check_dram(void)4873 awe_check_dram(void)
4874 {
4875 	if (awe_present) /* already initialized */
4876 		return;
4877 
4878 	if (memsize >= 0) { /* given by config file or module option */
4879 		memsize *= 1024; /* convert to Kbytes */
4880 		return;
4881 	}
4882 
4883 	awe_open_dram_for_check();
4884 
4885 	memsize = 0;
4886 
4887 	/* set up unique two id numbers */
4888 	awe_poke_dw(AWE_SMALW, AWE_DRAM_OFFSET);
4889 	awe_poke(AWE_SMLD, UNIQUE_ID1);
4890 	awe_poke(AWE_SMLD, UNIQUE_ID2);
4891 
4892 	while (memsize < AWE_MAX_DRAM_SIZE) {
4893 		awe_wait(5);
4894 		/* read a data on the DRAM start address */
4895 		awe_poke_dw(AWE_SMALR, AWE_DRAM_OFFSET);
4896 		awe_peek(AWE_SMLD); /* discard stale data  */
4897 		if (awe_peek(AWE_SMLD) != UNIQUE_ID1)
4898 			break;
4899 		if (awe_peek(AWE_SMLD) != UNIQUE_ID2)
4900 			break;
4901 		memsize += 512;  /* increment 512kbytes */
4902 		/* Write a unique data on the test address;
4903 		 * if the address is out of range, the data is written on
4904 		 * 0x200000(=AWE_DRAM_OFFSET).  Then the two id words are
4905 		 * broken by this data.
4906 		 */
4907 		awe_poke_dw(AWE_SMALW, AWE_DRAM_OFFSET + memsize*512L);
4908 		awe_poke(AWE_SMLD, UNIQUE_ID3);
4909 		awe_wait(5);
4910 		/* read a data on the just written DRAM address */
4911 		awe_poke_dw(AWE_SMALR, AWE_DRAM_OFFSET + memsize*512L);
4912 		awe_peek(AWE_SMLD); /* discard stale data  */
4913 		if (awe_peek(AWE_SMLD) != UNIQUE_ID3)
4914 			break;
4915 	}
4916 	awe_close_dram();
4917 
4918 	DEBUG(0,printk("AWE32: %d Kbytes memory detected\n", memsize));
4919 
4920 	/* convert to Kbytes */
4921 	memsize *= 1024;
4922 }
4923 
4924 
4925 /*----------------------------------------------------------------*/
4926 
4927 /*
4928  * chorus and reverb controls; from VV's guide
4929  */
4930 
4931 /* 5 parameters for each chorus mode; 3 x 16bit, 2 x 32bit */
4932 static char chorus_defined[AWE_CHORUS_NUMBERS];
4933 static awe_chorus_fx_rec chorus_parm[AWE_CHORUS_NUMBERS] = {
4934 	{0xE600, 0x03F6, 0xBC2C ,0x00000000, 0x0000006D}, /* chorus 1 */
4935 	{0xE608, 0x031A, 0xBC6E, 0x00000000, 0x0000017C}, /* chorus 2 */
4936 	{0xE610, 0x031A, 0xBC84, 0x00000000, 0x00000083}, /* chorus 3 */
4937 	{0xE620, 0x0269, 0xBC6E, 0x00000000, 0x0000017C}, /* chorus 4 */
4938 	{0xE680, 0x04D3, 0xBCA6, 0x00000000, 0x0000005B}, /* feedback */
4939 	{0xE6E0, 0x044E, 0xBC37, 0x00000000, 0x00000026}, /* flanger */
4940 	{0xE600, 0x0B06, 0xBC00, 0x0000E000, 0x00000083}, /* short delay */
4941 	{0xE6C0, 0x0B06, 0xBC00, 0x0000E000, 0x00000083}, /* short delay + feedback */
4942 };
4943 
4944 static int
awe_load_chorus_fx(awe_patch_info * patch,const char * addr,int count)4945 awe_load_chorus_fx(awe_patch_info *patch, const char *addr, int count)
4946 {
4947 	if (patch->optarg < AWE_CHORUS_PREDEFINED || patch->optarg >= AWE_CHORUS_NUMBERS) {
4948 		printk(KERN_WARNING "AWE32 Error: invalid chorus mode %d for uploading\n", patch->optarg);
4949 		return -EINVAL;
4950 	}
4951 	if (count < sizeof(awe_chorus_fx_rec)) {
4952 		printk(KERN_WARNING "AWE32 Error: too short chorus fx parameters\n");
4953 		return -EINVAL;
4954 	}
4955 	if (copy_from_user(&chorus_parm[patch->optarg], addr + AWE_PATCH_INFO_SIZE,
4956 			   sizeof(awe_chorus_fx_rec)))
4957 		return -EFAULT;
4958 	chorus_defined[patch->optarg] = TRUE;
4959 	return 0;
4960 }
4961 
4962 static void
awe_set_chorus_mode(int effect)4963 awe_set_chorus_mode(int effect)
4964 {
4965 	if (effect < 0 || effect >= AWE_CHORUS_NUMBERS ||
4966 	    (effect >= AWE_CHORUS_PREDEFINED && !chorus_defined[effect]))
4967 		return;
4968 	awe_poke(AWE_INIT3(9), chorus_parm[effect].feedback);
4969 	awe_poke(AWE_INIT3(12), chorus_parm[effect].delay_offset);
4970 	awe_poke(AWE_INIT4(3), chorus_parm[effect].lfo_depth);
4971 	awe_poke_dw(AWE_HWCF4, chorus_parm[effect].delay);
4972 	awe_poke_dw(AWE_HWCF5, chorus_parm[effect].lfo_freq);
4973 	awe_poke_dw(AWE_HWCF6, 0x8000);
4974 	awe_poke_dw(AWE_HWCF7, 0x0000);
4975 }
4976 
4977 static void
awe_update_chorus_mode(void)4978 awe_update_chorus_mode(void)
4979 {
4980 	awe_set_chorus_mode(ctrls[AWE_MD_CHORUS_MODE]);
4981 }
4982 
4983 /*----------------------------------------------------------------*/
4984 
4985 /* reverb mode settings; write the following 28 data of 16 bit length
4986  *   on the corresponding ports in the reverb_cmds array
4987  */
4988 static char reverb_defined[AWE_CHORUS_NUMBERS];
4989 static awe_reverb_fx_rec reverb_parm[AWE_REVERB_NUMBERS] = {
4990 {{  /* room 1 */
4991 	0xB488, 0xA450, 0x9550, 0x84B5, 0x383A, 0x3EB5, 0x72F4,
4992 	0x72A4, 0x7254, 0x7204, 0x7204, 0x7204, 0x4416, 0x4516,
4993 	0xA490, 0xA590, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
4994 	0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
4995 }},
4996 {{  /* room 2 */
4997 	0xB488, 0xA458, 0x9558, 0x84B5, 0x383A, 0x3EB5, 0x7284,
4998 	0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4448, 0x4548,
4999 	0xA440, 0xA540, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
5000 	0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
5001 }},
5002 {{  /* room 3 */
5003 	0xB488, 0xA460, 0x9560, 0x84B5, 0x383A, 0x3EB5, 0x7284,
5004 	0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4416, 0x4516,
5005 	0xA490, 0xA590, 0x842C, 0x852C, 0x842C, 0x852C, 0x842B,
5006 	0x852B, 0x842B, 0x852B, 0x842A, 0x852A, 0x842A, 0x852A,
5007 }},
5008 {{  /* hall 1 */
5009 	0xB488, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7284,
5010 	0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4448, 0x4548,
5011 	0xA440, 0xA540, 0x842B, 0x852B, 0x842B, 0x852B, 0x842A,
5012 	0x852A, 0x842A, 0x852A, 0x8429, 0x8529, 0x8429, 0x8529,
5013 }},
5014 {{  /* hall 2 */
5015 	0xB488, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7254,
5016 	0x7234, 0x7224, 0x7254, 0x7264, 0x7294, 0x44C3, 0x45C3,
5017 	0xA404, 0xA504, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
5018 	0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
5019 }},
5020 {{  /* plate */
5021 	0xB4FF, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7234,
5022 	0x7234, 0x7234, 0x7234, 0x7234, 0x7234, 0x4448, 0x4548,
5023 	0xA440, 0xA540, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
5024 	0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
5025 }},
5026 {{  /* delay */
5027 	0xB4FF, 0xA470, 0x9500, 0x84B5, 0x333A, 0x39B5, 0x7204,
5028 	0x7204, 0x7204, 0x7204, 0x7204, 0x72F4, 0x4400, 0x4500,
5029 	0xA4FF, 0xA5FF, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420,
5030 	0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520,
5031 }},
5032 {{  /* panning delay */
5033 	0xB4FF, 0xA490, 0x9590, 0x8474, 0x333A, 0x39B5, 0x7204,
5034 	0x7204, 0x7204, 0x7204, 0x7204, 0x72F4, 0x4400, 0x4500,
5035 	0xA4FF, 0xA5FF, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420,
5036 	0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520,
5037 }},
5038 };
5039 
5040 static struct ReverbCmdPair {
5041 	unsigned short cmd, port;
5042 } reverb_cmds[28] = {
5043   {AWE_INIT1(0x03)}, {AWE_INIT1(0x05)}, {AWE_INIT4(0x1F)}, {AWE_INIT1(0x07)},
5044   {AWE_INIT2(0x14)}, {AWE_INIT2(0x16)}, {AWE_INIT1(0x0F)}, {AWE_INIT1(0x17)},
5045   {AWE_INIT1(0x1F)}, {AWE_INIT2(0x07)}, {AWE_INIT2(0x0F)}, {AWE_INIT2(0x17)},
5046   {AWE_INIT2(0x1D)}, {AWE_INIT2(0x1F)}, {AWE_INIT3(0x01)}, {AWE_INIT3(0x03)},
5047   {AWE_INIT1(0x09)}, {AWE_INIT1(0x0B)}, {AWE_INIT1(0x11)}, {AWE_INIT1(0x13)},
5048   {AWE_INIT1(0x19)}, {AWE_INIT1(0x1B)}, {AWE_INIT2(0x01)}, {AWE_INIT2(0x03)},
5049   {AWE_INIT2(0x09)}, {AWE_INIT2(0x0B)}, {AWE_INIT2(0x11)}, {AWE_INIT2(0x13)},
5050 };
5051 
5052 static int
awe_load_reverb_fx(awe_patch_info * patch,const char * addr,int count)5053 awe_load_reverb_fx(awe_patch_info *patch, const char *addr, int count)
5054 {
5055 	if (patch->optarg < AWE_REVERB_PREDEFINED || patch->optarg >= AWE_REVERB_NUMBERS) {
5056 		printk(KERN_WARNING "AWE32 Error: invalid reverb mode %d for uploading\n", patch->optarg);
5057 		return -EINVAL;
5058 	}
5059 	if (count < sizeof(awe_reverb_fx_rec)) {
5060 		printk(KERN_WARNING "AWE32 Error: too short reverb fx parameters\n");
5061 		return -EINVAL;
5062 	}
5063 	if (copy_from_user(&reverb_parm[patch->optarg], addr + AWE_PATCH_INFO_SIZE,
5064 			   sizeof(awe_reverb_fx_rec)))
5065 		return -EFAULT;
5066 	reverb_defined[patch->optarg] = TRUE;
5067 	return 0;
5068 }
5069 
5070 static void
awe_set_reverb_mode(int effect)5071 awe_set_reverb_mode(int effect)
5072 {
5073 	int i;
5074 	if (effect < 0 || effect >= AWE_REVERB_NUMBERS ||
5075 	    (effect >= AWE_REVERB_PREDEFINED && !reverb_defined[effect]))
5076 		return;
5077 	for (i = 0; i < 28; i++)
5078 		awe_poke(reverb_cmds[i].cmd, reverb_cmds[i].port,
5079 			 reverb_parm[effect].parms[i]);
5080 }
5081 
5082 static void
awe_update_reverb_mode(void)5083 awe_update_reverb_mode(void)
5084 {
5085 	awe_set_reverb_mode(ctrls[AWE_MD_REVERB_MODE]);
5086 }
5087 
5088 /*
5089  * treble/bass equalizer control
5090  */
5091 
5092 static unsigned short bass_parm[12][3] = {
5093 	{0xD26A, 0xD36A, 0x0000}, /* -12 dB */
5094 	{0xD25B, 0xD35B, 0x0000}, /*  -8 */
5095 	{0xD24C, 0xD34C, 0x0000}, /*  -6 */
5096 	{0xD23D, 0xD33D, 0x0000}, /*  -4 */
5097 	{0xD21F, 0xD31F, 0x0000}, /*  -2 */
5098 	{0xC208, 0xC308, 0x0001}, /*   0 (HW default) */
5099 	{0xC219, 0xC319, 0x0001}, /*  +2 */
5100 	{0xC22A, 0xC32A, 0x0001}, /*  +4 */
5101 	{0xC24C, 0xC34C, 0x0001}, /*  +6 */
5102 	{0xC26E, 0xC36E, 0x0001}, /*  +8 */
5103 	{0xC248, 0xC348, 0x0002}, /* +10 */
5104 	{0xC26A, 0xC36A, 0x0002}, /* +12 dB */
5105 };
5106 
5107 static unsigned short treble_parm[12][9] = {
5108 	{0x821E, 0xC26A, 0x031E, 0xC36A, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001}, /* -12 dB */
5109 	{0x821E, 0xC25B, 0x031E, 0xC35B, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
5110 	{0x821E, 0xC24C, 0x031E, 0xC34C, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
5111 	{0x821E, 0xC23D, 0x031E, 0xC33D, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
5112 	{0x821E, 0xC21F, 0x031E, 0xC31F, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
5113 	{0x821E, 0xD208, 0x031E, 0xD308, 0x021E, 0xD208, 0x831E, 0xD308, 0x0002},
5114 	{0x821E, 0xD208, 0x031E, 0xD308, 0x021D, 0xD219, 0x831D, 0xD319, 0x0002},
5115 	{0x821E, 0xD208, 0x031E, 0xD308, 0x021C, 0xD22A, 0x831C, 0xD32A, 0x0002},
5116 	{0x821E, 0xD208, 0x031E, 0xD308, 0x021A, 0xD24C, 0x831A, 0xD34C, 0x0002},
5117 	{0x821E, 0xD208, 0x031E, 0xD308, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002}, /* +8 (HW default) */
5118 	{0x821D, 0xD219, 0x031D, 0xD319, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002},
5119 	{0x821C, 0xD22A, 0x031C, 0xD32A, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002}, /* +12 dB */
5120 };
5121 
5122 
5123 /*
5124  * set Emu8000 digital equalizer; from 0 to 11 [-12dB - 12dB]
5125  */
5126 static void
awe_equalizer(int bass,int treble)5127 awe_equalizer(int bass, int treble)
5128 {
5129 	unsigned short w;
5130 
5131 	if (bass < 0 || bass > 11 || treble < 0 || treble > 11)
5132 		return;
5133 	awe_poke(AWE_INIT4(0x01), bass_parm[bass][0]);
5134 	awe_poke(AWE_INIT4(0x11), bass_parm[bass][1]);
5135 	awe_poke(AWE_INIT3(0x11), treble_parm[treble][0]);
5136 	awe_poke(AWE_INIT3(0x13), treble_parm[treble][1]);
5137 	awe_poke(AWE_INIT3(0x1B), treble_parm[treble][2]);
5138 	awe_poke(AWE_INIT4(0x07), treble_parm[treble][3]);
5139 	awe_poke(AWE_INIT4(0x0B), treble_parm[treble][4]);
5140 	awe_poke(AWE_INIT4(0x0D), treble_parm[treble][5]);
5141 	awe_poke(AWE_INIT4(0x17), treble_parm[treble][6]);
5142 	awe_poke(AWE_INIT4(0x19), treble_parm[treble][7]);
5143 	w = bass_parm[bass][2] + treble_parm[treble][8];
5144 	awe_poke(AWE_INIT4(0x15), (unsigned short)(w + 0x0262));
5145 	awe_poke(AWE_INIT4(0x1D), (unsigned short)(w + 0x8362));
5146 }
5147 
awe_update_equalizer(void)5148 static void awe_update_equalizer(void)
5149 {
5150 	awe_equalizer(ctrls[AWE_MD_BASS_LEVEL], ctrls[AWE_MD_TREBLE_LEVEL]);
5151 }
5152 
5153 
5154 /*----------------------------------------------------------------*/
5155 
5156 #ifdef CONFIG_AWE32_MIDIEMU
5157 
5158 /*
5159  * Emu8000 MIDI Emulation
5160  */
5161 
5162 /*
5163  * midi queue record
5164  */
5165 
5166 /* queue type */
5167 enum { Q_NONE, Q_VARLEN, Q_READ, Q_SYSEX, };
5168 
5169 #define MAX_MIDIBUF	64
5170 
5171 /* midi status */
5172 typedef struct MidiStatus {
5173 	int queue;	/* queue type */
5174 	int qlen;	/* queue length */
5175 	int read;	/* chars read */
5176 	int status;	/* current status */
5177 	int chan;	/* current channel */
5178 	unsigned char buf[MAX_MIDIBUF];
5179 } MidiStatus;
5180 
5181 /* MIDI mode type */
5182 enum { MODE_GM, MODE_GS, MODE_XG, };
5183 
5184 /* NRPN / CC -> Emu8000 parameter converter */
5185 typedef struct {
5186 	int control;
5187 	int awe_effect;
5188 	unsigned short (*convert)(int val);
5189 } ConvTable;
5190 
5191 
5192 /*
5193  * prototypes
5194  */
5195 
5196 static int awe_midi_open(int dev, int mode, void (*input)(int,unsigned char), void (*output)(int));
5197 static void awe_midi_close(int dev);
5198 static int awe_midi_ioctl(int dev, unsigned cmd, caddr_t arg);
5199 static int awe_midi_outputc(int dev, unsigned char midi_byte);
5200 
5201 static void init_midi_status(MidiStatus *st);
5202 static void clear_rpn(void);
5203 static void get_midi_char(MidiStatus *st, int c);
5204 /*static void queue_varlen(MidiStatus *st, int c);*/
5205 static void special_event(MidiStatus *st, int c);
5206 static void queue_read(MidiStatus *st, int c);
5207 static void midi_note_on(MidiStatus *st);
5208 static void midi_note_off(MidiStatus *st);
5209 static void midi_key_pressure(MidiStatus *st);
5210 static void midi_channel_pressure(MidiStatus *st);
5211 static void midi_pitch_wheel(MidiStatus *st);
5212 static void midi_program_change(MidiStatus *st);
5213 static void midi_control_change(MidiStatus *st);
5214 static void midi_select_bank(MidiStatus *st, int val);
5215 static void midi_nrpn_event(MidiStatus *st);
5216 static void midi_rpn_event(MidiStatus *st);
5217 static void midi_detune(int chan, int coarse, int fine);
5218 static void midi_system_exclusive(MidiStatus *st);
5219 static int send_converted_effect(ConvTable *table, int num_tables, MidiStatus *st, int type, int val);
5220 static int add_converted_effect(ConvTable *table, int num_tables, MidiStatus *st, int type, int val);
5221 static int xg_control_change(MidiStatus *st, int cmd, int val);
5222 
5223 #define numberof(ary)	(sizeof(ary)/sizeof(ary[0]))
5224 
5225 
5226 /*
5227  * OSS Midi device record
5228  */
5229 
5230 static struct midi_operations awe_midi_operations =
5231 {
5232 	owner:		THIS_MODULE,
5233 	info:		{"AWE Midi Emu", 0, 0, SNDCARD_SB},
5234 	in_info:	{0},
5235 	open:		awe_midi_open, /*open*/
5236 	close:		awe_midi_close, /*close*/
5237 	ioctl:		awe_midi_ioctl, /*ioctl*/
5238 	outputc:	awe_midi_outputc, /*outputc*/
5239 };
5240 
5241 static int my_mididev = -1;
5242 
attach_midiemu(void)5243 static void __init attach_midiemu(void)
5244 {
5245 	if ((my_mididev = sound_alloc_mididev()) < 0)
5246 		printk ("Sound: Too many midi devices detected\n");
5247 	else
5248 		midi_devs[my_mididev] = &awe_midi_operations;
5249 }
5250 
unload_midiemu(void)5251 static void __exit unload_midiemu(void)
5252 {
5253 	if (my_mididev >= 0)
5254 		sound_unload_mididev(my_mididev);
5255 }
5256 
5257 
5258 /*
5259  * open/close midi device
5260  */
5261 
5262 static int midi_opened = FALSE;
5263 
5264 static int midi_mode;
5265 static int coarsetune = 0, finetune = 0;
5266 
5267 static int xg_mapping = TRUE;
5268 static int xg_bankmode = 0;
5269 
5270 /* effect sensitivity */
5271 
5272 #define FX_CUTOFF	0
5273 #define FX_RESONANCE	1
5274 #define FX_ATTACK	2
5275 #define FX_RELEASE	3
5276 #define FX_VIBRATE	4
5277 #define FX_VIBDEPTH	5
5278 #define FX_VIBDELAY	6
5279 #define FX_NUMS		7
5280 
5281 #define DEF_FX_CUTOFF		170
5282 #define DEF_FX_RESONANCE	6
5283 #define DEF_FX_ATTACK		50
5284 #define DEF_FX_RELEASE		50
5285 #define DEF_FX_VIBRATE		30
5286 #define DEF_FX_VIBDEPTH		4
5287 #define DEF_FX_VIBDELAY		1500
5288 
5289 /* effect sense: */
5290 static int gs_sense[] =
5291 {
5292 	DEF_FX_CUTOFF, DEF_FX_RESONANCE, DEF_FX_ATTACK, DEF_FX_RELEASE,
5293 	DEF_FX_VIBRATE, DEF_FX_VIBDEPTH, DEF_FX_VIBDELAY
5294 };
5295 static int xg_sense[] =
5296 {
5297 	DEF_FX_CUTOFF, DEF_FX_RESONANCE, DEF_FX_ATTACK, DEF_FX_RELEASE,
5298 	DEF_FX_VIBRATE, DEF_FX_VIBDEPTH, DEF_FX_VIBDELAY
5299 };
5300 
5301 
5302 /* current status */
5303 static MidiStatus curst;
5304 
5305 
5306 static int
awe_midi_open(int dev,int mode,void (* input)(int,unsigned char),void (* output)(int))5307 awe_midi_open (int dev, int mode,
5308 	       void (*input)(int,unsigned char),
5309 	       void (*output)(int))
5310 {
5311 	if (midi_opened)
5312 		return -EBUSY;
5313 
5314 	midi_opened = TRUE;
5315 
5316 	midi_mode = MODE_GM;
5317 
5318 	curst.queue = Q_NONE;
5319 	curst.qlen = 0;
5320 	curst.read = 0;
5321 	curst.status = 0;
5322 	curst.chan = 0;
5323 	memset(curst.buf, 0, sizeof(curst.buf));
5324 
5325 	init_midi_status(&curst);
5326 
5327 	return 0;
5328 }
5329 
5330 static void
awe_midi_close(int dev)5331 awe_midi_close (int dev)
5332 {
5333 	midi_opened = FALSE;
5334 }
5335 
5336 
5337 static int
awe_midi_ioctl(int dev,unsigned cmd,caddr_t arg)5338 awe_midi_ioctl (int dev, unsigned cmd, caddr_t arg)
5339 {
5340 	return -EPERM;
5341 }
5342 
5343 static int
awe_midi_outputc(int dev,unsigned char midi_byte)5344 awe_midi_outputc (int dev, unsigned char midi_byte)
5345 {
5346 	if (! midi_opened)
5347 		return 1;
5348 
5349 	/* force to change playing mode */
5350 	playing_mode = AWE_PLAY_MULTI;
5351 
5352 	get_midi_char(&curst, midi_byte);
5353 	return 1;
5354 }
5355 
5356 
5357 /*
5358  * initialize
5359  */
5360 
init_midi_status(MidiStatus * st)5361 static void init_midi_status(MidiStatus *st)
5362 {
5363 	clear_rpn();
5364 	coarsetune = 0;
5365 	finetune = 0;
5366 }
5367 
5368 
5369 /*
5370  * RPN & NRPN
5371  */
5372 
5373 #define MAX_MIDI_CHANNELS	16
5374 
5375 /* RPN & NRPN */
5376 static unsigned char nrpn[MAX_MIDI_CHANNELS];  /* current event is NRPN? */
5377 static int msb_bit;  /* current event is msb for RPN/NRPN */
5378 /* RPN & NRPN indeces */
5379 static unsigned char rpn_msb[MAX_MIDI_CHANNELS], rpn_lsb[MAX_MIDI_CHANNELS];
5380 /* RPN & NRPN values */
5381 static int rpn_val[MAX_MIDI_CHANNELS];
5382 
clear_rpn(void)5383 static void clear_rpn(void)
5384 {
5385 	int i;
5386 	for (i = 0; i < MAX_MIDI_CHANNELS; i++) {
5387 		nrpn[i] = 0;
5388 		rpn_msb[i] = 127;
5389 		rpn_lsb[i] = 127;
5390 		rpn_val[i] = 0;
5391 	}
5392 	msb_bit = 0;
5393 }
5394 
5395 
5396 /*
5397  * process midi queue
5398  */
5399 
5400 /* status event types */
5401 typedef void (*StatusEvent)(MidiStatus *st);
5402 static struct StatusEventList {
5403 	StatusEvent process;
5404 	int qlen;
5405 } status_event[8] = {
5406 	{midi_note_off, 2},
5407 	{midi_note_on, 2},
5408 	{midi_key_pressure, 2},
5409 	{midi_control_change, 2},
5410 	{midi_program_change, 1},
5411 	{midi_channel_pressure, 1},
5412 	{midi_pitch_wheel, 2},
5413 	{NULL, 0},
5414 };
5415 
5416 
5417 /* read a char from fifo and process it */
get_midi_char(MidiStatus * st,int c)5418 static void get_midi_char(MidiStatus *st, int c)
5419 {
5420 	if (c == 0xfe) {
5421 		/* ignore active sense */
5422 		st->queue = Q_NONE;
5423 		return;
5424 	}
5425 
5426 	switch (st->queue) {
5427 	/* case Q_VARLEN: queue_varlen(st, c); break;*/
5428 	case Q_READ:
5429 	case Q_SYSEX:
5430 		queue_read(st, c);
5431 		break;
5432 	case Q_NONE:
5433 		st->read = 0;
5434 		if ((c & 0xf0) == 0xf0) {
5435 			special_event(st, c);
5436 		} else if (c & 0x80) { /* status change */
5437 			st->status = (c >> 4) & 0x07;
5438 			st->chan = c & 0x0f;
5439 			st->queue = Q_READ;
5440 			st->qlen = status_event[st->status].qlen;
5441 			if (st->qlen == 0)
5442 				st->queue = Q_NONE;
5443 		}
5444 		break;
5445 	}
5446 }
5447 
5448 /* 0xfx events */
special_event(MidiStatus * st,int c)5449 static void special_event(MidiStatus *st, int c)
5450 {
5451 	switch (c) {
5452 	case 0xf0: /* system exclusive */
5453 		st->queue = Q_SYSEX;
5454 		st->qlen = 0;
5455 		break;
5456 	case 0xf1: /* MTC quarter frame */
5457 	case 0xf3: /* song select */
5458 		st->queue = Q_READ;
5459 		st->qlen = 1;
5460 		break;
5461 	case 0xf2: /* song position */
5462 		st->queue = Q_READ;
5463 		st->qlen = 2;
5464 		break;
5465 	}
5466 }
5467 
5468 #if 0
5469 /* read variable length value */
5470 static void queue_varlen(MidiStatus *st, int c)
5471 {
5472 	st->qlen += (c & 0x7f);
5473 	if (c & 0x80) {
5474 		st->qlen <<= 7;
5475 		return;
5476 	}
5477 	if (st->qlen <= 0) {
5478 		st->qlen = 0;
5479 		st->queue = Q_NONE;
5480 	}
5481 	st->queue = Q_READ;
5482 	st->read = 0;
5483 }
5484 #endif
5485 
5486 
5487 /* read a char */
queue_read(MidiStatus * st,int c)5488 static void queue_read(MidiStatus *st, int c)
5489 {
5490 	if (st->read < MAX_MIDIBUF) {
5491 		if (st->queue != Q_SYSEX)
5492 			c &= 0x7f;
5493 		st->buf[st->read] = (unsigned char)c;
5494 	}
5495 	st->read++;
5496 	if (st->queue == Q_SYSEX && c == 0xf7) {
5497 		midi_system_exclusive(st);
5498 		st->queue = Q_NONE;
5499 	} else if (st->queue == Q_READ && st->read >= st->qlen) {
5500 		if (status_event[st->status].process)
5501 			status_event[st->status].process(st);
5502 		st->queue = Q_NONE;
5503 	}
5504 }
5505 
5506 
5507 /*
5508  * status events
5509  */
5510 
5511 /* note on */
midi_note_on(MidiStatus * st)5512 static void midi_note_on(MidiStatus *st)
5513 {
5514 	DEBUG(2,printk("midi: note_on (%d) %d %d\n", st->chan, st->buf[0], st->buf[1]));
5515 	if (st->buf[1] == 0)
5516 		midi_note_off(st);
5517 	else
5518 		awe_start_note(0, st->chan, st->buf[0], st->buf[1]);
5519 }
5520 
5521 /* note off */
midi_note_off(MidiStatus * st)5522 static void midi_note_off(MidiStatus *st)
5523 {
5524 	DEBUG(2,printk("midi: note_off (%d) %d %d\n", st->chan, st->buf[0], st->buf[1]));
5525 	awe_kill_note(0, st->chan, st->buf[0], st->buf[1]);
5526 }
5527 
5528 /* key pressure change */
midi_key_pressure(MidiStatus * st)5529 static void midi_key_pressure(MidiStatus *st)
5530 {
5531 	awe_key_pressure(0, st->chan, st->buf[0], st->buf[1]);
5532 }
5533 
5534 /* channel pressure change */
midi_channel_pressure(MidiStatus * st)5535 static void midi_channel_pressure(MidiStatus *st)
5536 {
5537 	channels[st->chan].chan_press = st->buf[0];
5538 	awe_modwheel_change(st->chan, st->buf[0]);
5539 }
5540 
5541 /* pitch wheel change */
midi_pitch_wheel(MidiStatus * st)5542 static void midi_pitch_wheel(MidiStatus *st)
5543 {
5544 	int val = (int)st->buf[1] * 128 + st->buf[0];
5545 	awe_bender(0, st->chan, val);
5546 }
5547 
5548 /* program change */
midi_program_change(MidiStatus * st)5549 static void midi_program_change(MidiStatus *st)
5550 {
5551 	int preset;
5552 	preset = st->buf[0];
5553 	if (midi_mode == MODE_GS && IS_DRUM_CHANNEL(st->chan) && preset == 127)
5554 		preset = 0;
5555 	else if (midi_mode == MODE_XG && xg_mapping && IS_DRUM_CHANNEL(st->chan))
5556 		preset += 64;
5557 
5558 	awe_set_instr(0, st->chan, preset);
5559 }
5560 
5561 #define send_effect(chan,type,val) awe_send_effect(chan,-1,type,val)
5562 #define add_effect(chan,type,val) awe_send_effect(chan,-1,(type)|0x80,val)
5563 #define unset_effect(chan,type) awe_send_effect(chan,-1,(type)|0x40,0)
5564 
5565 /* midi control change */
midi_control_change(MidiStatus * st)5566 static void midi_control_change(MidiStatus *st)
5567 {
5568 	int cmd = st->buf[0];
5569 	int val = st->buf[1];
5570 
5571 	DEBUG(2,printk("midi: control (%d) %d %d\n", st->chan, cmd, val));
5572 	if (midi_mode == MODE_XG) {
5573 		if (xg_control_change(st, cmd, val))
5574 			return;
5575 	}
5576 
5577 	/* controls #31 - #64 are LSB of #0 - #31 */
5578 	msb_bit = 1;
5579 	if (cmd >= 0x20 && cmd < 0x40) {
5580 		msb_bit = 0;
5581 		cmd -= 0x20;
5582 	}
5583 
5584 	switch (cmd) {
5585 	case CTL_SOFT_PEDAL:
5586 		if (val == 127)
5587 			add_effect(st->chan, AWE_FX_CUTOFF, -160);
5588 		else
5589 			unset_effect(st->chan, AWE_FX_CUTOFF);
5590 		break;
5591 
5592 	case CTL_BANK_SELECT:
5593 		midi_select_bank(st, val);
5594 		break;
5595 
5596 	/* set RPN/NRPN parameter */
5597 	case CTL_REGIST_PARM_NUM_MSB:
5598 		nrpn[st->chan]=0; rpn_msb[st->chan]=val;
5599 		break;
5600 	case CTL_REGIST_PARM_NUM_LSB:
5601 		nrpn[st->chan]=0; rpn_lsb[st->chan]=val;
5602 		break;
5603 	case CTL_NONREG_PARM_NUM_MSB:
5604 		nrpn[st->chan]=1; rpn_msb[st->chan]=val;
5605 		break;
5606 	case CTL_NONREG_PARM_NUM_LSB:
5607 		nrpn[st->chan]=1; rpn_lsb[st->chan]=val;
5608 		break;
5609 
5610 	/* send RPN/NRPN entry */
5611 	case CTL_DATA_ENTRY:
5612 		if (msb_bit)
5613 			rpn_val[st->chan] = val * 128;
5614 		else
5615 			rpn_val[st->chan] |= val;
5616 		if (nrpn[st->chan])
5617 			midi_nrpn_event(st);
5618 		else
5619 			midi_rpn_event(st);
5620 		break;
5621 
5622 	/* increase/decrease data entry */
5623 	case CTL_DATA_INCREMENT:
5624 		rpn_val[st->chan]++;
5625 		midi_rpn_event(st);
5626 		break;
5627 	case CTL_DATA_DECREMENT:
5628 		rpn_val[st->chan]--;
5629 		midi_rpn_event(st);
5630 		break;
5631 
5632 	/* default */
5633 	default:
5634 		awe_controller(0, st->chan, cmd, val);
5635 		break;
5636 	}
5637 }
5638 
5639 /* tone bank change */
midi_select_bank(MidiStatus * st,int val)5640 static void midi_select_bank(MidiStatus *st, int val)
5641 {
5642 	if (midi_mode == MODE_XG && msb_bit) {
5643 		xg_bankmode = val;
5644 		/* XG MSB value; not normal bank selection */
5645 		switch (val) {
5646 		case 127: /* remap to drum channel */
5647 			awe_controller(0, st->chan, CTL_BANK_SELECT, 128);
5648 			break;
5649 		default: /* remap to normal channel */
5650 			awe_controller(0, st->chan, CTL_BANK_SELECT, val);
5651 			break;
5652 		}
5653 		return;
5654 	} else if (midi_mode == MODE_GS && !msb_bit)
5655 		/* ignore LSB bank in GS mode (used for mapping) */
5656 		return;
5657 
5658 	/* normal bank controls; accept both MSB and LSB */
5659 	if (! IS_DRUM_CHANNEL(st->chan)) {
5660 		if (midi_mode == MODE_XG) {
5661 			if (xg_bankmode) return;
5662 			if (val == 64 || val == 126)
5663 				val = 0;
5664 		} else if (midi_mode == MODE_GS && val == 127)
5665 			val = 0;
5666 		awe_controller(0, st->chan, CTL_BANK_SELECT, val);
5667 	}
5668 }
5669 
5670 
5671 /*
5672  * RPN events
5673  */
5674 
midi_rpn_event(MidiStatus * st)5675 static void midi_rpn_event(MidiStatus *st)
5676 {
5677 	int type;
5678 	type = (rpn_msb[st->chan]<<8) | rpn_lsb[st->chan];
5679 	switch (type) {
5680 	case 0x0000: /* Pitch bend sensitivity */
5681 		/* MSB only / 1 semitone per 128 */
5682 		if (msb_bit) {
5683 			channels[st->chan].bender_range =
5684 				rpn_val[st->chan] * 100 / 128;
5685 		}
5686 		break;
5687 
5688 	case 0x0001: /* fine tuning: */
5689 		/* MSB/LSB, 8192=center, 100/8192 cent step */
5690 		finetune = rpn_val[st->chan] - 8192;
5691 		midi_detune(st->chan, coarsetune, finetune);
5692 		break;
5693 
5694 	case 0x0002: /* coarse tuning */
5695 		/* MSB only / 8192=center, 1 semitone per 128 */
5696 		if (msb_bit) {
5697 			coarsetune = rpn_val[st->chan] - 8192;
5698 			midi_detune(st->chan, coarsetune, finetune);
5699 		}
5700 		break;
5701 
5702 	case 0x7F7F: /* "lock-in" RPN */
5703 		break;
5704 	}
5705 }
5706 
5707 
5708 /* tuning:
5709  *   coarse = -8192 to 8192 (100 cent per 128)
5710  *   fine = -8192 to 8192 (max=100cent)
5711  */
midi_detune(int chan,int coarse,int fine)5712 static void midi_detune(int chan, int coarse, int fine)
5713 {
5714 	/* 4096 = 1200 cents in AWE parameter */
5715 	int val;
5716 	val = coarse * 4096 / (12 * 128);
5717 	val += fine / 24;
5718 	if (val)
5719 		send_effect(chan, AWE_FX_INIT_PITCH, val);
5720 	else
5721 		unset_effect(chan, AWE_FX_INIT_PITCH);
5722 }
5723 
5724 
5725 /*
5726  * system exclusive message
5727  * GM/GS/XG macros are accepted
5728  */
5729 
midi_system_exclusive(MidiStatus * st)5730 static void midi_system_exclusive(MidiStatus *st)
5731 {
5732 	/* GM on */
5733 	static unsigned char gm_on_macro[] = {
5734 		0x7e,0x7f,0x09,0x01,
5735 	};
5736 	/* XG on */
5737 	static unsigned char xg_on_macro[] = {
5738 		0x43,0x10,0x4c,0x00,0x00,0x7e,0x00,
5739 	};
5740 	/* GS prefix
5741 	 * drum channel: XX=0x1?(channel), YY=0x15, ZZ=on/off
5742 	 * reverb mode: XX=0x01, YY=0x30, ZZ=0-7
5743 	 * chorus mode: XX=0x01, YY=0x38, ZZ=0-7
5744 	 */
5745 	static unsigned char gs_pfx_macro[] = {
5746 		0x41,0x10,0x42,0x12,0x40,/*XX,YY,ZZ*/
5747 	};
5748 
5749 #if 0
5750 	/* SC88 system mode set
5751 	 * single module mode: XX=1
5752 	 * double module mode: XX=0
5753 	 */
5754 	static unsigned char gs_mode_macro[] = {
5755 		0x41,0x10,0x42,0x12,0x00,0x00,0x7F,/*ZZ*/
5756 	};
5757 	/* SC88 display macro: XX=01:bitmap, 00:text
5758 	 */
5759 	static unsigned char gs_disp_macro[] = {
5760 		0x41,0x10,0x45,0x12,0x10,/*XX,00*/
5761 	};
5762 #endif
5763 
5764 	/* GM on */
5765 	if (memcmp(st->buf, gm_on_macro, sizeof(gm_on_macro)) == 0) {
5766 		if (midi_mode != MODE_GS && midi_mode != MODE_XG)
5767 			midi_mode = MODE_GM;
5768 		init_midi_status(st);
5769 	}
5770 
5771 	/* GS macros */
5772 	else if (memcmp(st->buf, gs_pfx_macro, sizeof(gs_pfx_macro)) == 0) {
5773 		if (midi_mode != MODE_GS && midi_mode != MODE_XG)
5774 			midi_mode = MODE_GS;
5775 
5776 		if (st->buf[5] == 0x00 && st->buf[6] == 0x7f && st->buf[7] == 0x00) {
5777 			/* GS reset */
5778 			init_midi_status(st);
5779 		}
5780 
5781 		else if ((st->buf[5] & 0xf0) == 0x10 && st->buf[6] == 0x15) {
5782 			/* drum pattern */
5783 			int p = st->buf[5] & 0x0f;
5784 			if (p == 0) p = 9;
5785 			else if (p < 10) p--;
5786 			if (st->buf[7] == 0)
5787 				DRUM_CHANNEL_OFF(p);
5788 			else
5789 				DRUM_CHANNEL_ON(p);
5790 
5791 		} else if ((st->buf[5] & 0xf0) == 0x10 && st->buf[6] == 0x21) {
5792 			/* program */
5793 			int p = st->buf[5] & 0x0f;
5794 			if (p == 0) p = 9;
5795 			else if (p < 10) p--;
5796 			if (! IS_DRUM_CHANNEL(p))
5797 				awe_set_instr(0, p, st->buf[7]);
5798 
5799 		} else if (st->buf[5] == 0x01 && st->buf[6] == 0x30) {
5800 			/* reverb mode */
5801 			awe_set_reverb_mode(st->buf[7]);
5802 
5803 		} else if (st->buf[5] == 0x01 && st->buf[6] == 0x38) {
5804 			/* chorus mode */
5805 			awe_set_chorus_mode(st->buf[7]);
5806 
5807 		} else if (st->buf[5] == 0x00 && st->buf[6] == 0x04) {
5808 			/* master volume */
5809 			awe_change_master_volume(st->buf[7]);
5810 
5811 		}
5812 	}
5813 
5814 	/* XG on */
5815 	else if (memcmp(st->buf, xg_on_macro, sizeof(xg_on_macro)) == 0) {
5816 		midi_mode = MODE_XG;
5817 		xg_mapping = TRUE;
5818 		xg_bankmode = 0;
5819 	}
5820 }
5821 
5822 
5823 /*----------------------------------------------------------------*/
5824 
5825 /*
5826  * convert NRPN/control values
5827  */
5828 
send_converted_effect(ConvTable * table,int num_tables,MidiStatus * st,int type,int val)5829 static int send_converted_effect(ConvTable *table, int num_tables, MidiStatus *st, int type, int val)
5830 {
5831 	int i, cval;
5832 	for (i = 0; i < num_tables; i++) {
5833 		if (table[i].control == type) {
5834 			cval = table[i].convert(val);
5835 			send_effect(st->chan, table[i].awe_effect, cval);
5836 			return TRUE;
5837 		}
5838 	}
5839 	return FALSE;
5840 }
5841 
add_converted_effect(ConvTable * table,int num_tables,MidiStatus * st,int type,int val)5842 static int add_converted_effect(ConvTable *table, int num_tables, MidiStatus *st, int type, int val)
5843 {
5844 	int i, cval;
5845 	for (i = 0; i < num_tables; i++) {
5846 		if (table[i].control == type) {
5847 			cval = table[i].convert(val);
5848 			add_effect(st->chan, table[i].awe_effect|0x80, cval);
5849 			return TRUE;
5850 		}
5851 	}
5852 	return FALSE;
5853 }
5854 
5855 
5856 /*
5857  * AWE32 NRPN effects
5858  */
5859 
5860 static unsigned short fx_delay(int val);
5861 static unsigned short fx_attack(int val);
5862 static unsigned short fx_hold(int val);
5863 static unsigned short fx_decay(int val);
5864 static unsigned short fx_the_value(int val);
5865 static unsigned short fx_twice_value(int val);
5866 static unsigned short fx_conv_pitch(int val);
5867 static unsigned short fx_conv_Q(int val);
5868 
5869 /* function for each NRPN */		/* [range]  units */
5870 #define fx_env1_delay	fx_delay	/* [0,5900] 4msec */
5871 #define fx_env1_attack	fx_attack	/* [0,5940] 1msec */
5872 #define fx_env1_hold	fx_hold		/* [0,8191] 1msec */
5873 #define fx_env1_decay	fx_decay	/* [0,5940] 4msec */
5874 #define fx_env1_release	fx_decay	/* [0,5940] 4msec */
5875 #define fx_env1_sustain	fx_the_value	/* [0,127] 0.75dB */
5876 #define fx_env1_pitch	fx_the_value	/* [-127,127] 9.375cents */
5877 #define fx_env1_cutoff	fx_the_value	/* [-127,127] 56.25cents */
5878 
5879 #define fx_env2_delay	fx_delay	/* [0,5900] 4msec */
5880 #define fx_env2_attack	fx_attack	/* [0,5940] 1msec */
5881 #define fx_env2_hold	fx_hold		/* [0,8191] 1msec */
5882 #define fx_env2_decay	fx_decay	/* [0,5940] 4msec */
5883 #define fx_env2_release	fx_decay	/* [0,5940] 4msec */
5884 #define fx_env2_sustain	fx_the_value	/* [0,127] 0.75dB */
5885 
5886 #define fx_lfo1_delay	fx_delay	/* [0,5900] 4msec */
5887 #define fx_lfo1_freq	fx_twice_value	/* [0,127] 84mHz */
5888 #define fx_lfo1_volume	fx_twice_value	/* [0,127] 0.1875dB */
5889 #define fx_lfo1_pitch	fx_the_value	/* [-127,127] 9.375cents */
5890 #define fx_lfo1_cutoff	fx_twice_value	/* [-64,63] 56.25cents */
5891 
5892 #define fx_lfo2_delay	fx_delay	/* [0,5900] 4msec */
5893 #define fx_lfo2_freq	fx_twice_value	/* [0,127] 84mHz */
5894 #define fx_lfo2_pitch	fx_the_value	/* [-127,127] 9.375cents */
5895 
5896 #define fx_init_pitch	fx_conv_pitch	/* [-8192,8192] cents */
5897 #define fx_chorus	fx_the_value	/* [0,255] -- */
5898 #define fx_reverb	fx_the_value	/* [0,255] -- */
5899 #define fx_cutoff	fx_twice_value	/* [0,127] 62Hz */
5900 #define fx_filterQ	fx_conv_Q	/* [0,127] -- */
5901 
fx_delay(int val)5902 static unsigned short fx_delay(int val)
5903 {
5904 	return (unsigned short)calc_parm_delay(val);
5905 }
5906 
fx_attack(int val)5907 static unsigned short fx_attack(int val)
5908 {
5909 	return (unsigned short)calc_parm_attack(val);
5910 }
5911 
fx_hold(int val)5912 static unsigned short fx_hold(int val)
5913 {
5914 	return (unsigned short)calc_parm_hold(val);
5915 }
5916 
fx_decay(int val)5917 static unsigned short fx_decay(int val)
5918 {
5919 	return (unsigned short)calc_parm_decay(val);
5920 }
5921 
fx_the_value(int val)5922 static unsigned short fx_the_value(int val)
5923 {
5924 	return (unsigned short)(val & 0xff);
5925 }
5926 
fx_twice_value(int val)5927 static unsigned short fx_twice_value(int val)
5928 {
5929 	return (unsigned short)((val * 2) & 0xff);
5930 }
5931 
fx_conv_pitch(int val)5932 static unsigned short fx_conv_pitch(int val)
5933 {
5934 	return (short)(val * 4096 / 1200);
5935 }
5936 
fx_conv_Q(int val)5937 static unsigned short fx_conv_Q(int val)
5938 {
5939 	return (unsigned short)((val / 8) & 0xff);
5940 }
5941 
5942 
5943 static ConvTable awe_effects[] =
5944 {
5945 	{ 0, AWE_FX_LFO1_DELAY,	fx_lfo1_delay},
5946 	{ 1, AWE_FX_LFO1_FREQ,	fx_lfo1_freq},
5947 	{ 2, AWE_FX_LFO2_DELAY,	fx_lfo2_delay},
5948 	{ 3, AWE_FX_LFO2_FREQ,	fx_lfo2_freq},
5949 
5950 	{ 4, AWE_FX_ENV1_DELAY,	fx_env1_delay},
5951 	{ 5, AWE_FX_ENV1_ATTACK,fx_env1_attack},
5952 	{ 6, AWE_FX_ENV1_HOLD,	fx_env1_hold},
5953 	{ 7, AWE_FX_ENV1_DECAY,	fx_env1_decay},
5954 	{ 8, AWE_FX_ENV1_SUSTAIN,	fx_env1_sustain},
5955 	{ 9, AWE_FX_ENV1_RELEASE,	fx_env1_release},
5956 
5957 	{10, AWE_FX_ENV2_DELAY,	fx_env2_delay},
5958 	{11, AWE_FX_ENV2_ATTACK,	fx_env2_attack},
5959 	{12, AWE_FX_ENV2_HOLD,	fx_env2_hold},
5960 	{13, AWE_FX_ENV2_DECAY,	fx_env2_decay},
5961 	{14, AWE_FX_ENV2_SUSTAIN,	fx_env2_sustain},
5962 	{15, AWE_FX_ENV2_RELEASE,	fx_env2_release},
5963 
5964 	{16, AWE_FX_INIT_PITCH,	fx_init_pitch},
5965 	{17, AWE_FX_LFO1_PITCH,	fx_lfo1_pitch},
5966 	{18, AWE_FX_LFO2_PITCH,	fx_lfo2_pitch},
5967 	{19, AWE_FX_ENV1_PITCH,	fx_env1_pitch},
5968 	{20, AWE_FX_LFO1_VOLUME,	fx_lfo1_volume},
5969 	{21, AWE_FX_CUTOFF,		fx_cutoff},
5970 	{22, AWE_FX_FILTERQ,	fx_filterQ},
5971 	{23, AWE_FX_LFO1_CUTOFF,	fx_lfo1_cutoff},
5972 	{24, AWE_FX_ENV1_CUTOFF,	fx_env1_cutoff},
5973 	{25, AWE_FX_CHORUS,		fx_chorus},
5974 	{26, AWE_FX_REVERB,		fx_reverb},
5975 };
5976 
5977 static int num_awe_effects = numberof(awe_effects);
5978 
5979 
5980 /*
5981  * GS(SC88) NRPN effects; still experimental
5982  */
5983 
5984 /* cutoff: quarter semitone step, max=255 */
gs_cutoff(int val)5985 static unsigned short gs_cutoff(int val)
5986 {
5987 	return (val - 64) * gs_sense[FX_CUTOFF] / 50;
5988 }
5989 
5990 /* resonance: 0 to 15(max) */
gs_filterQ(int val)5991 static unsigned short gs_filterQ(int val)
5992 {
5993 	return (val - 64) * gs_sense[FX_RESONANCE] / 50;
5994 }
5995 
5996 /* attack: */
gs_attack(int val)5997 static unsigned short gs_attack(int val)
5998 {
5999 	return -(val - 64) * gs_sense[FX_ATTACK] / 50;
6000 }
6001 
6002 /* decay: */
gs_decay(int val)6003 static unsigned short gs_decay(int val)
6004 {
6005 	return -(val - 64) * gs_sense[FX_RELEASE] / 50;
6006 }
6007 
6008 /* release: */
gs_release(int val)6009 static unsigned short gs_release(int val)
6010 {
6011 	return -(val - 64) * gs_sense[FX_RELEASE] / 50;
6012 }
6013 
6014 /* vibrato freq: 0.042Hz step, max=255 */
gs_vib_rate(int val)6015 static unsigned short gs_vib_rate(int val)
6016 {
6017 	return (val - 64) * gs_sense[FX_VIBRATE] / 50;
6018 }
6019 
6020 /* vibrato depth: max=127, 1 octave */
gs_vib_depth(int val)6021 static unsigned short gs_vib_depth(int val)
6022 {
6023 	return (val - 64) * gs_sense[FX_VIBDEPTH] / 50;
6024 }
6025 
6026 /* vibrato delay: -0.725msec step */
gs_vib_delay(int val)6027 static unsigned short gs_vib_delay(int val)
6028 {
6029 	return -(val - 64) * gs_sense[FX_VIBDELAY] / 50;
6030 }
6031 
6032 static ConvTable gs_effects[] =
6033 {
6034 	{32, AWE_FX_CUTOFF,	gs_cutoff},
6035 	{33, AWE_FX_FILTERQ,	gs_filterQ},
6036 	{99, AWE_FX_ENV2_ATTACK, gs_attack},
6037 	{100, AWE_FX_ENV2_DECAY, gs_decay},
6038 	{102, AWE_FX_ENV2_RELEASE, gs_release},
6039 	{8, AWE_FX_LFO1_FREQ, gs_vib_rate},
6040 	{9, AWE_FX_LFO1_VOLUME, gs_vib_depth},
6041 	{10, AWE_FX_LFO1_DELAY, gs_vib_delay},
6042 };
6043 
6044 static int num_gs_effects = numberof(gs_effects);
6045 
6046 
6047 /*
6048  * NRPN events: accept as AWE32/SC88 specific controls
6049  */
6050 
midi_nrpn_event(MidiStatus * st)6051 static void midi_nrpn_event(MidiStatus *st)
6052 {
6053 	if (rpn_msb[st->chan] == 127 && rpn_lsb[st->chan] <= 26) {
6054 		if (! msb_bit) /* both MSB/LSB necessary */
6055 			send_converted_effect(awe_effects, num_awe_effects,
6056 					      st, rpn_lsb[st->chan],
6057 					      rpn_val[st->chan] - 8192);
6058 	} else if (rpn_msb[st->chan] == 1) {
6059 		if (msb_bit) /* only MSB is valid */
6060 			add_converted_effect(gs_effects, num_gs_effects,
6061 					     st, rpn_lsb[st->chan],
6062 					     rpn_val[st->chan] / 128);
6063 	}
6064 }
6065 
6066 
6067 /*
6068  * XG control effects; still experimental
6069  */
6070 
6071 /* cutoff: quarter semitone step, max=255 */
xg_cutoff(int val)6072 static unsigned short xg_cutoff(int val)
6073 {
6074 	return (val - 64) * xg_sense[FX_CUTOFF] / 64;
6075 }
6076 
6077 /* resonance: 0(open) to 15(most nasal) */
xg_filterQ(int val)6078 static unsigned short xg_filterQ(int val)
6079 {
6080 	return (val - 64) * xg_sense[FX_RESONANCE] / 64;
6081 }
6082 
6083 /* attack: */
xg_attack(int val)6084 static unsigned short xg_attack(int val)
6085 {
6086 	return -(val - 64) * xg_sense[FX_ATTACK] / 64;
6087 }
6088 
6089 /* release: */
xg_release(int val)6090 static unsigned short xg_release(int val)
6091 {
6092 	return -(val - 64) * xg_sense[FX_RELEASE] / 64;
6093 }
6094 
6095 static ConvTable xg_effects[] =
6096 {
6097 	{71, AWE_FX_CUTOFF,	xg_cutoff},
6098 	{74, AWE_FX_FILTERQ,	xg_filterQ},
6099 	{72, AWE_FX_ENV2_RELEASE, xg_release},
6100 	{73, AWE_FX_ENV2_ATTACK, xg_attack},
6101 };
6102 
6103 static int num_xg_effects = numberof(xg_effects);
6104 
xg_control_change(MidiStatus * st,int cmd,int val)6105 static int xg_control_change(MidiStatus *st, int cmd, int val)
6106 {
6107 	return add_converted_effect(xg_effects, num_xg_effects, st, cmd, val);
6108 }
6109 
6110 #endif /* CONFIG_AWE32_MIDIEMU */
6111 
6112 
6113 /*----------------------------------------------------------------*/
6114 
6115 /*
6116  * device / lowlevel (module) interface
6117  */
6118 
attach_awe(void)6119 int __init attach_awe(void)
6120 {
6121 	return _attach_awe() ? 0 : -ENODEV;
6122 }
6123 
unload_awe(void)6124 void __exit unload_awe(void)
6125 {
6126 	_unload_awe();
6127 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
6128 	if (isapnp)
6129 		awe_deactivate_isapnp();
6130 #endif /* isapnp */
6131 }
6132 
6133 
6134 module_init(attach_awe);
6135 module_exit(unload_awe);
6136 
6137 #ifndef MODULE
setup_awe(char * str)6138 static int __init setup_awe(char *str)
6139 {
6140 	/* io, memsize, isapnp */
6141 	int ints[4];
6142 
6143 	str = get_options(str, ARRAY_SIZE(ints), ints);
6144 
6145 	io = ints[1];
6146 	memsize = ints[2];
6147 	isapnp = ints[3];
6148 
6149 	return 1;
6150 }
6151 
6152 __setup("awe=", setup_awe);
6153 #endif
6154