4 * Driver for the Gravis UltraSound wave table synth.
7 * Copyright (C) by Hannu Savolainen 1993-1997
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
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
20 #include <linux/init.h>
21 #include <linux/spinlock.h>
23 #define GUSPNP_AUTODETECT
25 #include "sound_config.h"
26 #include <linux/ultrasound.h>
31 #define GUS_BANK_SIZE (((iw_mode) ? 256*1024*1024 : 256*1024))
33 #define MAX_SAMPLE 150
36 #define NOT_SAMPLE 0xffff
40 unsigned long orig_freq;
41 unsigned long current_freq;
48 unsigned int initial_volume;
49 unsigned int current_volume;
50 int loop_irq_mode, loop_irq_parm;
51 #define LMODE_FINISH 1
53 #define LMODE_PCM_STOP 3
54 int volume_irq_mode, volume_irq_parm;
56 #define VMODE_ENVELOPE 2
57 #define VMODE_START_NOTE 3
60 unsigned char env_rate[6];
61 unsigned char env_offset[6];
64 * Volume computation parameters for gus_adagio_vol()
66 int main_vol, expression_vol, patch_vol;
68 /* Variables for "Ultraclick" removal */
69 int dev_pending, note_pending, volume_pending,
76 static struct voice_alloc_info *voice_alloc;
77 static struct address_info *gus_hw_config;
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;
84 static long gus_mem_size;
85 static long free_mem_ptr;
87 static int gus_no_dma;
89 static int gus_devnum;
90 static int volume_base, volume_scale, volume_method;
91 static int gus_recmask = SOUND_MASK_MIC;
92 static int recording_active;
93 static int only_read_access;
94 static int only_8_bits;
96 static int iw_mode = 0;
97 int gus_wave_volume = 60;
98 int gus_pcm_volume = 80;
100 static int gus_line_vol = 100, gus_mic_vol;
101 static unsigned char mix_image = 0x00;
103 int gus_timer_enabled = 0;
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
110 static int active_device;
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 */
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 */
122 static DECLARE_WAIT_QUEUE_HEAD(dram_sleeper);
125 * Variables and buffers for PCM output
128 #define MAX_PCM_BUFFERS (128*MAX_REALTIME_FACTOR) /* Don't change */
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;
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 DEFINE_SPINLOCK(gus_lock);
145 static struct voice_info voices[32];
147 static int freq_div_table[] =
170 static struct patch_info *samples;
171 static long sample_ptrs[MAX_SAMPLE + 1];
172 static int sample_map[32];
173 static int free_sample;
174 static int mixer_type;
177 static int patch_table[MAX_PATCH];
178 static int patch_map[32];
180 static struct synth_info gus_info = {
181 "Gravis UltraSound", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_GUS,
185 static void gus_poke(long addr, unsigned char data);
186 static void compute_and_set_volume(int voice, int volume, int ramp_time);
187 extern unsigned short gus_adagio_vol(int vel, int mainv, int xpn, int voicev);
188 extern unsigned short gus_linear_vol(int vol, int mainvol);
189 static void compute_volume(int voice, int volume);
190 static void do_volume_irq(int voice);
191 static void set_input_volumes(void);
192 static void gus_tmr_install(int io_base);
194 #define INSTANT_RAMP -1 /* Instant change. No ramping */
195 #define FAST_RAMP 0 /* Fastest possible ramp */
197 static void reset_sample_memory(void)
201 for (i = 0; i <= MAX_SAMPLE; i++)
203 for (i = 0; i < 32; i++)
205 for (i = 0; i < 32; i++)
208 gus_poke(0, 0); /* Put a silent sample to the beginning */
214 for (i = 0; i < MAX_PATCH; i++)
215 patch_table[i] = NOT_SAMPLE;
222 for (i = 0; i < 7; i++)
226 static void gus_poke(long addr, unsigned char data)
227 { /* Writes a byte to the DRAM */
228 outb((0x43), u_Command);
229 outb((addr & 0xff), u_DataLo);
230 outb(((addr >> 8) & 0xff), u_DataHi);
232 outb((0x44), u_Command);
233 outb(((addr >> 16) & 0xff), u_DataHi);
234 outb((data), u_DRAMIO);
237 static unsigned char gus_peek(long addr)
238 { /* Reads a byte from the DRAM */
241 outb((0x43), u_Command);
242 outb((addr & 0xff), u_DataLo);
243 outb(((addr >> 8) & 0xff), u_DataHi);
245 outb((0x44), u_Command);
246 outb(((addr >> 16) & 0xff), u_DataHi);
252 void gus_write8(int reg, unsigned int data)
253 { /* Writes to an indirect register (8 bit) */
254 outb((reg), u_Command);
255 outb(((unsigned char) (data & 0xff)), u_DataHi);
258 static unsigned char gus_read8(int reg)
260 /* Reads from an indirect register (8 bit). Offset 0x80. */
263 outb((reg | 0x80), u_Command);
269 static unsigned char gus_look8(int reg)
271 /* Reads from an indirect register (8 bit). No additional offset. */
274 outb((reg), u_Command);
280 static void gus_write16(int reg, unsigned int data)
282 /* Writes to an indirect register (16 bit) */
283 outb((reg), u_Command);
285 outb(((unsigned char) (data & 0xff)), u_DataLo);
286 outb(((unsigned char) ((data >> 8) & 0xff)), u_DataHi);
289 static unsigned short gus_read16(int reg)
291 /* Reads from an indirect register (16 bit). Offset 0x80. */
292 unsigned char hi, lo;
294 outb((reg | 0x80), u_Command);
299 return ((hi << 8) & 0xff00) | lo;
302 static unsigned short gus_look16(int reg)
304 /* Reads from an indirect register (16 bit). No additional offset. */
305 unsigned char hi, lo;
307 outb((reg), u_Command);
312 return ((hi << 8) & 0xff00) | lo;
315 static void gus_write_addr(int reg, unsigned long address, int frac, int is16bit)
317 /* Writes an 24 bit memory address */
318 unsigned long hold_address;
324 /* Interwave spesific address translations */
330 * Special processing required for 16 bit patches
333 hold_address = address;
334 address = address >> 1;
335 address &= 0x0001ffffL;
336 address |= (hold_address & 0x000c0000L);
339 gus_write16(reg, (unsigned short) ((address >> 7) & 0xffff));
340 gus_write16(reg + 1, (unsigned short) ((address << 9) & 0xffff)
342 /* Could writing twice fix problems with GUS_VOICE_POS()? Let's try. */
344 gus_write16(reg, (unsigned short) ((address >> 7) & 0xffff));
345 gus_write16(reg + 1, (unsigned short) ((address << 9) & 0xffff)
349 static void gus_select_voice(int voice)
351 if (voice < 0 || voice > 31)
353 outb((voice), u_Voice);
356 static void gus_select_max_voices(int nvoices)
365 voice_alloc->max_voice = nr_voices = nvoices;
366 gus_write8(0x0e, (nvoices - 1) | 0xc0);
369 static void gus_voice_on(unsigned int mode)
371 gus_write8(0x00, (unsigned char) (mode & 0xfc));
373 gus_write8(0x00, (unsigned char) (mode & 0xfc));
376 static void gus_voice_off(void)
378 gus_write8(0x00, gus_read8(0x00) | 0x03);
381 static void gus_voice_mode(unsigned int m)
383 unsigned char mode = (unsigned char) (m & 0xff);
385 gus_write8(0x00, (gus_read8(0x00) & 0x03) |
386 (mode & 0xfc)); /* Don't touch last two bits */
388 gus_write8(0x00, (gus_read8(0x00) & 0x03) | (mode & 0xfc));
391 static void gus_voice_freq(unsigned long freq)
393 unsigned long divisor = freq_div_table[nr_voices - 14];
396 /* Interwave plays at 44100 Hz with any number of voices */
398 fc = (unsigned short) (((freq << 9) + (44100 >> 1)) / 44100);
400 fc = (unsigned short) (((freq << 9) + (divisor >> 1)) / divisor);
403 gus_write16(0x01, fc);
406 static void gus_voice_volume(unsigned int vol)
408 gus_write8(0x0d, 0x03); /* Stop ramp before setting volume */
409 gus_write16(0x09, (unsigned short) (vol << 4));
412 static void gus_voice_balance(unsigned int balance)
414 gus_write8(0x0c, (unsigned char) (balance & 0xff));
417 static void gus_ramp_range(unsigned int low, unsigned int high)
419 gus_write8(0x07, (unsigned char) ((low >> 4) & 0xff));
420 gus_write8(0x08, (unsigned char) ((high >> 4) & 0xff));
423 static void gus_ramp_rate(unsigned int scale, unsigned int rate)
425 gus_write8(0x06, (unsigned char) (((scale & 0x03) << 6) | (rate & 0x3f)));
428 static void gus_rampon(unsigned int m)
430 unsigned char mode = (unsigned char) (m & 0xff);
432 gus_write8(0x0d, mode & 0xfc);
434 gus_write8(0x0d, mode & 0xfc);
437 static void gus_ramp_mode(unsigned int m)
439 unsigned char mode = (unsigned char) (m & 0xff);
441 gus_write8(0x0d, (gus_read8(0x0d) & 0x03) |
442 (mode & 0xfc)); /* Leave the last 2 bits alone */
444 gus_write8(0x0d, (gus_read8(0x0d) & 0x03) | (mode & 0xfc));
447 static void gus_rampoff(void)
449 gus_write8(0x0d, 0x03);
452 static void gus_set_voice_pos(int voice, long position)
456 if ((sample_no = sample_map[voice]) != -1) {
457 if (position < samples[sample_no].len) {
458 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
459 voices[voice].offset_pending = position;
461 gus_write_addr(0x0a, sample_ptrs[sample_no] + position, 0,
462 samples[sample_no].mode & WAVE_16_BITS);
467 static void gus_voice_init(int voice)
471 spin_lock_irqsave(&gus_lock,flags);
472 gus_select_voice(voice);
475 gus_write_addr(0x0a, 0, 0, 0); /* Set current position to 0 */
476 gus_write8(0x00, 0x03); /* Voice off */
477 gus_write8(0x0d, 0x03); /* Ramping off */
478 voice_alloc->map[voice] = 0;
479 voice_alloc->alloc_times[voice] = 0;
480 spin_unlock_irqrestore(&gus_lock,flags);
484 static void gus_voice_init2(int voice)
486 voices[voice].panning = 0;
487 voices[voice].mode = 0;
488 voices[voice].orig_freq = 20000;
489 voices[voice].current_freq = 20000;
490 voices[voice].bender = 0;
491 voices[voice].bender_range = 200;
492 voices[voice].initial_volume = 0;
493 voices[voice].current_volume = 0;
494 voices[voice].loop_irq_mode = 0;
495 voices[voice].loop_irq_parm = 0;
496 voices[voice].volume_irq_mode = 0;
497 voices[voice].volume_irq_parm = 0;
498 voices[voice].env_phase = 0;
499 voices[voice].main_vol = 127;
500 voices[voice].patch_vol = 127;
501 voices[voice].expression_vol = 127;
502 voices[voice].sample_pending = -1;
503 voices[voice].fixed_pitch = 0;
506 static void step_envelope(int voice)
508 unsigned vol, prev_vol, phase;
512 if (voices[voice].mode & WAVE_SUSTAIN_ON && voices[voice].env_phase == 2)
514 spin_lock_irqsave(&gus_lock,flags);
515 gus_select_voice(voice);
517 spin_unlock_irqrestore(&gus_lock,flags);
520 * Sustain phase begins. Continue envelope after receiving note off.
523 if (voices[voice].env_phase >= 5)
525 /* Envelope finished. Shoot the voice down */
526 gus_voice_init(voice);
529 prev_vol = voices[voice].current_volume;
530 phase = ++voices[voice].env_phase;
531 compute_volume(voice, voices[voice].midi_volume);
532 vol = voices[voice].initial_volume * voices[voice].env_offset[phase] / 255;
533 rate = voices[voice].env_rate[phase];
535 spin_lock_irqsave(&gus_lock,flags);
536 gus_select_voice(voice);
538 gus_voice_volume(prev_vol);
541 gus_write8(0x06, rate); /* Ramping rate */
543 voices[voice].volume_irq_mode = VMODE_ENVELOPE;
545 if (((vol - prev_vol) / 64) == 0) /* No significant volume change */
547 spin_unlock_irqrestore(&gus_lock,flags);
548 step_envelope(voice); /* Continue the envelope on the next step */
553 if (vol >= (4096 - 64))
555 gus_ramp_range(0, vol);
556 gus_rampon(0x20); /* Increasing volume, with IRQ */
562 gus_ramp_range(vol, 4030);
563 gus_rampon(0x60); /* Decreasing volume, with IRQ */
565 voices[voice].current_volume = vol;
566 spin_unlock_irqrestore(&gus_lock,flags);
569 static void init_envelope(int voice)
571 voices[voice].env_phase = -1;
572 voices[voice].current_volume = 64;
574 step_envelope(voice);
577 static void start_release(int voice)
579 if (gus_read8(0x00) & 0x03)
580 return; /* Voice already stopped */
582 voices[voice].env_phase = 2; /* Will be incremented by step_envelope */
584 voices[voice].current_volume = voices[voice].initial_volume =
585 gus_read16(0x09) >> 4; /* Get current volume */
587 voices[voice].mode &= ~WAVE_SUSTAIN_ON;
589 step_envelope(voice);
592 static void gus_voice_fade(int voice)
594 int instr_no = sample_map[voice], is16bits;
597 spin_lock_irqsave(&gus_lock,flags);
598 gus_select_voice(voice);
600 if (instr_no < 0 || instr_no > MAX_SAMPLE)
602 gus_write8(0x00, 0x03); /* Hard stop */
603 voice_alloc->map[voice] = 0;
604 spin_unlock_irqrestore(&gus_lock,flags);
607 is16bits = (samples[instr_no].mode & WAVE_16_BITS) ? 1 : 0; /* 8 or 16 bits */
609 if (voices[voice].mode & WAVE_ENVELOPES)
611 start_release(voice);
612 spin_unlock_irqrestore(&gus_lock,flags);
616 * Ramp the volume down but not too quickly.
618 if ((int) (gus_read16(0x09) >> 4) < 100) /* Get current volume */
622 gus_voice_init(voice);
623 spin_unlock_irqrestore(&gus_lock,flags);
626 gus_ramp_range(65, 4030);
628 gus_rampon(0x40 | 0x20); /* Down, once, with IRQ */
629 voices[voice].volume_irq_mode = VMODE_HALT;
630 spin_unlock_irqrestore(&gus_lock,flags);
633 static void gus_reset(void)
637 gus_select_max_voices(24);
640 volume_method = VOL_METHOD_ADAGIO;
642 for (i = 0; i < 32; i++)
644 gus_voice_init(i); /* Turn voice off */
649 static void gus_initialize(void)
652 unsigned char dma_image, irq_image, tmp;
654 static unsigned char gus_irq_map[16] = {
655 0, 0, 0, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7
658 static unsigned char gus_dma_map[8] = {
659 0, 1, 0, 2, 0, 3, 4, 5
662 spin_lock_irqsave(&gus_lock,flags);
663 gus_write8(0x4c, 0); /* Reset GF1 */
667 gus_write8(0x4c, 1); /* Release Reset */
672 * Clear all interrupts
675 gus_write8(0x41, 0); /* DMA control */
676 gus_write8(0x45, 0); /* Timer control */
677 gus_write8(0x49, 0); /* Sample control */
679 gus_select_max_voices(24);
681 inb(u_Status); /* Touch the status register */
683 gus_look8(0x41); /* Clear any pending DMA IRQs */
684 gus_look8(0x49); /* Clear any pending sample IRQs */
685 gus_read8(0x0f); /* Clear pending IRQs */
687 gus_reset(); /* Resets all voices */
689 gus_look8(0x41); /* Clear any pending DMA IRQs */
690 gus_look8(0x49); /* Clear any pending sample IRQs */
691 gus_read8(0x0f); /* Clear pending IRQs */
693 gus_write8(0x4c, 7); /* Master reset | DAC enable | IRQ enable */
696 * Set up for Digital ASIC
699 outb((0x05), gus_base + 0x0f);
701 mix_image |= 0x02; /* Disable line out (for a moment) */
702 outb((mix_image), u_Mixer);
704 outb((0x00), u_IRQDMAControl);
706 outb((0x00), gus_base + 0x0f);
709 * Now set up the DMA and IRQ interface
711 * The GUS supports two IRQs and two DMAs.
713 * Just one DMA channel is used. This prevents simultaneous ADC and DAC.
714 * Adding this support requires significant changes to the dmabuf.c, dsp.c
719 tmp = gus_irq_map[gus_irq];
720 if (!gus_pnp_flag && !tmp)
721 printk(KERN_WARNING "Warning! GUS IRQ not selected\n");
723 irq_image |= 0x40; /* Combine IRQ1 (GF1) and IRQ2 (Midi) */
726 if (gus_dma2 == gus_dma || gus_dma2 == -1)
729 dma_image = 0x40; /* Combine DMA1 (DRAM) and IRQ2 (ADC) */
731 tmp = gus_dma_map[gus_dma];
733 printk(KERN_WARNING "Warning! GUS DMA not selected\n");
739 /* Setup dual DMA channel mode for GUS MAX */
741 dma_image = gus_dma_map[gus_dma];
743 printk(KERN_WARNING "Warning! GUS DMA not selected\n");
745 tmp = gus_dma_map[gus_dma2] << 3;
748 printk(KERN_WARNING "Warning! Invalid GUS MAX DMA\n");
749 tmp = 0x40; /* Combine DMA channels */
756 * For some reason the IRQ and DMA addresses must be written twice
760 * Doing it first time
763 outb((mix_image), u_Mixer); /* Select DMA control */
764 outb((dma_image | 0x80), u_IRQDMAControl); /* Set DMA address */
766 outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
767 outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
770 * Doing it second time
773 outb((mix_image), u_Mixer); /* Select DMA control */
774 outb((dma_image), u_IRQDMAControl); /* Set DMA address */
776 outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
777 outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
779 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
781 mix_image &= ~0x02; /* Enable line out */
782 mix_image |= 0x08; /* Enable IRQ */
783 outb((mix_image), u_Mixer); /*
784 * Turn mixer channels on
785 * Note! Mic in is left off.
788 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
790 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
792 inb(u_Status); /* Touch the status register */
794 gus_look8(0x41); /* Clear any pending DMA IRQs */
795 gus_look8(0x49); /* Clear any pending sample IRQs */
797 gus_read8(0x0f); /* Clear pending IRQs */
800 gus_write8(0x19, gus_read8(0x19) | 0x01);
801 spin_unlock_irqrestore(&gus_lock,flags);
805 static void __init pnp_mem_init(void)
808 #define CHUNK_SIZE (256*1024)
809 #define BANK_SIZE (4*1024*1024)
810 #define CHUNKS_PER_BANK (BANK_SIZE/CHUNK_SIZE)
812 int bank, chunk, addr, total = 0;
814 int i, j, bits = -1, testbits = -1, nbanks = 0;
817 * This routine determines what kind of RAM is installed in each of the four
818 * SIMM banks and configures the DRAM address decode logic accordingly.
822 * Place the chip into enhanced mode
824 gus_write8(0x19, gus_read8(0x19) | 0x01);
825 gus_write8(0x53, gus_look8(0x53) & ~0x02); /* Select DRAM I/O access */
828 * Set memory configuration to 4 DRAM banks of 4M in each (16M total).
831 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | 0x000c);
834 * Perform the DRAM size detection for each bank individually.
836 for (bank = 0; bank < 4; bank++)
840 addr = bank * BANK_SIZE;
842 /* Clean check points of each chunk */
843 for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
845 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
846 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
849 /* Write a value to each chunk point and verify the result */
850 for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
852 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x55);
853 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0xAA);
855 if (gus_peek(addr + chunk * CHUNK_SIZE + 0L) == 0x55 &&
856 gus_peek(addr + chunk * CHUNK_SIZE + 1L) == 0xAA)
858 /* OK. There is RAM. Now check for possible shadows */
861 for (chunk2 = 0; ok && chunk2 < chunk; chunk2++)
862 if (gus_peek(addr + chunk2 * CHUNK_SIZE + 0L) ||
863 gus_peek(addr + chunk2 * CHUNK_SIZE + 1L))
864 ok = 0; /* Addressing wraps */
867 size = (chunk + 1) * CHUNK_SIZE;
869 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
870 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
872 bank_sizes[bank] = size;
875 DDB(printk("Interwave: Bank %d, size=%dk\n", bank, size / 1024));
878 if (nbanks == 0) /* No RAM - Give up */
880 printk(KERN_ERR "Sound: An Interwave audio chip detected but no DRAM\n");
881 printk(KERN_ERR "Sound: Unable to work with this card.\n");
882 gus_write8(0x19, gus_read8(0x19) & ~0x01);
888 * Now we know how much DRAM there is in each bank. The next step is
889 * to find a DRAM size encoding (0 to 12) which is best for the combination
892 * First try if any of the possible alternatives matches exactly the amount
896 for (i = 0; bits == -1 && i < 13; i++)
900 for (j = 0; bits != -1 && j < 4; j++)
901 if (mem_decode[i][j] != bank_sizes[j])
902 bits = -1; /* No hit */
906 * If necessary, try to find a combination where other than the last
907 * bank matches our configuration and the last bank is left oversized.
908 * In this way we don't leave holes in the middle of memory.
911 if (bits == -1) /* No luck yet */
913 for (i = 0; bits == -1 && i < 13; i++)
917 for (j = 0; bits != -1 && j < nbanks - 1; j++)
918 if (mem_decode[i][j] != bank_sizes[j])
919 bits = -1; /* No hit */
920 if (mem_decode[i][nbanks - 1] < bank_sizes[nbanks - 1])
921 bits = -1; /* The last bank is too small */
925 * The last resort is to search for a combination where the banks are
926 * smaller than the actual SIMMs. This leaves some memory in the banks
927 * unused but doesn't leave holes in the DRAM address space.
929 if (bits == -1) /* No luck yet */
931 for (i = 0; i < 13; i++)
934 for (j = 0; testbits != -1 && j < nbanks - 1; j++)
935 if (mem_decode[i][j] > bank_sizes[j]) {
938 if(testbits > bits) bits = testbits;
942 printk(KERN_INFO "Interwave: Can't use all installed RAM.\n");
943 printk(KERN_INFO "Interwave: Try reordering SIMMS.\n");
945 printk(KERN_INFO "Interwave: Can't find working DRAM encoding.\n");
946 printk(KERN_INFO "Interwave: Defaulting to 256k. Try reordering SIMMS.\n");
949 DDB(printk("Interwave: Selecting DRAM addressing mode %d\n", bits));
951 for (bank = 0; bank < 4; bank++)
953 DDB(printk(" Bank %d, mem=%dk (limit %dk)\n", bank, bank_sizes[bank] / 1024, mem_decode[bits][bank] / 1024));
955 if (bank_sizes[bank] > mem_decode[bits][bank])
956 total += mem_decode[bits][bank];
958 total += bank_sizes[bank];
961 DDB(printk("Total %dk of DRAM (enhanced mode)\n", total / 1024));
964 * Set the memory addressing mode.
966 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | bits);
968 /* Leave the chip into enhanced mode. Disable LFO */
969 gus_mem_size = total;
971 gus_write8(0x19, (gus_read8(0x19) | 0x01) & ~0x02);
974 int __init gus_wave_detect(int baseaddr)
976 unsigned long i, max_mem = 1024L;
980 if (!request_region(baseaddr, 16, "GUS"))
982 if (!request_region(baseaddr + 0x100, 12, "GUS")) { /* 0x10c-> is MAX */
983 release_region(baseaddr, 16);
989 gus_write8(0x4c, 0); /* Reset GF1 */
993 gus_write8(0x4c, 1); /* Release Reset */
997 #ifdef GUSPNP_AUTODETECT
998 val = gus_look8(0x5b); /* Version number register */
999 gus_write8(0x5b, ~val); /* Invert all bits */
1001 if ((gus_look8(0x5b) & 0xf0) == (val & 0xf0)) /* No change */
1003 if ((gus_look8(0x5b) & 0x0f) == ((~val) & 0x0f)) /* Change */
1005 DDB(printk("Interwave chip version %d detected\n", (val & 0xf0) >> 4));
1010 DDB(printk("Not an Interwave chip (%x)\n", gus_look8(0x5b)));
1014 gus_write8(0x5b, val); /* Restore all bits */
1022 /* See if there is first block there.... */
1024 if (gus_peek(0L) != 0xaa) {
1025 release_region(baseaddr + 0x100, 12);
1026 release_region(baseaddr, 16);
1030 /* Now zero it out so that I can check for mirroring .. */
1032 for (i = 1L; i < max_mem; i++)
1036 /* check for mirroring ... */
1037 if (gus_peek(0L) != 0)
1041 for (n = loc - 1, failed = 0; n <= loc; n++)
1043 gus_poke(loc, 0xaa);
1044 if (gus_peek(loc) != 0xaa)
1046 gus_poke(loc, 0x55);
1047 if (gus_peek(loc) != 0x55)
1053 gus_mem_size = i << 10;
1057 static int guswave_ioctl(int dev, unsigned int cmd, void __user *arg)
1062 case SNDCTL_SYNTH_INFO:
1063 gus_info.nr_voices = nr_voices;
1064 if (copy_to_user(arg, &gus_info, sizeof(gus_info)))
1068 case SNDCTL_SEQ_RESETSAMPLES:
1069 reset_sample_memory();
1072 case SNDCTL_SEQ_PERCMODE:
1075 case SNDCTL_SYNTH_MEMAVL:
1076 return (gus_mem_size == 0) ? 0 : gus_mem_size - free_mem_ptr - 32;
1083 static int guswave_set_instr(int dev, int voice, int instr_no)
1087 if (instr_no < 0 || instr_no > MAX_PATCH)
1088 instr_no = 0; /* Default to acoustic piano */
1090 if (voice < 0 || voice > 31)
1093 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1095 voices[voice].sample_pending = instr_no;
1098 sample_no = patch_table[instr_no];
1099 patch_map[voice] = -1;
1101 if (sample_no == NOT_SAMPLE)
1103 /* printk("GUS: Undefined patch %d for voice %d\n", instr_no, voice);*/
1104 return -EINVAL; /* Patch not defined */
1106 if (sample_ptrs[sample_no] == -1) /* Sample not loaded */
1108 /* printk("GUS: Sample #%d not loaded for patch %d (voice %d)\n", sample_no, instr_no, voice);*/
1111 sample_map[voice] = sample_no;
1112 patch_map[voice] = instr_no;
1116 static int guswave_kill_note(int dev, int voice, int note, int velocity)
1118 unsigned long flags;
1120 spin_lock_irqsave(&gus_lock,flags);
1121 /* voice_alloc->map[voice] = 0xffff; */
1122 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1124 voices[voice].kill_pending = 1;
1125 spin_unlock_irqrestore(&gus_lock,flags);
1129 spin_unlock_irqrestore(&gus_lock,flags);
1130 gus_voice_fade(voice);
1136 static void guswave_aftertouch(int dev, int voice, int pressure)
1140 static void guswave_panning(int dev, int voice, int value)
1142 if (voice >= 0 || voice < 32)
1143 voices[voice].panning = value;
1146 static void guswave_volume_method(int dev, int mode)
1148 if (mode == VOL_METHOD_LINEAR || mode == VOL_METHOD_ADAGIO)
1149 volume_method = mode;
1152 static void compute_volume(int voice, int volume)
1155 voices[voice].midi_volume = volume;
1157 switch (volume_method)
1159 case VOL_METHOD_ADAGIO:
1160 voices[voice].initial_volume =
1161 gus_adagio_vol(voices[voice].midi_volume, voices[voice].main_vol,
1162 voices[voice].expression_vol,
1163 voices[voice].patch_vol);
1166 case VOL_METHOD_LINEAR: /* Totally ignores patch-volume and expression */
1167 voices[voice].initial_volume = gus_linear_vol(volume, voices[voice].main_vol);
1171 voices[voice].initial_volume = volume_base +
1172 (voices[voice].midi_volume * volume_scale);
1175 if (voices[voice].initial_volume > 4030)
1176 voices[voice].initial_volume = 4030;
1179 static void compute_and_set_volume(int voice, int volume, int ramp_time)
1181 int curr, target, rate;
1182 unsigned long flags;
1184 compute_volume(voice, volume);
1185 voices[voice].current_volume = voices[voice].initial_volume;
1187 spin_lock_irqsave(&gus_lock,flags);
1189 * CAUTION! Interrupts disabled. Enable them before returning
1192 gus_select_voice(voice);
1194 curr = gus_read16(0x09) >> 4;
1195 target = voices[voice].initial_volume;
1197 if (ramp_time == INSTANT_RAMP)
1200 gus_voice_volume(target);
1201 spin_unlock_irqrestore(&gus_lock,flags);
1204 if (ramp_time == FAST_RAMP)
1208 gus_ramp_rate(0, rate);
1210 if ((target - curr) / 64 == 0) /* Close enough to target. */
1213 gus_voice_volume(target);
1214 spin_unlock_irqrestore(&gus_lock,flags);
1219 if (target > (4095 - 65))
1221 gus_ramp_range(curr, target);
1222 gus_rampon(0x00); /* Ramp up, once, no IRQ */
1229 gus_ramp_range(target, curr);
1230 gus_rampon(0x40); /* Ramp down, once, no irq */
1232 spin_unlock_irqrestore(&gus_lock,flags);
1235 static void dynamic_volume_change(int voice)
1237 unsigned char status;
1238 unsigned long flags;
1240 spin_lock_irqsave(&gus_lock,flags);
1241 gus_select_voice(voice);
1242 status = gus_read8(0x00); /* Get voice status */
1243 spin_unlock_irqrestore(&gus_lock,flags);
1246 return; /* Voice was not running */
1248 if (!(voices[voice].mode & WAVE_ENVELOPES))
1250 compute_and_set_volume(voice, voices[voice].midi_volume, 1);
1255 * Voice is running and has envelopes.
1258 spin_lock_irqsave(&gus_lock,flags);
1259 gus_select_voice(voice);
1260 status = gus_read8(0x0d); /* Ramping status */
1261 spin_unlock_irqrestore(&gus_lock,flags);
1263 if (status & 0x03) /* Sustain phase? */
1265 compute_and_set_volume(voice, voices[voice].midi_volume, 1);
1268 if (voices[voice].env_phase < 0)
1271 compute_volume(voice, voices[voice].midi_volume);
1275 static void guswave_controller(int dev, int voice, int ctrl_num, int value)
1277 unsigned long flags;
1280 if (voice < 0 || voice > 31)
1285 case CTRL_PITCH_BENDER:
1286 voices[voice].bender = value;
1288 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1290 freq = compute_finetune(voices[voice].orig_freq, value, voices[voice].bender_range, 0);
1291 voices[voice].current_freq = freq;
1293 spin_lock_irqsave(&gus_lock,flags);
1294 gus_select_voice(voice);
1295 gus_voice_freq(freq);
1296 spin_unlock_irqrestore(&gus_lock,flags);
1300 case CTRL_PITCH_BENDER_RANGE:
1301 voices[voice].bender_range = value;
1303 case CTL_EXPRESSION:
1305 case CTRL_EXPRESSION:
1306 if (volume_method == VOL_METHOD_ADAGIO)
1308 voices[voice].expression_vol = value;
1309 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1310 dynamic_volume_change(voice);
1315 voices[voice].panning = (value * 2) - 128;
1318 case CTL_MAIN_VOLUME:
1319 value = (value * 100) / 16383;
1321 case CTRL_MAIN_VOLUME:
1322 voices[voice].main_vol = value;
1323 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1324 dynamic_volume_change(voice);
1332 static int guswave_start_note2(int dev, int voice, int note_num, int volume)
1334 int sample, best_sample, best_delta, delta_freq;
1335 int is16bits, samplep, patch, pan;
1336 unsigned long note_freq, base_note, freq, flags;
1337 unsigned char mode = 0;
1339 if (voice < 0 || voice > 31)
1341 /* printk("GUS: Invalid voice\n");*/
1344 if (note_num == 255)
1346 if (voices[voice].mode & WAVE_ENVELOPES)
1348 voices[voice].midi_volume = volume;
1349 dynamic_volume_change(voice);
1352 compute_and_set_volume(voice, volume, 1);
1355 if ((patch = patch_map[voice]) == -1)
1357 if ((samplep = patch_table[patch]) == NOT_SAMPLE)
1361 note_freq = note_to_freq(note_num);
1364 * Find a sample within a patch so that the note_freq is between low_note
1369 best_sample = samplep;
1370 best_delta = 1000000;
1371 while (samplep != 0 && samplep != NOT_SAMPLE && sample == -1)
1373 delta_freq = note_freq - samples[samplep].base_note;
1375 delta_freq = -delta_freq;
1376 if (delta_freq < best_delta)
1378 best_sample = samplep;
1379 best_delta = delta_freq;
1381 if (samples[samplep].low_note <= note_freq &&
1382 note_freq <= samples[samplep].high_note)
1387 samplep = samples[samplep].key; /* Link to next sample */
1390 sample = best_sample;
1394 /* printk("GUS: Patch %d not defined for note %d\n", patch, note_num);*/
1395 return 0; /* Should play default patch ??? */
1397 is16bits = (samples[sample].mode & WAVE_16_BITS) ? 1 : 0;
1398 voices[voice].mode = samples[sample].mode;
1399 voices[voice].patch_vol = samples[sample].volume;
1402 gus_write8(0x15, 0x00); /* RAM, Reset voice deactivate bit of SMSI */
1404 if (voices[voice].mode & WAVE_ENVELOPES)
1408 for (i = 0; i < 6; i++)
1410 voices[voice].env_rate[i] = samples[sample].env_rate[i];
1411 voices[voice].env_offset[i] = samples[sample].env_offset[i];
1414 sample_map[voice] = sample;
1416 if (voices[voice].fixed_pitch) /* Fixed pitch */
1418 freq = samples[sample].base_freq;
1422 base_note = samples[sample].base_note / 100;
1425 freq = samples[sample].base_freq * note_freq / base_note;
1428 voices[voice].orig_freq = freq;
1431 * Since the pitch bender may have been set before playing the note, we
1432 * have to calculate the bending now.
1435 freq = compute_finetune(voices[voice].orig_freq, voices[voice].bender,
1436 voices[voice].bender_range, 0);
1437 voices[voice].current_freq = freq;
1439 pan = (samples[sample].panning + voices[voice].panning) / 32;
1446 if (samples[sample].mode & WAVE_16_BITS)
1448 mode |= 0x04; /* 16 bits */
1449 if ((sample_ptrs[sample] / GUS_BANK_SIZE) !=
1450 ((sample_ptrs[sample] + samples[sample].len) / GUS_BANK_SIZE))
1451 printk(KERN_ERR "GUS: Sample address error\n");
1453 spin_lock_irqsave(&gus_lock,flags);
1454 gus_select_voice(voice);
1458 spin_unlock_irqrestore(&gus_lock,flags);
1460 if (voices[voice].mode & WAVE_ENVELOPES)
1462 compute_volume(voice, volume);
1463 init_envelope(voice);
1467 compute_and_set_volume(voice, volume, 0);
1470 spin_lock_irqsave(&gus_lock,flags);
1471 gus_select_voice(voice);
1473 if (samples[sample].mode & WAVE_LOOP_BACK)
1474 gus_write_addr(0x0a, sample_ptrs[sample] + samples[sample].len -
1475 voices[voice].offset_pending, 0, is16bits); /* start=end */
1477 gus_write_addr(0x0a, sample_ptrs[sample] + voices[voice].offset_pending, 0, is16bits); /* Sample start=begin */
1479 if (samples[sample].mode & WAVE_LOOPING)
1483 if (samples[sample].mode & WAVE_BIDIR_LOOP)
1486 if (samples[sample].mode & WAVE_LOOP_BACK)
1488 gus_write_addr(0x0a, sample_ptrs[sample] + samples[sample].loop_end -
1489 voices[voice].offset_pending,
1490 (samples[sample].fractions >> 4) & 0x0f, is16bits);
1493 gus_write_addr(0x02, sample_ptrs[sample] + samples[sample].loop_start,
1494 samples[sample].fractions & 0x0f, is16bits); /* Loop start location */
1495 gus_write_addr(0x04, sample_ptrs[sample] + samples[sample].loop_end,
1496 (samples[sample].fractions >> 4) & 0x0f, is16bits); /* Loop end location */
1500 mode |= 0x20; /* Loop IRQ at the end */
1501 voices[voice].loop_irq_mode = LMODE_FINISH; /* Ramp down at the end */
1502 voices[voice].loop_irq_parm = 1;
1503 gus_write_addr(0x02, sample_ptrs[sample], 0, is16bits); /* Loop start location */
1504 gus_write_addr(0x04, sample_ptrs[sample] + samples[sample].len - 1,
1505 (samples[sample].fractions >> 4) & 0x0f, is16bits); /* Loop end location */
1507 gus_voice_freq(freq);
1508 gus_voice_balance(pan);
1510 spin_unlock_irqrestore(&gus_lock,flags);
1516 * New guswave_start_note by Andrew J. Robinson attempts to minimize clicking
1517 * when the note playing on the voice is changed. It uses volume
1521 static int guswave_start_note(int dev, int voice, int note_num, int volume)
1523 unsigned long flags;
1527 spin_lock_irqsave(&gus_lock,flags);
1528 if (note_num == 255)
1530 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1532 voices[voice].volume_pending = volume;
1536 ret_val = guswave_start_note2(dev, voice, note_num, volume);
1541 gus_select_voice(voice);
1542 mode = gus_read8(0x00);
1544 gus_write8(0x00, mode & 0xdf); /* No interrupt! */
1546 voices[voice].offset_pending = 0;
1547 voices[voice].kill_pending = 0;
1548 voices[voice].volume_irq_mode = 0;
1549 voices[voice].loop_irq_mode = 0;
1551 if (voices[voice].sample_pending >= 0)
1553 spin_unlock_irqrestore(&gus_lock,flags); /* Run temporarily with interrupts enabled */
1554 guswave_set_instr(voices[voice].dev_pending, voice, voices[voice].sample_pending);
1555 voices[voice].sample_pending = -1;
1556 spin_lock_irqsave(&gus_lock,flags);
1557 gus_select_voice(voice); /* Reselect the voice (just to be sure) */
1559 if ((mode & 0x01) || (int) ((gus_read16(0x09) >> 4) < (unsigned) 2065))
1561 ret_val = guswave_start_note2(dev, voice, note_num, volume);
1565 voices[voice].dev_pending = dev;
1566 voices[voice].note_pending = note_num;
1567 voices[voice].volume_pending = volume;
1568 voices[voice].volume_irq_mode = VMODE_START_NOTE;
1571 gus_ramp_range(2000, 4065);
1572 gus_ramp_rate(0, 63); /* Fastest possible rate */
1573 gus_rampon(0x20 | 0x40); /* Ramp down, once, irq */
1576 spin_unlock_irqrestore(&gus_lock,flags);
1580 static void guswave_reset(int dev)
1584 for (i = 0; i < 32; i++)
1591 static int guswave_open(int dev, int mode)
1598 voice_alloc->timestamp = 0;
1600 if (gus_no_wave_dma) {
1603 if ((err = DMAbuf_open_dma(gus_devnum)) < 0)
1605 /* printk( "GUS: Loading samples without DMA\n"); */
1606 gus_no_dma = 1; /* Upload samples using PIO */
1612 init_waitqueue_head(&dram_sleeper);
1614 active_device = GUS_DEV_WAVE;
1616 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
1619 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
1624 static void guswave_close(int dev)
1631 DMAbuf_close_dma(gus_devnum);
1634 static int guswave_load_patch(int dev, int format, const char __user *addr,
1635 int offs, int count, int pmgr_flag)
1637 struct patch_info patch;
1641 unsigned long blk_sz, blk_end, left, src_offs, target;
1643 sizeof_patch = (long) &patch.data[0] - (long) &patch; /* Header size */
1645 if (format != GUS_PATCH)
1647 /* printk("GUS Error: Invalid patch format (key) 0x%x\n", format);*/
1650 if (count < sizeof_patch)
1652 /* printk("GUS Error: Patch header too short\n");*/
1655 count -= sizeof_patch;
1657 if (free_sample >= MAX_SAMPLE)
1659 /* printk("GUS: Sample table full\n");*/
1663 * Copy the header from user space but ignore the first bytes which have
1664 * been transferred already.
1667 if (copy_from_user(&((char *) &patch)[offs], &(addr)[offs],
1668 sizeof_patch - offs))
1671 if (patch.mode & WAVE_ROM)
1673 if (gus_mem_size == 0)
1676 instr = patch.instr_no;
1678 if (instr < 0 || instr > MAX_PATCH)
1680 /* printk(KERN_ERR "GUS: Invalid patch number %d\n", instr);*/
1683 if (count < patch.len)
1685 /* printk(KERN_ERR "GUS Warning: Patch record too short (%d<%d)\n", count, (int) patch.len);*/
1688 if (patch.len <= 0 || patch.len > gus_mem_size)
1690 /* printk(KERN_ERR "GUS: Invalid sample length %d\n", (int) patch.len);*/
1693 if (patch.mode & WAVE_LOOPING)
1695 if (patch.loop_start < 0 || patch.loop_start >= patch.len)
1697 /* printk(KERN_ERR "GUS: Invalid loop start\n");*/
1700 if (patch.loop_end < patch.loop_start || patch.loop_end > patch.len)
1702 /* printk(KERN_ERR "GUS: Invalid loop end\n");*/
1706 free_mem_ptr = (free_mem_ptr + 31) & ~31; /* 32 byte alignment */
1708 if (patch.mode & WAVE_16_BITS)
1711 * 16 bit samples must fit one 256k bank.
1713 if (patch.len >= GUS_BANK_SIZE)
1715 /* printk("GUS: Sample (16 bit) too long %d\n", (int) patch.len);*/
1718 if ((free_mem_ptr / GUS_BANK_SIZE) !=
1719 ((free_mem_ptr + patch.len) / GUS_BANK_SIZE))
1721 unsigned long tmp_mem =
1723 ((free_mem_ptr / GUS_BANK_SIZE) + 1) * GUS_BANK_SIZE;
1725 if ((tmp_mem + patch.len) > gus_mem_size)
1728 free_mem_ptr = tmp_mem; /* This leaves unusable memory */
1731 if ((free_mem_ptr + patch.len) > gus_mem_size)
1734 sample_ptrs[free_sample] = free_mem_ptr;
1737 * Tremolo is not possible with envelopes
1740 if (patch.mode & WAVE_ENVELOPES)
1741 patch.mode &= ~WAVE_TREMOLO;
1743 if (!(patch.mode & WAVE_FRACTIONS))
1745 patch.fractions = 0;
1747 memcpy((char *) &samples[free_sample], &patch, sizeof_patch);
1750 * Link this_one sample to the list of samples for patch 'instr'.
1753 samples[free_sample].key = patch_table[instr];
1754 patch_table[instr] = free_sample;
1757 * Use DMA to transfer the wave data to the DRAM
1762 target = free_mem_ptr;
1764 while (left) /* Not completely transferred yet */
1766 blk_sz = audio_devs[gus_devnum]->dmap_out->bytes_in_use;
1771 * DMA cannot cross bank (256k) boundaries. Check for that.
1774 blk_end = target + blk_sz;
1776 if ((target / GUS_BANK_SIZE) != (blk_end / GUS_BANK_SIZE))
1778 /* Split the block */
1779 blk_end &= ~(GUS_BANK_SIZE - 1);
1780 blk_sz = blk_end - target;
1785 * For some reason the DMA is not possible. We have to use PIO.
1790 for (i = 0; i < blk_sz; i++)
1792 get_user(*(unsigned char *) &data, (unsigned char __user *) &((addr)[sizeof_patch + i]));
1793 if (patch.mode & WAVE_UNSIGNED)
1794 if (!(patch.mode & WAVE_16_BITS) || (i & 0x01))
1795 data ^= 0x80; /* Convert to signed */
1796 gus_poke(target + i, data);
1801 unsigned long address, hold_address;
1802 unsigned char dma_command;
1803 unsigned long flags;
1805 if (audio_devs[gus_devnum]->dmap_out->raw_buf == NULL)
1807 printk(KERN_ERR "GUS: DMA buffer == NULL\n");
1811 * OK, move now. First in and then out.
1814 if (copy_from_user(audio_devs[gus_devnum]->dmap_out->raw_buf,
1815 &(addr)[sizeof_patch + src_offs],
1819 spin_lock_irqsave(&gus_lock,flags);
1820 gus_write8(0x41, 0); /* Disable GF1 DMA */
1821 DMAbuf_start_dma(gus_devnum, audio_devs[gus_devnum]->dmap_out->raw_buf_phys,
1822 blk_sz, DMA_MODE_WRITE);
1825 * Set the DRAM address for the wave data
1830 /* Different address translation in enhanced mode */
1835 address = target >> 1; /* Convert to 16 bit word address */
1839 hi = (unsigned char) ((address >> 16) & 0xf0);
1840 hi += (unsigned char) (address & 0x0f);
1842 gus_write16(0x42, (address >> 4) & 0xffff); /* DMA address (low) */
1843 gus_write8(0x50, hi);
1848 if (audio_devs[gus_devnum]->dmap_out->dma > 3)
1850 hold_address = address;
1851 address = address >> 1;
1852 address &= 0x0001ffffL;
1853 address |= (hold_address & 0x000c0000L);
1855 gus_write16(0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
1859 * Start the DMA transfer
1862 dma_command = 0x21; /* IRQ enable, DMA start */
1863 if (patch.mode & WAVE_UNSIGNED)
1864 dma_command |= 0x80; /* Invert MSB */
1865 if (patch.mode & WAVE_16_BITS)
1866 dma_command |= 0x40; /* 16 bit _DATA_ */
1867 if (audio_devs[gus_devnum]->dmap_out->dma > 3)
1868 dma_command |= 0x04; /* 16 bit DMA _channel_ */
1871 * Sleep here until the DRAM DMA done interrupt is served
1873 active_device = GUS_DEV_WAVE;
1874 gus_write8(0x41, dma_command); /* Lets go luteet (=bugs) */
1876 spin_unlock_irqrestore(&gus_lock,flags); /* opens a race */
1877 if (!interruptible_sleep_on_timeout(&dram_sleeper, HZ))
1878 printk("GUS: DMA Transfer timed out\n");
1889 gus_write8(0x41, 0); /* Stop DMA */
1892 free_mem_ptr += patch.len;
1897 static void guswave_hw_control(int dev, unsigned char *event_rec)
1900 unsigned short p1, p2;
1902 unsigned long flags;
1905 voice = event_rec[3];
1906 p1 = *(unsigned short *) &event_rec[4];
1907 p2 = *(unsigned short *) &event_rec[6];
1908 plong = *(unsigned int *) &event_rec[4];
1910 if ((voices[voice].volume_irq_mode == VMODE_START_NOTE) &&
1911 (cmd != _GUS_VOICESAMPLE) && (cmd != _GUS_VOICE_POS))
1912 do_volume_irq(voice);
1916 case _GUS_NUMVOICES:
1917 spin_lock_irqsave(&gus_lock,flags);
1918 gus_select_voice(voice);
1919 gus_select_max_voices(p1);
1920 spin_unlock_irqrestore(&gus_lock,flags);
1923 case _GUS_VOICESAMPLE:
1924 guswave_set_instr(dev, voice, p1);
1928 spin_lock_irqsave(&gus_lock,flags);
1929 gus_select_voice(voice);
1930 p1 &= ~0x20; /* Don't allow interrupts */
1932 spin_unlock_irqrestore(&gus_lock,flags);
1936 spin_lock_irqsave(&gus_lock,flags);
1937 gus_select_voice(voice);
1939 spin_unlock_irqrestore(&gus_lock,flags);
1942 case _GUS_VOICEFADE:
1943 gus_voice_fade(voice);
1946 case _GUS_VOICEMODE:
1947 spin_lock_irqsave(&gus_lock,flags);
1948 gus_select_voice(voice);
1949 p1 &= ~0x20; /* Don't allow interrupts */
1951 spin_unlock_irqrestore(&gus_lock,flags);
1954 case _GUS_VOICEBALA:
1955 spin_lock_irqsave(&gus_lock,flags);
1956 gus_select_voice(voice);
1957 gus_voice_balance(p1);
1958 spin_unlock_irqrestore(&gus_lock,flags);
1961 case _GUS_VOICEFREQ:
1962 spin_lock_irqsave(&gus_lock,flags);
1963 gus_select_voice(voice);
1964 gus_voice_freq(plong);
1965 spin_unlock_irqrestore(&gus_lock,flags);
1969 spin_lock_irqsave(&gus_lock,flags);
1970 gus_select_voice(voice);
1971 gus_voice_volume(p1);
1972 spin_unlock_irqrestore(&gus_lock,flags);
1975 case _GUS_VOICEVOL2: /* Just update the software voice level */
1976 voices[voice].initial_volume = voices[voice].current_volume = p1;
1979 case _GUS_RAMPRANGE:
1980 if (voices[voice].mode & WAVE_ENVELOPES)
1982 spin_lock_irqsave(&gus_lock,flags);
1983 gus_select_voice(voice);
1984 gus_ramp_range(p1, p2);
1985 spin_unlock_irqrestore(&gus_lock,flags);
1989 if (voices[voice].mode & WAVE_ENVELOPES)
1990 break; /* NJET-NJET */
1991 spin_lock_irqsave(&gus_lock,flags);
1992 gus_select_voice(voice);
1993 gus_ramp_rate(p1, p2);
1994 spin_unlock_irqrestore(&gus_lock,flags);
1998 if (voices[voice].mode & WAVE_ENVELOPES)
2000 spin_lock_irqsave(&gus_lock,flags);
2001 gus_select_voice(voice);
2002 p1 &= ~0x20; /* Don't allow interrupts */
2004 spin_unlock_irqrestore(&gus_lock,flags);
2008 if (voices[voice].mode & WAVE_ENVELOPES)
2010 spin_lock_irqsave(&gus_lock,flags);
2011 gus_select_voice(voice);
2012 p1 &= ~0x20; /* Don't allow interrupts */
2014 spin_unlock_irqrestore(&gus_lock,flags);
2018 if (voices[voice].mode & WAVE_ENVELOPES)
2019 break; /* NEJ-NEJ */
2020 spin_lock_irqsave(&gus_lock,flags);
2021 gus_select_voice(voice);
2023 spin_unlock_irqrestore(&gus_lock,flags);
2026 case _GUS_VOLUME_SCALE:
2031 case _GUS_VOICE_POS:
2032 spin_lock_irqsave(&gus_lock,flags);
2033 gus_select_voice(voice);
2034 gus_set_voice_pos(voice, plong);
2035 spin_unlock_irqrestore(&gus_lock,flags);
2043 static int gus_audio_set_speed(int speed)
2046 speed = gus_audio_speed;
2054 gus_audio_speed = speed;
2056 if (only_read_access)
2058 /* Compute nearest valid recording speed and return it */
2060 /* speed = (9878400 / (gus_audio_speed + 2)) / 16; */
2061 speed = (((9878400 + gus_audio_speed / 2) / (gus_audio_speed + 2)) + 8) / 16;
2062 speed = (9878400 / (speed * 16)) - 2;
2067 static int gus_audio_set_channels(int channels)
2070 return gus_audio_channels;
2075 gus_audio_channels = channels;
2079 static int gus_audio_set_bits(int bits)
2082 return gus_audio_bits;
2084 if (bits != 8 && bits != 16)
2090 gus_audio_bits = bits;
2094 static int gus_audio_ioctl(int dev, unsigned int cmd, void __user *arg)
2100 case SOUND_PCM_WRITE_RATE:
2101 if (get_user(val, (int __user*)arg))
2103 val = gus_audio_set_speed(val);
2106 case SOUND_PCM_READ_RATE:
2107 val = gus_audio_speed;
2110 case SNDCTL_DSP_STEREO:
2111 if (get_user(val, (int __user *)arg))
2113 val = gus_audio_set_channels(val + 1) - 1;
2116 case SOUND_PCM_WRITE_CHANNELS:
2117 if (get_user(val, (int __user *)arg))
2119 val = gus_audio_set_channels(val);
2122 case SOUND_PCM_READ_CHANNELS:
2123 val = gus_audio_channels;
2126 case SNDCTL_DSP_SETFMT:
2127 if (get_user(val, (int __user *)arg))
2129 val = gus_audio_set_bits(val);
2132 case SOUND_PCM_READ_BITS:
2133 val = gus_audio_bits;
2136 case SOUND_PCM_WRITE_FILTER: /* NOT POSSIBLE */
2137 case SOUND_PCM_READ_FILTER:
2143 return put_user(val, (int __user *)arg);
2146 static void gus_audio_reset(int dev)
2148 if (recording_active)
2150 gus_write8(0x49, 0x00); /* Halt recording */
2151 set_input_volumes();
2155 static int saved_iw_mode; /* A hack hack hack */
2157 static int gus_audio_open(int dev, int mode)
2162 if (gus_pnp_flag && mode & OPEN_READ)
2164 /* printk(KERN_ERR "GUS: Audio device #%d is playback only.\n", dev);*/
2172 saved_iw_mode = iw_mode;
2175 /* There are some problems with audio in enhanced mode so disable it */
2176 gus_write8(0x19, gus_read8(0x19) & ~0x01); /* Disable enhanced mode */
2181 reset_sample_memory();
2182 gus_select_max_voices(14);
2187 if (mode & OPEN_READ)
2189 recording_active = 1;
2190 set_input_volumes();
2192 only_read_access = !(mode & OPEN_WRITE);
2193 only_8_bits = mode & OPEN_READ;
2195 audio_devs[dev]->format_mask = AFMT_U8;
2197 audio_devs[dev]->format_mask = AFMT_U8 | AFMT_S16_LE;
2202 static void gus_audio_close(int dev)
2204 iw_mode = saved_iw_mode;
2210 if (recording_active)
2212 gus_write8(0x49, 0x00); /* Halt recording */
2213 set_input_volumes();
2215 recording_active = 0;
2218 static void gus_audio_update_volume(void)
2220 unsigned long flags;
2223 if (pcm_active && pcm_opened)
2224 for (voice = 0; voice < gus_audio_channels; voice++)
2226 spin_lock_irqsave(&gus_lock,flags);
2227 gus_select_voice(voice);
2229 gus_voice_volume(1530 + (25 * gus_pcm_volume));
2230 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume));
2231 spin_unlock_irqrestore(&gus_lock,flags);
2235 static void play_next_pcm_block(void)
2237 unsigned long flags;
2238 int speed = gus_audio_speed;
2239 int this_one, is16bits, chn;
2240 unsigned long dram_loc;
2241 unsigned char mode[2], ramp_mode[2];
2246 this_one = pcm_head;
2248 for (chn = 0; chn < gus_audio_channels; chn++)
2251 ramp_mode[chn] = 0x03; /* Ramping and rollover off */
2255 mode[chn] |= 0x20; /* Loop IRQ */
2256 voices[chn].loop_irq_mode = LMODE_PCM;
2258 if (gus_audio_bits != 8)
2261 mode[chn] |= 0x04; /* 16 bit data */
2266 dram_loc = this_one * pcm_bsize;
2267 dram_loc += chn * pcm_banksize;
2269 if (this_one == (pcm_nblk - 1)) /* Last fragment of the DRAM buffer */
2271 mode[chn] |= 0x08; /* Enable loop */
2272 ramp_mode[chn] = 0x03; /* Disable rollover bit */
2277 ramp_mode[chn] = 0x04; /* Enable rollover bit */
2279 spin_lock_irqsave(&gus_lock,flags);
2280 gus_select_voice(chn);
2281 gus_voice_freq(speed);
2283 if (gus_audio_channels == 1)
2284 gus_voice_balance(7); /* mono */
2286 gus_voice_balance(0); /* left */
2288 gus_voice_balance(15); /* right */
2290 if (!pcm_active) /* Playback not already active */
2293 * The playback was not started yet (or there has been a pause).
2294 * Start the voice (again) and ask for a rollover irq at the end of
2295 * this_one block. If this_one one is last of the buffers, use just
2296 * the normal loop with irq.
2301 gus_voice_volume(1530 + (25 * gus_pcm_volume));
2302 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume));
2304 gus_write_addr(0x0a, chn * pcm_banksize, 0, is16bits); /* Starting position */
2305 gus_write_addr(0x02, chn * pcm_banksize, 0, is16bits); /* Loop start */
2308 gus_write_addr(0x04, pcm_banksize + (pcm_bsize * pcm_nblk) - 1,
2309 0, is16bits); /* Loop end location */
2312 gus_write_addr(0x04, dram_loc + pcm_bsize - 1,
2313 0, is16bits); /* Loop end location */
2315 mode[chn] |= 0x08; /* Enable looping */
2316 spin_unlock_irqrestore(&gus_lock,flags);
2318 for (chn = 0; chn < gus_audio_channels; chn++)
2320 spin_lock_irqsave(&gus_lock,flags);
2321 gus_select_voice(chn);
2322 gus_write8(0x0d, ramp_mode[chn]);
2324 gus_write8(0x15, 0x00); /* Reset voice deactivate bit of SMSI */
2325 gus_voice_on(mode[chn]);
2326 spin_unlock_irqrestore(&gus_lock,flags);
2331 static void gus_transfer_output_block(int dev, unsigned long buf,
2332 int total_count, int intrflag, int chn)
2335 * This routine transfers one block of audio data to the DRAM. In mono mode
2336 * it's called just once. When in stereo mode, this_one routine is called
2337 * once for both channels.
2339 * The left/mono channel data is transferred to the beginning of dram and the
2340 * right data to the area pointed by gus_page_size.
2343 int this_one, count;
2344 unsigned long flags;
2345 unsigned char dma_command;
2346 unsigned long address, hold_address;
2348 spin_lock_irqsave(&gus_lock,flags);
2350 count = total_count / gus_audio_channels;
2354 if (pcm_qlen >= pcm_nblk)
2355 printk(KERN_WARNING "GUS Warning: PCM buffers out of sync\n");
2357 this_one = pcm_current_block = pcm_tail;
2359 pcm_tail = (pcm_tail + 1) % pcm_nblk;
2360 pcm_datasize[this_one] = count;
2363 this_one = pcm_current_block;
2365 gus_write8(0x41, 0); /* Disable GF1 DMA */
2366 DMAbuf_start_dma(dev, buf + (chn * count), count, DMA_MODE_WRITE);
2368 address = this_one * pcm_bsize;
2369 address += chn * pcm_banksize;
2371 if (audio_devs[dev]->dmap_out->dma > 3)
2373 hold_address = address;
2374 address = address >> 1;
2375 address &= 0x0001ffffL;
2376 address |= (hold_address & 0x000c0000L);
2378 gus_write16(0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
2380 dma_command = 0x21; /* IRQ enable, DMA start */
2382 if (gus_audio_bits != 8)
2383 dma_command |= 0x40; /* 16 bit _DATA_ */
2385 dma_command |= 0x80; /* Invert MSB */
2387 if (audio_devs[dev]->dmap_out->dma > 3)
2388 dma_command |= 0x04; /* 16 bit DMA channel */
2390 gus_write8(0x41, dma_command); /* Kick start */
2392 if (chn == (gus_audio_channels - 1)) /* Last channel */
2395 * Last (right or mono) channel data
2397 dma_active = 1; /* DMA started. There is a unacknowledged buffer */
2398 active_device = GUS_DEV_PCM_DONE;
2399 if (!pcm_active && (pcm_qlen > 1 || count < pcm_bsize))
2401 play_next_pcm_block();
2407 * Left channel data. The right channel
2408 * is transferred after DMA interrupt
2410 active_device = GUS_DEV_PCM_CONTINUE;
2413 spin_unlock_irqrestore(&gus_lock,flags);
2416 static void gus_uninterleave8(char *buf, int l)
2418 /* This routine uninterleaves 8 bit stereo output (LRLRLR->LLLRRR) */
2419 int i, p = 0, halfsize = l / 2;
2420 char *buf2 = buf + halfsize, *src = bounce_buf;
2422 memcpy(bounce_buf, buf, l);
2424 for (i = 0; i < halfsize; i++)
2426 buf[i] = src[p++]; /* Left channel */
2427 buf2[i] = src[p++]; /* Right channel */
2431 static void gus_uninterleave16(short *buf, int l)
2433 /* This routine uninterleaves 16 bit stereo output (LRLRLR->LLLRRR) */
2434 int i, p = 0, halfsize = l / 2;
2435 short *buf2 = buf + halfsize, *src = (short *) bounce_buf;
2437 memcpy(bounce_buf, (char *) buf, l * 2);
2439 for (i = 0; i < halfsize; i++)
2441 buf[i] = src[p++]; /* Left channel */
2442 buf2[i] = src[p++]; /* Right channel */
2446 static void gus_audio_output_block(int dev, unsigned long buf, int total_count,
2449 struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
2451 dmap->flags |= DMA_NODMA | DMA_NOTIMEOUT;
2453 pcm_current_buf = buf;
2454 pcm_current_count = total_count;
2455 pcm_current_intrflag = intrflag;
2456 pcm_current_dev = dev;
2457 if (gus_audio_channels == 2)
2459 char *b = dmap->raw_buf + (buf - dmap->raw_buf_phys);
2461 if (gus_audio_bits == 8)
2462 gus_uninterleave8(b, total_count);
2464 gus_uninterleave16((short *) b, total_count / 2);
2466 gus_transfer_output_block(dev, buf, total_count, intrflag, 0);
2469 static void gus_audio_start_input(int dev, unsigned long buf, int count,
2472 unsigned long flags;
2475 spin_lock_irqsave(&gus_lock,flags);
2477 DMAbuf_start_dma(dev, buf, count, DMA_MODE_READ);
2478 mode = 0xa0; /* DMA IRQ enabled, invert MSB */
2480 if (audio_devs[dev]->dmap_in->dma > 3)
2481 mode |= 0x04; /* 16 bit DMA channel */
2482 if (gus_audio_channels > 1)
2483 mode |= 0x02; /* Stereo */
2484 mode |= 0x01; /* DMA enable */
2486 gus_write8(0x49, mode);
2487 spin_unlock_irqrestore(&gus_lock,flags);
2490 static int gus_audio_prepare_for_input(int dev, int bsize, int bcount)
2494 gus_audio_bsize = bsize;
2495 audio_devs[dev]->dmap_in->flags |= DMA_NODMA;
2496 rate = (((9878400 + gus_audio_speed / 2) / (gus_audio_speed + 2)) + 8) / 16;
2498 gus_write8(0x48, rate & 0xff); /* Set sampling rate */
2500 if (gus_audio_bits != 8)
2502 /* printk("GUS Error: 16 bit recording not supported\n");*/
2508 static int gus_audio_prepare_for_output(int dev, int bsize, int bcount)
2512 long mem_ptr, mem_size;
2514 audio_devs[dev]->dmap_out->flags |= DMA_NODMA | DMA_NOTIMEOUT;
2516 mem_size = gus_mem_size / gus_audio_channels;
2518 if (mem_size > (256 * 1024))
2519 mem_size = 256 * 1024;
2521 pcm_bsize = bsize / gus_audio_channels;
2522 pcm_head = pcm_tail = pcm_qlen = 0;
2524 pcm_nblk = 2; /* MAX_PCM_BUFFERS; */
2525 if ((pcm_bsize * pcm_nblk) > mem_size)
2526 pcm_nblk = mem_size / pcm_bsize;
2528 for (i = 0; i < pcm_nblk; i++)
2529 pcm_datasize[i] = 0;
2531 pcm_banksize = pcm_nblk * pcm_bsize;
2533 if (gus_audio_bits != 8 && pcm_banksize == (256 * 1024))
2535 gus_write8(0x41, 0); /* Disable GF1 DMA */
2539 static int gus_local_qlen(int dev)
2545 static struct audio_driver gus_audio_driver =
2547 .owner = THIS_MODULE,
2548 .open = gus_audio_open,
2549 .close = gus_audio_close,
2550 .output_block = gus_audio_output_block,
2551 .start_input = gus_audio_start_input,
2552 .ioctl = gus_audio_ioctl,
2553 .prepare_for_input = gus_audio_prepare_for_input,
2554 .prepare_for_output = gus_audio_prepare_for_output,
2555 .halt_io = gus_audio_reset,
2556 .local_qlen = gus_local_qlen,
2559 static void guswave_setup_voice(int dev, int voice, int chn)
2561 struct channel_info *info = &synth_devs[dev]->chn_info[chn];
2563 guswave_set_instr(dev, voice, info->pgm_num);
2564 voices[voice].expression_vol = info->controllers[CTL_EXPRESSION]; /* Just MSB */
2565 voices[voice].main_vol = (info->controllers[CTL_MAIN_VOLUME] * 100) / (unsigned) 128;
2566 voices[voice].panning = (info->controllers[CTL_PAN] * 2) - 128;
2567 voices[voice].bender = 0;
2568 voices[voice].bender_range = info->bender_range;
2571 voices[voice].fixed_pitch = 1;
2574 static void guswave_bender(int dev, int voice, int value)
2577 unsigned long flags;
2579 voices[voice].bender = value - 8192;
2580 freq = compute_finetune(voices[voice].orig_freq, value - 8192, voices[voice].bender_range, 0);
2581 voices[voice].current_freq = freq;
2583 spin_lock_irqsave(&gus_lock,flags);
2584 gus_select_voice(voice);
2585 gus_voice_freq(freq);
2586 spin_unlock_irqrestore(&gus_lock,flags);
2589 static int guswave_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc)
2591 int i, p, best = -1, best_time = 0x7fffffff;
2595 * First look for a completely stopped voice
2598 for (i = 0; i < alloc->max_voice; i++)
2600 if (alloc->map[p] == 0)
2605 if (alloc->alloc_times[p] < best_time)
2608 best_time = alloc->alloc_times[p];
2610 p = (p + 1) % alloc->max_voice;
2614 * Then look for a releasing voice
2617 for (i = 0; i < alloc->max_voice; i++)
2619 if (alloc->map[p] == 0xffff)
2624 p = (p + 1) % alloc->max_voice;
2633 static struct synth_operations guswave_operations =
2635 .owner = THIS_MODULE,
2639 .synth_type = SYNTH_TYPE_SAMPLE,
2640 .synth_subtype = SAMPLE_TYPE_GUS,
2641 .open = guswave_open,
2642 .close = guswave_close,
2643 .ioctl = guswave_ioctl,
2644 .kill_note = guswave_kill_note,
2645 .start_note = guswave_start_note,
2646 .set_instr = guswave_set_instr,
2647 .reset = guswave_reset,
2648 .hw_control = guswave_hw_control,
2649 .load_patch = guswave_load_patch,
2650 .aftertouch = guswave_aftertouch,
2651 .controller = guswave_controller,
2652 .panning = guswave_panning,
2653 .volume_method = guswave_volume_method,
2654 .bender = guswave_bender,
2655 .alloc_voice = guswave_alloc,
2656 .setup_voice = guswave_setup_voice
2659 static void set_input_volumes(void)
2661 unsigned long flags;
2662 unsigned char mask = 0xff & ~0x06; /* Just line out enabled */
2664 if (have_gus_max) /* Don't disturb GUS MAX */
2667 spin_lock_irqsave(&gus_lock,flags);
2670 * Enable channels having vol > 10%
2671 * Note! bit 0x01 means the line in DISABLED while 0x04 means
2672 * the mic in ENABLED.
2674 if (gus_line_vol > 10)
2676 if (gus_mic_vol > 10)
2679 if (recording_active)
2682 * Disable channel, if not selected for recording
2684 if (!(gus_recmask & SOUND_MASK_LINE))
2686 if (!(gus_recmask & SOUND_MASK_MIC))
2690 mix_image |= mask & 0x07;
2691 outb((mix_image), u_Mixer);
2693 spin_unlock_irqrestore(&gus_lock,flags);
2696 #define MIX_DEVS (SOUND_MASK_MIC|SOUND_MASK_LINE| \
2697 SOUND_MASK_SYNTH|SOUND_MASK_PCM)
2699 int gus_default_mixer_ioctl(int dev, unsigned int cmd, void __user *arg)
2703 if (((cmd >> 8) & 0xff) != 'M')
2706 if (!access_ok(VERIFY_WRITE, arg, sizeof(int)))
2709 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
2711 if (__get_user(val, (int __user *) arg))
2716 case SOUND_MIXER_RECSRC:
2717 gus_recmask = val & MIX_DEVS;
2718 if (!(gus_recmask & (SOUND_MASK_MIC | SOUND_MASK_LINE)))
2719 gus_recmask = SOUND_MASK_MIC;
2720 /* Note! Input volumes are updated during next open for recording */
2724 case SOUND_MIXER_MIC:
2731 set_input_volumes();
2732 val = vol | (vol << 8);
2735 case SOUND_MIXER_LINE:
2742 set_input_volumes();
2743 val = vol | (vol << 8);
2746 case SOUND_MIXER_PCM:
2747 gus_pcm_volume = val & 0xff;
2748 if (gus_pcm_volume < 0)
2750 if (gus_pcm_volume > 100)
2751 gus_pcm_volume = 100;
2752 gus_audio_update_volume();
2753 val = gus_pcm_volume | (gus_pcm_volume << 8);
2756 case SOUND_MIXER_SYNTH:
2757 gus_wave_volume = val & 0xff;
2758 if (gus_wave_volume < 0)
2759 gus_wave_volume = 0;
2760 if (gus_wave_volume > 100)
2761 gus_wave_volume = 100;
2762 if (active_device == GUS_DEV_WAVE)
2765 for (voice = 0; voice < nr_voices; voice++)
2766 dynamic_volume_change(voice); /* Apply the new vol */
2768 val = gus_wave_volume | (gus_wave_volume << 8);
2782 case SOUND_MIXER_RECSRC:
2786 case SOUND_MIXER_DEVMASK:
2790 case SOUND_MIXER_STEREODEVS:
2794 case SOUND_MIXER_RECMASK:
2795 val = SOUND_MASK_MIC | SOUND_MASK_LINE;
2798 case SOUND_MIXER_CAPS:
2802 case SOUND_MIXER_MIC:
2803 val = gus_mic_vol | (gus_mic_vol << 8);
2806 case SOUND_MIXER_LINE:
2807 val = gus_line_vol | (gus_line_vol << 8);
2810 case SOUND_MIXER_PCM:
2811 val = gus_pcm_volume | (gus_pcm_volume << 8);
2814 case SOUND_MIXER_SYNTH:
2815 val = gus_wave_volume | (gus_wave_volume << 8);
2822 return __put_user(val, (int __user *)arg);
2825 static struct mixer_operations gus_mixer_operations =
2827 .owner = THIS_MODULE,
2829 .name = "Gravis Ultrasound",
2830 .ioctl = gus_default_mixer_ioctl
2833 static int __init gus_default_mixer_init(void)
2837 if ((n = sound_alloc_mixerdev()) != -1)
2840 * Don't install if there is another
2843 mixer_devs[n] = &gus_mixer_operations;
2848 * Enable all mixer channels on the GF1 side. Otherwise recording will
2849 * not be possible using GUS MAX.
2852 mix_image |= 0x04; /* All channels enabled */
2853 outb((mix_image), u_Mixer);
2858 void __init gus_wave_init(struct address_info *hw_config)
2860 unsigned long flags;
2862 char *model_num = "2.4";
2864 int gus_type = 0x24; /* 2.4 */
2866 int irq = hw_config->irq, dma = hw_config->dma, dma2 = hw_config->dma2;
2869 hw_config->slots[0] = -1; /* No wave */
2870 hw_config->slots[1] = -1; /* No ad1848 */
2871 hw_config->slots[4] = -1; /* No audio */
2872 hw_config->slots[5] = -1; /* No mixer */
2876 if (irq < 0 || irq > 15)
2878 printk(KERN_ERR "ERROR! Invalid IRQ#%d. GUS Disabled", irq);
2883 if (dma < 0 || dma > 7 || dma == 4)
2885 printk(KERN_ERR "ERROR! Invalid DMA#%d. GUS Disabled", dma);
2891 gus_hw_config = hw_config;
2897 * Try to identify the GUS model.
2899 * Versions < 3.6 don't have the digital ASIC. Try to probe it first.
2902 spin_lock_irqsave(&gus_lock,flags);
2903 outb((0x20), gus_base + 0x0f);
2904 val = inb(gus_base + 0x0f);
2905 spin_unlock_irqrestore(&gus_lock,flags);
2907 if (gus_pnp_flag || (val != 0xff && (val & 0x06))) /* Should be 0x02?? */
2912 ad_flags = 0x12345678; /* Interwave "magic" */
2914 * It has the digital ASIC so the card is at least v3.4.
2915 * Next try to detect the true model.
2918 if (gus_pnp_flag) /* Hack hack hack */
2921 val = inb(u_MixSelect);
2924 * Value 255 means pre-3.7 which don't have mixer.
2925 * Values 5 thru 9 mean v3.7 which has a ICS2101 mixer.
2926 * 10 and above is GUS MAX which has the CS4231 codec/mixer.
2930 if (val == 255 || val < 5)
2939 mixer_type = ICS2101;
2940 request_region(u_MixSelect, 1, "GUS mixer");
2944 struct resource *ports;
2945 ports = request_region(gus_base + 0x10c, 4, "ad1848");
2948 mixer_type = CS4231;
2949 #ifdef CONFIG_SOUND_GUSMAX
2951 unsigned char max_config = 0x40; /* Codec enable */
2957 max_config |= 0x10; /* 16 bit capture DMA */
2960 max_config |= 0x20; /* 16 bit playback DMA */
2962 max_config |= (gus_base >> 4) & 0x0f; /* Extract the X from 2X0 */
2964 outb((max_config), gus_base + 0x106); /* UltraMax control */
2970 if (ad1848_detect(ports, &ad_flags, hw_config->osp))
2972 char *name = "GUS MAX";
2973 int old_num_mixers = num_mixers;
2978 gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
2979 gus_wave_volume = 90;
2981 if (hw_config->name)
2982 name = hw_config->name;
2984 hw_config->slots[1] = ad1848_init(name, ports,
2985 -irq, gus_dma2, /* Playback DMA */
2986 gus_dma, /* Capture DMA */
2987 1, /* Share DMA channels with GF1 */
2991 if (num_mixers > old_num_mixers)
2993 /* GUS has it's own mixer map */
2994 AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_SYNTH);
2995 AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_CD);
2996 AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE);
3000 release_region(gus_base + 0x10c, 4);
3002 printk(KERN_WARNING "GUS: No CS4231 ??");
3005 printk(KERN_ERR "GUS MAX found, but not compiled in\n");
3012 * ASIC not detected so the card must be 2.2 or 2.4.
3013 * There could still be the 16-bit/mixer daughter card.
3017 if (hw_config->name)
3018 snprintf(tmp, sizeof(tmp), "%s (%dk)", hw_config->name,
3019 (int) gus_mem_size / 1024);
3020 else if (gus_pnp_flag)
3021 snprintf(tmp, sizeof(tmp), "Gravis UltraSound PnP (%dk)",
3022 (int) gus_mem_size / 1024);
3024 snprintf(tmp, sizeof(tmp), "Gravis UltraSound %s (%dk)", model_num,
3025 (int) gus_mem_size / 1024);
3028 samples = (struct patch_info *)vmalloc((MAX_SAMPLE + 1) * sizeof(*samples));
3029 if (samples == NULL)
3031 printk(KERN_WARNING "gus_init: Cant allocate memory for instrument tables\n");
3034 conf_printf(tmp, hw_config);
3035 strlcpy(gus_info.name, tmp, sizeof(gus_info.name));
3037 if ((sdev = sound_alloc_synthdev()) == -1)
3038 printk(KERN_WARNING "gus_init: Too many synthesizers\n");
3041 voice_alloc = &guswave_operations.alloc;
3043 guswave_operations.id = "IWAVE";
3044 hw_config->slots[0] = sdev;
3045 synth_devs[sdev] = &guswave_operations;
3047 gus_tmr_install(gus_base + 8);
3050 reset_sample_memory();
3054 if ((gus_mem_size > 0) && !gus_no_wave_dma)
3056 hw_config->slots[4] = -1;
3057 if ((gus_devnum = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
3060 sizeof(struct audio_driver),
3062 ((!iw_mode && dma2 != dma && dma2 != -1) ?
3064 AFMT_U8 | AFMT_S16_LE,
3065 NULL, dma, dma2)) < 0)
3070 hw_config->slots[4] = gus_devnum;
3071 audio_devs[gus_devnum]->min_fragment = 9; /* 512k */
3072 audio_devs[gus_devnum]->max_fragment = 11; /* 8k (must match size of bounce_buf */
3073 audio_devs[gus_devnum]->mixer_dev = -1; /* Next mixer# */
3074 audio_devs[gus_devnum]->flags |= DMA_HARDSTOP;
3078 * Mixer dependent initialization.
3084 gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
3085 gus_wave_volume = 90;
3086 request_region(u_MixSelect, 1, "GUS mixer");
3087 hw_config->slots[5] = ics2101_mixer_init();
3088 audio_devs[gus_devnum]->mixer_dev = hw_config->slots[5]; /* Next mixer# */
3092 /* Initialized elsewhere (ad1848.c) */
3094 hw_config->slots[5] = gus_default_mixer_init();
3095 audio_devs[gus_devnum]->mixer_dev = hw_config->slots[5]; /* Next mixer# */
3100 void __exit gus_wave_unload(struct address_info *hw_config)
3102 #ifdef CONFIG_SOUND_GUSMAX
3105 ad1848_unload(gus_base + 0x10c,
3107 gus_dma2, /* Playback DMA */
3108 gus_dma, /* Capture DMA */
3109 1); /* Share DMA channels with GF1 */
3113 if (mixer_type == ICS2101)
3115 release_region(u_MixSelect, 1);
3117 if (hw_config->slots[0] != -1)
3118 sound_unload_synthdev(hw_config->slots[0]);
3119 if (hw_config->slots[1] != -1)
3120 sound_unload_audiodev(hw_config->slots[1]);
3121 if (hw_config->slots[2] != -1)
3122 sound_unload_mididev(hw_config->slots[2]);
3123 if (hw_config->slots[4] != -1)
3124 sound_unload_audiodev(hw_config->slots[4]);
3125 if (hw_config->slots[5] != -1)
3126 sound_unload_mixerdev(hw_config->slots[5]);
3131 /* called in interrupt context */
3132 static void do_loop_irq(int voice)
3137 spin_lock(&gus_lock);
3138 gus_select_voice(voice);
3140 tmp = gus_read8(0x00);
3142 * Disable wave IRQ for this_one voice
3144 gus_write8(0x00, tmp);
3146 if (tmp & 0x03) /* Voice stopped */
3147 voice_alloc->map[voice] = 0;
3149 mode = voices[voice].loop_irq_mode;
3150 voices[voice].loop_irq_mode = 0;
3151 parm = voices[voice].loop_irq_parm;
3155 case LMODE_FINISH: /*
3156 * Final loop finished, shoot volume down
3159 if ((int) (gus_read16(0x09) >> 4) < 100) /*
3160 * Get current volume
3165 gus_voice_init(voice);
3168 gus_ramp_range(65, 4065);
3169 gus_ramp_rate(0, 63); /*
3170 * Fastest possible rate
3172 gus_rampon(0x20 | 0x40); /*
3173 * Ramp down, once, irq
3175 voices[voice].volume_irq_mode = VMODE_HALT;
3178 case LMODE_PCM_STOP:
3179 pcm_active = 0; /* Signal to the play_next_pcm_block routine */
3183 pcm_head = (pcm_head + 1) % pcm_nblk;
3184 if (pcm_qlen && pcm_active)
3186 play_next_pcm_block();
3190 /* Underrun. Just stop the voice */
3191 gus_select_voice(0); /* Left channel */
3194 gus_select_voice(1); /* Right channel */
3201 * If the queue was full before this interrupt, the DMA transfer was
3202 * suspended. Let it continue now.
3205 if (audio_devs[gus_devnum]->dmap_out->qlen > 0)
3206 DMAbuf_outputintr(gus_devnum, 0);
3213 spin_unlock(&gus_lock);
3216 static void do_volume_irq(int voice)
3220 unsigned long flags;
3222 spin_lock_irqsave(&gus_lock,flags);
3224 gus_select_voice(voice);
3225 tmp = gus_read8(0x0d);
3227 * Disable volume ramp IRQ
3229 gus_write8(0x0d, tmp);
3231 mode = voices[voice].volume_irq_mode;
3232 voices[voice].volume_irq_mode = 0;
3233 parm = voices[voice].volume_irq_parm;
3237 case VMODE_HALT: /* Decay phase finished */
3239 gus_write8(0x15, 0x02); /* Set voice deactivate bit of SMSI */
3240 spin_unlock_irqrestore(&gus_lock,flags);
3241 gus_voice_init(voice);
3244 case VMODE_ENVELOPE:
3246 spin_unlock_irqrestore(&gus_lock,flags);
3247 step_envelope(voice);
3250 case VMODE_START_NOTE:
3251 spin_unlock_irqrestore(&gus_lock,flags);
3252 guswave_start_note2(voices[voice].dev_pending, voice,
3253 voices[voice].note_pending, voices[voice].volume_pending);
3254 if (voices[voice].kill_pending)
3255 guswave_kill_note(voices[voice].dev_pending, voice,
3256 voices[voice].note_pending, 0);
3258 if (voices[voice].sample_pending >= 0)
3260 guswave_set_instr(voices[voice].dev_pending, voice,
3261 voices[voice].sample_pending);
3262 voices[voice].sample_pending = -1;
3267 spin_unlock_irqrestore(&gus_lock,flags);
3270 /* called in irq context */
3271 void gus_voice_irq(void)
3273 unsigned long wave_ignore = 0, volume_ignore = 0;
3274 unsigned long voice_bit;
3276 unsigned char src, voice;
3280 src = gus_read8(0x0f); /*
3286 if (src == (0x80 | 0x40))
3291 voice_bit = 1 << voice;
3293 if (!(src & 0x80)) /*
3296 if (!(wave_ignore & voice_bit) && (int) voice < nr_voices) /*
3301 wave_ignore |= voice_bit;
3304 if (!(src & 0x40)) /*
3305 * Volume IRQ pending
3307 if (!(volume_ignore & voice_bit) && (int) voice < nr_voices) /*
3312 volume_ignore |= voice_bit;
3313 do_volume_irq(voice);
3318 void guswave_dma_irq(void)
3320 unsigned char status;
3322 status = gus_look8(0x41); /* Get DMA IRQ Status */
3323 if (status & 0x40) /* DMA interrupt pending */
3324 switch (active_device)
3327 wake_up(&dram_sleeper);
3330 case GUS_DEV_PCM_CONTINUE: /* Left channel data transferred */
3331 gus_write8(0x41, 0); /* Disable GF1 DMA */
3332 gus_transfer_output_block(pcm_current_dev, pcm_current_buf,
3334 pcm_current_intrflag, 1);
3337 case GUS_DEV_PCM_DONE: /* Right or mono channel data transferred */
3338 gus_write8(0x41, 0); /* Disable GF1 DMA */
3339 if (pcm_qlen < pcm_nblk)
3344 if (audio_devs[gus_devnum]->dmap_out->qlen > 0)
3345 DMAbuf_outputintr(gus_devnum, 0);
3353 status = gus_look8(0x49); /*
3354 * Get Sampling IRQ Status
3356 if (status & 0x40) /*
3357 * Sampling Irq pending
3360 DMAbuf_inputintr(gus_devnum);
3368 static volatile int select_addr, data_addr;
3369 static volatile int curr_timer;
3371 void gus_timer_command(unsigned int addr, unsigned int val)
3375 outb(((unsigned char) (addr & 0xff)), select_addr);
3377 for (i = 0; i < 2; i++)
3380 outb(((unsigned char) (val & 0xff)), data_addr);
3382 for (i = 0; i < 2; i++)
3386 static void arm_timer(int timer, unsigned int interval)
3392 gus_write8(0x46, 256 - interval); /* Set counter for timer 1 */
3393 gus_write8(0x45, 0x04); /* Enable timer 1 IRQ */
3394 gus_timer_command(0x04, 0x01); /* Start timer 1 */
3398 gus_write8(0x47, 256 - interval); /* Set counter for timer 2 */
3399 gus_write8(0x45, 0x08); /* Enable timer 2 IRQ */
3400 gus_timer_command(0x04, 0x02); /* Start timer 2 */
3403 gus_timer_enabled = 1;
3406 static unsigned int gus_tmr_start(int dev, unsigned int usecs_per_tick)
3408 int timer_no, resolution;
3411 if (usecs_per_tick > (256 * 80))
3414 resolution = 320; /* usec */
3419 resolution = 80; /* usec */
3421 divisor = (usecs_per_tick + (resolution / 2)) / resolution;
3422 arm_timer(timer_no, divisor);
3424 return divisor * resolution;
3427 static void gus_tmr_disable(int dev)
3429 gus_write8(0x45, 0); /* Disable both timers */
3430 gus_timer_enabled = 0;
3433 static void gus_tmr_restart(int dev)
3435 if (curr_timer == 1)
3436 gus_write8(0x45, 0x04); /* Start timer 1 again */
3438 gus_write8(0x45, 0x08); /* Start timer 2 again */
3439 gus_timer_enabled = 1;
3442 static struct sound_lowlev_timer gus_tmr =
3451 static void gus_tmr_install(int io_base)
3453 struct sound_lowlev_timer *tmr;
3455 select_addr = io_base;
3456 data_addr = io_base + 1;
3460 #ifdef THIS_GETS_FIXED
3461 sound_timer_init(&gus_tmr, "GUS");