1 /*
2 * sound/gus_wave.c
3 *
4 * Driver for the Gravis UltraSound wave table synth.
5 *
6 *
7 * Copyright (C) by Hannu Savolainen 1993-1997
8 *
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
11 * for more info.
12 *
13 *
14 * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
15 * Frank van de Pol : Fixed GUS MAX interrupt handling. Enabled simultanious
16 * usage of CS4231A codec, GUS wave and MIDI for GUS MAX.
17 * Bartlomiej Zolnierkiewicz : added some __init/__exit
18 */
19
20 #include <linux/init.h>
21 #include <linux/config.h>
22
23 #define GUSPNP_AUTODETECT
24
25 #include "sound_config.h"
26 #include <linux/ultrasound.h>
27
28 #include "gus.h"
29 #include "gus_hw.h"
30
31 #define GUS_BANK_SIZE (((iw_mode) ? 256*1024*1024 : 256*1024))
32
33 #define MAX_SAMPLE 150
34 #define MAX_PATCH 256
35
36 #define NOT_SAMPLE 0xffff
37
38 struct voice_info
39 {
40 unsigned long orig_freq;
41 unsigned long current_freq;
42 unsigned long mode;
43 int fixed_pitch;
44 int bender;
45 int bender_range;
46 int panning;
47 int midi_volume;
48 unsigned int initial_volume;
49 unsigned int current_volume;
50 int loop_irq_mode, loop_irq_parm;
51 #define LMODE_FINISH 1
52 #define LMODE_PCM 2
53 #define LMODE_PCM_STOP 3
54 int volume_irq_mode, volume_irq_parm;
55 #define VMODE_HALT 1
56 #define VMODE_ENVELOPE 2
57 #define VMODE_START_NOTE 3
58
59 int env_phase;
60 unsigned char env_rate[6];
61 unsigned char env_offset[6];
62
63 /*
64 * Volume computation parameters for gus_adagio_vol()
65 */
66 int main_vol, expression_vol, patch_vol;
67
68 /* Variables for "Ultraclick" removal */
69 int dev_pending, note_pending, volume_pending,
70 sample_pending;
71 char kill_pending;
72 long offset_pending;
73
74 };
75
76 static struct voice_alloc_info *voice_alloc;
77 static struct address_info *gus_hw_config;
78 extern int gus_base;
79 extern int gus_irq, gus_dma;
80 extern int gus_pnp_flag;
81 extern int gus_no_wave_dma;
82 static int gus_dma2 = -1;
83 static int dual_dma_mode = 0;
84 static long gus_mem_size = 0;
85 static long free_mem_ptr = 0;
86 static int gus_busy = 0;
87 static int gus_no_dma = 0;
88 static int nr_voices = 0;
89 static int gus_devnum = 0;
90 static int volume_base, volume_scale, volume_method;
91 static int gus_recmask = SOUND_MASK_MIC;
92 static int recording_active = 0;
93 static int only_read_access = 0;
94 static int only_8_bits = 0;
95
96 int iw_mode = 0;
97 int gus_wave_volume = 60;
98 int gus_pcm_volume = 80;
99 int have_gus_max = 0;
100 static int gus_line_vol = 100, gus_mic_vol = 0;
101 static unsigned char mix_image = 0x00;
102
103 int gus_timer_enabled = 0;
104
105 /*
106 * Current version of this driver doesn't allow synth and PCM functions
107 * at the same time. The active_device specifies the active driver
108 */
109
110 static int active_device = 0;
111
112 #define GUS_DEV_WAVE 1 /* Wave table synth */
113 #define GUS_DEV_PCM_DONE 2 /* PCM device, transfer done */
114 #define GUS_DEV_PCM_CONTINUE 3 /* PCM device, transfer done ch. 1/2 */
115
116 static int gus_audio_speed;
117 static int gus_audio_channels;
118 static int gus_audio_bits;
119 static int gus_audio_bsize;
120 static char bounce_buf[8 * 1024]; /* Must match value set to max_fragment */
121
122 static DECLARE_WAIT_QUEUE_HEAD(dram_sleeper);
123
124 /*
125 * Variables and buffers for PCM output
126 */
127
128 #define MAX_PCM_BUFFERS (128*MAX_REALTIME_FACTOR) /* Don't change */
129
130 static int pcm_bsize, pcm_nblk, pcm_banksize;
131 static int pcm_datasize[MAX_PCM_BUFFERS];
132 static volatile int pcm_head, pcm_tail, pcm_qlen;
133 static volatile int pcm_active;
134 static volatile int dma_active;
135 static int pcm_opened = 0;
136 static int pcm_current_dev;
137 static int pcm_current_block;
138 static unsigned long pcm_current_buf;
139 static int pcm_current_count;
140 static int pcm_current_intrflag;
141
142 extern int *gus_osp;
143
144 static struct voice_info voices[32];
145
146 static int freq_div_table[] =
147 {
148 44100, /* 14 */
149 41160, /* 15 */
150 38587, /* 16 */
151 36317, /* 17 */
152 34300, /* 18 */
153 32494, /* 19 */
154 30870, /* 20 */
155 29400, /* 21 */
156 28063, /* 22 */
157 26843, /* 23 */
158 25725, /* 24 */
159 24696, /* 25 */
160 23746, /* 26 */
161 22866, /* 27 */
162 22050, /* 28 */
163 21289, /* 29 */
164 20580, /* 30 */
165 19916, /* 31 */
166 19293 /* 32 */
167 };
168
169 static struct patch_info *samples = NULL;
170 static long sample_ptrs[MAX_SAMPLE + 1];
171 static int sample_map[32];
172 static int free_sample;
173 static int mixer_type = 0;
174
175
176 static int patch_table[MAX_PATCH];
177 static int patch_map[32];
178
179 static struct synth_info gus_info = {
180 "Gravis UltraSound", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_GUS,
181 0, 16, 0, MAX_PATCH
182 };
183
184 static void gus_poke(long addr, unsigned char data);
185 static void compute_and_set_volume(int voice, int volume, int ramp_time);
186 extern unsigned short gus_adagio_vol(int vel, int mainv, int xpn, int voicev);
187 extern unsigned short gus_linear_vol(int vol, int mainvol);
188 static void compute_volume(int voice, int volume);
189 static void do_volume_irq(int voice);
190 static void set_input_volumes(void);
191 static void gus_tmr_install(int io_base);
192
193 #define INSTANT_RAMP -1 /* Instant change. No ramping */
194 #define FAST_RAMP 0 /* Fastest possible ramp */
195
reset_sample_memory(void)196 static void reset_sample_memory(void)
197 {
198 int i;
199
200 for (i = 0; i <= MAX_SAMPLE; i++)
201 sample_ptrs[i] = -1;
202 for (i = 0; i < 32; i++)
203 sample_map[i] = -1;
204 for (i = 0; i < 32; i++)
205 patch_map[i] = -1;
206
207 gus_poke(0, 0); /* Put a silent sample to the beginning */
208 gus_poke(1, 0);
209 free_mem_ptr = 2;
210
211 free_sample = 0;
212
213 for (i = 0; i < MAX_PATCH; i++)
214 patch_table[i] = NOT_SAMPLE;
215 }
216
gus_delay(void)217 void gus_delay(void)
218 {
219 int i;
220
221 for (i = 0; i < 7; i++)
222 inb(u_DRAMIO);
223 }
224
gus_poke(long addr,unsigned char data)225 static void gus_poke(long addr, unsigned char data)
226 { /* Writes a byte to the DRAM */
227 unsigned long flags;
228
229 save_flags(flags);
230 cli();
231 outb((0x43), u_Command);
232 outb((addr & 0xff), u_DataLo);
233 outb(((addr >> 8) & 0xff), u_DataHi);
234
235 outb((0x44), u_Command);
236 outb(((addr >> 16) & 0xff), u_DataHi);
237 outb((data), u_DRAMIO);
238 restore_flags(flags);
239 }
240
gus_peek(long addr)241 static unsigned char gus_peek(long addr)
242 { /* Reads a byte from the DRAM */
243 unsigned long flags;
244 unsigned char tmp;
245
246 save_flags(flags);
247 cli();
248 outb((0x43), u_Command);
249 outb((addr & 0xff), u_DataLo);
250 outb(((addr >> 8) & 0xff), u_DataHi);
251
252 outb((0x44), u_Command);
253 outb(((addr >> 16) & 0xff), u_DataHi);
254 tmp = inb(u_DRAMIO);
255 restore_flags(flags);
256
257 return tmp;
258 }
259
gus_write8(int reg,unsigned int data)260 void gus_write8(int reg, unsigned int data)
261 { /* Writes to an indirect register (8 bit) */
262 unsigned long flags;
263
264 save_flags(flags);
265 cli();
266
267 outb((reg), u_Command);
268 outb(((unsigned char) (data & 0xff)), u_DataHi);
269
270 restore_flags(flags);
271 }
272
gus_read8(int reg)273 static unsigned char gus_read8(int reg)
274 {
275 /* Reads from an indirect register (8 bit). Offset 0x80. */
276 unsigned long flags;
277 unsigned char val;
278
279 save_flags(flags);
280 cli();
281 outb((reg | 0x80), u_Command);
282 val = inb(u_DataHi);
283 restore_flags(flags);
284
285 return val;
286 }
287
gus_look8(int reg)288 static unsigned char gus_look8(int reg)
289 {
290 /* Reads from an indirect register (8 bit). No additional offset. */
291 unsigned long flags;
292 unsigned char val;
293
294 save_flags(flags);
295 cli();
296 outb((reg), u_Command);
297 val = inb(u_DataHi);
298 restore_flags(flags);
299
300 return val;
301 }
302
gus_write16(int reg,unsigned int data)303 static void gus_write16(int reg, unsigned int data)
304 {
305 /* Writes to an indirect register (16 bit) */
306 unsigned long flags;
307
308 save_flags(flags);
309 cli();
310
311 outb((reg), u_Command);
312
313 outb(((unsigned char) (data & 0xff)), u_DataLo);
314 outb(((unsigned char) ((data >> 8) & 0xff)), u_DataHi);
315
316 restore_flags(flags);
317 }
318
gus_read16(int reg)319 static unsigned short gus_read16(int reg)
320 {
321 /* Reads from an indirect register (16 bit). Offset 0x80. */
322 unsigned long flags;
323 unsigned char hi, lo;
324
325 save_flags(flags);
326 cli();
327
328 outb((reg | 0x80), u_Command);
329
330 lo = inb(u_DataLo);
331 hi = inb(u_DataHi);
332
333 restore_flags(flags);
334
335 return ((hi << 8) & 0xff00) | lo;
336 }
337
gus_look16(int reg)338 static unsigned short gus_look16(int reg)
339 {
340 /* Reads from an indirect register (16 bit). No additional offset. */
341 unsigned long flags;
342 unsigned char hi, lo;
343
344 save_flags(flags);
345 cli();
346
347 outb((reg), u_Command);
348
349 lo = inb(u_DataLo);
350 hi = inb(u_DataHi);
351
352 restore_flags(flags);
353
354 return ((hi << 8) & 0xff00) | lo;
355 }
356
gus_write_addr(int reg,unsigned long address,int frac,int is16bit)357 static void gus_write_addr(int reg, unsigned long address, int frac, int is16bit)
358 {
359 /* Writes an 24 bit memory address */
360 unsigned long hold_address;
361 unsigned long flags;
362
363 save_flags(flags);
364 cli();
365 if (is16bit)
366 {
367 if (iw_mode)
368 {
369 /* Interwave spesific address translations */
370 address >>= 1;
371 }
372 else
373 {
374 /*
375 * Special processing required for 16 bit patches
376 */
377
378 hold_address = address;
379 address = address >> 1;
380 address &= 0x0001ffffL;
381 address |= (hold_address & 0x000c0000L);
382 }
383 }
384 gus_write16(reg, (unsigned short) ((address >> 7) & 0xffff));
385 gus_write16(reg + 1, (unsigned short) ((address << 9) & 0xffff)
386 + (frac << 5));
387 /* Could writing twice fix problems with GUS_VOICE_POS()? Let's try. */
388 gus_delay();
389 gus_write16(reg, (unsigned short) ((address >> 7) & 0xffff));
390 gus_write16(reg + 1, (unsigned short) ((address << 9) & 0xffff)
391 + (frac << 5));
392 restore_flags(flags);
393 }
394
gus_select_voice(int voice)395 static void gus_select_voice(int voice)
396 {
397 if (voice < 0 || voice > 31)
398 return;
399 outb((voice), u_Voice);
400 }
401
gus_select_max_voices(int nvoices)402 static void gus_select_max_voices(int nvoices)
403 {
404 if (iw_mode)
405 nvoices = 32;
406 if (nvoices < 14)
407 nvoices = 14;
408 if (nvoices > 32)
409 nvoices = 32;
410
411 voice_alloc->max_voice = nr_voices = nvoices;
412 gus_write8(0x0e, (nvoices - 1) | 0xc0);
413 }
414
gus_voice_on(unsigned int mode)415 static void gus_voice_on(unsigned int mode)
416 {
417 gus_write8(0x00, (unsigned char) (mode & 0xfc));
418 gus_delay();
419 gus_write8(0x00, (unsigned char) (mode & 0xfc));
420 }
421
gus_voice_off(void)422 static void gus_voice_off(void)
423 {
424 gus_write8(0x00, gus_read8(0x00) | 0x03);
425 }
426
gus_voice_mode(unsigned int m)427 static void gus_voice_mode(unsigned int m)
428 {
429 unsigned char mode = (unsigned char) (m & 0xff);
430
431 gus_write8(0x00, (gus_read8(0x00) & 0x03) |
432 (mode & 0xfc)); /* Don't touch last two bits */
433 gus_delay();
434 gus_write8(0x00, (gus_read8(0x00) & 0x03) | (mode & 0xfc));
435 }
436
gus_voice_freq(unsigned long freq)437 static void gus_voice_freq(unsigned long freq)
438 {
439 unsigned long divisor = freq_div_table[nr_voices - 14];
440 unsigned short fc;
441
442 /* Interwave plays at 44100 Hz with any number of voices */
443 if (iw_mode)
444 fc = (unsigned short) (((freq << 9) + (44100 >> 1)) / 44100);
445 else
446 fc = (unsigned short) (((freq << 9) + (divisor >> 1)) / divisor);
447 fc = fc << 1;
448
449 gus_write16(0x01, fc);
450 }
451
gus_voice_volume(unsigned int vol)452 static void gus_voice_volume(unsigned int vol)
453 {
454 gus_write8(0x0d, 0x03); /* Stop ramp before setting volume */
455 gus_write16(0x09, (unsigned short) (vol << 4));
456 }
457
gus_voice_balance(unsigned int balance)458 static void gus_voice_balance(unsigned int balance)
459 {
460 gus_write8(0x0c, (unsigned char) (balance & 0xff));
461 }
462
gus_ramp_range(unsigned int low,unsigned int high)463 static void gus_ramp_range(unsigned int low, unsigned int high)
464 {
465 gus_write8(0x07, (unsigned char) ((low >> 4) & 0xff));
466 gus_write8(0x08, (unsigned char) ((high >> 4) & 0xff));
467 }
468
gus_ramp_rate(unsigned int scale,unsigned int rate)469 static void gus_ramp_rate(unsigned int scale, unsigned int rate)
470 {
471 gus_write8(0x06, (unsigned char) (((scale & 0x03) << 6) | (rate & 0x3f)));
472 }
473
gus_rampon(unsigned int m)474 static void gus_rampon(unsigned int m)
475 {
476 unsigned char mode = (unsigned char) (m & 0xff);
477
478 gus_write8(0x0d, mode & 0xfc);
479 gus_delay();
480 gus_write8(0x0d, mode & 0xfc);
481 }
482
gus_ramp_mode(unsigned int m)483 static void gus_ramp_mode(unsigned int m)
484 {
485 unsigned char mode = (unsigned char) (m & 0xff);
486
487 gus_write8(0x0d, (gus_read8(0x0d) & 0x03) |
488 (mode & 0xfc)); /* Leave the last 2 bits alone */
489 gus_delay();
490 gus_write8(0x0d, (gus_read8(0x0d) & 0x03) | (mode & 0xfc));
491 }
492
gus_rampoff(void)493 static void gus_rampoff(void)
494 {
495 gus_write8(0x0d, 0x03);
496 }
497
gus_set_voice_pos(int voice,long position)498 static void gus_set_voice_pos(int voice, long position)
499 {
500 int sample_no;
501
502 if ((sample_no = sample_map[voice]) != -1) {
503 if (position < samples[sample_no].len) {
504 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
505 voices[voice].offset_pending = position;
506 else
507 gus_write_addr(0x0a, sample_ptrs[sample_no] + position, 0,
508 samples[sample_no].mode & WAVE_16_BITS);
509 }
510 }
511 }
512
gus_voice_init(int voice)513 static void gus_voice_init(int voice)
514 {
515 unsigned long flags;
516
517 save_flags(flags);
518 cli();
519 gus_select_voice(voice);
520 gus_voice_volume(0);
521 gus_voice_off();
522 gus_write_addr(0x0a, 0, 0, 0); /* Set current position to 0 */
523 gus_write8(0x00, 0x03); /* Voice off */
524 gus_write8(0x0d, 0x03); /* Ramping off */
525 voice_alloc->map[voice] = 0;
526 voice_alloc->alloc_times[voice] = 0;
527 restore_flags(flags);
528
529 }
530
gus_voice_init2(int voice)531 static void gus_voice_init2(int voice)
532 {
533 voices[voice].panning = 0;
534 voices[voice].mode = 0;
535 voices[voice].orig_freq = 20000;
536 voices[voice].current_freq = 20000;
537 voices[voice].bender = 0;
538 voices[voice].bender_range = 200;
539 voices[voice].initial_volume = 0;
540 voices[voice].current_volume = 0;
541 voices[voice].loop_irq_mode = 0;
542 voices[voice].loop_irq_parm = 0;
543 voices[voice].volume_irq_mode = 0;
544 voices[voice].volume_irq_parm = 0;
545 voices[voice].env_phase = 0;
546 voices[voice].main_vol = 127;
547 voices[voice].patch_vol = 127;
548 voices[voice].expression_vol = 127;
549 voices[voice].sample_pending = -1;
550 voices[voice].fixed_pitch = 0;
551 }
552
step_envelope(int voice)553 static void step_envelope(int voice)
554 {
555 unsigned vol, prev_vol, phase;
556 unsigned char rate;
557 long int flags;
558
559 if (voices[voice].mode & WAVE_SUSTAIN_ON && voices[voice].env_phase == 2)
560 {
561 save_flags(flags);
562 cli();
563 gus_select_voice(voice);
564 gus_rampoff();
565 restore_flags(flags);
566 return;
567 /*
568 * Sustain phase begins. Continue envelope after receiving note off.
569 */
570 }
571 if (voices[voice].env_phase >= 5)
572 {
573 /* Envelope finished. Shoot the voice down */
574 gus_voice_init(voice);
575 return;
576 }
577 prev_vol = voices[voice].current_volume;
578 phase = ++voices[voice].env_phase;
579 compute_volume(voice, voices[voice].midi_volume);
580 vol = voices[voice].initial_volume * voices[voice].env_offset[phase] / 255;
581 rate = voices[voice].env_rate[phase];
582
583 save_flags(flags);
584 cli();
585 gus_select_voice(voice);
586
587 gus_voice_volume(prev_vol);
588
589
590 gus_write8(0x06, rate); /* Ramping rate */
591
592 voices[voice].volume_irq_mode = VMODE_ENVELOPE;
593
594 if (((vol - prev_vol) / 64) == 0) /* No significant volume change */
595 {
596 restore_flags(flags);
597 step_envelope(voice); /* Continue the envelope on the next step */
598 return;
599 }
600 if (vol > prev_vol)
601 {
602 if (vol >= (4096 - 64))
603 vol = 4096 - 65;
604 gus_ramp_range(0, vol);
605 gus_rampon(0x20); /* Increasing volume, with IRQ */
606 }
607 else
608 {
609 if (vol <= 64)
610 vol = 65;
611 gus_ramp_range(vol, 4030);
612 gus_rampon(0x60); /* Decreasing volume, with IRQ */
613 }
614 voices[voice].current_volume = vol;
615 restore_flags(flags);
616 }
617
init_envelope(int voice)618 static void init_envelope(int voice)
619 {
620 voices[voice].env_phase = -1;
621 voices[voice].current_volume = 64;
622
623 step_envelope(voice);
624 }
625
start_release(int voice,long int flags)626 static void start_release(int voice, long int flags)
627 {
628 if (gus_read8(0x00) & 0x03)
629 return; /* Voice already stopped */
630
631 voices[voice].env_phase = 2; /* Will be incremented by step_envelope */
632
633 voices[voice].current_volume = voices[voice].initial_volume =
634 gus_read16(0x09) >> 4; /* Get current volume */
635
636 voices[voice].mode &= ~WAVE_SUSTAIN_ON;
637 gus_rampoff();
638 restore_flags(flags);
639 step_envelope(voice);
640 }
641
gus_voice_fade(int voice)642 static void gus_voice_fade(int voice)
643 {
644 int instr_no = sample_map[voice], is16bits;
645 long int flags;
646
647 save_flags(flags);
648 cli();
649 gus_select_voice(voice);
650
651 if (instr_no < 0 || instr_no > MAX_SAMPLE)
652 {
653 gus_write8(0x00, 0x03); /* Hard stop */
654 voice_alloc->map[voice] = 0;
655 restore_flags(flags);
656 return;
657 }
658 is16bits = (samples[instr_no].mode & WAVE_16_BITS) ? 1 : 0; /* 8 or 16 bits */
659
660 if (voices[voice].mode & WAVE_ENVELOPES)
661 {
662 start_release(voice, flags);
663 restore_flags(flags);
664 return;
665 }
666 /*
667 * Ramp the volume down but not too quickly.
668 */
669 if ((int) (gus_read16(0x09) >> 4) < 100) /* Get current volume */
670 {
671 gus_voice_off();
672 gus_rampoff();
673 gus_voice_init(voice);
674 restore_flags(flags);
675 return;
676 }
677 gus_ramp_range(65, 4030);
678 gus_ramp_rate(2, 4);
679 gus_rampon(0x40 | 0x20); /* Down, once, with IRQ */
680 voices[voice].volume_irq_mode = VMODE_HALT;
681 restore_flags(flags);
682 }
683
gus_reset(void)684 static void gus_reset(void)
685 {
686 int i;
687
688 gus_select_max_voices(24);
689 volume_base = 3071;
690 volume_scale = 4;
691 volume_method = VOL_METHOD_ADAGIO;
692
693 for (i = 0; i < 32; i++)
694 {
695 gus_voice_init(i); /* Turn voice off */
696 gus_voice_init2(i);
697 }
698 }
699
gus_initialize(void)700 static void gus_initialize(void)
701 {
702 unsigned long flags;
703 unsigned char dma_image, irq_image, tmp;
704
705 static unsigned char gus_irq_map[16] = {
706 0, 0, 0, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7
707 };
708
709 static unsigned char gus_dma_map[8] = {
710 0, 1, 0, 2, 0, 3, 4, 5
711 };
712
713 save_flags(flags);
714 cli();
715 gus_write8(0x4c, 0); /* Reset GF1 */
716 gus_delay();
717 gus_delay();
718
719 gus_write8(0x4c, 1); /* Release Reset */
720 gus_delay();
721 gus_delay();
722
723 /*
724 * Clear all interrupts
725 */
726
727 gus_write8(0x41, 0); /* DMA control */
728 gus_write8(0x45, 0); /* Timer control */
729 gus_write8(0x49, 0); /* Sample control */
730
731 gus_select_max_voices(24);
732
733 inb(u_Status); /* Touch the status register */
734
735 gus_look8(0x41); /* Clear any pending DMA IRQs */
736 gus_look8(0x49); /* Clear any pending sample IRQs */
737 gus_read8(0x0f); /* Clear pending IRQs */
738
739 gus_reset(); /* Resets all voices */
740
741 gus_look8(0x41); /* Clear any pending DMA IRQs */
742 gus_look8(0x49); /* Clear any pending sample IRQs */
743 gus_read8(0x0f); /* Clear pending IRQs */
744
745 gus_write8(0x4c, 7); /* Master reset | DAC enable | IRQ enable */
746
747 /*
748 * Set up for Digital ASIC
749 */
750
751 outb((0x05), gus_base + 0x0f);
752
753 mix_image |= 0x02; /* Disable line out (for a moment) */
754 outb((mix_image), u_Mixer);
755
756 outb((0x00), u_IRQDMAControl);
757
758 outb((0x00), gus_base + 0x0f);
759
760 /*
761 * Now set up the DMA and IRQ interface
762 *
763 * The GUS supports two IRQs and two DMAs.
764 *
765 * Just one DMA channel is used. This prevents simultaneous ADC and DAC.
766 * Adding this support requires significant changes to the dmabuf.c, dsp.c
767 * and audio.c also.
768 */
769
770 irq_image = 0;
771 tmp = gus_irq_map[gus_irq];
772 if (!gus_pnp_flag && !tmp)
773 printk(KERN_WARNING "Warning! GUS IRQ not selected\n");
774 irq_image |= tmp;
775 irq_image |= 0x40; /* Combine IRQ1 (GF1) and IRQ2 (Midi) */
776
777 dual_dma_mode = 1;
778 if (gus_dma2 == gus_dma || gus_dma2 == -1)
779 {
780 dual_dma_mode = 0;
781 dma_image = 0x40; /* Combine DMA1 (DRAM) and IRQ2 (ADC) */
782
783 tmp = gus_dma_map[gus_dma];
784 if (!tmp)
785 printk(KERN_WARNING "Warning! GUS DMA not selected\n");
786
787 dma_image |= tmp;
788 }
789 else
790 {
791 /* Setup dual DMA channel mode for GUS MAX */
792
793 dma_image = gus_dma_map[gus_dma];
794 if (!dma_image)
795 printk(KERN_WARNING "Warning! GUS DMA not selected\n");
796
797 tmp = gus_dma_map[gus_dma2] << 3;
798 if (!tmp)
799 {
800 printk(KERN_WARNING "Warning! Invalid GUS MAX DMA\n");
801 tmp = 0x40; /* Combine DMA channels */
802 dual_dma_mode = 0;
803 }
804 dma_image |= tmp;
805 }
806
807 /*
808 * For some reason the IRQ and DMA addresses must be written twice
809 */
810
811 /*
812 * Doing it first time
813 */
814
815 outb((mix_image), u_Mixer); /* Select DMA control */
816 outb((dma_image | 0x80), u_IRQDMAControl); /* Set DMA address */
817
818 outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
819 outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
820
821 /*
822 * Doing it second time
823 */
824
825 outb((mix_image), u_Mixer); /* Select DMA control */
826 outb((dma_image), u_IRQDMAControl); /* Set DMA address */
827
828 outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
829 outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
830
831 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
832
833 mix_image &= ~0x02; /* Enable line out */
834 mix_image |= 0x08; /* Enable IRQ */
835 outb((mix_image), u_Mixer); /*
836 * Turn mixer channels on
837 * Note! Mic in is left off.
838 */
839
840 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
841
842 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
843
844 inb(u_Status); /* Touch the status register */
845
846 gus_look8(0x41); /* Clear any pending DMA IRQs */
847 gus_look8(0x49); /* Clear any pending sample IRQs */
848
849 gus_read8(0x0f); /* Clear pending IRQs */
850
851 if (iw_mode)
852 gus_write8(0x19, gus_read8(0x19) | 0x01);
853 restore_flags(flags);
854 }
855
856
pnp_mem_init(void)857 static void __init pnp_mem_init(void)
858 {
859 #include "iwmem.h"
860 #define CHUNK_SIZE (256*1024)
861 #define BANK_SIZE (4*1024*1024)
862 #define CHUNKS_PER_BANK (BANK_SIZE/CHUNK_SIZE)
863
864 int bank, chunk, addr, total = 0;
865 int bank_sizes[4];
866 int i, j, bits = -1, testbits = -1, nbanks = 0;
867
868 /*
869 * This routine determines what kind of RAM is installed in each of the four
870 * SIMM banks and configures the DRAM address decode logic accordingly.
871 */
872
873 /*
874 * Place the chip into enhanced mode
875 */
876 gus_write8(0x19, gus_read8(0x19) | 0x01);
877 gus_write8(0x53, gus_look8(0x53) & ~0x02); /* Select DRAM I/O access */
878
879 /*
880 * Set memory configuration to 4 DRAM banks of 4M in each (16M total).
881 */
882
883 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | 0x000c);
884
885 /*
886 * Perform the DRAM size detection for each bank individually.
887 */
888 for (bank = 0; bank < 4; bank++)
889 {
890 int size = 0;
891
892 addr = bank * BANK_SIZE;
893
894 /* Clean check points of each chunk */
895 for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
896 {
897 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
898 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
899 }
900
901 /* Write a value to each chunk point and verify the result */
902 for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
903 {
904 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x55);
905 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0xAA);
906
907 if (gus_peek(addr + chunk * CHUNK_SIZE + 0L) == 0x55 &&
908 gus_peek(addr + chunk * CHUNK_SIZE + 1L) == 0xAA)
909 {
910 /* OK. There is RAM. Now check for possible shadows */
911 int ok = 1, chunk2;
912
913 for (chunk2 = 0; ok && chunk2 < chunk; chunk2++)
914 if (gus_peek(addr + chunk2 * CHUNK_SIZE + 0L) ||
915 gus_peek(addr + chunk2 * CHUNK_SIZE + 1L))
916 ok = 0; /* Addressing wraps */
917
918 if (ok)
919 size = (chunk + 1) * CHUNK_SIZE;
920 }
921 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
922 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
923 }
924 bank_sizes[bank] = size;
925 if (size)
926 nbanks = bank + 1;
927 DDB(printk("Interwave: Bank %d, size=%dk\n", bank, size / 1024));
928 }
929
930 if (nbanks == 0) /* No RAM - Give up */
931 {
932 printk(KERN_ERR "Sound: An Interwave audio chip detected but no DRAM\n");
933 printk(KERN_ERR "Sound: Unable to work with this card.\n");
934 gus_write8(0x19, gus_read8(0x19) & ~0x01);
935 gus_mem_size = 0;
936 return;
937 }
938
939 /*
940 * Now we know how much DRAM there is in each bank. The next step is
941 * to find a DRAM size encoding (0 to 12) which is best for the combination
942 * we have.
943 *
944 * First try if any of the possible alternatives matches exactly the amount
945 * of memory we have.
946 */
947
948 for (i = 0; bits == -1 && i < 13; i++)
949 {
950 bits = i;
951
952 for (j = 0; bits != -1 && j < 4; j++)
953 if (mem_decode[i][j] != bank_sizes[j])
954 bits = -1; /* No hit */
955 }
956
957 /*
958 * If necessary, try to find a combination where other than the last
959 * bank matches our configuration and the last bank is left oversized.
960 * In this way we don't leave holes in the middle of memory.
961 */
962
963 if (bits == -1) /* No luck yet */
964 {
965 for (i = 0; bits == -1 && i < 13; i++)
966 {
967 bits = i;
968
969 for (j = 0; bits != -1 && j < nbanks - 1; j++)
970 if (mem_decode[i][j] != bank_sizes[j])
971 bits = -1; /* No hit */
972 if (mem_decode[i][nbanks - 1] < bank_sizes[nbanks - 1])
973 bits = -1; /* The last bank is too small */
974 }
975 }
976 /*
977 * The last resort is to search for a combination where the banks are
978 * smaller than the actual SIMMs. This leaves some memory in the banks
979 * unused but doesn't leave holes in the DRAM address space.
980 */
981 if (bits == -1) /* No luck yet */
982 {
983 for (i = 0; i < 13; i++)
984 {
985 testbits = i;
986 for (j = 0; testbits != -1 && j < nbanks - 1; j++)
987 if (mem_decode[i][j] > bank_sizes[j]) {
988 testbits = -1;
989 }
990 if(testbits > bits) bits = testbits;
991 }
992 if (bits != -1)
993 {
994 printk(KERN_INFO "Interwave: Can't use all installed RAM.\n");
995 printk(KERN_INFO "Interwave: Try reordering SIMMS.\n");
996 }
997 printk(KERN_INFO "Interwave: Can't find working DRAM encoding.\n");
998 printk(KERN_INFO "Interwave: Defaulting to 256k. Try reordering SIMMS.\n");
999 bits = 0;
1000 }
1001 DDB(printk("Interwave: Selecting DRAM addressing mode %d\n", bits));
1002
1003 for (bank = 0; bank < 4; bank++)
1004 {
1005 DDB(printk(" Bank %d, mem=%dk (limit %dk)\n", bank, bank_sizes[bank] / 1024, mem_decode[bits][bank] / 1024));
1006
1007 if (bank_sizes[bank] > mem_decode[bits][bank])
1008 total += mem_decode[bits][bank];
1009 else
1010 total += bank_sizes[bank];
1011 }
1012
1013 DDB(printk("Total %dk of DRAM (enhanced mode)\n", total / 1024));
1014
1015 /*
1016 * Set the memory addressing mode.
1017 */
1018 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | bits);
1019
1020 /* Leave the chip into enhanced mode. Disable LFO */
1021 gus_mem_size = total;
1022 iw_mode = 1;
1023 gus_write8(0x19, (gus_read8(0x19) | 0x01) & ~0x02);
1024 }
1025
gus_wave_detect(int baseaddr)1026 int __init gus_wave_detect(int baseaddr)
1027 {
1028 unsigned long i, max_mem = 1024L;
1029 unsigned long loc;
1030 unsigned char val;
1031
1032 gus_base = baseaddr;
1033
1034 gus_write8(0x4c, 0); /* Reset GF1 */
1035 gus_delay();
1036 gus_delay();
1037
1038 gus_write8(0x4c, 1); /* Release Reset */
1039 gus_delay();
1040 gus_delay();
1041
1042 #ifdef GUSPNP_AUTODETECT
1043 val = gus_look8(0x5b); /* Version number register */
1044 gus_write8(0x5b, ~val); /* Invert all bits */
1045
1046 if ((gus_look8(0x5b) & 0xf0) == (val & 0xf0)) /* No change */
1047 {
1048 if ((gus_look8(0x5b) & 0x0f) == ((~val) & 0x0f)) /* Change */
1049 {
1050 DDB(printk("Interwave chip version %d detected\n", (val & 0xf0) >> 4));
1051 gus_pnp_flag = 1;
1052 }
1053 else
1054 {
1055 DDB(printk("Not an Interwave chip (%x)\n", gus_look8(0x5b)));
1056 gus_pnp_flag = 0;
1057 }
1058 }
1059 gus_write8(0x5b, val); /* Restore all bits */
1060 #endif
1061
1062 if (gus_pnp_flag)
1063 pnp_mem_init();
1064 if (iw_mode)
1065 return 1;
1066
1067 /* See if there is first block there.... */
1068 gus_poke(0L, 0xaa);
1069 if (gus_peek(0L) != 0xaa)
1070 return (0);
1071
1072 /* Now zero it out so that I can check for mirroring .. */
1073 gus_poke(0L, 0x00);
1074 for (i = 1L; i < max_mem; i++)
1075 {
1076 int n, failed;
1077
1078 /* check for mirroring ... */
1079 if (gus_peek(0L) != 0)
1080 break;
1081 loc = i << 10;
1082
1083 for (n = loc - 1, failed = 0; n <= loc; n++)
1084 {
1085 gus_poke(loc, 0xaa);
1086 if (gus_peek(loc) != 0xaa)
1087 failed = 1;
1088 gus_poke(loc, 0x55);
1089 if (gus_peek(loc) != 0x55)
1090 failed = 1;
1091 }
1092 if (failed)
1093 break;
1094 }
1095 gus_mem_size = i << 10;
1096 return 1;
1097 }
1098
guswave_ioctl(int dev,unsigned int cmd,caddr_t arg)1099 static int guswave_ioctl(int dev, unsigned int cmd, caddr_t arg)
1100 {
1101
1102 switch (cmd)
1103 {
1104 case SNDCTL_SYNTH_INFO:
1105 gus_info.nr_voices = nr_voices;
1106 if (copy_to_user(arg, &gus_info, sizeof(gus_info)))
1107 return -EFAULT;
1108 return 0;
1109
1110 case SNDCTL_SEQ_RESETSAMPLES:
1111 reset_sample_memory();
1112 return 0;
1113
1114 case SNDCTL_SEQ_PERCMODE:
1115 return 0;
1116
1117 case SNDCTL_SYNTH_MEMAVL:
1118 return (gus_mem_size == 0) ? 0 : gus_mem_size - free_mem_ptr - 32;
1119
1120 default:
1121 return -EINVAL;
1122 }
1123 }
1124
guswave_set_instr(int dev,int voice,int instr_no)1125 static int guswave_set_instr(int dev, int voice, int instr_no)
1126 {
1127 int sample_no;
1128
1129 if (instr_no < 0 || instr_no > MAX_PATCH)
1130 instr_no = 0; /* Default to acoustic piano */
1131
1132 if (voice < 0 || voice > 31)
1133 return -EINVAL;
1134
1135 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1136 {
1137 voices[voice].sample_pending = instr_no;
1138 return 0;
1139 }
1140 sample_no = patch_table[instr_no];
1141 patch_map[voice] = -1;
1142
1143 if (sample_no == NOT_SAMPLE)
1144 {
1145 /* printk("GUS: Undefined patch %d for voice %d\n", instr_no, voice);*/
1146 return -EINVAL; /* Patch not defined */
1147 }
1148 if (sample_ptrs[sample_no] == -1) /* Sample not loaded */
1149 {
1150 /* printk("GUS: Sample #%d not loaded for patch %d (voice %d)\n", sample_no, instr_no, voice);*/
1151 return -EINVAL;
1152 }
1153 sample_map[voice] = sample_no;
1154 patch_map[voice] = instr_no;
1155 return 0;
1156 }
1157
guswave_kill_note(int dev,int voice,int note,int velocity)1158 static int guswave_kill_note(int dev, int voice, int note, int velocity)
1159 {
1160 unsigned long flags;
1161
1162 save_flags(flags);
1163 cli();
1164 /* voice_alloc->map[voice] = 0xffff; */
1165 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1166 {
1167 voices[voice].kill_pending = 1;
1168 restore_flags(flags);
1169 }
1170 else
1171 {
1172 restore_flags(flags);
1173 gus_voice_fade(voice);
1174 }
1175
1176 return 0;
1177 }
1178
guswave_aftertouch(int dev,int voice,int pressure)1179 static void guswave_aftertouch(int dev, int voice, int pressure)
1180 {
1181 }
1182
guswave_panning(int dev,int voice,int value)1183 static void guswave_panning(int dev, int voice, int value)
1184 {
1185 if (voice >= 0 || voice < 32)
1186 voices[voice].panning = value;
1187 }
1188
guswave_volume_method(int dev,int mode)1189 static void guswave_volume_method(int dev, int mode)
1190 {
1191 if (mode == VOL_METHOD_LINEAR || mode == VOL_METHOD_ADAGIO)
1192 volume_method = mode;
1193 }
1194
compute_volume(int voice,int volume)1195 static void compute_volume(int voice, int volume)
1196 {
1197 if (volume < 128)
1198 voices[voice].midi_volume = volume;
1199
1200 switch (volume_method)
1201 {
1202 case VOL_METHOD_ADAGIO:
1203 voices[voice].initial_volume =
1204 gus_adagio_vol(voices[voice].midi_volume, voices[voice].main_vol,
1205 voices[voice].expression_vol,
1206 voices[voice].patch_vol);
1207 break;
1208
1209 case VOL_METHOD_LINEAR: /* Totally ignores patch-volume and expression */
1210 voices[voice].initial_volume = gus_linear_vol(volume, voices[voice].main_vol);
1211 break;
1212
1213 default:
1214 voices[voice].initial_volume = volume_base +
1215 (voices[voice].midi_volume * volume_scale);
1216 }
1217
1218 if (voices[voice].initial_volume > 4030)
1219 voices[voice].initial_volume = 4030;
1220 }
1221
compute_and_set_volume(int voice,int volume,int ramp_time)1222 static void compute_and_set_volume(int voice, int volume, int ramp_time)
1223 {
1224 int curr, target, rate;
1225 unsigned long flags;
1226
1227 compute_volume(voice, volume);
1228 voices[voice].current_volume = voices[voice].initial_volume;
1229
1230 save_flags(flags);
1231 cli();
1232 /*
1233 * CAUTION! Interrupts disabled. Enable them before returning
1234 */
1235
1236 gus_select_voice(voice);
1237
1238 curr = gus_read16(0x09) >> 4;
1239 target = voices[voice].initial_volume;
1240
1241 if (ramp_time == INSTANT_RAMP)
1242 {
1243 gus_rampoff();
1244 gus_voice_volume(target);
1245 restore_flags(flags);
1246 return;
1247 }
1248 if (ramp_time == FAST_RAMP)
1249 rate = 63;
1250 else
1251 rate = 16;
1252 gus_ramp_rate(0, rate);
1253
1254 if ((target - curr) / 64 == 0) /* Close enough to target. */
1255 {
1256 gus_rampoff();
1257 gus_voice_volume(target);
1258 restore_flags(flags);
1259 return;
1260 }
1261 if (target > curr)
1262 {
1263 if (target > (4095 - 65))
1264 target = 4095 - 65;
1265 gus_ramp_range(curr, target);
1266 gus_rampon(0x00); /* Ramp up, once, no IRQ */
1267 }
1268 else
1269 {
1270 if (target < 65)
1271 target = 65;
1272
1273 gus_ramp_range(target, curr);
1274 gus_rampon(0x40); /* Ramp down, once, no irq */
1275 }
1276 restore_flags(flags);
1277 }
1278
dynamic_volume_change(int voice)1279 static void dynamic_volume_change(int voice)
1280 {
1281 unsigned char status;
1282 unsigned long flags;
1283
1284 save_flags(flags);
1285 cli();
1286 gus_select_voice(voice);
1287 status = gus_read8(0x00); /* Get voice status */
1288 restore_flags(flags);
1289
1290 if (status & 0x03)
1291 return; /* Voice was not running */
1292
1293 if (!(voices[voice].mode & WAVE_ENVELOPES))
1294 {
1295 compute_and_set_volume(voice, voices[voice].midi_volume, 1);
1296 return;
1297 }
1298
1299 /*
1300 * Voice is running and has envelopes.
1301 */
1302
1303 save_flags(flags);
1304 cli();
1305 gus_select_voice(voice);
1306 status = gus_read8(0x0d); /* Ramping status */
1307 restore_flags(flags);
1308
1309 if (status & 0x03) /* Sustain phase? */
1310 {
1311 compute_and_set_volume(voice, voices[voice].midi_volume, 1);
1312 return;
1313 }
1314 if (voices[voice].env_phase < 0)
1315 return;
1316
1317 compute_volume(voice, voices[voice].midi_volume);
1318
1319 }
1320
guswave_controller(int dev,int voice,int ctrl_num,int value)1321 static void guswave_controller(int dev, int voice, int ctrl_num, int value)
1322 {
1323 unsigned long flags;
1324 unsigned long freq;
1325
1326 if (voice < 0 || voice > 31)
1327 return;
1328
1329 switch (ctrl_num)
1330 {
1331 case CTRL_PITCH_BENDER:
1332 voices[voice].bender = value;
1333
1334 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1335 {
1336 freq = compute_finetune(voices[voice].orig_freq, value, voices[voice].bender_range, 0);
1337 voices[voice].current_freq = freq;
1338
1339 save_flags(flags);
1340 cli();
1341 gus_select_voice(voice);
1342 gus_voice_freq(freq);
1343 restore_flags(flags);
1344 }
1345 break;
1346
1347 case CTRL_PITCH_BENDER_RANGE:
1348 voices[voice].bender_range = value;
1349 break;
1350 case CTL_EXPRESSION:
1351 value /= 128;
1352 case CTRL_EXPRESSION:
1353 if (volume_method == VOL_METHOD_ADAGIO)
1354 {
1355 voices[voice].expression_vol = value;
1356 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1357 dynamic_volume_change(voice);
1358 }
1359 break;
1360
1361 case CTL_PAN:
1362 voices[voice].panning = (value * 2) - 128;
1363 break;
1364
1365 case CTL_MAIN_VOLUME:
1366 value = (value * 100) / 16383;
1367
1368 case CTRL_MAIN_VOLUME:
1369 voices[voice].main_vol = value;
1370 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1371 dynamic_volume_change(voice);
1372 break;
1373
1374 default:
1375 break;
1376 }
1377 }
1378
guswave_start_note2(int dev,int voice,int note_num,int volume)1379 static int guswave_start_note2(int dev, int voice, int note_num, int volume)
1380 {
1381 int sample, best_sample, best_delta, delta_freq;
1382 int is16bits, samplep, patch, pan;
1383 unsigned long note_freq, base_note, freq, flags;
1384 unsigned char mode = 0;
1385
1386 if (voice < 0 || voice > 31)
1387 {
1388 /* printk("GUS: Invalid voice\n");*/
1389 return -EINVAL;
1390 }
1391 if (note_num == 255)
1392 {
1393 if (voices[voice].mode & WAVE_ENVELOPES)
1394 {
1395 voices[voice].midi_volume = volume;
1396 dynamic_volume_change(voice);
1397 return 0;
1398 }
1399 compute_and_set_volume(voice, volume, 1);
1400 return 0;
1401 }
1402 if ((patch = patch_map[voice]) == -1)
1403 return -EINVAL;
1404 if ((samplep = patch_table[patch]) == NOT_SAMPLE)
1405 {
1406 return -EINVAL;
1407 }
1408 note_freq = note_to_freq(note_num);
1409
1410 /*
1411 * Find a sample within a patch so that the note_freq is between low_note
1412 * and high_note.
1413 */
1414 sample = -1;
1415
1416 best_sample = samplep;
1417 best_delta = 1000000;
1418 while (samplep != 0 && samplep != NOT_SAMPLE && sample == -1)
1419 {
1420 delta_freq = note_freq - samples[samplep].base_note;
1421 if (delta_freq < 0)
1422 delta_freq = -delta_freq;
1423 if (delta_freq < best_delta)
1424 {
1425 best_sample = samplep;
1426 best_delta = delta_freq;
1427 }
1428 if (samples[samplep].low_note <= note_freq &&
1429 note_freq <= samples[samplep].high_note)
1430 {
1431 sample = samplep;
1432 }
1433 else
1434 samplep = samples[samplep].key; /* Link to next sample */
1435 }
1436 if (sample == -1)
1437 sample = best_sample;
1438
1439 if (sample == -1)
1440 {
1441 /* printk("GUS: Patch %d not defined for note %d\n", patch, note_num);*/
1442 return 0; /* Should play default patch ??? */
1443 }
1444 is16bits = (samples[sample].mode & WAVE_16_BITS) ? 1 : 0;
1445 voices[voice].mode = samples[sample].mode;
1446 voices[voice].patch_vol = samples[sample].volume;
1447
1448 if (iw_mode)
1449 gus_write8(0x15, 0x00); /* RAM, Reset voice deactivate bit of SMSI */
1450
1451 if (voices[voice].mode & WAVE_ENVELOPES)
1452 {
1453 int i;
1454
1455 for (i = 0; i < 6; i++)
1456 {
1457 voices[voice].env_rate[i] = samples[sample].env_rate[i];
1458 voices[voice].env_offset[i] = samples[sample].env_offset[i];
1459 }
1460 }
1461 sample_map[voice] = sample;
1462
1463 if (voices[voice].fixed_pitch) /* Fixed pitch */
1464 {
1465 freq = samples[sample].base_freq;
1466 }
1467 else
1468 {
1469 base_note = samples[sample].base_note / 100;
1470 note_freq /= 100;
1471
1472 freq = samples[sample].base_freq * note_freq / base_note;
1473 }
1474
1475 voices[voice].orig_freq = freq;
1476
1477 /*
1478 * Since the pitch bender may have been set before playing the note, we
1479 * have to calculate the bending now.
1480 */
1481
1482 freq = compute_finetune(voices[voice].orig_freq, voices[voice].bender,
1483 voices[voice].bender_range, 0);
1484 voices[voice].current_freq = freq;
1485
1486 pan = (samples[sample].panning + voices[voice].panning) / 32;
1487 pan += 7;
1488 if (pan < 0)
1489 pan = 0;
1490 if (pan > 15)
1491 pan = 15;
1492
1493 if (samples[sample].mode & WAVE_16_BITS)
1494 {
1495 mode |= 0x04; /* 16 bits */
1496 if ((sample_ptrs[sample] / GUS_BANK_SIZE) !=
1497 ((sample_ptrs[sample] + samples[sample].len) / GUS_BANK_SIZE))
1498 printk(KERN_ERR "GUS: Sample address error\n");
1499 }
1500 /*************************************************************************
1501 * CAUTION! Interrupts disabled. Don't return before enabling
1502 *************************************************************************/
1503
1504 save_flags(flags);
1505 cli();
1506 gus_select_voice(voice);
1507 gus_voice_off();
1508 gus_rampoff();
1509
1510 restore_flags(flags);
1511
1512 if (voices[voice].mode & WAVE_ENVELOPES)
1513 {
1514 compute_volume(voice, volume);
1515 init_envelope(voice);
1516 }
1517 else
1518 {
1519 compute_and_set_volume(voice, volume, 0);
1520 }
1521
1522 save_flags(flags);
1523 cli();
1524 gus_select_voice(voice);
1525
1526 if (samples[sample].mode & WAVE_LOOP_BACK)
1527 gus_write_addr(0x0a, sample_ptrs[sample] + samples[sample].len -
1528 voices[voice].offset_pending, 0, is16bits); /* start=end */
1529 else
1530 gus_write_addr(0x0a, sample_ptrs[sample] + voices[voice].offset_pending, 0, is16bits); /* Sample start=begin */
1531
1532 if (samples[sample].mode & WAVE_LOOPING)
1533 {
1534 mode |= 0x08;
1535
1536 if (samples[sample].mode & WAVE_BIDIR_LOOP)
1537 mode |= 0x10;
1538
1539 if (samples[sample].mode & WAVE_LOOP_BACK)
1540 {
1541 gus_write_addr(0x0a, sample_ptrs[sample] + samples[sample].loop_end -
1542 voices[voice].offset_pending,
1543 (samples[sample].fractions >> 4) & 0x0f, is16bits);
1544 mode |= 0x40;
1545 }
1546 gus_write_addr(0x02, sample_ptrs[sample] + samples[sample].loop_start,
1547 samples[sample].fractions & 0x0f, is16bits); /* Loop start location */
1548 gus_write_addr(0x04, sample_ptrs[sample] + samples[sample].loop_end,
1549 (samples[sample].fractions >> 4) & 0x0f, is16bits); /* Loop end location */
1550 }
1551 else
1552 {
1553 mode |= 0x20; /* Loop IRQ at the end */
1554 voices[voice].loop_irq_mode = LMODE_FINISH; /* Ramp down at the end */
1555 voices[voice].loop_irq_parm = 1;
1556 gus_write_addr(0x02, sample_ptrs[sample], 0, is16bits); /* Loop start location */
1557 gus_write_addr(0x04, sample_ptrs[sample] + samples[sample].len - 1,
1558 (samples[sample].fractions >> 4) & 0x0f, is16bits); /* Loop end location */
1559 }
1560 gus_voice_freq(freq);
1561 gus_voice_balance(pan);
1562 gus_voice_on(mode);
1563 restore_flags(flags);
1564
1565 return 0;
1566 }
1567
1568 /*
1569 * New guswave_start_note by Andrew J. Robinson attempts to minimize clicking
1570 * when the note playing on the voice is changed. It uses volume
1571 * ramping.
1572 */
1573
guswave_start_note(int dev,int voice,int note_num,int volume)1574 static int guswave_start_note(int dev, int voice, int note_num, int volume)
1575 {
1576 long int flags;
1577 int mode;
1578 int ret_val = 0;
1579
1580 save_flags(flags);
1581 cli();
1582 if (note_num == 255)
1583 {
1584 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1585 {
1586 voices[voice].volume_pending = volume;
1587 }
1588 else
1589 {
1590 ret_val = guswave_start_note2(dev, voice, note_num, volume);
1591 }
1592 }
1593 else
1594 {
1595 gus_select_voice(voice);
1596 mode = gus_read8(0x00);
1597 if (mode & 0x20)
1598 gus_write8(0x00, mode & 0xdf); /* No interrupt! */
1599
1600 voices[voice].offset_pending = 0;
1601 voices[voice].kill_pending = 0;
1602 voices[voice].volume_irq_mode = 0;
1603 voices[voice].loop_irq_mode = 0;
1604
1605 if (voices[voice].sample_pending >= 0)
1606 {
1607 restore_flags(flags); /* Run temporarily with interrupts enabled */
1608 guswave_set_instr(voices[voice].dev_pending, voice, voices[voice].sample_pending);
1609 voices[voice].sample_pending = -1;
1610 save_flags(flags);
1611 cli();
1612 gus_select_voice(voice); /* Reselect the voice (just to be sure) */
1613 }
1614 if ((mode & 0x01) || (int) ((gus_read16(0x09) >> 4) < (unsigned) 2065))
1615 {
1616 ret_val = guswave_start_note2(dev, voice, note_num, volume);
1617 }
1618 else
1619 {
1620 voices[voice].dev_pending = dev;
1621 voices[voice].note_pending = note_num;
1622 voices[voice].volume_pending = volume;
1623 voices[voice].volume_irq_mode = VMODE_START_NOTE;
1624
1625 gus_rampoff();
1626 gus_ramp_range(2000, 4065);
1627 gus_ramp_rate(0, 63); /* Fastest possible rate */
1628 gus_rampon(0x20 | 0x40); /* Ramp down, once, irq */
1629 }
1630 }
1631 restore_flags(flags);
1632 return ret_val;
1633 }
1634
guswave_reset(int dev)1635 static void guswave_reset(int dev)
1636 {
1637 int i;
1638
1639 for (i = 0; i < 32; i++)
1640 {
1641 gus_voice_init(i);
1642 gus_voice_init2(i);
1643 }
1644 }
1645
guswave_open(int dev,int mode)1646 static int guswave_open(int dev, int mode)
1647 {
1648 int err;
1649
1650 if (gus_busy)
1651 return -EBUSY;
1652
1653 voice_alloc->timestamp = 0;
1654
1655 if (gus_no_wave_dma) {
1656 gus_no_dma = 1;
1657 } else {
1658 if ((err = DMAbuf_open_dma(gus_devnum)) < 0)
1659 {
1660 /* printk( "GUS: Loading samples without DMA\n"); */
1661 gus_no_dma = 1; /* Upload samples using PIO */
1662 }
1663 else
1664 gus_no_dma = 0;
1665 }
1666
1667 init_waitqueue_head(&dram_sleeper);
1668 gus_busy = 1;
1669 active_device = GUS_DEV_WAVE;
1670
1671 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
1672 gus_initialize();
1673 gus_reset();
1674 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
1675
1676 return 0;
1677 }
1678
guswave_close(int dev)1679 static void guswave_close(int dev)
1680 {
1681 gus_busy = 0;
1682 active_device = 0;
1683 gus_reset();
1684
1685 if (!gus_no_dma)
1686 DMAbuf_close_dma(gus_devnum);
1687 }
1688
guswave_load_patch(int dev,int format,const char * addr,int offs,int count,int pmgr_flag)1689 static int guswave_load_patch(int dev, int format, const char *addr,
1690 int offs, int count, int pmgr_flag)
1691 {
1692 struct patch_info patch;
1693 int instr;
1694 long sizeof_patch;
1695
1696 unsigned long blk_sz, blk_end, left, src_offs, target;
1697
1698 sizeof_patch = (long) &patch.data[0] - (long) &patch; /* Header size */
1699
1700 if (format != GUS_PATCH)
1701 {
1702 /* printk("GUS Error: Invalid patch format (key) 0x%x\n", format);*/
1703 return -EINVAL;
1704 }
1705 if (count < sizeof_patch)
1706 {
1707 /* printk("GUS Error: Patch header too short\n");*/
1708 return -EINVAL;
1709 }
1710 count -= sizeof_patch;
1711
1712 if (free_sample >= MAX_SAMPLE)
1713 {
1714 /* printk("GUS: Sample table full\n");*/
1715 return -ENOSPC;
1716 }
1717 /*
1718 * Copy the header from user space but ignore the first bytes which have
1719 * been transferred already.
1720 */
1721
1722 copy_from_user(&((char *) &patch)[offs], &(addr)[offs], sizeof_patch - offs);
1723
1724 if (patch.mode & WAVE_ROM)
1725 return -EINVAL;
1726 if (gus_mem_size == 0)
1727 return -ENOSPC;
1728
1729 instr = patch.instr_no;
1730
1731 if (instr < 0 || instr > MAX_PATCH)
1732 {
1733 /* printk(KERN_ERR "GUS: Invalid patch number %d\n", instr);*/
1734 return -EINVAL;
1735 }
1736 if (count < patch.len)
1737 {
1738 /* printk(KERN_ERR "GUS Warning: Patch record too short (%d<%d)\n", count, (int) patch.len);*/
1739 patch.len = count;
1740 }
1741 if (patch.len <= 0 || patch.len > gus_mem_size)
1742 {
1743 /* printk(KERN_ERR "GUS: Invalid sample length %d\n", (int) patch.len);*/
1744 return -EINVAL;
1745 }
1746 if (patch.mode & WAVE_LOOPING)
1747 {
1748 if (patch.loop_start < 0 || patch.loop_start >= patch.len)
1749 {
1750 /* printk(KERN_ERR "GUS: Invalid loop start\n");*/
1751 return -EINVAL;
1752 }
1753 if (patch.loop_end < patch.loop_start || patch.loop_end > patch.len)
1754 {
1755 /* printk(KERN_ERR "GUS: Invalid loop end\n");*/
1756 return -EINVAL;
1757 }
1758 }
1759 free_mem_ptr = (free_mem_ptr + 31) & ~31; /* 32 byte alignment */
1760
1761 if (patch.mode & WAVE_16_BITS)
1762 {
1763 /*
1764 * 16 bit samples must fit one 256k bank.
1765 */
1766 if (patch.len >= GUS_BANK_SIZE)
1767 {
1768 /* printk("GUS: Sample (16 bit) too long %d\n", (int) patch.len);*/
1769 return -ENOSPC;
1770 }
1771 if ((free_mem_ptr / GUS_BANK_SIZE) !=
1772 ((free_mem_ptr + patch.len) / GUS_BANK_SIZE))
1773 {
1774 unsigned long tmp_mem =
1775 /* Align to 256K */
1776 ((free_mem_ptr / GUS_BANK_SIZE) + 1) * GUS_BANK_SIZE;
1777
1778 if ((tmp_mem + patch.len) > gus_mem_size)
1779 return -ENOSPC;
1780
1781 free_mem_ptr = tmp_mem; /* This leaves unusable memory */
1782 }
1783 }
1784 if ((free_mem_ptr + patch.len) > gus_mem_size)
1785 return -ENOSPC;
1786
1787 sample_ptrs[free_sample] = free_mem_ptr;
1788
1789 /*
1790 * Tremolo is not possible with envelopes
1791 */
1792
1793 if (patch.mode & WAVE_ENVELOPES)
1794 patch.mode &= ~WAVE_TREMOLO;
1795
1796 if (!(patch.mode & WAVE_FRACTIONS))
1797 {
1798 patch.fractions = 0;
1799 }
1800 memcpy((char *) &samples[free_sample], &patch, sizeof_patch);
1801
1802 /*
1803 * Link this_one sample to the list of samples for patch 'instr'.
1804 */
1805
1806 samples[free_sample].key = patch_table[instr];
1807 patch_table[instr] = free_sample;
1808
1809 /*
1810 * Use DMA to transfer the wave data to the DRAM
1811 */
1812
1813 left = patch.len;
1814 src_offs = 0;
1815 target = free_mem_ptr;
1816
1817 while (left) /* Not completely transferred yet */
1818 {
1819 blk_sz = audio_devs[gus_devnum]->dmap_out->bytes_in_use;
1820 if (blk_sz > left)
1821 blk_sz = left;
1822
1823 /*
1824 * DMA cannot cross bank (256k) boundaries. Check for that.
1825 */
1826
1827 blk_end = target + blk_sz;
1828
1829 if ((target / GUS_BANK_SIZE) != (blk_end / GUS_BANK_SIZE))
1830 {
1831 /* Split the block */
1832 blk_end &= ~(GUS_BANK_SIZE - 1);
1833 blk_sz = blk_end - target;
1834 }
1835 if (gus_no_dma)
1836 {
1837 /*
1838 * For some reason the DMA is not possible. We have to use PIO.
1839 */
1840 long i;
1841 unsigned char data;
1842
1843 for (i = 0; i < blk_sz; i++)
1844 {
1845 get_user(*(unsigned char *) &data, (unsigned char *) &((addr)[sizeof_patch + i]));
1846 if (patch.mode & WAVE_UNSIGNED)
1847 if (!(patch.mode & WAVE_16_BITS) || (i & 0x01))
1848 data ^= 0x80; /* Convert to signed */
1849 gus_poke(target + i, data);
1850 }
1851 }
1852 else
1853 {
1854 unsigned long address, hold_address;
1855 unsigned char dma_command;
1856 unsigned long flags;
1857
1858 if (audio_devs[gus_devnum]->dmap_out->raw_buf == NULL)
1859 {
1860 printk(KERN_ERR "GUS: DMA buffer == NULL\n");
1861 return -ENOSPC;
1862 }
1863 /*
1864 * OK, move now. First in and then out.
1865 */
1866
1867 copy_from_user(audio_devs[gus_devnum]->dmap_out->raw_buf, &(addr)[sizeof_patch + src_offs], blk_sz);
1868
1869 save_flags(flags);
1870 cli();
1871 /******** INTERRUPTS DISABLED NOW ********/
1872 gus_write8(0x41, 0); /* Disable GF1 DMA */
1873 DMAbuf_start_dma(gus_devnum, audio_devs[gus_devnum]->dmap_out->raw_buf_phys,
1874 blk_sz, DMA_MODE_WRITE);
1875
1876 /*
1877 * Set the DRAM address for the wave data
1878 */
1879
1880 if (iw_mode)
1881 {
1882 /* Different address translation in enhanced mode */
1883
1884 unsigned char hi;
1885
1886 if (gus_dma > 4)
1887 address = target >> 1; /* Convert to 16 bit word address */
1888 else
1889 address = target;
1890
1891 hi = (unsigned char) ((address >> 16) & 0xf0);
1892 hi += (unsigned char) (address & 0x0f);
1893
1894 gus_write16(0x42, (address >> 4) & 0xffff); /* DMA address (low) */
1895 gus_write8(0x50, hi);
1896 }
1897 else
1898 {
1899 address = target;
1900 if (audio_devs[gus_devnum]->dmap_out->dma > 3)
1901 {
1902 hold_address = address;
1903 address = address >> 1;
1904 address &= 0x0001ffffL;
1905 address |= (hold_address & 0x000c0000L);
1906 }
1907 gus_write16(0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
1908 }
1909
1910 /*
1911 * Start the DMA transfer
1912 */
1913
1914 dma_command = 0x21; /* IRQ enable, DMA start */
1915 if (patch.mode & WAVE_UNSIGNED)
1916 dma_command |= 0x80; /* Invert MSB */
1917 if (patch.mode & WAVE_16_BITS)
1918 dma_command |= 0x40; /* 16 bit _DATA_ */
1919 if (audio_devs[gus_devnum]->dmap_out->dma > 3)
1920 dma_command |= 0x04; /* 16 bit DMA _channel_ */
1921
1922 gus_write8(0x41, dma_command); /* Lets go luteet (=bugs) */
1923
1924 /*
1925 * Sleep here until the DRAM DMA done interrupt is served
1926 */
1927 active_device = GUS_DEV_WAVE;
1928
1929 if (!interruptible_sleep_on_timeout(&dram_sleeper, HZ))
1930 printk("GUS: DMA Transfer timed out\n");
1931 restore_flags(flags);
1932 }
1933
1934 /*
1935 * Now the next part
1936 */
1937
1938 left -= blk_sz;
1939 src_offs += blk_sz;
1940 target += blk_sz;
1941
1942 gus_write8(0x41, 0); /* Stop DMA */
1943 }
1944
1945 free_mem_ptr += patch.len;
1946 free_sample++;
1947 return 0;
1948 }
1949
guswave_hw_control(int dev,unsigned char * event_rec)1950 static void guswave_hw_control(int dev, unsigned char *event_rec)
1951 {
1952 int voice, cmd;
1953 unsigned short p1, p2;
1954 unsigned int plong;
1955 unsigned long flags;
1956
1957 cmd = event_rec[2];
1958 voice = event_rec[3];
1959 p1 = *(unsigned short *) &event_rec[4];
1960 p2 = *(unsigned short *) &event_rec[6];
1961 plong = *(unsigned int *) &event_rec[4];
1962
1963 if ((voices[voice].volume_irq_mode == VMODE_START_NOTE) &&
1964 (cmd != _GUS_VOICESAMPLE) && (cmd != _GUS_VOICE_POS))
1965 do_volume_irq(voice);
1966
1967 switch (cmd)
1968 {
1969 case _GUS_NUMVOICES:
1970 save_flags(flags);
1971 cli();
1972 gus_select_voice(voice);
1973 gus_select_max_voices(p1);
1974 restore_flags(flags);
1975 break;
1976
1977 case _GUS_VOICESAMPLE:
1978 guswave_set_instr(dev, voice, p1);
1979 break;
1980
1981 case _GUS_VOICEON:
1982 save_flags(flags);
1983 cli();
1984 gus_select_voice(voice);
1985 p1 &= ~0x20; /* Don't allow interrupts */
1986 gus_voice_on(p1);
1987 restore_flags(flags);
1988 break;
1989
1990 case _GUS_VOICEOFF:
1991 save_flags(flags);
1992 cli();
1993 gus_select_voice(voice);
1994 gus_voice_off();
1995 restore_flags(flags);
1996 break;
1997
1998 case _GUS_VOICEFADE:
1999 gus_voice_fade(voice);
2000 break;
2001
2002 case _GUS_VOICEMODE:
2003 save_flags(flags);
2004 cli();
2005 gus_select_voice(voice);
2006 p1 &= ~0x20; /* Don't allow interrupts */
2007 gus_voice_mode(p1);
2008 restore_flags(flags);
2009 break;
2010
2011 case _GUS_VOICEBALA:
2012 save_flags(flags);
2013 cli();
2014 gus_select_voice(voice);
2015 gus_voice_balance(p1);
2016 restore_flags(flags);
2017 break;
2018
2019 case _GUS_VOICEFREQ:
2020 save_flags(flags);
2021 cli();
2022 gus_select_voice(voice);
2023 gus_voice_freq(plong);
2024 restore_flags(flags);
2025 break;
2026
2027 case _GUS_VOICEVOL:
2028 save_flags(flags);
2029 cli();
2030 gus_select_voice(voice);
2031 gus_voice_volume(p1);
2032 restore_flags(flags);
2033 break;
2034
2035 case _GUS_VOICEVOL2: /* Just update the software voice level */
2036 voices[voice].initial_volume = voices[voice].current_volume = p1;
2037 break;
2038
2039 case _GUS_RAMPRANGE:
2040 if (voices[voice].mode & WAVE_ENVELOPES)
2041 break; /* NO-NO */
2042 save_flags(flags);
2043 cli();
2044 gus_select_voice(voice);
2045 gus_ramp_range(p1, p2);
2046 restore_flags(flags);
2047 break;
2048
2049 case _GUS_RAMPRATE:
2050 if (voices[voice].mode & WAVE_ENVELOPES)
2051 break; /* NJET-NJET */
2052 save_flags(flags);
2053 cli();
2054 gus_select_voice(voice);
2055 gus_ramp_rate(p1, p2);
2056 restore_flags(flags);
2057 break;
2058
2059 case _GUS_RAMPMODE:
2060 if (voices[voice].mode & WAVE_ENVELOPES)
2061 break; /* NO-NO */
2062 save_flags(flags);
2063 cli();
2064 gus_select_voice(voice);
2065 p1 &= ~0x20; /* Don't allow interrupts */
2066 gus_ramp_mode(p1);
2067 restore_flags(flags);
2068 break;
2069
2070 case _GUS_RAMPON:
2071 if (voices[voice].mode & WAVE_ENVELOPES)
2072 break; /* EI-EI */
2073 save_flags(flags);
2074 cli();
2075 gus_select_voice(voice);
2076 p1 &= ~0x20; /* Don't allow interrupts */
2077 gus_rampon(p1);
2078 restore_flags(flags);
2079 break;
2080
2081 case _GUS_RAMPOFF:
2082 if (voices[voice].mode & WAVE_ENVELOPES)
2083 break; /* NEJ-NEJ */
2084 save_flags(flags);
2085 cli();
2086 gus_select_voice(voice);
2087 gus_rampoff();
2088 restore_flags(flags);
2089 break;
2090
2091 case _GUS_VOLUME_SCALE:
2092 volume_base = p1;
2093 volume_scale = p2;
2094 break;
2095
2096 case _GUS_VOICE_POS:
2097 save_flags(flags);
2098 cli();
2099 gus_select_voice(voice);
2100 gus_set_voice_pos(voice, plong);
2101 restore_flags(flags);
2102 break;
2103
2104 default:
2105 break;
2106 }
2107 }
2108
gus_audio_set_speed(int speed)2109 static int gus_audio_set_speed(int speed)
2110 {
2111 if (speed <= 0)
2112 speed = gus_audio_speed;
2113
2114 if (speed < 4000)
2115 speed = 4000;
2116
2117 if (speed > 44100)
2118 speed = 44100;
2119
2120 gus_audio_speed = speed;
2121
2122 if (only_read_access)
2123 {
2124 /* Compute nearest valid recording speed and return it */
2125
2126 /* speed = (9878400 / (gus_audio_speed + 2)) / 16; */
2127 speed = (((9878400 + gus_audio_speed / 2) / (gus_audio_speed + 2)) + 8) / 16;
2128 speed = (9878400 / (speed * 16)) - 2;
2129 }
2130 return speed;
2131 }
2132
gus_audio_set_channels(int channels)2133 static int gus_audio_set_channels(int channels)
2134 {
2135 if (!channels)
2136 return gus_audio_channels;
2137 if (channels > 2)
2138 channels = 2;
2139 if (channels < 1)
2140 channels = 1;
2141 gus_audio_channels = channels;
2142 return channels;
2143 }
2144
gus_audio_set_bits(int bits)2145 static int gus_audio_set_bits(int bits)
2146 {
2147 if (!bits)
2148 return gus_audio_bits;
2149
2150 if (bits != 8 && bits != 16)
2151 bits = 8;
2152
2153 if (only_8_bits)
2154 bits = 8;
2155
2156 gus_audio_bits = bits;
2157 return bits;
2158 }
2159
gus_audio_ioctl(int dev,unsigned int cmd,caddr_t arg)2160 static int gus_audio_ioctl(int dev, unsigned int cmd, caddr_t arg)
2161 {
2162 int val;
2163
2164 switch (cmd)
2165 {
2166 case SOUND_PCM_WRITE_RATE:
2167 if (get_user(val, (int *)arg))
2168 return -EFAULT;
2169 val = gus_audio_set_speed(val);
2170 break;
2171
2172 case SOUND_PCM_READ_RATE:
2173 val = gus_audio_speed;
2174 break;
2175
2176 case SNDCTL_DSP_STEREO:
2177 if (get_user(val, (int *)arg))
2178 return -EFAULT;
2179 val = gus_audio_set_channels(val + 1) - 1;
2180 break;
2181
2182 case SOUND_PCM_WRITE_CHANNELS:
2183 if (get_user(val, (int *)arg))
2184 return -EFAULT;
2185 val = gus_audio_set_channels(val);
2186 break;
2187
2188 case SOUND_PCM_READ_CHANNELS:
2189 val = gus_audio_channels;
2190 break;
2191
2192 case SNDCTL_DSP_SETFMT:
2193 if (get_user(val, (int *)arg))
2194 return -EFAULT;
2195 val = gus_audio_set_bits(val);
2196 break;
2197
2198 case SOUND_PCM_READ_BITS:
2199 val = gus_audio_bits;
2200 break;
2201
2202 case SOUND_PCM_WRITE_FILTER: /* NOT POSSIBLE */
2203 case SOUND_PCM_READ_FILTER:
2204 val = -EINVAL;
2205 break;
2206 default:
2207 return -EINVAL;
2208 }
2209 return put_user(val, (int *)arg);
2210 }
2211
gus_audio_reset(int dev)2212 static void gus_audio_reset(int dev)
2213 {
2214 if (recording_active)
2215 {
2216 gus_write8(0x49, 0x00); /* Halt recording */
2217 set_input_volumes();
2218 }
2219 }
2220
2221 static int saved_iw_mode; /* A hack hack hack */
2222
gus_audio_open(int dev,int mode)2223 static int gus_audio_open(int dev, int mode)
2224 {
2225 if (gus_busy)
2226 return -EBUSY;
2227
2228 if (gus_pnp_flag && mode & OPEN_READ)
2229 {
2230 /* printk(KERN_ERR "GUS: Audio device #%d is playback only.\n", dev);*/
2231 return -EIO;
2232 }
2233 gus_initialize();
2234
2235 gus_busy = 1;
2236 active_device = 0;
2237
2238 saved_iw_mode = iw_mode;
2239 if (iw_mode)
2240 {
2241 /* There are some problems with audio in enhanced mode so disable it */
2242 gus_write8(0x19, gus_read8(0x19) & ~0x01); /* Disable enhanced mode */
2243 iw_mode = 0;
2244 }
2245
2246 gus_reset();
2247 reset_sample_memory();
2248 gus_select_max_voices(14);
2249
2250 pcm_active = 0;
2251 dma_active = 0;
2252 pcm_opened = 1;
2253 if (mode & OPEN_READ)
2254 {
2255 recording_active = 1;
2256 set_input_volumes();
2257 }
2258 only_read_access = !(mode & OPEN_WRITE);
2259 only_8_bits = mode & OPEN_READ;
2260 if (only_8_bits)
2261 audio_devs[dev]->format_mask = AFMT_U8;
2262 else
2263 audio_devs[dev]->format_mask = AFMT_U8 | AFMT_S16_LE;
2264
2265 return 0;
2266 }
2267
gus_audio_close(int dev)2268 static void gus_audio_close(int dev)
2269 {
2270 iw_mode = saved_iw_mode;
2271 gus_reset();
2272 gus_busy = 0;
2273 pcm_opened = 0;
2274 active_device = 0;
2275
2276 if (recording_active)
2277 {
2278 gus_write8(0x49, 0x00); /* Halt recording */
2279 set_input_volumes();
2280 }
2281 recording_active = 0;
2282 }
2283
gus_audio_update_volume(void)2284 static void gus_audio_update_volume(void)
2285 {
2286 unsigned long flags;
2287 int voice;
2288
2289 if (pcm_active && pcm_opened)
2290 for (voice = 0; voice < gus_audio_channels; voice++)
2291 {
2292 save_flags(flags);
2293 cli();
2294 gus_select_voice(voice);
2295 gus_rampoff();
2296 gus_voice_volume(1530 + (25 * gus_pcm_volume));
2297 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume));
2298 restore_flags(flags);
2299 }
2300 }
2301
play_next_pcm_block(void)2302 static void play_next_pcm_block(void)
2303 {
2304 unsigned long flags;
2305 int speed = gus_audio_speed;
2306 int this_one, is16bits, chn;
2307 unsigned long dram_loc;
2308 unsigned char mode[2], ramp_mode[2];
2309
2310 if (!pcm_qlen)
2311 return;
2312
2313 this_one = pcm_head;
2314
2315 for (chn = 0; chn < gus_audio_channels; chn++)
2316 {
2317 mode[chn] = 0x00;
2318 ramp_mode[chn] = 0x03; /* Ramping and rollover off */
2319
2320 if (chn == 0)
2321 {
2322 mode[chn] |= 0x20; /* Loop IRQ */
2323 voices[chn].loop_irq_mode = LMODE_PCM;
2324 }
2325 if (gus_audio_bits != 8)
2326 {
2327 is16bits = 1;
2328 mode[chn] |= 0x04; /* 16 bit data */
2329 }
2330 else
2331 is16bits = 0;
2332
2333 dram_loc = this_one * pcm_bsize;
2334 dram_loc += chn * pcm_banksize;
2335
2336 if (this_one == (pcm_nblk - 1)) /* Last fragment of the DRAM buffer */
2337 {
2338 mode[chn] |= 0x08; /* Enable loop */
2339 ramp_mode[chn] = 0x03; /* Disable rollover bit */
2340 }
2341 else
2342 {
2343 if (chn == 0)
2344 ramp_mode[chn] = 0x04; /* Enable rollover bit */
2345 }
2346 save_flags(flags);
2347 cli();
2348 gus_select_voice(chn);
2349 gus_voice_freq(speed);
2350
2351 if (gus_audio_channels == 1)
2352 gus_voice_balance(7); /* mono */
2353 else if (chn == 0)
2354 gus_voice_balance(0); /* left */
2355 else
2356 gus_voice_balance(15); /* right */
2357
2358 if (!pcm_active) /* Playback not already active */
2359 {
2360 /*
2361 * The playback was not started yet (or there has been a pause).
2362 * Start the voice (again) and ask for a rollover irq at the end of
2363 * this_one block. If this_one one is last of the buffers, use just
2364 * the normal loop with irq.
2365 */
2366
2367 gus_voice_off();
2368 gus_rampoff();
2369 gus_voice_volume(1530 + (25 * gus_pcm_volume));
2370 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume));
2371
2372 gus_write_addr(0x0a, chn * pcm_banksize, 0, is16bits); /* Starting position */
2373 gus_write_addr(0x02, chn * pcm_banksize, 0, is16bits); /* Loop start */
2374
2375 if (chn != 0)
2376 gus_write_addr(0x04, pcm_banksize + (pcm_bsize * pcm_nblk) - 1,
2377 0, is16bits); /* Loop end location */
2378 }
2379 if (chn == 0)
2380 gus_write_addr(0x04, dram_loc + pcm_bsize - 1,
2381 0, is16bits); /* Loop end location */
2382 else
2383 mode[chn] |= 0x08; /* Enable looping */
2384 restore_flags(flags);
2385 }
2386 for (chn = 0; chn < gus_audio_channels; chn++)
2387 {
2388 save_flags(flags);
2389 cli();
2390 gus_select_voice(chn);
2391 gus_write8(0x0d, ramp_mode[chn]);
2392 if (iw_mode)
2393 gus_write8(0x15, 0x00); /* Reset voice deactivate bit of SMSI */
2394 gus_voice_on(mode[chn]);
2395 restore_flags(flags);
2396 }
2397 pcm_active = 1;
2398 }
2399
gus_transfer_output_block(int dev,unsigned long buf,int total_count,int intrflag,int chn)2400 static void gus_transfer_output_block(int dev, unsigned long buf,
2401 int total_count, int intrflag, int chn)
2402 {
2403 /*
2404 * This routine transfers one block of audio data to the DRAM. In mono mode
2405 * it's called just once. When in stereo mode, this_one routine is called
2406 * once for both channels.
2407 *
2408 * The left/mono channel data is transferred to the beginning of dram and the
2409 * right data to the area pointed by gus_page_size.
2410 */
2411
2412 int this_one, count;
2413 unsigned long flags;
2414 unsigned char dma_command;
2415 unsigned long address, hold_address;
2416
2417 save_flags(flags);
2418 cli();
2419
2420 count = total_count / gus_audio_channels;
2421
2422 if (chn == 0)
2423 {
2424 if (pcm_qlen >= pcm_nblk)
2425 printk(KERN_WARNING "GUS Warning: PCM buffers out of sync\n");
2426
2427 this_one = pcm_current_block = pcm_tail;
2428 pcm_qlen++;
2429 pcm_tail = (pcm_tail + 1) % pcm_nblk;
2430 pcm_datasize[this_one] = count;
2431 }
2432 else
2433 this_one = pcm_current_block;
2434
2435 gus_write8(0x41, 0); /* Disable GF1 DMA */
2436 DMAbuf_start_dma(dev, buf + (chn * count), count, DMA_MODE_WRITE);
2437
2438 address = this_one * pcm_bsize;
2439 address += chn * pcm_banksize;
2440
2441 if (audio_devs[dev]->dmap_out->dma > 3)
2442 {
2443 hold_address = address;
2444 address = address >> 1;
2445 address &= 0x0001ffffL;
2446 address |= (hold_address & 0x000c0000L);
2447 }
2448 gus_write16(0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
2449
2450 dma_command = 0x21; /* IRQ enable, DMA start */
2451
2452 if (gus_audio_bits != 8)
2453 dma_command |= 0x40; /* 16 bit _DATA_ */
2454 else
2455 dma_command |= 0x80; /* Invert MSB */
2456
2457 if (audio_devs[dev]->dmap_out->dma > 3)
2458 dma_command |= 0x04; /* 16 bit DMA channel */
2459
2460 gus_write8(0x41, dma_command); /* Kick start */
2461
2462 if (chn == (gus_audio_channels - 1)) /* Last channel */
2463 {
2464 /*
2465 * Last (right or mono) channel data
2466 */
2467 dma_active = 1; /* DMA started. There is a unacknowledged buffer */
2468 active_device = GUS_DEV_PCM_DONE;
2469 if (!pcm_active && (pcm_qlen > 1 || count < pcm_bsize))
2470 {
2471 play_next_pcm_block();
2472 }
2473 }
2474 else
2475 {
2476 /*
2477 * Left channel data. The right channel
2478 * is transferred after DMA interrupt
2479 */
2480 active_device = GUS_DEV_PCM_CONTINUE;
2481 }
2482
2483 restore_flags(flags);
2484 }
2485
gus_uninterleave8(char * buf,int l)2486 static void gus_uninterleave8(char *buf, int l)
2487 {
2488 /* This routine uninterleaves 8 bit stereo output (LRLRLR->LLLRRR) */
2489 int i, p = 0, halfsize = l / 2;
2490 char *buf2 = buf + halfsize, *src = bounce_buf;
2491
2492 memcpy(bounce_buf, buf, l);
2493
2494 for (i = 0; i < halfsize; i++)
2495 {
2496 buf[i] = src[p++]; /* Left channel */
2497 buf2[i] = src[p++]; /* Right channel */
2498 }
2499 }
2500
gus_uninterleave16(short * buf,int l)2501 static void gus_uninterleave16(short *buf, int l)
2502 {
2503 /* This routine uninterleaves 16 bit stereo output (LRLRLR->LLLRRR) */
2504 int i, p = 0, halfsize = l / 2;
2505 short *buf2 = buf + halfsize, *src = (short *) bounce_buf;
2506
2507 memcpy(bounce_buf, (char *) buf, l * 2);
2508
2509 for (i = 0; i < halfsize; i++)
2510 {
2511 buf[i] = src[p++]; /* Left channel */
2512 buf2[i] = src[p++]; /* Right channel */
2513 }
2514 }
2515
gus_audio_output_block(int dev,unsigned long buf,int total_count,int intrflag)2516 static void gus_audio_output_block(int dev, unsigned long buf, int total_count,
2517 int intrflag)
2518 {
2519 struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
2520
2521 dmap->flags |= DMA_NODMA | DMA_NOTIMEOUT;
2522
2523 pcm_current_buf = buf;
2524 pcm_current_count = total_count;
2525 pcm_current_intrflag = intrflag;
2526 pcm_current_dev = dev;
2527 if (gus_audio_channels == 2)
2528 {
2529 char *b = dmap->raw_buf + (buf - dmap->raw_buf_phys);
2530
2531 if (gus_audio_bits == 8)
2532 gus_uninterleave8(b, total_count);
2533 else
2534 gus_uninterleave16((short *) b, total_count / 2);
2535 }
2536 gus_transfer_output_block(dev, buf, total_count, intrflag, 0);
2537 }
2538
gus_audio_start_input(int dev,unsigned long buf,int count,int intrflag)2539 static void gus_audio_start_input(int dev, unsigned long buf, int count,
2540 int intrflag)
2541 {
2542 unsigned long flags;
2543 unsigned char mode;
2544
2545 save_flags(flags);
2546 cli();
2547
2548 DMAbuf_start_dma(dev, buf, count, DMA_MODE_READ);
2549 mode = 0xa0; /* DMA IRQ enabled, invert MSB */
2550
2551 if (audio_devs[dev]->dmap_in->dma > 3)
2552 mode |= 0x04; /* 16 bit DMA channel */
2553 if (gus_audio_channels > 1)
2554 mode |= 0x02; /* Stereo */
2555 mode |= 0x01; /* DMA enable */
2556
2557 gus_write8(0x49, mode);
2558 restore_flags(flags);
2559 }
2560
gus_audio_prepare_for_input(int dev,int bsize,int bcount)2561 static int gus_audio_prepare_for_input(int dev, int bsize, int bcount)
2562 {
2563 unsigned int rate;
2564
2565 gus_audio_bsize = bsize;
2566 audio_devs[dev]->dmap_in->flags |= DMA_NODMA;
2567 rate = (((9878400 + gus_audio_speed / 2) / (gus_audio_speed + 2)) + 8) / 16;
2568
2569 gus_write8(0x48, rate & 0xff); /* Set sampling rate */
2570
2571 if (gus_audio_bits != 8)
2572 {
2573 /* printk("GUS Error: 16 bit recording not supported\n");*/
2574 return -EINVAL;
2575 }
2576 return 0;
2577 }
2578
gus_audio_prepare_for_output(int dev,int bsize,int bcount)2579 static int gus_audio_prepare_for_output(int dev, int bsize, int bcount)
2580 {
2581 int i;
2582
2583 long mem_ptr, mem_size;
2584
2585 audio_devs[dev]->dmap_out->flags |= DMA_NODMA | DMA_NOTIMEOUT;
2586 mem_ptr = 0;
2587 mem_size = gus_mem_size / gus_audio_channels;
2588
2589 if (mem_size > (256 * 1024))
2590 mem_size = 256 * 1024;
2591
2592 pcm_bsize = bsize / gus_audio_channels;
2593 pcm_head = pcm_tail = pcm_qlen = 0;
2594
2595 pcm_nblk = 2; /* MAX_PCM_BUFFERS; */
2596 if ((pcm_bsize * pcm_nblk) > mem_size)
2597 pcm_nblk = mem_size / pcm_bsize;
2598
2599 for (i = 0; i < pcm_nblk; i++)
2600 pcm_datasize[i] = 0;
2601
2602 pcm_banksize = pcm_nblk * pcm_bsize;
2603
2604 if (gus_audio_bits != 8 && pcm_banksize == (256 * 1024))
2605 pcm_nblk--;
2606 gus_write8(0x41, 0); /* Disable GF1 DMA */
2607 return 0;
2608 }
2609
gus_local_qlen(int dev)2610 static int gus_local_qlen(int dev)
2611 {
2612 return pcm_qlen;
2613 }
2614
2615
2616 static struct audio_driver gus_audio_driver =
2617 {
2618 owner: THIS_MODULE,
2619 open: gus_audio_open,
2620 close: gus_audio_close,
2621 output_block: gus_audio_output_block,
2622 start_input: gus_audio_start_input,
2623 ioctl: gus_audio_ioctl,
2624 prepare_for_input: gus_audio_prepare_for_input,
2625 prepare_for_output: gus_audio_prepare_for_output,
2626 halt_io: gus_audio_reset,
2627 local_qlen: gus_local_qlen,
2628 };
2629
guswave_setup_voice(int dev,int voice,int chn)2630 static void guswave_setup_voice(int dev, int voice, int chn)
2631 {
2632 struct channel_info *info = &synth_devs[dev]->chn_info[chn];
2633
2634 guswave_set_instr(dev, voice, info->pgm_num);
2635 voices[voice].expression_vol = info->controllers[CTL_EXPRESSION]; /* Just MSB */
2636 voices[voice].main_vol = (info->controllers[CTL_MAIN_VOLUME] * 100) / (unsigned) 128;
2637 voices[voice].panning = (info->controllers[CTL_PAN] * 2) - 128;
2638 voices[voice].bender = 0;
2639 voices[voice].bender_range = info->bender_range;
2640
2641 if (chn == 9)
2642 voices[voice].fixed_pitch = 1;
2643 }
2644
guswave_bender(int dev,int voice,int value)2645 static void guswave_bender(int dev, int voice, int value)
2646 {
2647 int freq;
2648 unsigned long flags;
2649
2650 voices[voice].bender = value - 8192;
2651 freq = compute_finetune(voices[voice].orig_freq, value - 8192, voices[voice].bender_range, 0);
2652 voices[voice].current_freq = freq;
2653
2654 save_flags(flags);
2655 cli();
2656 gus_select_voice(voice);
2657 gus_voice_freq(freq);
2658 restore_flags(flags);
2659 }
2660
guswave_alloc(int dev,int chn,int note,struct voice_alloc_info * alloc)2661 static int guswave_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc)
2662 {
2663 int i, p, best = -1, best_time = 0x7fffffff;
2664
2665 p = alloc->ptr;
2666 /*
2667 * First look for a completely stopped voice
2668 */
2669
2670 for (i = 0; i < alloc->max_voice; i++)
2671 {
2672 if (alloc->map[p] == 0)
2673 {
2674 alloc->ptr = p;
2675 return p;
2676 }
2677 if (alloc->alloc_times[p] < best_time)
2678 {
2679 best = p;
2680 best_time = alloc->alloc_times[p];
2681 }
2682 p = (p + 1) % alloc->max_voice;
2683 }
2684
2685 /*
2686 * Then look for a releasing voice
2687 */
2688
2689 for (i = 0; i < alloc->max_voice; i++)
2690 {
2691 if (alloc->map[p] == 0xffff)
2692 {
2693 alloc->ptr = p;
2694 return p;
2695 }
2696 p = (p + 1) % alloc->max_voice;
2697 }
2698 if (best >= 0)
2699 p = best;
2700
2701 alloc->ptr = p;
2702 return p;
2703 }
2704
2705 static struct synth_operations guswave_operations =
2706 {
2707 owner: THIS_MODULE,
2708 id: "GUS",
2709 info: &gus_info,
2710 midi_dev: 0,
2711 synth_type: SYNTH_TYPE_SAMPLE,
2712 synth_subtype: SAMPLE_TYPE_GUS,
2713 open: guswave_open,
2714 close: guswave_close,
2715 ioctl: guswave_ioctl,
2716 kill_note: guswave_kill_note,
2717 start_note: guswave_start_note,
2718 set_instr: guswave_set_instr,
2719 reset: guswave_reset,
2720 hw_control: guswave_hw_control,
2721 load_patch: guswave_load_patch,
2722 aftertouch: guswave_aftertouch,
2723 controller: guswave_controller,
2724 panning: guswave_panning,
2725 volume_method: guswave_volume_method,
2726 bender: guswave_bender,
2727 alloc_voice: guswave_alloc,
2728 setup_voice: guswave_setup_voice
2729 };
2730
set_input_volumes(void)2731 static void set_input_volumes(void)
2732 {
2733 unsigned long flags;
2734 unsigned char mask = 0xff & ~0x06; /* Just line out enabled */
2735
2736 if (have_gus_max) /* Don't disturb GUS MAX */
2737 return;
2738
2739 save_flags(flags);
2740 cli();
2741
2742 /*
2743 * Enable channels having vol > 10%
2744 * Note! bit 0x01 means the line in DISABLED while 0x04 means
2745 * the mic in ENABLED.
2746 */
2747 if (gus_line_vol > 10)
2748 mask &= ~0x01;
2749 if (gus_mic_vol > 10)
2750 mask |= 0x04;
2751
2752 if (recording_active)
2753 {
2754 /*
2755 * Disable channel, if not selected for recording
2756 */
2757 if (!(gus_recmask & SOUND_MASK_LINE))
2758 mask |= 0x01;
2759 if (!(gus_recmask & SOUND_MASK_MIC))
2760 mask &= ~0x04;
2761 }
2762 mix_image &= ~0x07;
2763 mix_image |= mask & 0x07;
2764 outb((mix_image), u_Mixer);
2765
2766 restore_flags(flags);
2767 }
2768
2769 #define MIX_DEVS (SOUND_MASK_MIC|SOUND_MASK_LINE| \
2770 SOUND_MASK_SYNTH|SOUND_MASK_PCM)
2771
gus_default_mixer_ioctl(int dev,unsigned int cmd,caddr_t arg)2772 int gus_default_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg)
2773 {
2774 int vol, val;
2775
2776 if (((cmd >> 8) & 0xff) != 'M')
2777 return -EINVAL;
2778
2779 if (!access_ok(VERIFY_WRITE, (int *)arg, sizeof(int)))
2780 return -EFAULT;
2781
2782 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
2783 {
2784 if (__get_user(val, (int *) arg))
2785 return -EFAULT;
2786
2787 switch (cmd & 0xff)
2788 {
2789 case SOUND_MIXER_RECSRC:
2790 gus_recmask = val & MIX_DEVS;
2791 if (!(gus_recmask & (SOUND_MASK_MIC | SOUND_MASK_LINE)))
2792 gus_recmask = SOUND_MASK_MIC;
2793 /* Note! Input volumes are updated during next open for recording */
2794 val = gus_recmask;
2795 break;
2796
2797 case SOUND_MIXER_MIC:
2798 vol = val & 0xff;
2799 if (vol < 0)
2800 vol = 0;
2801 if (vol > 100)
2802 vol = 100;
2803 gus_mic_vol = vol;
2804 set_input_volumes();
2805 val = vol | (vol << 8);
2806 break;
2807
2808 case SOUND_MIXER_LINE:
2809 vol = val & 0xff;
2810 if (vol < 0)
2811 vol = 0;
2812 if (vol > 100)
2813 vol = 100;
2814 gus_line_vol = vol;
2815 set_input_volumes();
2816 val = vol | (vol << 8);
2817 break;
2818
2819 case SOUND_MIXER_PCM:
2820 gus_pcm_volume = val & 0xff;
2821 if (gus_pcm_volume < 0)
2822 gus_pcm_volume = 0;
2823 if (gus_pcm_volume > 100)
2824 gus_pcm_volume = 100;
2825 gus_audio_update_volume();
2826 val = gus_pcm_volume | (gus_pcm_volume << 8);
2827 break;
2828
2829 case SOUND_MIXER_SYNTH:
2830 gus_wave_volume = val & 0xff;
2831 if (gus_wave_volume < 0)
2832 gus_wave_volume = 0;
2833 if (gus_wave_volume > 100)
2834 gus_wave_volume = 100;
2835 if (active_device == GUS_DEV_WAVE)
2836 {
2837 int voice;
2838 for (voice = 0; voice < nr_voices; voice++)
2839 dynamic_volume_change(voice); /* Apply the new vol */
2840 }
2841 val = gus_wave_volume | (gus_wave_volume << 8);
2842 break;
2843
2844 default:
2845 return -EINVAL;
2846 }
2847 }
2848 else
2849 {
2850 switch (cmd & 0xff)
2851 {
2852 /*
2853 * Return parameters
2854 */
2855 case SOUND_MIXER_RECSRC:
2856 val = gus_recmask;
2857 break;
2858
2859 case SOUND_MIXER_DEVMASK:
2860 val = MIX_DEVS;
2861 break;
2862
2863 case SOUND_MIXER_STEREODEVS:
2864 val = 0;
2865 break;
2866
2867 case SOUND_MIXER_RECMASK:
2868 val = SOUND_MASK_MIC | SOUND_MASK_LINE;
2869 break;
2870
2871 case SOUND_MIXER_CAPS:
2872 val = 0;
2873 break;
2874
2875 case SOUND_MIXER_MIC:
2876 val = gus_mic_vol | (gus_mic_vol << 8);
2877 break;
2878
2879 case SOUND_MIXER_LINE:
2880 val = gus_line_vol | (gus_line_vol << 8);
2881 break;
2882
2883 case SOUND_MIXER_PCM:
2884 val = gus_pcm_volume | (gus_pcm_volume << 8);
2885 break;
2886
2887 case SOUND_MIXER_SYNTH:
2888 val = gus_wave_volume | (gus_wave_volume << 8);
2889 break;
2890
2891 default:
2892 return -EINVAL;
2893 }
2894 }
2895 return __put_user(val, (int *)arg);
2896 }
2897
2898 static struct mixer_operations gus_mixer_operations =
2899 {
2900 owner: THIS_MODULE,
2901 id: "GUS",
2902 name: "Gravis Ultrasound",
2903 ioctl: gus_default_mixer_ioctl
2904 };
2905
gus_default_mixer_init(void)2906 static int __init gus_default_mixer_init(void)
2907 {
2908 int n;
2909
2910 if ((n = sound_alloc_mixerdev()) != -1)
2911 {
2912 /*
2913 * Don't install if there is another
2914 * mixer
2915 */
2916 mixer_devs[n] = &gus_mixer_operations;
2917 }
2918 if (have_gus_max)
2919 {
2920 /*
2921 * Enable all mixer channels on the GF1 side. Otherwise recording will
2922 * not be possible using GUS MAX.
2923 */
2924 mix_image &= ~0x07;
2925 mix_image |= 0x04; /* All channels enabled */
2926 outb((mix_image), u_Mixer);
2927 }
2928 return n;
2929 }
2930
gus_wave_init(struct address_info * hw_config)2931 void __init gus_wave_init(struct address_info *hw_config)
2932 {
2933 unsigned long flags;
2934 unsigned char val;
2935 char *model_num = "2.4";
2936 char tmp[64], tmp2[64];
2937 int gus_type = 0x24; /* 2.4 */
2938
2939 int irq = hw_config->irq, dma = hw_config->dma, dma2 = hw_config->dma2;
2940 int sdev;
2941
2942 hw_config->slots[0] = -1; /* No wave */
2943 hw_config->slots[1] = -1; /* No ad1848 */
2944 hw_config->slots[4] = -1; /* No audio */
2945 hw_config->slots[5] = -1; /* No mixer */
2946
2947 if (!gus_pnp_flag)
2948 {
2949 if (irq < 0 || irq > 15)
2950 {
2951 printk(KERN_ERR "ERROR! Invalid IRQ#%d. GUS Disabled", irq);
2952 return;
2953 }
2954 }
2955
2956 if (dma < 0 || dma > 7 || dma == 4)
2957 {
2958 printk(KERN_ERR "ERROR! Invalid DMA#%d. GUS Disabled", dma);
2959 return;
2960 }
2961 gus_irq = irq;
2962 gus_dma = dma;
2963 gus_dma2 = dma2;
2964 gus_hw_config = hw_config;
2965
2966 if (gus_dma2 == -1)
2967 gus_dma2 = dma;
2968
2969 /*
2970 * Try to identify the GUS model.
2971 *
2972 * Versions < 3.6 don't have the digital ASIC. Try to probe it first.
2973 */
2974
2975 save_flags(flags);
2976 cli();
2977 outb((0x20), gus_base + 0x0f);
2978 val = inb(gus_base + 0x0f);
2979 restore_flags(flags);
2980
2981 if (gus_pnp_flag || (val != 0xff && (val & 0x06))) /* Should be 0x02?? */
2982 {
2983 int ad_flags = 0;
2984
2985 if (gus_pnp_flag)
2986 ad_flags = 0x12345678; /* Interwave "magic" */
2987 /*
2988 * It has the digital ASIC so the card is at least v3.4.
2989 * Next try to detect the true model.
2990 */
2991
2992 if (gus_pnp_flag) /* Hack hack hack */
2993 val = 10;
2994 else
2995 val = inb(u_MixSelect);
2996
2997 /*
2998 * Value 255 means pre-3.7 which don't have mixer.
2999 * Values 5 thru 9 mean v3.7 which has a ICS2101 mixer.
3000 * 10 and above is GUS MAX which has the CS4231 codec/mixer.
3001 *
3002 */
3003
3004 if (val == 255 || val < 5)
3005 {
3006 model_num = "3.4";
3007 gus_type = 0x34;
3008 }
3009 else if (val < 10)
3010 {
3011 model_num = "3.7";
3012 gus_type = 0x37;
3013 mixer_type = ICS2101;
3014 request_region(u_MixSelect, 1, "GUS mixer");
3015 }
3016 else
3017 {
3018 model_num = "MAX";
3019 gus_type = 0x40;
3020 mixer_type = CS4231;
3021 #ifdef CONFIG_SOUND_GUSMAX
3022 {
3023 unsigned char max_config = 0x40; /* Codec enable */
3024
3025 if (gus_dma2 == -1)
3026 gus_dma2 = gus_dma;
3027
3028 if (gus_dma > 3)
3029 max_config |= 0x10; /* 16 bit capture DMA */
3030
3031 if (gus_dma2 > 3)
3032 max_config |= 0x20; /* 16 bit playback DMA */
3033
3034 max_config |= (gus_base >> 4) & 0x0f; /* Extract the X from 2X0 */
3035
3036 outb((max_config), gus_base + 0x106); /* UltraMax control */
3037 }
3038
3039 if (ad1848_detect(gus_base + 0x10c, &ad_flags, hw_config->osp))
3040 {
3041 char *name = "GUS MAX";
3042 int old_num_mixers = num_mixers;
3043
3044 if (gus_pnp_flag)
3045 name = "GUS PnP";
3046
3047 gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
3048 gus_wave_volume = 90;
3049 have_gus_max = 1;
3050 if (hw_config->name)
3051 name = hw_config->name;
3052
3053 hw_config->slots[1] = ad1848_init(name, gus_base + 0x10c,
3054 -irq, gus_dma2, /* Playback DMA */
3055 gus_dma, /* Capture DMA */
3056 1, /* Share DMA channels with GF1 */
3057 hw_config->osp,
3058 THIS_MODULE);
3059
3060 if (num_mixers > old_num_mixers)
3061 {
3062 /* GUS has it's own mixer map */
3063 AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_SYNTH);
3064 AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_CD);
3065 AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE);
3066 }
3067 }
3068 else
3069 printk(KERN_WARNING "GUS: No CS4231 ??");
3070 #else
3071 printk(KERN_ERR "GUS MAX found, but not compiled in\n");
3072 #endif
3073 }
3074 }
3075 else
3076 {
3077 /*
3078 * ASIC not detected so the card must be 2.2 or 2.4.
3079 * There could still be the 16-bit/mixer daughter card.
3080 */
3081 }
3082
3083 if (hw_config->name)
3084 {
3085 strncpy(tmp, hw_config->name, 45);
3086 tmp[45] = 0;
3087 sprintf(tmp2, "%s (%dk)", tmp, (int) gus_mem_size / 1024);
3088 tmp2[sizeof(tmp2) - 1] = 0;
3089 }
3090 else if (gus_pnp_flag)
3091 {
3092 sprintf(tmp2, "Gravis UltraSound PnP (%dk)",
3093 (int) gus_mem_size / 1024);
3094 }
3095 else
3096 sprintf(tmp2, "Gravis UltraSound %s (%dk)", model_num, (int) gus_mem_size / 1024);
3097
3098
3099 samples = (struct patch_info *)vmalloc((MAX_SAMPLE + 1) * sizeof(*samples));
3100 if (samples == NULL)
3101 {
3102 printk(KERN_WARNING "gus_init: Cant allocate memory for instrument tables\n");
3103 return;
3104 }
3105 conf_printf(tmp2, hw_config);
3106 tmp2[sizeof(gus_info.name) - 1] = 0;
3107 strcpy(gus_info.name, tmp2);
3108
3109 if ((sdev = sound_alloc_synthdev()) == -1)
3110 printk(KERN_WARNING "gus_init: Too many synthesizers\n");
3111 else
3112 {
3113 voice_alloc = &guswave_operations.alloc;
3114 if (iw_mode)
3115 guswave_operations.id = "IWAVE";
3116 hw_config->slots[0] = sdev;
3117 synth_devs[sdev] = &guswave_operations;
3118 sequencer_init();
3119 gus_tmr_install(gus_base + 8);
3120 }
3121
3122 reset_sample_memory();
3123
3124 gus_initialize();
3125
3126 if ((gus_mem_size > 0) && !gus_no_wave_dma)
3127 {
3128 hw_config->slots[4] = -1;
3129 if ((gus_devnum = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
3130 "Ultrasound",
3131 &gus_audio_driver,
3132 sizeof(struct audio_driver),
3133 NEEDS_RESTART |
3134 ((!iw_mode && dma2 != dma && dma2 != -1) ?
3135 DMA_DUPLEX : 0),
3136 AFMT_U8 | AFMT_S16_LE,
3137 NULL, dma, dma2)) < 0)
3138 {
3139 return;
3140 }
3141
3142 hw_config->slots[4] = gus_devnum;
3143 audio_devs[gus_devnum]->min_fragment = 9; /* 512k */
3144 audio_devs[gus_devnum]->max_fragment = 11; /* 8k (must match size of bounce_buf */
3145 audio_devs[gus_devnum]->mixer_dev = -1; /* Next mixer# */
3146 audio_devs[gus_devnum]->flags |= DMA_HARDSTOP;
3147 }
3148
3149 /*
3150 * Mixer dependent initialization.
3151 */
3152
3153 switch (mixer_type)
3154 {
3155 case ICS2101:
3156 gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
3157 gus_wave_volume = 90;
3158 request_region(u_MixSelect, 1, "GUS mixer");
3159 hw_config->slots[5] = ics2101_mixer_init();
3160 audio_devs[gus_devnum]->mixer_dev = hw_config->slots[5]; /* Next mixer# */
3161 return;
3162
3163 case CS4231:
3164 /* Initialized elsewhere (ad1848.c) */
3165 default:
3166 hw_config->slots[5] = gus_default_mixer_init();
3167 audio_devs[gus_devnum]->mixer_dev = hw_config->slots[5]; /* Next mixer# */
3168 return;
3169 }
3170 }
3171
gus_wave_unload(struct address_info * hw_config)3172 void __exit gus_wave_unload(struct address_info *hw_config)
3173 {
3174 #ifdef CONFIG_SOUND_GUSMAX
3175 if (have_gus_max)
3176 {
3177 ad1848_unload(gus_base + 0x10c,
3178 -gus_irq,
3179 gus_dma2, /* Playback DMA */
3180 gus_dma, /* Capture DMA */
3181 1); /* Share DMA channels with GF1 */
3182 }
3183 #endif
3184
3185 if (mixer_type == ICS2101)
3186 {
3187 release_region(u_MixSelect, 1);
3188 }
3189 if (hw_config->slots[0] != -1)
3190 sound_unload_synthdev(hw_config->slots[0]);
3191 if (hw_config->slots[1] != -1)
3192 sound_unload_audiodev(hw_config->slots[1]);
3193 if (hw_config->slots[2] != -1)
3194 sound_unload_mididev(hw_config->slots[2]);
3195 if (hw_config->slots[4] != -1)
3196 sound_unload_audiodev(hw_config->slots[4]);
3197 if (hw_config->slots[5] != -1)
3198 sound_unload_mixerdev(hw_config->slots[5]);
3199
3200 if(samples)
3201 vfree(samples);
3202 samples=NULL;
3203 }
3204
do_loop_irq(int voice)3205 static void do_loop_irq(int voice)
3206 {
3207 unsigned char tmp;
3208 int mode, parm;
3209 unsigned long flags;
3210
3211 save_flags(flags);
3212 cli();
3213 gus_select_voice(voice);
3214
3215 tmp = gus_read8(0x00);
3216 tmp &= ~0x20; /*
3217 * Disable wave IRQ for this_one voice
3218 */
3219 gus_write8(0x00, tmp);
3220
3221 if (tmp & 0x03) /* Voice stopped */
3222 voice_alloc->map[voice] = 0;
3223
3224 mode = voices[voice].loop_irq_mode;
3225 voices[voice].loop_irq_mode = 0;
3226 parm = voices[voice].loop_irq_parm;
3227
3228 switch (mode)
3229 {
3230 case LMODE_FINISH: /*
3231 * Final loop finished, shoot volume down
3232 */
3233
3234 if ((int) (gus_read16(0x09) >> 4) < 100) /*
3235 * Get current volume
3236 */
3237 {
3238 gus_voice_off();
3239 gus_rampoff();
3240 gus_voice_init(voice);
3241 break;
3242 }
3243 gus_ramp_range(65, 4065);
3244 gus_ramp_rate(0, 63); /*
3245 * Fastest possible rate
3246 */
3247 gus_rampon(0x20 | 0x40); /*
3248 * Ramp down, once, irq
3249 */
3250 voices[voice].volume_irq_mode = VMODE_HALT;
3251 break;
3252
3253 case LMODE_PCM_STOP:
3254 pcm_active = 0; /* Signal to the play_next_pcm_block routine */
3255 case LMODE_PCM:
3256 {
3257 pcm_qlen--;
3258 pcm_head = (pcm_head + 1) % pcm_nblk;
3259 if (pcm_qlen && pcm_active)
3260 {
3261 play_next_pcm_block();
3262 }
3263 else
3264 {
3265 /* Underrun. Just stop the voice */
3266 gus_select_voice(0); /* Left channel */
3267 gus_voice_off();
3268 gus_rampoff();
3269 gus_select_voice(1); /* Right channel */
3270 gus_voice_off();
3271 gus_rampoff();
3272 pcm_active = 0;
3273 }
3274
3275 /*
3276 * If the queue was full before this interrupt, the DMA transfer was
3277 * suspended. Let it continue now.
3278 */
3279
3280 if (audio_devs[gus_devnum]->dmap_out->qlen > 0)
3281 DMAbuf_outputintr(gus_devnum, 0);
3282 }
3283 break;
3284
3285 default:
3286 break;
3287 }
3288 restore_flags(flags);
3289 }
3290
do_volume_irq(int voice)3291 static void do_volume_irq(int voice)
3292 {
3293 unsigned char tmp;
3294 int mode, parm;
3295 unsigned long flags;
3296
3297 save_flags(flags);
3298 cli();
3299
3300 gus_select_voice(voice);
3301 tmp = gus_read8(0x0d);
3302 tmp &= ~0x20; /*
3303 * Disable volume ramp IRQ
3304 */
3305 gus_write8(0x0d, tmp);
3306
3307 mode = voices[voice].volume_irq_mode;
3308 voices[voice].volume_irq_mode = 0;
3309 parm = voices[voice].volume_irq_parm;
3310
3311 switch (mode)
3312 {
3313 case VMODE_HALT: /* Decay phase finished */
3314 if (iw_mode)
3315 gus_write8(0x15, 0x02); /* Set voice deactivate bit of SMSI */
3316 restore_flags(flags);
3317 gus_voice_init(voice);
3318 break;
3319
3320 case VMODE_ENVELOPE:
3321 gus_rampoff();
3322 restore_flags(flags);
3323 step_envelope(voice);
3324 break;
3325
3326 case VMODE_START_NOTE:
3327 restore_flags(flags);
3328 guswave_start_note2(voices[voice].dev_pending, voice,
3329 voices[voice].note_pending, voices[voice].volume_pending);
3330 if (voices[voice].kill_pending)
3331 guswave_kill_note(voices[voice].dev_pending, voice,
3332 voices[voice].note_pending, 0);
3333
3334 if (voices[voice].sample_pending >= 0)
3335 {
3336 guswave_set_instr(voices[voice].dev_pending, voice,
3337 voices[voice].sample_pending);
3338 voices[voice].sample_pending = -1;
3339 }
3340 break;
3341
3342 default:
3343 restore_flags(flags);
3344 }
3345 restore_flags(flags);
3346 }
3347
gus_voice_irq(void)3348 void gus_voice_irq(void)
3349 {
3350 unsigned long wave_ignore = 0, volume_ignore = 0;
3351 unsigned long voice_bit;
3352
3353 unsigned char src, voice;
3354
3355 while (1)
3356 {
3357 src = gus_read8(0x0f); /*
3358 * Get source info
3359 */
3360 voice = src & 0x1f;
3361 src &= 0xc0;
3362
3363 if (src == (0x80 | 0x40))
3364 return; /*
3365 * No interrupt
3366 */
3367
3368 voice_bit = 1 << voice;
3369
3370 if (!(src & 0x80)) /*
3371 * Wave IRQ pending
3372 */
3373 if (!(wave_ignore & voice_bit) && (int) voice < nr_voices) /*
3374 * Not done
3375 * yet
3376 */
3377 {
3378 wave_ignore |= voice_bit;
3379 do_loop_irq(voice);
3380 }
3381 if (!(src & 0x40)) /*
3382 * Volume IRQ pending
3383 */
3384 if (!(volume_ignore & voice_bit) && (int) voice < nr_voices) /*
3385 * Not done
3386 * yet
3387 */
3388 {
3389 volume_ignore |= voice_bit;
3390 do_volume_irq(voice);
3391 }
3392 }
3393 }
3394
guswave_dma_irq(void)3395 void guswave_dma_irq(void)
3396 {
3397 unsigned char status;
3398
3399 status = gus_look8(0x41); /* Get DMA IRQ Status */
3400 if (status & 0x40) /* DMA interrupt pending */
3401 switch (active_device)
3402 {
3403 case GUS_DEV_WAVE:
3404 wake_up(&dram_sleeper);
3405 break;
3406
3407 case GUS_DEV_PCM_CONTINUE: /* Left channel data transferred */
3408 gus_write8(0x41, 0); /* Disable GF1 DMA */
3409 gus_transfer_output_block(pcm_current_dev, pcm_current_buf,
3410 pcm_current_count,
3411 pcm_current_intrflag, 1);
3412 break;
3413
3414 case GUS_DEV_PCM_DONE: /* Right or mono channel data transferred */
3415 gus_write8(0x41, 0); /* Disable GF1 DMA */
3416 if (pcm_qlen < pcm_nblk)
3417 {
3418 dma_active = 0;
3419 if (gus_busy)
3420 {
3421 if (audio_devs[gus_devnum]->dmap_out->qlen > 0)
3422 DMAbuf_outputintr(gus_devnum, 0);
3423 }
3424 }
3425 break;
3426
3427 default:
3428 break;
3429 }
3430 status = gus_look8(0x49); /*
3431 * Get Sampling IRQ Status
3432 */
3433 if (status & 0x40) /*
3434 * Sampling Irq pending
3435 */
3436 {
3437 DMAbuf_inputintr(gus_devnum);
3438 }
3439 }
3440
3441 /*
3442 * Timer stuff
3443 */
3444
3445 static volatile int select_addr, data_addr;
3446 static volatile int curr_timer = 0;
3447
gus_timer_command(unsigned int addr,unsigned int val)3448 void gus_timer_command(unsigned int addr, unsigned int val)
3449 {
3450 int i;
3451
3452 outb(((unsigned char) (addr & 0xff)), select_addr);
3453
3454 for (i = 0; i < 2; i++)
3455 inb(select_addr);
3456
3457 outb(((unsigned char) (val & 0xff)), data_addr);
3458
3459 for (i = 0; i < 2; i++)
3460 inb(select_addr);
3461 }
3462
arm_timer(int timer,unsigned int interval)3463 static void arm_timer(int timer, unsigned int interval)
3464 {
3465 curr_timer = timer;
3466
3467 if (timer == 1)
3468 {
3469 gus_write8(0x46, 256 - interval); /* Set counter for timer 1 */
3470 gus_write8(0x45, 0x04); /* Enable timer 1 IRQ */
3471 gus_timer_command(0x04, 0x01); /* Start timer 1 */
3472 }
3473 else
3474 {
3475 gus_write8(0x47, 256 - interval); /* Set counter for timer 2 */
3476 gus_write8(0x45, 0x08); /* Enable timer 2 IRQ */
3477 gus_timer_command(0x04, 0x02); /* Start timer 2 */
3478 }
3479
3480 gus_timer_enabled = 1;
3481 }
3482
gus_tmr_start(int dev,unsigned int usecs_per_tick)3483 static unsigned int gus_tmr_start(int dev, unsigned int usecs_per_tick)
3484 {
3485 int timer_no, resolution;
3486 int divisor;
3487
3488 if (usecs_per_tick > (256 * 80))
3489 {
3490 timer_no = 2;
3491 resolution = 320; /* usec */
3492 }
3493 else
3494 {
3495 timer_no = 1;
3496 resolution = 80; /* usec */
3497 }
3498 divisor = (usecs_per_tick + (resolution / 2)) / resolution;
3499 arm_timer(timer_no, divisor);
3500
3501 return divisor * resolution;
3502 }
3503
gus_tmr_disable(int dev)3504 static void gus_tmr_disable(int dev)
3505 {
3506 gus_write8(0x45, 0); /* Disable both timers */
3507 gus_timer_enabled = 0;
3508 }
3509
gus_tmr_restart(int dev)3510 static void gus_tmr_restart(int dev)
3511 {
3512 if (curr_timer == 1)
3513 gus_write8(0x45, 0x04); /* Start timer 1 again */
3514 else
3515 gus_write8(0x45, 0x08); /* Start timer 2 again */
3516 gus_timer_enabled = 1;
3517 }
3518
3519 static struct sound_lowlev_timer gus_tmr =
3520 {
3521 0,
3522 1,
3523 gus_tmr_start,
3524 gus_tmr_disable,
3525 gus_tmr_restart
3526 };
3527
gus_tmr_install(int io_base)3528 static void gus_tmr_install(int io_base)
3529 {
3530 struct sound_lowlev_timer *tmr;
3531
3532 select_addr = io_base;
3533 data_addr = io_base + 1;
3534
3535 tmr = &gus_tmr;
3536
3537 #ifdef THIS_GETS_FIXED
3538 sound_timer_init(&gus_tmr, "GUS");
3539 #endif
3540 }
3541