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/config.h>
22 #include <linux/spinlock.h>
24 #define GUSPNP_AUTODETECT
26 #include "sound_config.h"
27 #include <linux/ultrasound.h>
32 #define GUS_BANK_SIZE (((iw_mode) ? 256*1024*1024 : 256*1024))
34 #define MAX_SAMPLE 150
37 #define NOT_SAMPLE 0xffff
41 unsigned long orig_freq;
42 unsigned long current_freq;
49 unsigned int initial_volume;
50 unsigned int current_volume;
51 int loop_irq_mode, loop_irq_parm;
52 #define LMODE_FINISH 1
54 #define LMODE_PCM_STOP 3
55 int volume_irq_mode, volume_irq_parm;
57 #define VMODE_ENVELOPE 2
58 #define VMODE_START_NOTE 3
61 unsigned char env_rate[6];
62 unsigned char env_offset[6];
65 * Volume computation parameters for gus_adagio_vol()
67 int main_vol, expression_vol, patch_vol;
69 /* Variables for "Ultraclick" removal */
70 int dev_pending, note_pending, volume_pending,
77 static struct voice_alloc_info *voice_alloc;
78 static struct address_info *gus_hw_config;
80 extern int gus_irq, gus_dma;
81 extern int gus_pnp_flag;
82 extern int gus_no_wave_dma;
83 static int gus_dma2 = -1;
84 static int dual_dma_mode;
85 static long gus_mem_size;
86 static long free_mem_ptr;
88 static int gus_no_dma;
90 static int gus_devnum;
91 static int volume_base, volume_scale, volume_method;
92 static int gus_recmask = SOUND_MASK_MIC;
93 static int recording_active;
94 static int only_read_access;
95 static int only_8_bits;
98 int gus_wave_volume = 60;
99 int gus_pcm_volume = 80;
100 int have_gus_max = 0;
101 static int gus_line_vol = 100, gus_mic_vol;
102 static unsigned char mix_image = 0x00;
104 int gus_timer_enabled = 0;
107 * Current version of this driver doesn't allow synth and PCM functions
108 * at the same time. The active_device specifies the active driver
111 static int active_device;
113 #define GUS_DEV_WAVE 1 /* Wave table synth */
114 #define GUS_DEV_PCM_DONE 2 /* PCM device, transfer done */
115 #define GUS_DEV_PCM_CONTINUE 3 /* PCM device, transfer done ch. 1/2 */
117 static int gus_audio_speed;
118 static int gus_audio_channels;
119 static int gus_audio_bits;
120 static int gus_audio_bsize;
121 static char bounce_buf[8 * 1024]; /* Must match value set to max_fragment */
123 static DECLARE_WAIT_QUEUE_HEAD(dram_sleeper);
126 * Variables and buffers for PCM output
129 #define MAX_PCM_BUFFERS (128*MAX_REALTIME_FACTOR) /* Don't change */
131 static int pcm_bsize, pcm_nblk, pcm_banksize;
132 static int pcm_datasize[MAX_PCM_BUFFERS];
133 static volatile int pcm_head, pcm_tail, pcm_qlen;
134 static volatile int pcm_active;
135 static volatile int dma_active;
136 static int pcm_opened;
137 static int pcm_current_dev;
138 static int pcm_current_block;
139 static unsigned long pcm_current_buf;
140 static int pcm_current_count;
141 static int pcm_current_intrflag;
142 spinlock_t gus_lock=SPIN_LOCK_UNLOCKED;
146 static struct voice_info voices[32];
148 static int freq_div_table[] =
171 static struct patch_info *samples;
172 static long sample_ptrs[MAX_SAMPLE + 1];
173 static int sample_map[32];
174 static int free_sample;
175 static int mixer_type;
178 static int patch_table[MAX_PATCH];
179 static int patch_map[32];
181 static struct synth_info gus_info = {
182 "Gravis UltraSound", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_GUS,
186 static void gus_poke(long addr, unsigned char data);
187 static void compute_and_set_volume(int voice, int volume, int ramp_time);
188 extern unsigned short gus_adagio_vol(int vel, int mainv, int xpn, int voicev);
189 extern unsigned short gus_linear_vol(int vol, int mainvol);
190 static void compute_volume(int voice, int volume);
191 static void do_volume_irq(int voice);
192 static void set_input_volumes(void);
193 static void gus_tmr_install(int io_base);
195 #define INSTANT_RAMP -1 /* Instant change. No ramping */
196 #define FAST_RAMP 0 /* Fastest possible ramp */
198 static void reset_sample_memory(void)
202 for (i = 0; i <= MAX_SAMPLE; i++)
204 for (i = 0; i < 32; i++)
206 for (i = 0; i < 32; i++)
209 gus_poke(0, 0); /* Put a silent sample to the beginning */
215 for (i = 0; i < MAX_PATCH; i++)
216 patch_table[i] = NOT_SAMPLE;
223 for (i = 0; i < 7; i++)
227 static void gus_poke(long addr, unsigned char data)
228 { /* Writes a byte to the DRAM */
229 outb((0x43), u_Command);
230 outb((addr & 0xff), u_DataLo);
231 outb(((addr >> 8) & 0xff), u_DataHi);
233 outb((0x44), u_Command);
234 outb(((addr >> 16) & 0xff), u_DataHi);
235 outb((data), u_DRAMIO);
238 static unsigned char gus_peek(long addr)
239 { /* Reads a byte from the DRAM */
242 outb((0x43), u_Command);
243 outb((addr & 0xff), u_DataLo);
244 outb(((addr >> 8) & 0xff), u_DataHi);
246 outb((0x44), u_Command);
247 outb(((addr >> 16) & 0xff), u_DataHi);
253 void gus_write8(int reg, unsigned int data)
254 { /* Writes to an indirect register (8 bit) */
255 outb((reg), u_Command);
256 outb(((unsigned char) (data & 0xff)), u_DataHi);
259 static unsigned char gus_read8(int reg)
261 /* Reads from an indirect register (8 bit). Offset 0x80. */
264 outb((reg | 0x80), u_Command);
270 static unsigned char gus_look8(int reg)
272 /* Reads from an indirect register (8 bit). No additional offset. */
275 outb((reg), u_Command);
281 static void gus_write16(int reg, unsigned int data)
283 /* Writes to an indirect register (16 bit) */
284 outb((reg), u_Command);
286 outb(((unsigned char) (data & 0xff)), u_DataLo);
287 outb(((unsigned char) ((data >> 8) & 0xff)), u_DataHi);
290 static unsigned short gus_read16(int reg)
292 /* Reads from an indirect register (16 bit). Offset 0x80. */
293 unsigned char hi, lo;
295 outb((reg | 0x80), u_Command);
300 return ((hi << 8) & 0xff00) | lo;
303 static unsigned short gus_look16(int reg)
305 /* Reads from an indirect register (16 bit). No additional offset. */
306 unsigned char hi, lo;
308 outb((reg), u_Command);
313 return ((hi << 8) & 0xff00) | lo;
316 static void gus_write_addr(int reg, unsigned long address, int frac, int is16bit)
318 /* Writes an 24 bit memory address */
319 unsigned long hold_address;
325 /* Interwave spesific address translations */
331 * Special processing required for 16 bit patches
334 hold_address = address;
335 address = address >> 1;
336 address &= 0x0001ffffL;
337 address |= (hold_address & 0x000c0000L);
340 gus_write16(reg, (unsigned short) ((address >> 7) & 0xffff));
341 gus_write16(reg + 1, (unsigned short) ((address << 9) & 0xffff)
343 /* Could writing twice fix problems with GUS_VOICE_POS()? Let's try. */
345 gus_write16(reg, (unsigned short) ((address >> 7) & 0xffff));
346 gus_write16(reg + 1, (unsigned short) ((address << 9) & 0xffff)
350 static void gus_select_voice(int voice)
352 if (voice < 0 || voice > 31)
354 outb((voice), u_Voice);
357 static void gus_select_max_voices(int nvoices)
366 voice_alloc->max_voice = nr_voices = nvoices;
367 gus_write8(0x0e, (nvoices - 1) | 0xc0);
370 static void gus_voice_on(unsigned int mode)
372 gus_write8(0x00, (unsigned char) (mode & 0xfc));
374 gus_write8(0x00, (unsigned char) (mode & 0xfc));
377 static void gus_voice_off(void)
379 gus_write8(0x00, gus_read8(0x00) | 0x03);
382 static void gus_voice_mode(unsigned int m)
384 unsigned char mode = (unsigned char) (m & 0xff);
386 gus_write8(0x00, (gus_read8(0x00) & 0x03) |
387 (mode & 0xfc)); /* Don't touch last two bits */
389 gus_write8(0x00, (gus_read8(0x00) & 0x03) | (mode & 0xfc));
392 static void gus_voice_freq(unsigned long freq)
394 unsigned long divisor = freq_div_table[nr_voices - 14];
397 /* Interwave plays at 44100 Hz with any number of voices */
399 fc = (unsigned short) (((freq << 9) + (44100 >> 1)) / 44100);
401 fc = (unsigned short) (((freq << 9) + (divisor >> 1)) / divisor);
404 gus_write16(0x01, fc);
407 static void gus_voice_volume(unsigned int vol)
409 gus_write8(0x0d, 0x03); /* Stop ramp before setting volume */
410 gus_write16(0x09, (unsigned short) (vol << 4));
413 static void gus_voice_balance(unsigned int balance)
415 gus_write8(0x0c, (unsigned char) (balance & 0xff));
418 static void gus_ramp_range(unsigned int low, unsigned int high)
420 gus_write8(0x07, (unsigned char) ((low >> 4) & 0xff));
421 gus_write8(0x08, (unsigned char) ((high >> 4) & 0xff));
424 static void gus_ramp_rate(unsigned int scale, unsigned int rate)
426 gus_write8(0x06, (unsigned char) (((scale & 0x03) << 6) | (rate & 0x3f)));
429 static void gus_rampon(unsigned int m)
431 unsigned char mode = (unsigned char) (m & 0xff);
433 gus_write8(0x0d, mode & 0xfc);
435 gus_write8(0x0d, mode & 0xfc);
438 static void gus_ramp_mode(unsigned int m)
440 unsigned char mode = (unsigned char) (m & 0xff);
442 gus_write8(0x0d, (gus_read8(0x0d) & 0x03) |
443 (mode & 0xfc)); /* Leave the last 2 bits alone */
445 gus_write8(0x0d, (gus_read8(0x0d) & 0x03) | (mode & 0xfc));
448 static void gus_rampoff(void)
450 gus_write8(0x0d, 0x03);
453 static void gus_set_voice_pos(int voice, long position)
457 if ((sample_no = sample_map[voice]) != -1) {
458 if (position < samples[sample_no].len) {
459 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
460 voices[voice].offset_pending = position;
462 gus_write_addr(0x0a, sample_ptrs[sample_no] + position, 0,
463 samples[sample_no].mode & WAVE_16_BITS);
468 static void gus_voice_init(int voice)
472 spin_lock_irqsave(&gus_lock,flags);
473 gus_select_voice(voice);
476 gus_write_addr(0x0a, 0, 0, 0); /* Set current position to 0 */
477 gus_write8(0x00, 0x03); /* Voice off */
478 gus_write8(0x0d, 0x03); /* Ramping off */
479 voice_alloc->map[voice] = 0;
480 voice_alloc->alloc_times[voice] = 0;
481 spin_unlock_irqrestore(&gus_lock,flags);
485 static void gus_voice_init2(int voice)
487 voices[voice].panning = 0;
488 voices[voice].mode = 0;
489 voices[voice].orig_freq = 20000;
490 voices[voice].current_freq = 20000;
491 voices[voice].bender = 0;
492 voices[voice].bender_range = 200;
493 voices[voice].initial_volume = 0;
494 voices[voice].current_volume = 0;
495 voices[voice].loop_irq_mode = 0;
496 voices[voice].loop_irq_parm = 0;
497 voices[voice].volume_irq_mode = 0;
498 voices[voice].volume_irq_parm = 0;
499 voices[voice].env_phase = 0;
500 voices[voice].main_vol = 127;
501 voices[voice].patch_vol = 127;
502 voices[voice].expression_vol = 127;
503 voices[voice].sample_pending = -1;
504 voices[voice].fixed_pitch = 0;
507 static void step_envelope(int voice)
509 unsigned vol, prev_vol, phase;
513 if (voices[voice].mode & WAVE_SUSTAIN_ON && voices[voice].env_phase == 2)
515 spin_lock_irqsave(&gus_lock,flags);
516 gus_select_voice(voice);
518 spin_unlock_irqrestore(&gus_lock,flags);
521 * Sustain phase begins. Continue envelope after receiving note off.
524 if (voices[voice].env_phase >= 5)
526 /* Envelope finished. Shoot the voice down */
527 gus_voice_init(voice);
530 prev_vol = voices[voice].current_volume;
531 phase = ++voices[voice].env_phase;
532 compute_volume(voice, voices[voice].midi_volume);
533 vol = voices[voice].initial_volume * voices[voice].env_offset[phase] / 255;
534 rate = voices[voice].env_rate[phase];
536 spin_lock_irqsave(&gus_lock,flags);
537 gus_select_voice(voice);
539 gus_voice_volume(prev_vol);
542 gus_write8(0x06, rate); /* Ramping rate */
544 voices[voice].volume_irq_mode = VMODE_ENVELOPE;
546 if (((vol - prev_vol) / 64) == 0) /* No significant volume change */
548 spin_unlock_irqrestore(&gus_lock,flags);
549 step_envelope(voice); /* Continue the envelope on the next step */
554 if (vol >= (4096 - 64))
556 gus_ramp_range(0, vol);
557 gus_rampon(0x20); /* Increasing volume, with IRQ */
563 gus_ramp_range(vol, 4030);
564 gus_rampon(0x60); /* Decreasing volume, with IRQ */
566 voices[voice].current_volume = vol;
567 spin_unlock_irqrestore(&gus_lock,flags);
570 static void init_envelope(int voice)
572 voices[voice].env_phase = -1;
573 voices[voice].current_volume = 64;
575 step_envelope(voice);
578 static void start_release(int voice)
580 if (gus_read8(0x00) & 0x03)
581 return; /* Voice already stopped */
583 voices[voice].env_phase = 2; /* Will be incremented by step_envelope */
585 voices[voice].current_volume = voices[voice].initial_volume =
586 gus_read16(0x09) >> 4; /* Get current volume */
588 voices[voice].mode &= ~WAVE_SUSTAIN_ON;
590 step_envelope(voice);
593 static void gus_voice_fade(int voice)
595 int instr_no = sample_map[voice], is16bits;
598 spin_lock_irqsave(&gus_lock,flags);
599 gus_select_voice(voice);
601 if (instr_no < 0 || instr_no > MAX_SAMPLE)
603 gus_write8(0x00, 0x03); /* Hard stop */
604 voice_alloc->map[voice] = 0;
605 spin_unlock_irqrestore(&gus_lock,flags);
608 is16bits = (samples[instr_no].mode & WAVE_16_BITS) ? 1 : 0; /* 8 or 16 bits */
610 if (voices[voice].mode & WAVE_ENVELOPES)
612 start_release(voice);
613 spin_unlock_irqrestore(&gus_lock,flags);
617 * Ramp the volume down but not too quickly.
619 if ((int) (gus_read16(0x09) >> 4) < 100) /* Get current volume */
623 gus_voice_init(voice);
624 spin_unlock_irqrestore(&gus_lock,flags);
627 gus_ramp_range(65, 4030);
629 gus_rampon(0x40 | 0x20); /* Down, once, with IRQ */
630 voices[voice].volume_irq_mode = VMODE_HALT;
631 spin_unlock_irqrestore(&gus_lock,flags);
634 static void gus_reset(void)
638 gus_select_max_voices(24);
641 volume_method = VOL_METHOD_ADAGIO;
643 for (i = 0; i < 32; i++)
645 gus_voice_init(i); /* Turn voice off */
650 static void gus_initialize(void)
653 unsigned char dma_image, irq_image, tmp;
655 static unsigned char gus_irq_map[16] = {
656 0, 0, 0, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7
659 static unsigned char gus_dma_map[8] = {
660 0, 1, 0, 2, 0, 3, 4, 5
663 spin_lock_irqsave(&gus_lock,flags);
664 gus_write8(0x4c, 0); /* Reset GF1 */
668 gus_write8(0x4c, 1); /* Release Reset */
673 * Clear all interrupts
676 gus_write8(0x41, 0); /* DMA control */
677 gus_write8(0x45, 0); /* Timer control */
678 gus_write8(0x49, 0); /* Sample control */
680 gus_select_max_voices(24);
682 inb(u_Status); /* Touch the status register */
684 gus_look8(0x41); /* Clear any pending DMA IRQs */
685 gus_look8(0x49); /* Clear any pending sample IRQs */
686 gus_read8(0x0f); /* Clear pending IRQs */
688 gus_reset(); /* Resets all voices */
690 gus_look8(0x41); /* Clear any pending DMA IRQs */
691 gus_look8(0x49); /* Clear any pending sample IRQs */
692 gus_read8(0x0f); /* Clear pending IRQs */
694 gus_write8(0x4c, 7); /* Master reset | DAC enable | IRQ enable */
697 * Set up for Digital ASIC
700 outb((0x05), gus_base + 0x0f);
702 mix_image |= 0x02; /* Disable line out (for a moment) */
703 outb((mix_image), u_Mixer);
705 outb((0x00), u_IRQDMAControl);
707 outb((0x00), gus_base + 0x0f);
710 * Now set up the DMA and IRQ interface
712 * The GUS supports two IRQs and two DMAs.
714 * Just one DMA channel is used. This prevents simultaneous ADC and DAC.
715 * Adding this support requires significant changes to the dmabuf.c, dsp.c
720 tmp = gus_irq_map[gus_irq];
721 if (!gus_pnp_flag && !tmp)
722 printk(KERN_WARNING "Warning! GUS IRQ not selected\n");
724 irq_image |= 0x40; /* Combine IRQ1 (GF1) and IRQ2 (Midi) */
727 if (gus_dma2 == gus_dma || gus_dma2 == -1)
730 dma_image = 0x40; /* Combine DMA1 (DRAM) and IRQ2 (ADC) */
732 tmp = gus_dma_map[gus_dma];
734 printk(KERN_WARNING "Warning! GUS DMA not selected\n");
740 /* Setup dual DMA channel mode for GUS MAX */
742 dma_image = gus_dma_map[gus_dma];
744 printk(KERN_WARNING "Warning! GUS DMA not selected\n");
746 tmp = gus_dma_map[gus_dma2] << 3;
749 printk(KERN_WARNING "Warning! Invalid GUS MAX DMA\n");
750 tmp = 0x40; /* Combine DMA channels */
757 * For some reason the IRQ and DMA addresses must be written twice
761 * Doing it first time
764 outb((mix_image), u_Mixer); /* Select DMA control */
765 outb((dma_image | 0x80), u_IRQDMAControl); /* Set DMA address */
767 outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
768 outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
771 * Doing it second time
774 outb((mix_image), u_Mixer); /* Select DMA control */
775 outb((dma_image), u_IRQDMAControl); /* Set DMA address */
777 outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
778 outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
780 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
782 mix_image &= ~0x02; /* Enable line out */
783 mix_image |= 0x08; /* Enable IRQ */
784 outb((mix_image), u_Mixer); /*
785 * Turn mixer channels on
786 * Note! Mic in is left off.
789 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
791 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
793 inb(u_Status); /* Touch the status register */
795 gus_look8(0x41); /* Clear any pending DMA IRQs */
796 gus_look8(0x49); /* Clear any pending sample IRQs */
798 gus_read8(0x0f); /* Clear pending IRQs */
801 gus_write8(0x19, gus_read8(0x19) | 0x01);
802 spin_unlock_irqrestore(&gus_lock,flags);
806 static void __init pnp_mem_init(void)
809 #define CHUNK_SIZE (256*1024)
810 #define BANK_SIZE (4*1024*1024)
811 #define CHUNKS_PER_BANK (BANK_SIZE/CHUNK_SIZE)
813 int bank, chunk, addr, total = 0;
815 int i, j, bits = -1, testbits = -1, nbanks = 0;
818 * This routine determines what kind of RAM is installed in each of the four
819 * SIMM banks and configures the DRAM address decode logic accordingly.
823 * Place the chip into enhanced mode
825 gus_write8(0x19, gus_read8(0x19) | 0x01);
826 gus_write8(0x53, gus_look8(0x53) & ~0x02); /* Select DRAM I/O access */
829 * Set memory configuration to 4 DRAM banks of 4M in each (16M total).
832 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | 0x000c);
835 * Perform the DRAM size detection for each bank individually.
837 for (bank = 0; bank < 4; bank++)
841 addr = bank * BANK_SIZE;
843 /* Clean check points of each chunk */
844 for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
846 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
847 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
850 /* Write a value to each chunk point and verify the result */
851 for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
853 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x55);
854 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0xAA);
856 if (gus_peek(addr + chunk * CHUNK_SIZE + 0L) == 0x55 &&
857 gus_peek(addr + chunk * CHUNK_SIZE + 1L) == 0xAA)
859 /* OK. There is RAM. Now check for possible shadows */
862 for (chunk2 = 0; ok && chunk2 < chunk; chunk2++)
863 if (gus_peek(addr + chunk2 * CHUNK_SIZE + 0L) ||
864 gus_peek(addr + chunk2 * CHUNK_SIZE + 1L))
865 ok = 0; /* Addressing wraps */
868 size = (chunk + 1) * CHUNK_SIZE;
870 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
871 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
873 bank_sizes[bank] = size;
876 DDB(printk("Interwave: Bank %d, size=%dk\n", bank, size / 1024));
879 if (nbanks == 0) /* No RAM - Give up */
881 printk(KERN_ERR "Sound: An Interwave audio chip detected but no DRAM\n");
882 printk(KERN_ERR "Sound: Unable to work with this card.\n");
883 gus_write8(0x19, gus_read8(0x19) & ~0x01);
889 * Now we know how much DRAM there is in each bank. The next step is
890 * to find a DRAM size encoding (0 to 12) which is best for the combination
893 * First try if any of the possible alternatives matches exactly the amount
897 for (i = 0; bits == -1 && i < 13; i++)
901 for (j = 0; bits != -1 && j < 4; j++)
902 if (mem_decode[i][j] != bank_sizes[j])
903 bits = -1; /* No hit */
907 * If necessary, try to find a combination where other than the last
908 * bank matches our configuration and the last bank is left oversized.
909 * In this way we don't leave holes in the middle of memory.
912 if (bits == -1) /* No luck yet */
914 for (i = 0; bits == -1 && i < 13; i++)
918 for (j = 0; bits != -1 && j < nbanks - 1; j++)
919 if (mem_decode[i][j] != bank_sizes[j])
920 bits = -1; /* No hit */
921 if (mem_decode[i][nbanks - 1] < bank_sizes[nbanks - 1])
922 bits = -1; /* The last bank is too small */
926 * The last resort is to search for a combination where the banks are
927 * smaller than the actual SIMMs. This leaves some memory in the banks
928 * unused but doesn't leave holes in the DRAM address space.
930 if (bits == -1) /* No luck yet */
932 for (i = 0; i < 13; i++)
935 for (j = 0; testbits != -1 && j < nbanks - 1; j++)
936 if (mem_decode[i][j] > bank_sizes[j]) {
939 if(testbits > bits) bits = testbits;
943 printk(KERN_INFO "Interwave: Can't use all installed RAM.\n");
944 printk(KERN_INFO "Interwave: Try reordering SIMMS.\n");
946 printk(KERN_INFO "Interwave: Can't find working DRAM encoding.\n");
947 printk(KERN_INFO "Interwave: Defaulting to 256k. Try reordering SIMMS.\n");
950 DDB(printk("Interwave: Selecting DRAM addressing mode %d\n", bits));
952 for (bank = 0; bank < 4; bank++)
954 DDB(printk(" Bank %d, mem=%dk (limit %dk)\n", bank, bank_sizes[bank] / 1024, mem_decode[bits][bank] / 1024));
956 if (bank_sizes[bank] > mem_decode[bits][bank])
957 total += mem_decode[bits][bank];
959 total += bank_sizes[bank];
962 DDB(printk("Total %dk of DRAM (enhanced mode)\n", total / 1024));
965 * Set the memory addressing mode.
967 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | bits);
969 /* Leave the chip into enhanced mode. Disable LFO */
970 gus_mem_size = total;
972 gus_write8(0x19, (gus_read8(0x19) | 0x01) & ~0x02);
975 int __init gus_wave_detect(int baseaddr)
977 unsigned long i, max_mem = 1024L;
983 gus_write8(0x4c, 0); /* Reset GF1 */
987 gus_write8(0x4c, 1); /* Release Reset */
991 #ifdef GUSPNP_AUTODETECT
992 val = gus_look8(0x5b); /* Version number register */
993 gus_write8(0x5b, ~val); /* Invert all bits */
995 if ((gus_look8(0x5b) & 0xf0) == (val & 0xf0)) /* No change */
997 if ((gus_look8(0x5b) & 0x0f) == ((~val) & 0x0f)) /* Change */
999 DDB(printk("Interwave chip version %d detected\n", (val & 0xf0) >> 4));
1004 DDB(printk("Not an Interwave chip (%x)\n", gus_look8(0x5b)));
1008 gus_write8(0x5b, val); /* Restore all bits */
1016 /* See if there is first block there.... */
1018 if (gus_peek(0L) != 0xaa)
1021 /* Now zero it out so that I can check for mirroring .. */
1023 for (i = 1L; i < max_mem; i++)
1027 /* check for mirroring ... */
1028 if (gus_peek(0L) != 0)
1032 for (n = loc - 1, failed = 0; n <= loc; n++)
1034 gus_poke(loc, 0xaa);
1035 if (gus_peek(loc) != 0xaa)
1037 gus_poke(loc, 0x55);
1038 if (gus_peek(loc) != 0x55)
1044 gus_mem_size = i << 10;
1048 static int guswave_ioctl(int dev, unsigned int cmd, void __user *arg)
1053 case SNDCTL_SYNTH_INFO:
1054 gus_info.nr_voices = nr_voices;
1055 if (copy_to_user(arg, &gus_info, sizeof(gus_info)))
1059 case SNDCTL_SEQ_RESETSAMPLES:
1060 reset_sample_memory();
1063 case SNDCTL_SEQ_PERCMODE:
1066 case SNDCTL_SYNTH_MEMAVL:
1067 return (gus_mem_size == 0) ? 0 : gus_mem_size - free_mem_ptr - 32;
1074 static int guswave_set_instr(int dev, int voice, int instr_no)
1078 if (instr_no < 0 || instr_no > MAX_PATCH)
1079 instr_no = 0; /* Default to acoustic piano */
1081 if (voice < 0 || voice > 31)
1084 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1086 voices[voice].sample_pending = instr_no;
1089 sample_no = patch_table[instr_no];
1090 patch_map[voice] = -1;
1092 if (sample_no == NOT_SAMPLE)
1094 /* printk("GUS: Undefined patch %d for voice %d\n", instr_no, voice);*/
1095 return -EINVAL; /* Patch not defined */
1097 if (sample_ptrs[sample_no] == -1) /* Sample not loaded */
1099 /* printk("GUS: Sample #%d not loaded for patch %d (voice %d)\n", sample_no, instr_no, voice);*/
1102 sample_map[voice] = sample_no;
1103 patch_map[voice] = instr_no;
1107 static int guswave_kill_note(int dev, int voice, int note, int velocity)
1109 unsigned long flags;
1111 spin_lock_irqsave(&gus_lock,flags);
1112 /* voice_alloc->map[voice] = 0xffff; */
1113 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1115 voices[voice].kill_pending = 1;
1116 spin_unlock_irqrestore(&gus_lock,flags);
1120 spin_unlock_irqrestore(&gus_lock,flags);
1121 gus_voice_fade(voice);
1127 static void guswave_aftertouch(int dev, int voice, int pressure)
1131 static void guswave_panning(int dev, int voice, int value)
1133 if (voice >= 0 || voice < 32)
1134 voices[voice].panning = value;
1137 static void guswave_volume_method(int dev, int mode)
1139 if (mode == VOL_METHOD_LINEAR || mode == VOL_METHOD_ADAGIO)
1140 volume_method = mode;
1143 static void compute_volume(int voice, int volume)
1146 voices[voice].midi_volume = volume;
1148 switch (volume_method)
1150 case VOL_METHOD_ADAGIO:
1151 voices[voice].initial_volume =
1152 gus_adagio_vol(voices[voice].midi_volume, voices[voice].main_vol,
1153 voices[voice].expression_vol,
1154 voices[voice].patch_vol);
1157 case VOL_METHOD_LINEAR: /* Totally ignores patch-volume and expression */
1158 voices[voice].initial_volume = gus_linear_vol(volume, voices[voice].main_vol);
1162 voices[voice].initial_volume = volume_base +
1163 (voices[voice].midi_volume * volume_scale);
1166 if (voices[voice].initial_volume > 4030)
1167 voices[voice].initial_volume = 4030;
1170 static void compute_and_set_volume(int voice, int volume, int ramp_time)
1172 int curr, target, rate;
1173 unsigned long flags;
1175 compute_volume(voice, volume);
1176 voices[voice].current_volume = voices[voice].initial_volume;
1178 spin_lock_irqsave(&gus_lock,flags);
1180 * CAUTION! Interrupts disabled. Enable them before returning
1183 gus_select_voice(voice);
1185 curr = gus_read16(0x09) >> 4;
1186 target = voices[voice].initial_volume;
1188 if (ramp_time == INSTANT_RAMP)
1191 gus_voice_volume(target);
1192 spin_unlock_irqrestore(&gus_lock,flags);
1195 if (ramp_time == FAST_RAMP)
1199 gus_ramp_rate(0, rate);
1201 if ((target - curr) / 64 == 0) /* Close enough to target. */
1204 gus_voice_volume(target);
1205 spin_unlock_irqrestore(&gus_lock,flags);
1210 if (target > (4095 - 65))
1212 gus_ramp_range(curr, target);
1213 gus_rampon(0x00); /* Ramp up, once, no IRQ */
1220 gus_ramp_range(target, curr);
1221 gus_rampon(0x40); /* Ramp down, once, no irq */
1223 spin_unlock_irqrestore(&gus_lock,flags);
1226 static void dynamic_volume_change(int voice)
1228 unsigned char status;
1229 unsigned long flags;
1231 spin_lock_irqsave(&gus_lock,flags);
1232 gus_select_voice(voice);
1233 status = gus_read8(0x00); /* Get voice status */
1234 spin_unlock_irqrestore(&gus_lock,flags);
1237 return; /* Voice was not running */
1239 if (!(voices[voice].mode & WAVE_ENVELOPES))
1241 compute_and_set_volume(voice, voices[voice].midi_volume, 1);
1246 * Voice is running and has envelopes.
1249 spin_lock_irqsave(&gus_lock,flags);
1250 gus_select_voice(voice);
1251 status = gus_read8(0x0d); /* Ramping status */
1252 spin_unlock_irqrestore(&gus_lock,flags);
1254 if (status & 0x03) /* Sustain phase? */
1256 compute_and_set_volume(voice, voices[voice].midi_volume, 1);
1259 if (voices[voice].env_phase < 0)
1262 compute_volume(voice, voices[voice].midi_volume);
1266 static void guswave_controller(int dev, int voice, int ctrl_num, int value)
1268 unsigned long flags;
1271 if (voice < 0 || voice > 31)
1276 case CTRL_PITCH_BENDER:
1277 voices[voice].bender = value;
1279 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1281 freq = compute_finetune(voices[voice].orig_freq, value, voices[voice].bender_range, 0);
1282 voices[voice].current_freq = freq;
1284 spin_lock_irqsave(&gus_lock,flags);
1285 gus_select_voice(voice);
1286 gus_voice_freq(freq);
1287 spin_unlock_irqrestore(&gus_lock,flags);
1291 case CTRL_PITCH_BENDER_RANGE:
1292 voices[voice].bender_range = value;
1294 case CTL_EXPRESSION:
1296 case CTRL_EXPRESSION:
1297 if (volume_method == VOL_METHOD_ADAGIO)
1299 voices[voice].expression_vol = value;
1300 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1301 dynamic_volume_change(voice);
1306 voices[voice].panning = (value * 2) - 128;
1309 case CTL_MAIN_VOLUME:
1310 value = (value * 100) / 16383;
1312 case CTRL_MAIN_VOLUME:
1313 voices[voice].main_vol = value;
1314 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1315 dynamic_volume_change(voice);
1323 static int guswave_start_note2(int dev, int voice, int note_num, int volume)
1325 int sample, best_sample, best_delta, delta_freq;
1326 int is16bits, samplep, patch, pan;
1327 unsigned long note_freq, base_note, freq, flags;
1328 unsigned char mode = 0;
1330 if (voice < 0 || voice > 31)
1332 /* printk("GUS: Invalid voice\n");*/
1335 if (note_num == 255)
1337 if (voices[voice].mode & WAVE_ENVELOPES)
1339 voices[voice].midi_volume = volume;
1340 dynamic_volume_change(voice);
1343 compute_and_set_volume(voice, volume, 1);
1346 if ((patch = patch_map[voice]) == -1)
1348 if ((samplep = patch_table[patch]) == NOT_SAMPLE)
1352 note_freq = note_to_freq(note_num);
1355 * Find a sample within a patch so that the note_freq is between low_note
1360 best_sample = samplep;
1361 best_delta = 1000000;
1362 while (samplep != 0 && samplep != NOT_SAMPLE && sample == -1)
1364 delta_freq = note_freq - samples[samplep].base_note;
1366 delta_freq = -delta_freq;
1367 if (delta_freq < best_delta)
1369 best_sample = samplep;
1370 best_delta = delta_freq;
1372 if (samples[samplep].low_note <= note_freq &&
1373 note_freq <= samples[samplep].high_note)
1378 samplep = samples[samplep].key; /* Link to next sample */
1381 sample = best_sample;
1385 /* printk("GUS: Patch %d not defined for note %d\n", patch, note_num);*/
1386 return 0; /* Should play default patch ??? */
1388 is16bits = (samples[sample].mode & WAVE_16_BITS) ? 1 : 0;
1389 voices[voice].mode = samples[sample].mode;
1390 voices[voice].patch_vol = samples[sample].volume;
1393 gus_write8(0x15, 0x00); /* RAM, Reset voice deactivate bit of SMSI */
1395 if (voices[voice].mode & WAVE_ENVELOPES)
1399 for (i = 0; i < 6; i++)
1401 voices[voice].env_rate[i] = samples[sample].env_rate[i];
1402 voices[voice].env_offset[i] = samples[sample].env_offset[i];
1405 sample_map[voice] = sample;
1407 if (voices[voice].fixed_pitch) /* Fixed pitch */
1409 freq = samples[sample].base_freq;
1413 base_note = samples[sample].base_note / 100;
1416 freq = samples[sample].base_freq * note_freq / base_note;
1419 voices[voice].orig_freq = freq;
1422 * Since the pitch bender may have been set before playing the note, we
1423 * have to calculate the bending now.
1426 freq = compute_finetune(voices[voice].orig_freq, voices[voice].bender,
1427 voices[voice].bender_range, 0);
1428 voices[voice].current_freq = freq;
1430 pan = (samples[sample].panning + voices[voice].panning) / 32;
1437 if (samples[sample].mode & WAVE_16_BITS)
1439 mode |= 0x04; /* 16 bits */
1440 if ((sample_ptrs[sample] / GUS_BANK_SIZE) !=
1441 ((sample_ptrs[sample] + samples[sample].len) / GUS_BANK_SIZE))
1442 printk(KERN_ERR "GUS: Sample address error\n");
1444 spin_lock_irqsave(&gus_lock,flags);
1445 gus_select_voice(voice);
1449 spin_unlock_irqrestore(&gus_lock,flags);
1451 if (voices[voice].mode & WAVE_ENVELOPES)
1453 compute_volume(voice, volume);
1454 init_envelope(voice);
1458 compute_and_set_volume(voice, volume, 0);
1461 spin_lock_irqsave(&gus_lock,flags);
1462 gus_select_voice(voice);
1464 if (samples[sample].mode & WAVE_LOOP_BACK)
1465 gus_write_addr(0x0a, sample_ptrs[sample] + samples[sample].len -
1466 voices[voice].offset_pending, 0, is16bits); /* start=end */
1468 gus_write_addr(0x0a, sample_ptrs[sample] + voices[voice].offset_pending, 0, is16bits); /* Sample start=begin */
1470 if (samples[sample].mode & WAVE_LOOPING)
1474 if (samples[sample].mode & WAVE_BIDIR_LOOP)
1477 if (samples[sample].mode & WAVE_LOOP_BACK)
1479 gus_write_addr(0x0a, sample_ptrs[sample] + samples[sample].loop_end -
1480 voices[voice].offset_pending,
1481 (samples[sample].fractions >> 4) & 0x0f, is16bits);
1484 gus_write_addr(0x02, sample_ptrs[sample] + samples[sample].loop_start,
1485 samples[sample].fractions & 0x0f, is16bits); /* Loop start location */
1486 gus_write_addr(0x04, sample_ptrs[sample] + samples[sample].loop_end,
1487 (samples[sample].fractions >> 4) & 0x0f, is16bits); /* Loop end location */
1491 mode |= 0x20; /* Loop IRQ at the end */
1492 voices[voice].loop_irq_mode = LMODE_FINISH; /* Ramp down at the end */
1493 voices[voice].loop_irq_parm = 1;
1494 gus_write_addr(0x02, sample_ptrs[sample], 0, is16bits); /* Loop start location */
1495 gus_write_addr(0x04, sample_ptrs[sample] + samples[sample].len - 1,
1496 (samples[sample].fractions >> 4) & 0x0f, is16bits); /* Loop end location */
1498 gus_voice_freq(freq);
1499 gus_voice_balance(pan);
1501 spin_unlock_irqrestore(&gus_lock,flags);
1507 * New guswave_start_note by Andrew J. Robinson attempts to minimize clicking
1508 * when the note playing on the voice is changed. It uses volume
1512 static int guswave_start_note(int dev, int voice, int note_num, int volume)
1514 unsigned long flags;
1518 spin_lock_irqsave(&gus_lock,flags);
1519 if (note_num == 255)
1521 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1523 voices[voice].volume_pending = volume;
1527 ret_val = guswave_start_note2(dev, voice, note_num, volume);
1532 gus_select_voice(voice);
1533 mode = gus_read8(0x00);
1535 gus_write8(0x00, mode & 0xdf); /* No interrupt! */
1537 voices[voice].offset_pending = 0;
1538 voices[voice].kill_pending = 0;
1539 voices[voice].volume_irq_mode = 0;
1540 voices[voice].loop_irq_mode = 0;
1542 if (voices[voice].sample_pending >= 0)
1544 spin_unlock_irqrestore(&gus_lock,flags); /* Run temporarily with interrupts enabled */
1545 guswave_set_instr(voices[voice].dev_pending, voice, voices[voice].sample_pending);
1546 voices[voice].sample_pending = -1;
1547 spin_lock_irqsave(&gus_lock,flags);
1548 gus_select_voice(voice); /* Reselect the voice (just to be sure) */
1550 if ((mode & 0x01) || (int) ((gus_read16(0x09) >> 4) < (unsigned) 2065))
1552 ret_val = guswave_start_note2(dev, voice, note_num, volume);
1556 voices[voice].dev_pending = dev;
1557 voices[voice].note_pending = note_num;
1558 voices[voice].volume_pending = volume;
1559 voices[voice].volume_irq_mode = VMODE_START_NOTE;
1562 gus_ramp_range(2000, 4065);
1563 gus_ramp_rate(0, 63); /* Fastest possible rate */
1564 gus_rampon(0x20 | 0x40); /* Ramp down, once, irq */
1567 spin_unlock_irqrestore(&gus_lock,flags);
1571 static void guswave_reset(int dev)
1575 for (i = 0; i < 32; i++)
1582 static int guswave_open(int dev, int mode)
1589 voice_alloc->timestamp = 0;
1591 if (gus_no_wave_dma) {
1594 if ((err = DMAbuf_open_dma(gus_devnum)) < 0)
1596 /* printk( "GUS: Loading samples without DMA\n"); */
1597 gus_no_dma = 1; /* Upload samples using PIO */
1603 init_waitqueue_head(&dram_sleeper);
1605 active_device = GUS_DEV_WAVE;
1607 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
1610 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
1615 static void guswave_close(int dev)
1622 DMAbuf_close_dma(gus_devnum);
1625 static int guswave_load_patch(int dev, int format, const char __user *addr,
1626 int offs, int count, int pmgr_flag)
1628 struct patch_info patch;
1632 unsigned long blk_sz, blk_end, left, src_offs, target;
1634 sizeof_patch = (long) &patch.data[0] - (long) &patch; /* Header size */
1636 if (format != GUS_PATCH)
1638 /* printk("GUS Error: Invalid patch format (key) 0x%x\n", format);*/
1641 if (count < sizeof_patch)
1643 /* printk("GUS Error: Patch header too short\n");*/
1646 count -= sizeof_patch;
1648 if (free_sample >= MAX_SAMPLE)
1650 /* printk("GUS: Sample table full\n");*/
1654 * Copy the header from user space but ignore the first bytes which have
1655 * been transferred already.
1658 if (copy_from_user(&((char *) &patch)[offs], &(addr)[offs],
1659 sizeof_patch - offs))
1662 if (patch.mode & WAVE_ROM)
1664 if (gus_mem_size == 0)
1667 instr = patch.instr_no;
1669 if (instr < 0 || instr > MAX_PATCH)
1671 /* printk(KERN_ERR "GUS: Invalid patch number %d\n", instr);*/
1674 if (count < patch.len)
1676 /* printk(KERN_ERR "GUS Warning: Patch record too short (%d<%d)\n", count, (int) patch.len);*/
1679 if (patch.len <= 0 || patch.len > gus_mem_size)
1681 /* printk(KERN_ERR "GUS: Invalid sample length %d\n", (int) patch.len);*/
1684 if (patch.mode & WAVE_LOOPING)
1686 if (patch.loop_start < 0 || patch.loop_start >= patch.len)
1688 /* printk(KERN_ERR "GUS: Invalid loop start\n");*/
1691 if (patch.loop_end < patch.loop_start || patch.loop_end > patch.len)
1693 /* printk(KERN_ERR "GUS: Invalid loop end\n");*/
1697 free_mem_ptr = (free_mem_ptr + 31) & ~31; /* 32 byte alignment */
1699 if (patch.mode & WAVE_16_BITS)
1702 * 16 bit samples must fit one 256k bank.
1704 if (patch.len >= GUS_BANK_SIZE)
1706 /* printk("GUS: Sample (16 bit) too long %d\n", (int) patch.len);*/
1709 if ((free_mem_ptr / GUS_BANK_SIZE) !=
1710 ((free_mem_ptr + patch.len) / GUS_BANK_SIZE))
1712 unsigned long tmp_mem =
1714 ((free_mem_ptr / GUS_BANK_SIZE) + 1) * GUS_BANK_SIZE;
1716 if ((tmp_mem + patch.len) > gus_mem_size)
1719 free_mem_ptr = tmp_mem; /* This leaves unusable memory */
1722 if ((free_mem_ptr + patch.len) > gus_mem_size)
1725 sample_ptrs[free_sample] = free_mem_ptr;
1728 * Tremolo is not possible with envelopes
1731 if (patch.mode & WAVE_ENVELOPES)
1732 patch.mode &= ~WAVE_TREMOLO;
1734 if (!(patch.mode & WAVE_FRACTIONS))
1736 patch.fractions = 0;
1738 memcpy((char *) &samples[free_sample], &patch, sizeof_patch);
1741 * Link this_one sample to the list of samples for patch 'instr'.
1744 samples[free_sample].key = patch_table[instr];
1745 patch_table[instr] = free_sample;
1748 * Use DMA to transfer the wave data to the DRAM
1753 target = free_mem_ptr;
1755 while (left) /* Not completely transferred yet */
1757 blk_sz = audio_devs[gus_devnum]->dmap_out->bytes_in_use;
1762 * DMA cannot cross bank (256k) boundaries. Check for that.
1765 blk_end = target + blk_sz;
1767 if ((target / GUS_BANK_SIZE) != (blk_end / GUS_BANK_SIZE))
1769 /* Split the block */
1770 blk_end &= ~(GUS_BANK_SIZE - 1);
1771 blk_sz = blk_end - target;
1776 * For some reason the DMA is not possible. We have to use PIO.
1781 for (i = 0; i < blk_sz; i++)
1783 get_user(*(unsigned char *) &data, (unsigned char __user *) &((addr)[sizeof_patch + i]));
1784 if (patch.mode & WAVE_UNSIGNED)
1785 if (!(patch.mode & WAVE_16_BITS) || (i & 0x01))
1786 data ^= 0x80; /* Convert to signed */
1787 gus_poke(target + i, data);
1792 unsigned long address, hold_address;
1793 unsigned char dma_command;
1794 unsigned long flags;
1796 if (audio_devs[gus_devnum]->dmap_out->raw_buf == NULL)
1798 printk(KERN_ERR "GUS: DMA buffer == NULL\n");
1802 * OK, move now. First in and then out.
1805 if (copy_from_user(audio_devs[gus_devnum]->dmap_out->raw_buf,
1806 &(addr)[sizeof_patch + src_offs],
1810 spin_lock_irqsave(&gus_lock,flags);
1811 gus_write8(0x41, 0); /* Disable GF1 DMA */
1812 DMAbuf_start_dma(gus_devnum, audio_devs[gus_devnum]->dmap_out->raw_buf_phys,
1813 blk_sz, DMA_MODE_WRITE);
1816 * Set the DRAM address for the wave data
1821 /* Different address translation in enhanced mode */
1826 address = target >> 1; /* Convert to 16 bit word address */
1830 hi = (unsigned char) ((address >> 16) & 0xf0);
1831 hi += (unsigned char) (address & 0x0f);
1833 gus_write16(0x42, (address >> 4) & 0xffff); /* DMA address (low) */
1834 gus_write8(0x50, hi);
1839 if (audio_devs[gus_devnum]->dmap_out->dma > 3)
1841 hold_address = address;
1842 address = address >> 1;
1843 address &= 0x0001ffffL;
1844 address |= (hold_address & 0x000c0000L);
1846 gus_write16(0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
1850 * Start the DMA transfer
1853 dma_command = 0x21; /* IRQ enable, DMA start */
1854 if (patch.mode & WAVE_UNSIGNED)
1855 dma_command |= 0x80; /* Invert MSB */
1856 if (patch.mode & WAVE_16_BITS)
1857 dma_command |= 0x40; /* 16 bit _DATA_ */
1858 if (audio_devs[gus_devnum]->dmap_out->dma > 3)
1859 dma_command |= 0x04; /* 16 bit DMA _channel_ */
1862 * Sleep here until the DRAM DMA done interrupt is served
1864 active_device = GUS_DEV_WAVE;
1865 gus_write8(0x41, dma_command); /* Lets go luteet (=bugs) */
1867 spin_unlock_irqrestore(&gus_lock,flags); /* opens a race */
1868 if (!interruptible_sleep_on_timeout(&dram_sleeper, HZ))
1869 printk("GUS: DMA Transfer timed out\n");
1880 gus_write8(0x41, 0); /* Stop DMA */
1883 free_mem_ptr += patch.len;
1888 static void guswave_hw_control(int dev, unsigned char *event_rec)
1891 unsigned short p1, p2;
1893 unsigned long flags;
1896 voice = event_rec[3];
1897 p1 = *(unsigned short *) &event_rec[4];
1898 p2 = *(unsigned short *) &event_rec[6];
1899 plong = *(unsigned int *) &event_rec[4];
1901 if ((voices[voice].volume_irq_mode == VMODE_START_NOTE) &&
1902 (cmd != _GUS_VOICESAMPLE) && (cmd != _GUS_VOICE_POS))
1903 do_volume_irq(voice);
1907 case _GUS_NUMVOICES:
1908 spin_lock_irqsave(&gus_lock,flags);
1909 gus_select_voice(voice);
1910 gus_select_max_voices(p1);
1911 spin_unlock_irqrestore(&gus_lock,flags);
1914 case _GUS_VOICESAMPLE:
1915 guswave_set_instr(dev, voice, p1);
1919 spin_lock_irqsave(&gus_lock,flags);
1920 gus_select_voice(voice);
1921 p1 &= ~0x20; /* Don't allow interrupts */
1923 spin_unlock_irqrestore(&gus_lock,flags);
1927 spin_lock_irqsave(&gus_lock,flags);
1928 gus_select_voice(voice);
1930 spin_unlock_irqrestore(&gus_lock,flags);
1933 case _GUS_VOICEFADE:
1934 gus_voice_fade(voice);
1937 case _GUS_VOICEMODE:
1938 spin_lock_irqsave(&gus_lock,flags);
1939 gus_select_voice(voice);
1940 p1 &= ~0x20; /* Don't allow interrupts */
1942 spin_unlock_irqrestore(&gus_lock,flags);
1945 case _GUS_VOICEBALA:
1946 spin_lock_irqsave(&gus_lock,flags);
1947 gus_select_voice(voice);
1948 gus_voice_balance(p1);
1949 spin_unlock_irqrestore(&gus_lock,flags);
1952 case _GUS_VOICEFREQ:
1953 spin_lock_irqsave(&gus_lock,flags);
1954 gus_select_voice(voice);
1955 gus_voice_freq(plong);
1956 spin_unlock_irqrestore(&gus_lock,flags);
1960 spin_lock_irqsave(&gus_lock,flags);
1961 gus_select_voice(voice);
1962 gus_voice_volume(p1);
1963 spin_unlock_irqrestore(&gus_lock,flags);
1966 case _GUS_VOICEVOL2: /* Just update the software voice level */
1967 voices[voice].initial_volume = voices[voice].current_volume = p1;
1970 case _GUS_RAMPRANGE:
1971 if (voices[voice].mode & WAVE_ENVELOPES)
1973 spin_lock_irqsave(&gus_lock,flags);
1974 gus_select_voice(voice);
1975 gus_ramp_range(p1, p2);
1976 spin_unlock_irqrestore(&gus_lock,flags);
1980 if (voices[voice].mode & WAVE_ENVELOPES)
1981 break; /* NJET-NJET */
1982 spin_lock_irqsave(&gus_lock,flags);
1983 gus_select_voice(voice);
1984 gus_ramp_rate(p1, p2);
1985 spin_unlock_irqrestore(&gus_lock,flags);
1989 if (voices[voice].mode & WAVE_ENVELOPES)
1991 spin_lock_irqsave(&gus_lock,flags);
1992 gus_select_voice(voice);
1993 p1 &= ~0x20; /* Don't allow interrupts */
1995 spin_unlock_irqrestore(&gus_lock,flags);
1999 if (voices[voice].mode & WAVE_ENVELOPES)
2001 spin_lock_irqsave(&gus_lock,flags);
2002 gus_select_voice(voice);
2003 p1 &= ~0x20; /* Don't allow interrupts */
2005 spin_unlock_irqrestore(&gus_lock,flags);
2009 if (voices[voice].mode & WAVE_ENVELOPES)
2010 break; /* NEJ-NEJ */
2011 spin_lock_irqsave(&gus_lock,flags);
2012 gus_select_voice(voice);
2014 spin_unlock_irqrestore(&gus_lock,flags);
2017 case _GUS_VOLUME_SCALE:
2022 case _GUS_VOICE_POS:
2023 spin_lock_irqsave(&gus_lock,flags);
2024 gus_select_voice(voice);
2025 gus_set_voice_pos(voice, plong);
2026 spin_unlock_irqrestore(&gus_lock,flags);
2034 static int gus_audio_set_speed(int speed)
2037 speed = gus_audio_speed;
2045 gus_audio_speed = speed;
2047 if (only_read_access)
2049 /* Compute nearest valid recording speed and return it */
2051 /* speed = (9878400 / (gus_audio_speed + 2)) / 16; */
2052 speed = (((9878400 + gus_audio_speed / 2) / (gus_audio_speed + 2)) + 8) / 16;
2053 speed = (9878400 / (speed * 16)) - 2;
2058 static int gus_audio_set_channels(int channels)
2061 return gus_audio_channels;
2066 gus_audio_channels = channels;
2070 static int gus_audio_set_bits(int bits)
2073 return gus_audio_bits;
2075 if (bits != 8 && bits != 16)
2081 gus_audio_bits = bits;
2085 static int gus_audio_ioctl(int dev, unsigned int cmd, void __user *arg)
2091 case SOUND_PCM_WRITE_RATE:
2092 if (get_user(val, (int __user*)arg))
2094 val = gus_audio_set_speed(val);
2097 case SOUND_PCM_READ_RATE:
2098 val = gus_audio_speed;
2101 case SNDCTL_DSP_STEREO:
2102 if (get_user(val, (int __user *)arg))
2104 val = gus_audio_set_channels(val + 1) - 1;
2107 case SOUND_PCM_WRITE_CHANNELS:
2108 if (get_user(val, (int __user *)arg))
2110 val = gus_audio_set_channels(val);
2113 case SOUND_PCM_READ_CHANNELS:
2114 val = gus_audio_channels;
2117 case SNDCTL_DSP_SETFMT:
2118 if (get_user(val, (int __user *)arg))
2120 val = gus_audio_set_bits(val);
2123 case SOUND_PCM_READ_BITS:
2124 val = gus_audio_bits;
2127 case SOUND_PCM_WRITE_FILTER: /* NOT POSSIBLE */
2128 case SOUND_PCM_READ_FILTER:
2134 return put_user(val, (int __user *)arg);
2137 static void gus_audio_reset(int dev)
2139 if (recording_active)
2141 gus_write8(0x49, 0x00); /* Halt recording */
2142 set_input_volumes();
2146 static int saved_iw_mode; /* A hack hack hack */
2148 static int gus_audio_open(int dev, int mode)
2153 if (gus_pnp_flag && mode & OPEN_READ)
2155 /* printk(KERN_ERR "GUS: Audio device #%d is playback only.\n", dev);*/
2163 saved_iw_mode = iw_mode;
2166 /* There are some problems with audio in enhanced mode so disable it */
2167 gus_write8(0x19, gus_read8(0x19) & ~0x01); /* Disable enhanced mode */
2172 reset_sample_memory();
2173 gus_select_max_voices(14);
2178 if (mode & OPEN_READ)
2180 recording_active = 1;
2181 set_input_volumes();
2183 only_read_access = !(mode & OPEN_WRITE);
2184 only_8_bits = mode & OPEN_READ;
2186 audio_devs[dev]->format_mask = AFMT_U8;
2188 audio_devs[dev]->format_mask = AFMT_U8 | AFMT_S16_LE;
2193 static void gus_audio_close(int dev)
2195 iw_mode = saved_iw_mode;
2201 if (recording_active)
2203 gus_write8(0x49, 0x00); /* Halt recording */
2204 set_input_volumes();
2206 recording_active = 0;
2209 static void gus_audio_update_volume(void)
2211 unsigned long flags;
2214 if (pcm_active && pcm_opened)
2215 for (voice = 0; voice < gus_audio_channels; voice++)
2217 spin_lock_irqsave(&gus_lock,flags);
2218 gus_select_voice(voice);
2220 gus_voice_volume(1530 + (25 * gus_pcm_volume));
2221 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume));
2222 spin_unlock_irqrestore(&gus_lock,flags);
2226 static void play_next_pcm_block(void)
2228 unsigned long flags;
2229 int speed = gus_audio_speed;
2230 int this_one, is16bits, chn;
2231 unsigned long dram_loc;
2232 unsigned char mode[2], ramp_mode[2];
2237 this_one = pcm_head;
2239 for (chn = 0; chn < gus_audio_channels; chn++)
2242 ramp_mode[chn] = 0x03; /* Ramping and rollover off */
2246 mode[chn] |= 0x20; /* Loop IRQ */
2247 voices[chn].loop_irq_mode = LMODE_PCM;
2249 if (gus_audio_bits != 8)
2252 mode[chn] |= 0x04; /* 16 bit data */
2257 dram_loc = this_one * pcm_bsize;
2258 dram_loc += chn * pcm_banksize;
2260 if (this_one == (pcm_nblk - 1)) /* Last fragment of the DRAM buffer */
2262 mode[chn] |= 0x08; /* Enable loop */
2263 ramp_mode[chn] = 0x03; /* Disable rollover bit */
2268 ramp_mode[chn] = 0x04; /* Enable rollover bit */
2270 spin_lock_irqsave(&gus_lock,flags);
2271 gus_select_voice(chn);
2272 gus_voice_freq(speed);
2274 if (gus_audio_channels == 1)
2275 gus_voice_balance(7); /* mono */
2277 gus_voice_balance(0); /* left */
2279 gus_voice_balance(15); /* right */
2281 if (!pcm_active) /* Playback not already active */
2284 * The playback was not started yet (or there has been a pause).
2285 * Start the voice (again) and ask for a rollover irq at the end of
2286 * this_one block. If this_one one is last of the buffers, use just
2287 * the normal loop with irq.
2292 gus_voice_volume(1530 + (25 * gus_pcm_volume));
2293 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume));
2295 gus_write_addr(0x0a, chn * pcm_banksize, 0, is16bits); /* Starting position */
2296 gus_write_addr(0x02, chn * pcm_banksize, 0, is16bits); /* Loop start */
2299 gus_write_addr(0x04, pcm_banksize + (pcm_bsize * pcm_nblk) - 1,
2300 0, is16bits); /* Loop end location */
2303 gus_write_addr(0x04, dram_loc + pcm_bsize - 1,
2304 0, is16bits); /* Loop end location */
2306 mode[chn] |= 0x08; /* Enable looping */
2307 spin_unlock_irqrestore(&gus_lock,flags);
2309 for (chn = 0; chn < gus_audio_channels; chn++)
2311 spin_lock_irqsave(&gus_lock,flags);
2312 gus_select_voice(chn);
2313 gus_write8(0x0d, ramp_mode[chn]);
2315 gus_write8(0x15, 0x00); /* Reset voice deactivate bit of SMSI */
2316 gus_voice_on(mode[chn]);
2317 spin_unlock_irqrestore(&gus_lock,flags);
2322 static void gus_transfer_output_block(int dev, unsigned long buf,
2323 int total_count, int intrflag, int chn)
2326 * This routine transfers one block of audio data to the DRAM. In mono mode
2327 * it's called just once. When in stereo mode, this_one routine is called
2328 * once for both channels.
2330 * The left/mono channel data is transferred to the beginning of dram and the
2331 * right data to the area pointed by gus_page_size.
2334 int this_one, count;
2335 unsigned long flags;
2336 unsigned char dma_command;
2337 unsigned long address, hold_address;
2339 spin_lock_irqsave(&gus_lock,flags);
2341 count = total_count / gus_audio_channels;
2345 if (pcm_qlen >= pcm_nblk)
2346 printk(KERN_WARNING "GUS Warning: PCM buffers out of sync\n");
2348 this_one = pcm_current_block = pcm_tail;
2350 pcm_tail = (pcm_tail + 1) % pcm_nblk;
2351 pcm_datasize[this_one] = count;
2354 this_one = pcm_current_block;
2356 gus_write8(0x41, 0); /* Disable GF1 DMA */
2357 DMAbuf_start_dma(dev, buf + (chn * count), count, DMA_MODE_WRITE);
2359 address = this_one * pcm_bsize;
2360 address += chn * pcm_banksize;
2362 if (audio_devs[dev]->dmap_out->dma > 3)
2364 hold_address = address;
2365 address = address >> 1;
2366 address &= 0x0001ffffL;
2367 address |= (hold_address & 0x000c0000L);
2369 gus_write16(0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
2371 dma_command = 0x21; /* IRQ enable, DMA start */
2373 if (gus_audio_bits != 8)
2374 dma_command |= 0x40; /* 16 bit _DATA_ */
2376 dma_command |= 0x80; /* Invert MSB */
2378 if (audio_devs[dev]->dmap_out->dma > 3)
2379 dma_command |= 0x04; /* 16 bit DMA channel */
2381 gus_write8(0x41, dma_command); /* Kick start */
2383 if (chn == (gus_audio_channels - 1)) /* Last channel */
2386 * Last (right or mono) channel data
2388 dma_active = 1; /* DMA started. There is a unacknowledged buffer */
2389 active_device = GUS_DEV_PCM_DONE;
2390 if (!pcm_active && (pcm_qlen > 1 || count < pcm_bsize))
2392 play_next_pcm_block();
2398 * Left channel data. The right channel
2399 * is transferred after DMA interrupt
2401 active_device = GUS_DEV_PCM_CONTINUE;
2404 spin_unlock_irqrestore(&gus_lock,flags);
2407 static void gus_uninterleave8(char *buf, int l)
2409 /* This routine uninterleaves 8 bit stereo output (LRLRLR->LLLRRR) */
2410 int i, p = 0, halfsize = l / 2;
2411 char *buf2 = buf + halfsize, *src = bounce_buf;
2413 memcpy(bounce_buf, buf, l);
2415 for (i = 0; i < halfsize; i++)
2417 buf[i] = src[p++]; /* Left channel */
2418 buf2[i] = src[p++]; /* Right channel */
2422 static void gus_uninterleave16(short *buf, int l)
2424 /* This routine uninterleaves 16 bit stereo output (LRLRLR->LLLRRR) */
2425 int i, p = 0, halfsize = l / 2;
2426 short *buf2 = buf + halfsize, *src = (short *) bounce_buf;
2428 memcpy(bounce_buf, (char *) buf, l * 2);
2430 for (i = 0; i < halfsize; i++)
2432 buf[i] = src[p++]; /* Left channel */
2433 buf2[i] = src[p++]; /* Right channel */
2437 static void gus_audio_output_block(int dev, unsigned long buf, int total_count,
2440 struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
2442 dmap->flags |= DMA_NODMA | DMA_NOTIMEOUT;
2444 pcm_current_buf = buf;
2445 pcm_current_count = total_count;
2446 pcm_current_intrflag = intrflag;
2447 pcm_current_dev = dev;
2448 if (gus_audio_channels == 2)
2450 char *b = dmap->raw_buf + (buf - dmap->raw_buf_phys);
2452 if (gus_audio_bits == 8)
2453 gus_uninterleave8(b, total_count);
2455 gus_uninterleave16((short *) b, total_count / 2);
2457 gus_transfer_output_block(dev, buf, total_count, intrflag, 0);
2460 static void gus_audio_start_input(int dev, unsigned long buf, int count,
2463 unsigned long flags;
2466 spin_lock_irqsave(&gus_lock,flags);
2468 DMAbuf_start_dma(dev, buf, count, DMA_MODE_READ);
2469 mode = 0xa0; /* DMA IRQ enabled, invert MSB */
2471 if (audio_devs[dev]->dmap_in->dma > 3)
2472 mode |= 0x04; /* 16 bit DMA channel */
2473 if (gus_audio_channels > 1)
2474 mode |= 0x02; /* Stereo */
2475 mode |= 0x01; /* DMA enable */
2477 gus_write8(0x49, mode);
2478 spin_unlock_irqrestore(&gus_lock,flags);
2481 static int gus_audio_prepare_for_input(int dev, int bsize, int bcount)
2485 gus_audio_bsize = bsize;
2486 audio_devs[dev]->dmap_in->flags |= DMA_NODMA;
2487 rate = (((9878400 + gus_audio_speed / 2) / (gus_audio_speed + 2)) + 8) / 16;
2489 gus_write8(0x48, rate & 0xff); /* Set sampling rate */
2491 if (gus_audio_bits != 8)
2493 /* printk("GUS Error: 16 bit recording not supported\n");*/
2499 static int gus_audio_prepare_for_output(int dev, int bsize, int bcount)
2503 long mem_ptr, mem_size;
2505 audio_devs[dev]->dmap_out->flags |= DMA_NODMA | DMA_NOTIMEOUT;
2507 mem_size = gus_mem_size / gus_audio_channels;
2509 if (mem_size > (256 * 1024))
2510 mem_size = 256 * 1024;
2512 pcm_bsize = bsize / gus_audio_channels;
2513 pcm_head = pcm_tail = pcm_qlen = 0;
2515 pcm_nblk = 2; /* MAX_PCM_BUFFERS; */
2516 if ((pcm_bsize * pcm_nblk) > mem_size)
2517 pcm_nblk = mem_size / pcm_bsize;
2519 for (i = 0; i < pcm_nblk; i++)
2520 pcm_datasize[i] = 0;
2522 pcm_banksize = pcm_nblk * pcm_bsize;
2524 if (gus_audio_bits != 8 && pcm_banksize == (256 * 1024))
2526 gus_write8(0x41, 0); /* Disable GF1 DMA */
2530 static int gus_local_qlen(int dev)
2536 static struct audio_driver gus_audio_driver =
2538 .owner = THIS_MODULE,
2539 .open = gus_audio_open,
2540 .close = gus_audio_close,
2541 .output_block = gus_audio_output_block,
2542 .start_input = gus_audio_start_input,
2543 .ioctl = gus_audio_ioctl,
2544 .prepare_for_input = gus_audio_prepare_for_input,
2545 .prepare_for_output = gus_audio_prepare_for_output,
2546 .halt_io = gus_audio_reset,
2547 .local_qlen = gus_local_qlen,
2550 static void guswave_setup_voice(int dev, int voice, int chn)
2552 struct channel_info *info = &synth_devs[dev]->chn_info[chn];
2554 guswave_set_instr(dev, voice, info->pgm_num);
2555 voices[voice].expression_vol = info->controllers[CTL_EXPRESSION]; /* Just MSB */
2556 voices[voice].main_vol = (info->controllers[CTL_MAIN_VOLUME] * 100) / (unsigned) 128;
2557 voices[voice].panning = (info->controllers[CTL_PAN] * 2) - 128;
2558 voices[voice].bender = 0;
2559 voices[voice].bender_range = info->bender_range;
2562 voices[voice].fixed_pitch = 1;
2565 static void guswave_bender(int dev, int voice, int value)
2568 unsigned long flags;
2570 voices[voice].bender = value - 8192;
2571 freq = compute_finetune(voices[voice].orig_freq, value - 8192, voices[voice].bender_range, 0);
2572 voices[voice].current_freq = freq;
2574 spin_lock_irqsave(&gus_lock,flags);
2575 gus_select_voice(voice);
2576 gus_voice_freq(freq);
2577 spin_unlock_irqrestore(&gus_lock,flags);
2580 static int guswave_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc)
2582 int i, p, best = -1, best_time = 0x7fffffff;
2586 * First look for a completely stopped voice
2589 for (i = 0; i < alloc->max_voice; i++)
2591 if (alloc->map[p] == 0)
2596 if (alloc->alloc_times[p] < best_time)
2599 best_time = alloc->alloc_times[p];
2601 p = (p + 1) % alloc->max_voice;
2605 * Then look for a releasing voice
2608 for (i = 0; i < alloc->max_voice; i++)
2610 if (alloc->map[p] == 0xffff)
2615 p = (p + 1) % alloc->max_voice;
2624 static struct synth_operations guswave_operations =
2626 .owner = THIS_MODULE,
2630 .synth_type = SYNTH_TYPE_SAMPLE,
2631 .synth_subtype = SAMPLE_TYPE_GUS,
2632 .open = guswave_open,
2633 .close = guswave_close,
2634 .ioctl = guswave_ioctl,
2635 .kill_note = guswave_kill_note,
2636 .start_note = guswave_start_note,
2637 .set_instr = guswave_set_instr,
2638 .reset = guswave_reset,
2639 .hw_control = guswave_hw_control,
2640 .load_patch = guswave_load_patch,
2641 .aftertouch = guswave_aftertouch,
2642 .controller = guswave_controller,
2643 .panning = guswave_panning,
2644 .volume_method = guswave_volume_method,
2645 .bender = guswave_bender,
2646 .alloc_voice = guswave_alloc,
2647 .setup_voice = guswave_setup_voice
2650 static void set_input_volumes(void)
2652 unsigned long flags;
2653 unsigned char mask = 0xff & ~0x06; /* Just line out enabled */
2655 if (have_gus_max) /* Don't disturb GUS MAX */
2658 spin_lock_irqsave(&gus_lock,flags);
2661 * Enable channels having vol > 10%
2662 * Note! bit 0x01 means the line in DISABLED while 0x04 means
2663 * the mic in ENABLED.
2665 if (gus_line_vol > 10)
2667 if (gus_mic_vol > 10)
2670 if (recording_active)
2673 * Disable channel, if not selected for recording
2675 if (!(gus_recmask & SOUND_MASK_LINE))
2677 if (!(gus_recmask & SOUND_MASK_MIC))
2681 mix_image |= mask & 0x07;
2682 outb((mix_image), u_Mixer);
2684 spin_unlock_irqrestore(&gus_lock,flags);
2687 #define MIX_DEVS (SOUND_MASK_MIC|SOUND_MASK_LINE| \
2688 SOUND_MASK_SYNTH|SOUND_MASK_PCM)
2690 int gus_default_mixer_ioctl(int dev, unsigned int cmd, void __user *arg)
2694 if (((cmd >> 8) & 0xff) != 'M')
2697 if (!access_ok(VERIFY_WRITE, arg, sizeof(int)))
2700 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
2702 if (__get_user(val, (int __user *) arg))
2707 case SOUND_MIXER_RECSRC:
2708 gus_recmask = val & MIX_DEVS;
2709 if (!(gus_recmask & (SOUND_MASK_MIC | SOUND_MASK_LINE)))
2710 gus_recmask = SOUND_MASK_MIC;
2711 /* Note! Input volumes are updated during next open for recording */
2715 case SOUND_MIXER_MIC:
2722 set_input_volumes();
2723 val = vol | (vol << 8);
2726 case SOUND_MIXER_LINE:
2733 set_input_volumes();
2734 val = vol | (vol << 8);
2737 case SOUND_MIXER_PCM:
2738 gus_pcm_volume = val & 0xff;
2739 if (gus_pcm_volume < 0)
2741 if (gus_pcm_volume > 100)
2742 gus_pcm_volume = 100;
2743 gus_audio_update_volume();
2744 val = gus_pcm_volume | (gus_pcm_volume << 8);
2747 case SOUND_MIXER_SYNTH:
2748 gus_wave_volume = val & 0xff;
2749 if (gus_wave_volume < 0)
2750 gus_wave_volume = 0;
2751 if (gus_wave_volume > 100)
2752 gus_wave_volume = 100;
2753 if (active_device == GUS_DEV_WAVE)
2756 for (voice = 0; voice < nr_voices; voice++)
2757 dynamic_volume_change(voice); /* Apply the new vol */
2759 val = gus_wave_volume | (gus_wave_volume << 8);
2773 case SOUND_MIXER_RECSRC:
2777 case SOUND_MIXER_DEVMASK:
2781 case SOUND_MIXER_STEREODEVS:
2785 case SOUND_MIXER_RECMASK:
2786 val = SOUND_MASK_MIC | SOUND_MASK_LINE;
2789 case SOUND_MIXER_CAPS:
2793 case SOUND_MIXER_MIC:
2794 val = gus_mic_vol | (gus_mic_vol << 8);
2797 case SOUND_MIXER_LINE:
2798 val = gus_line_vol | (gus_line_vol << 8);
2801 case SOUND_MIXER_PCM:
2802 val = gus_pcm_volume | (gus_pcm_volume << 8);
2805 case SOUND_MIXER_SYNTH:
2806 val = gus_wave_volume | (gus_wave_volume << 8);
2813 return __put_user(val, (int __user *)arg);
2816 static struct mixer_operations gus_mixer_operations =
2818 .owner = THIS_MODULE,
2820 .name = "Gravis Ultrasound",
2821 .ioctl = gus_default_mixer_ioctl
2824 static int __init gus_default_mixer_init(void)
2828 if ((n = sound_alloc_mixerdev()) != -1)
2831 * Don't install if there is another
2834 mixer_devs[n] = &gus_mixer_operations;
2839 * Enable all mixer channels on the GF1 side. Otherwise recording will
2840 * not be possible using GUS MAX.
2843 mix_image |= 0x04; /* All channels enabled */
2844 outb((mix_image), u_Mixer);
2849 void __init gus_wave_init(struct address_info *hw_config)
2851 unsigned long flags;
2853 char *model_num = "2.4";
2855 int gus_type = 0x24; /* 2.4 */
2857 int irq = hw_config->irq, dma = hw_config->dma, dma2 = hw_config->dma2;
2860 hw_config->slots[0] = -1; /* No wave */
2861 hw_config->slots[1] = -1; /* No ad1848 */
2862 hw_config->slots[4] = -1; /* No audio */
2863 hw_config->slots[5] = -1; /* No mixer */
2867 if (irq < 0 || irq > 15)
2869 printk(KERN_ERR "ERROR! Invalid IRQ#%d. GUS Disabled", irq);
2874 if (dma < 0 || dma > 7 || dma == 4)
2876 printk(KERN_ERR "ERROR! Invalid DMA#%d. GUS Disabled", dma);
2882 gus_hw_config = hw_config;
2888 * Try to identify the GUS model.
2890 * Versions < 3.6 don't have the digital ASIC. Try to probe it first.
2893 spin_lock_irqsave(&gus_lock,flags);
2894 outb((0x20), gus_base + 0x0f);
2895 val = inb(gus_base + 0x0f);
2896 spin_unlock_irqrestore(&gus_lock,flags);
2898 if (gus_pnp_flag || (val != 0xff && (val & 0x06))) /* Should be 0x02?? */
2903 ad_flags = 0x12345678; /* Interwave "magic" */
2905 * It has the digital ASIC so the card is at least v3.4.
2906 * Next try to detect the true model.
2909 if (gus_pnp_flag) /* Hack hack hack */
2912 val = inb(u_MixSelect);
2915 * Value 255 means pre-3.7 which don't have mixer.
2916 * Values 5 thru 9 mean v3.7 which has a ICS2101 mixer.
2917 * 10 and above is GUS MAX which has the CS4231 codec/mixer.
2921 if (val == 255 || val < 5)
2930 mixer_type = ICS2101;
2931 request_region(u_MixSelect, 1, "GUS mixer");
2937 mixer_type = CS4231;
2938 #ifdef CONFIG_SOUND_GUSMAX
2940 unsigned char max_config = 0x40; /* Codec enable */
2946 max_config |= 0x10; /* 16 bit capture DMA */
2949 max_config |= 0x20; /* 16 bit playback DMA */
2951 max_config |= (gus_base >> 4) & 0x0f; /* Extract the X from 2X0 */
2953 outb((max_config), gus_base + 0x106); /* UltraMax control */
2956 if (ad1848_detect(gus_base + 0x10c, &ad_flags, hw_config->osp))
2958 char *name = "GUS MAX";
2959 int old_num_mixers = num_mixers;
2964 gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
2965 gus_wave_volume = 90;
2967 if (hw_config->name)
2968 name = hw_config->name;
2970 hw_config->slots[1] = ad1848_init(name, gus_base + 0x10c,
2971 -irq, gus_dma2, /* Playback DMA */
2972 gus_dma, /* Capture DMA */
2973 1, /* Share DMA channels with GF1 */
2977 if (num_mixers > old_num_mixers)
2979 /* GUS has it's own mixer map */
2980 AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_SYNTH);
2981 AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_CD);
2982 AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE);
2986 printk(KERN_WARNING "GUS: No CS4231 ??");
2988 printk(KERN_ERR "GUS MAX found, but not compiled in\n");
2995 * ASIC not detected so the card must be 2.2 or 2.4.
2996 * There could still be the 16-bit/mixer daughter card.
3000 if (hw_config->name)
3001 snprintf(tmp, sizeof(tmp), "%s (%dk)", hw_config->name,
3002 (int) gus_mem_size / 1024);
3003 else if (gus_pnp_flag)
3004 snprintf(tmp, sizeof(tmp), "Gravis UltraSound PnP (%dk)",
3005 (int) gus_mem_size / 1024);
3007 snprintf(tmp, sizeof(tmp), "Gravis UltraSound %s (%dk)", model_num,
3008 (int) gus_mem_size / 1024);
3011 samples = (struct patch_info *)vmalloc((MAX_SAMPLE + 1) * sizeof(*samples));
3012 if (samples == NULL)
3014 printk(KERN_WARNING "gus_init: Cant allocate memory for instrument tables\n");
3017 conf_printf(tmp, hw_config);
3018 strlcpy(gus_info.name, tmp, sizeof(gus_info.name));
3020 if ((sdev = sound_alloc_synthdev()) == -1)
3021 printk(KERN_WARNING "gus_init: Too many synthesizers\n");
3024 voice_alloc = &guswave_operations.alloc;
3026 guswave_operations.id = "IWAVE";
3027 hw_config->slots[0] = sdev;
3028 synth_devs[sdev] = &guswave_operations;
3030 gus_tmr_install(gus_base + 8);
3033 reset_sample_memory();
3037 if ((gus_mem_size > 0) && !gus_no_wave_dma)
3039 hw_config->slots[4] = -1;
3040 if ((gus_devnum = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
3043 sizeof(struct audio_driver),
3045 ((!iw_mode && dma2 != dma && dma2 != -1) ?
3047 AFMT_U8 | AFMT_S16_LE,
3048 NULL, dma, dma2)) < 0)
3053 hw_config->slots[4] = gus_devnum;
3054 audio_devs[gus_devnum]->min_fragment = 9; /* 512k */
3055 audio_devs[gus_devnum]->max_fragment = 11; /* 8k (must match size of bounce_buf */
3056 audio_devs[gus_devnum]->mixer_dev = -1; /* Next mixer# */
3057 audio_devs[gus_devnum]->flags |= DMA_HARDSTOP;
3061 * Mixer dependent initialization.
3067 gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
3068 gus_wave_volume = 90;
3069 request_region(u_MixSelect, 1, "GUS mixer");
3070 hw_config->slots[5] = ics2101_mixer_init();
3071 audio_devs[gus_devnum]->mixer_dev = hw_config->slots[5]; /* Next mixer# */
3075 /* Initialized elsewhere (ad1848.c) */
3077 hw_config->slots[5] = gus_default_mixer_init();
3078 audio_devs[gus_devnum]->mixer_dev = hw_config->slots[5]; /* Next mixer# */
3083 void __exit gus_wave_unload(struct address_info *hw_config)
3085 #ifdef CONFIG_SOUND_GUSMAX
3088 ad1848_unload(gus_base + 0x10c,
3090 gus_dma2, /* Playback DMA */
3091 gus_dma, /* Capture DMA */
3092 1); /* Share DMA channels with GF1 */
3096 if (mixer_type == ICS2101)
3098 release_region(u_MixSelect, 1);
3100 if (hw_config->slots[0] != -1)
3101 sound_unload_synthdev(hw_config->slots[0]);
3102 if (hw_config->slots[1] != -1)
3103 sound_unload_audiodev(hw_config->slots[1]);
3104 if (hw_config->slots[2] != -1)
3105 sound_unload_mididev(hw_config->slots[2]);
3106 if (hw_config->slots[4] != -1)
3107 sound_unload_audiodev(hw_config->slots[4]);
3108 if (hw_config->slots[5] != -1)
3109 sound_unload_mixerdev(hw_config->slots[5]);
3115 /* called in interrupt context */
3116 static void do_loop_irq(int voice)
3121 spin_lock(&gus_lock);
3122 gus_select_voice(voice);
3124 tmp = gus_read8(0x00);
3126 * Disable wave IRQ for this_one voice
3128 gus_write8(0x00, tmp);
3130 if (tmp & 0x03) /* Voice stopped */
3131 voice_alloc->map[voice] = 0;
3133 mode = voices[voice].loop_irq_mode;
3134 voices[voice].loop_irq_mode = 0;
3135 parm = voices[voice].loop_irq_parm;
3139 case LMODE_FINISH: /*
3140 * Final loop finished, shoot volume down
3143 if ((int) (gus_read16(0x09) >> 4) < 100) /*
3144 * Get current volume
3149 gus_voice_init(voice);
3152 gus_ramp_range(65, 4065);
3153 gus_ramp_rate(0, 63); /*
3154 * Fastest possible rate
3156 gus_rampon(0x20 | 0x40); /*
3157 * Ramp down, once, irq
3159 voices[voice].volume_irq_mode = VMODE_HALT;
3162 case LMODE_PCM_STOP:
3163 pcm_active = 0; /* Signal to the play_next_pcm_block routine */
3167 pcm_head = (pcm_head + 1) % pcm_nblk;
3168 if (pcm_qlen && pcm_active)
3170 play_next_pcm_block();
3174 /* Underrun. Just stop the voice */
3175 gus_select_voice(0); /* Left channel */
3178 gus_select_voice(1); /* Right channel */
3185 * If the queue was full before this interrupt, the DMA transfer was
3186 * suspended. Let it continue now.
3189 if (audio_devs[gus_devnum]->dmap_out->qlen > 0)
3190 DMAbuf_outputintr(gus_devnum, 0);
3197 spin_unlock(&gus_lock);
3200 static void do_volume_irq(int voice)
3204 unsigned long flags;
3206 spin_lock_irqsave(&gus_lock,flags);
3208 gus_select_voice(voice);
3209 tmp = gus_read8(0x0d);
3211 * Disable volume ramp IRQ
3213 gus_write8(0x0d, tmp);
3215 mode = voices[voice].volume_irq_mode;
3216 voices[voice].volume_irq_mode = 0;
3217 parm = voices[voice].volume_irq_parm;
3221 case VMODE_HALT: /* Decay phase finished */
3223 gus_write8(0x15, 0x02); /* Set voice deactivate bit of SMSI */
3224 spin_unlock_irqrestore(&gus_lock,flags);
3225 gus_voice_init(voice);
3228 case VMODE_ENVELOPE:
3230 spin_unlock_irqrestore(&gus_lock,flags);
3231 step_envelope(voice);
3234 case VMODE_START_NOTE:
3235 spin_unlock_irqrestore(&gus_lock,flags);
3236 guswave_start_note2(voices[voice].dev_pending, voice,
3237 voices[voice].note_pending, voices[voice].volume_pending);
3238 if (voices[voice].kill_pending)
3239 guswave_kill_note(voices[voice].dev_pending, voice,
3240 voices[voice].note_pending, 0);
3242 if (voices[voice].sample_pending >= 0)
3244 guswave_set_instr(voices[voice].dev_pending, voice,
3245 voices[voice].sample_pending);
3246 voices[voice].sample_pending = -1;
3251 spin_unlock_irqrestore(&gus_lock,flags);
3254 /* called in irq context */
3255 void gus_voice_irq(void)
3257 unsigned long wave_ignore = 0, volume_ignore = 0;
3258 unsigned long voice_bit;
3260 unsigned char src, voice;
3264 src = gus_read8(0x0f); /*
3270 if (src == (0x80 | 0x40))
3275 voice_bit = 1 << voice;
3277 if (!(src & 0x80)) /*
3280 if (!(wave_ignore & voice_bit) && (int) voice < nr_voices) /*
3285 wave_ignore |= voice_bit;
3288 if (!(src & 0x40)) /*
3289 * Volume IRQ pending
3291 if (!(volume_ignore & voice_bit) && (int) voice < nr_voices) /*
3296 volume_ignore |= voice_bit;
3297 do_volume_irq(voice);
3302 void guswave_dma_irq(void)
3304 unsigned char status;
3306 status = gus_look8(0x41); /* Get DMA IRQ Status */
3307 if (status & 0x40) /* DMA interrupt pending */
3308 switch (active_device)
3311 wake_up(&dram_sleeper);
3314 case GUS_DEV_PCM_CONTINUE: /* Left channel data transferred */
3315 gus_write8(0x41, 0); /* Disable GF1 DMA */
3316 gus_transfer_output_block(pcm_current_dev, pcm_current_buf,
3318 pcm_current_intrflag, 1);
3321 case GUS_DEV_PCM_DONE: /* Right or mono channel data transferred */
3322 gus_write8(0x41, 0); /* Disable GF1 DMA */
3323 if (pcm_qlen < pcm_nblk)
3328 if (audio_devs[gus_devnum]->dmap_out->qlen > 0)
3329 DMAbuf_outputintr(gus_devnum, 0);
3337 status = gus_look8(0x49); /*
3338 * Get Sampling IRQ Status
3340 if (status & 0x40) /*
3341 * Sampling Irq pending
3344 DMAbuf_inputintr(gus_devnum);
3352 static volatile int select_addr, data_addr;
3353 static volatile int curr_timer;
3355 void gus_timer_command(unsigned int addr, unsigned int val)
3359 outb(((unsigned char) (addr & 0xff)), select_addr);
3361 for (i = 0; i < 2; i++)
3364 outb(((unsigned char) (val & 0xff)), data_addr);
3366 for (i = 0; i < 2; i++)
3370 static void arm_timer(int timer, unsigned int interval)
3376 gus_write8(0x46, 256 - interval); /* Set counter for timer 1 */
3377 gus_write8(0x45, 0x04); /* Enable timer 1 IRQ */
3378 gus_timer_command(0x04, 0x01); /* Start timer 1 */
3382 gus_write8(0x47, 256 - interval); /* Set counter for timer 2 */
3383 gus_write8(0x45, 0x08); /* Enable timer 2 IRQ */
3384 gus_timer_command(0x04, 0x02); /* Start timer 2 */
3387 gus_timer_enabled = 1;
3390 static unsigned int gus_tmr_start(int dev, unsigned int usecs_per_tick)
3392 int timer_no, resolution;
3395 if (usecs_per_tick > (256 * 80))
3398 resolution = 320; /* usec */
3403 resolution = 80; /* usec */
3405 divisor = (usecs_per_tick + (resolution / 2)) / resolution;
3406 arm_timer(timer_no, divisor);
3408 return divisor * resolution;
3411 static void gus_tmr_disable(int dev)
3413 gus_write8(0x45, 0); /* Disable both timers */
3414 gus_timer_enabled = 0;
3417 static void gus_tmr_restart(int dev)
3419 if (curr_timer == 1)
3420 gus_write8(0x45, 0x04); /* Start timer 1 again */
3422 gus_write8(0x45, 0x08); /* Start timer 2 again */
3423 gus_timer_enabled = 1;
3426 static struct sound_lowlev_timer gus_tmr =
3435 static void gus_tmr_install(int io_base)
3437 struct sound_lowlev_timer *tmr;
3439 select_addr = io_base;
3440 data_addr = io_base + 1;
3444 #ifdef THIS_GETS_FIXED
3445 sound_timer_init(&gus_tmr, "GUS");