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.
20 #include <linux/config.h>
22 #define GUSPNP_AUTODETECT
24 #include "sound_config.h"
25 #include <linux/ultrasound.h>
30 #define GUS_BANK_SIZE (((iw_mode) ? 256*1024*1024 : 256*1024))
32 #define MAX_SAMPLE 150
35 #define NOT_SAMPLE 0xffff
39 unsigned long orig_freq
;
40 unsigned long current_freq
;
47 unsigned int initial_volume
;
48 unsigned int current_volume
;
49 int loop_irq_mode
, loop_irq_parm
;
50 #define LMODE_FINISH 1
52 #define LMODE_PCM_STOP 3
53 int volume_irq_mode
, volume_irq_parm
;
55 #define VMODE_ENVELOPE 2
56 #define VMODE_START_NOTE 3
59 unsigned char env_rate
[6];
60 unsigned char env_offset
[6];
63 * Volume computation parameters for gus_adagio_vol()
65 int main_vol
, expression_vol
, patch_vol
;
67 /* Variables for "Ultraclick" removal */
68 int dev_pending
, note_pending
, volume_pending
,
75 static struct voice_alloc_info
*voice_alloc
;
76 static struct address_info
*gus_hw_config
;
78 extern int gus_irq
, gus_dma
;
79 extern int gus_pnp_flag
;
80 extern int gus_no_wave_dma
;
81 static int gus_dma2
= -1;
82 static int dual_dma_mode
= 0;
83 static long gus_mem_size
= 0;
84 static long free_mem_ptr
= 0;
85 static int gus_busy
= 0;
86 static int gus_no_dma
= 0;
87 static int nr_voices
= 0;
88 static int gus_devnum
= 0;
89 static int volume_base
, volume_scale
, volume_method
;
90 static int gus_recmask
= SOUND_MASK_MIC
;
91 static int recording_active
= 0;
92 static int only_read_access
= 0;
93 static int only_8_bits
= 0;
96 int gus_wave_volume
= 60;
97 int gus_pcm_volume
= 80;
99 static int gus_line_vol
= 100, gus_mic_vol
= 0;
100 static unsigned char mix_image
= 0x00;
102 int gus_timer_enabled
= 0;
105 * Current version of this driver doesn't allow synth and PCM functions
106 * at the same time. The active_device specifies the active driver
109 static int active_device
= 0;
111 #define GUS_DEV_WAVE 1 /* Wave table synth */
112 #define GUS_DEV_PCM_DONE 2 /* PCM device, transfer done */
113 #define GUS_DEV_PCM_CONTINUE 3 /* PCM device, transfer done ch. 1/2 */
115 static int gus_audio_speed
;
116 static int gus_audio_channels
;
117 static int gus_audio_bits
;
118 static int gus_audio_bsize
;
119 static char bounce_buf
[8 * 1024]; /* Must match value set to max_fragment */
121 static DECLARE_WAIT_QUEUE_HEAD(dram_sleeper
);
124 * Variables and buffers for PCM output
127 #define MAX_PCM_BUFFERS (128*MAX_REALTIME_FACTOR) /* Don't change */
129 static int pcm_bsize
, pcm_nblk
, pcm_banksize
;
130 static int pcm_datasize
[MAX_PCM_BUFFERS
];
131 static volatile int pcm_head
, pcm_tail
, pcm_qlen
;
132 static volatile int pcm_active
;
133 static volatile int dma_active
;
134 static int pcm_opened
= 0;
135 static int pcm_current_dev
;
136 static int pcm_current_block
;
137 static unsigned long pcm_current_buf
;
138 static int pcm_current_count
;
139 static int pcm_current_intrflag
;
143 static struct voice_info voices
[32];
145 static int freq_div_table
[] =
168 static struct patch_info
*samples
= NULL
;
169 static long sample_ptrs
[MAX_SAMPLE
+ 1];
170 static int sample_map
[32];
171 static int free_sample
;
172 static int mixer_type
= 0;
175 static int patch_table
[MAX_PATCH
];
176 static int patch_map
[32];
178 static struct synth_info gus_info
= {
179 "Gravis UltraSound", 0, SYNTH_TYPE_SAMPLE
, SAMPLE_TYPE_GUS
,
183 static void gus_poke(long addr
, unsigned char data
);
184 static void compute_and_set_volume(int voice
, int volume
, int ramp_time
);
185 extern unsigned short gus_adagio_vol(int vel
, int mainv
, int xpn
, int voicev
);
186 extern unsigned short gus_linear_vol(int vol
, int mainvol
);
187 static void compute_volume(int voice
, int volume
);
188 static void do_volume_irq(int voice
);
189 static void set_input_volumes(void);
190 static void gus_tmr_install(int io_base
);
192 #define INSTANT_RAMP -1 /* Instant change. No ramping */
193 #define FAST_RAMP 0 /* Fastest possible ramp */
195 static void reset_sample_memory(void)
199 for (i
= 0; i
<= MAX_SAMPLE
; i
++)
201 for (i
= 0; i
< 32; i
++)
203 for (i
= 0; i
< 32; i
++)
206 gus_poke(0, 0); /* Put a silent sample to the beginning */
212 for (i
= 0; i
< MAX_PATCH
; i
++)
213 patch_table
[i
] = NOT_SAMPLE
;
220 for (i
= 0; i
< 7; i
++)
224 static void gus_poke(long addr
, unsigned char data
)
225 { /* Writes a byte to the DRAM */
230 outb((0x43), u_Command
);
231 outb((addr
& 0xff), u_DataLo
);
232 outb(((addr
>> 8) & 0xff), u_DataHi
);
234 outb((0x44), u_Command
);
235 outb(((addr
>> 16) & 0xff), u_DataHi
);
236 outb((data
), u_DRAMIO
);
237 restore_flags(flags
);
240 static unsigned char gus_peek(long addr
)
241 { /* Reads a byte from the DRAM */
247 outb((0x43), u_Command
);
248 outb((addr
& 0xff), u_DataLo
);
249 outb(((addr
>> 8) & 0xff), u_DataHi
);
251 outb((0x44), u_Command
);
252 outb(((addr
>> 16) & 0xff), u_DataHi
);
254 restore_flags(flags
);
259 void gus_write8(int reg
, unsigned int data
)
260 { /* Writes to an indirect register (8 bit) */
266 outb((reg
), u_Command
);
267 outb(((unsigned char) (data
& 0xff)), u_DataHi
);
269 restore_flags(flags
);
272 static unsigned char gus_read8(int reg
)
274 /* Reads from an indirect register (8 bit). Offset 0x80. */
280 outb((reg
| 0x80), u_Command
);
282 restore_flags(flags
);
287 static unsigned char gus_look8(int reg
)
289 /* Reads from an indirect register (8 bit). No additional offset. */
295 outb((reg
), u_Command
);
297 restore_flags(flags
);
302 static void gus_write16(int reg
, unsigned int data
)
304 /* Writes to an indirect register (16 bit) */
310 outb((reg
), u_Command
);
312 outb(((unsigned char) (data
& 0xff)), u_DataLo
);
313 outb(((unsigned char) ((data
>> 8) & 0xff)), u_DataHi
);
315 restore_flags(flags
);
318 static unsigned short gus_read16(int reg
)
320 /* Reads from an indirect register (16 bit). Offset 0x80. */
322 unsigned char hi
, lo
;
327 outb((reg
| 0x80), u_Command
);
332 restore_flags(flags
);
334 return ((hi
<< 8) & 0xff00) | lo
;
337 static unsigned short gus_look16(int reg
)
339 /* Reads from an indirect register (16 bit). No additional offset. */
341 unsigned char hi
, lo
;
346 outb((reg
), u_Command
);
351 restore_flags(flags
);
353 return ((hi
<< 8) & 0xff00) | lo
;
356 static void gus_write_addr(int reg
, unsigned long address
, int frac
, int is16bit
)
358 /* Writes an 24 bit memory address */
359 unsigned long hold_address
;
368 /* Interwave spesific address translations */
374 * Special processing required for 16 bit patches
377 hold_address
= address
;
378 address
= address
>> 1;
379 address
&= 0x0001ffffL
;
380 address
|= (hold_address
& 0x000c0000L
);
383 gus_write16(reg
, (unsigned short) ((address
>> 7) & 0xffff));
384 gus_write16(reg
+ 1, (unsigned short) ((address
<< 9) & 0xffff)
386 /* Could writing twice fix problems with GUS_VOICE_POS()? Let's try. */
388 gus_write16(reg
, (unsigned short) ((address
>> 7) & 0xffff));
389 gus_write16(reg
+ 1, (unsigned short) ((address
<< 9) & 0xffff)
391 restore_flags(flags
);
394 static void gus_select_voice(int voice
)
396 if (voice
< 0 || voice
> 31)
398 outb((voice
), u_Voice
);
401 static void gus_select_max_voices(int nvoices
)
410 voice_alloc
->max_voice
= nr_voices
= nvoices
;
411 gus_write8(0x0e, (nvoices
- 1) | 0xc0);
414 static void gus_voice_on(unsigned int mode
)
416 gus_write8(0x00, (unsigned char) (mode
& 0xfc));
418 gus_write8(0x00, (unsigned char) (mode
& 0xfc));
421 static void gus_voice_off(void)
423 gus_write8(0x00, gus_read8(0x00) | 0x03);
426 static void gus_voice_mode(unsigned int m
)
428 unsigned char mode
= (unsigned char) (m
& 0xff);
430 gus_write8(0x00, (gus_read8(0x00) & 0x03) |
431 (mode
& 0xfc)); /* Don't touch last two bits */
433 gus_write8(0x00, (gus_read8(0x00) & 0x03) | (mode
& 0xfc));
436 static void gus_voice_freq(unsigned long freq
)
438 unsigned long divisor
= freq_div_table
[nr_voices
- 14];
441 /* Interwave plays at 44100 Hz with any number of voices */
443 fc
= (unsigned short) (((freq
<< 9) + (44100 >> 1)) / 44100);
445 fc
= (unsigned short) (((freq
<< 9) + (divisor
>> 1)) / divisor
);
448 gus_write16(0x01, fc
);
451 static void gus_voice_volume(unsigned int vol
)
453 gus_write8(0x0d, 0x03); /* Stop ramp before setting volume */
454 gus_write16(0x09, (unsigned short) (vol
<< 4));
457 static void gus_voice_balance(unsigned int balance
)
459 gus_write8(0x0c, (unsigned char) (balance
& 0xff));
462 static void gus_ramp_range(unsigned int low
, unsigned int high
)
464 gus_write8(0x07, (unsigned char) ((low
>> 4) & 0xff));
465 gus_write8(0x08, (unsigned char) ((high
>> 4) & 0xff));
468 static void gus_ramp_rate(unsigned int scale
, unsigned int rate
)
470 gus_write8(0x06, (unsigned char) (((scale
& 0x03) << 6) | (rate
& 0x3f)));
473 static void gus_rampon(unsigned int m
)
475 unsigned char mode
= (unsigned char) (m
& 0xff);
477 gus_write8(0x0d, mode
& 0xfc);
479 gus_write8(0x0d, mode
& 0xfc);
482 static void gus_ramp_mode(unsigned int m
)
484 unsigned char mode
= (unsigned char) (m
& 0xff);
486 gus_write8(0x0d, (gus_read8(0x0d) & 0x03) |
487 (mode
& 0xfc)); /* Leave the last 2 bits alone */
489 gus_write8(0x0d, (gus_read8(0x0d) & 0x03) | (mode
& 0xfc));
492 static void gus_rampoff(void)
494 gus_write8(0x0d, 0x03);
497 static void gus_set_voice_pos(int voice
, long position
)
501 if ((sample_no
= sample_map
[voice
]) != -1) {
502 if (position
< samples
[sample_no
].len
) {
503 if (voices
[voice
].volume_irq_mode
== VMODE_START_NOTE
)
504 voices
[voice
].offset_pending
= position
;
506 gus_write_addr(0x0a, sample_ptrs
[sample_no
] + position
, 0,
507 samples
[sample_no
].mode
& WAVE_16_BITS
);
512 static void gus_voice_init(int voice
)
518 gus_select_voice(voice
);
521 gus_write_addr(0x0a, 0, 0, 0); /* Set current position to 0 */
522 gus_write8(0x00, 0x03); /* Voice off */
523 gus_write8(0x0d, 0x03); /* Ramping off */
524 voice_alloc
->map
[voice
] = 0;
525 voice_alloc
->alloc_times
[voice
] = 0;
526 restore_flags(flags
);
530 static void gus_voice_init2(int voice
)
532 voices
[voice
].panning
= 0;
533 voices
[voice
].mode
= 0;
534 voices
[voice
].orig_freq
= 20000;
535 voices
[voice
].current_freq
= 20000;
536 voices
[voice
].bender
= 0;
537 voices
[voice
].bender_range
= 200;
538 voices
[voice
].initial_volume
= 0;
539 voices
[voice
].current_volume
= 0;
540 voices
[voice
].loop_irq_mode
= 0;
541 voices
[voice
].loop_irq_parm
= 0;
542 voices
[voice
].volume_irq_mode
= 0;
543 voices
[voice
].volume_irq_parm
= 0;
544 voices
[voice
].env_phase
= 0;
545 voices
[voice
].main_vol
= 127;
546 voices
[voice
].patch_vol
= 127;
547 voices
[voice
].expression_vol
= 127;
548 voices
[voice
].sample_pending
= -1;
549 voices
[voice
].fixed_pitch
= 0;
552 static void step_envelope(int voice
)
554 unsigned vol
, prev_vol
, phase
;
558 if (voices
[voice
].mode
& WAVE_SUSTAIN_ON
&& voices
[voice
].env_phase
== 2)
562 gus_select_voice(voice
);
564 restore_flags(flags
);
567 * Sustain phase begins. Continue envelope after receiving note off.
570 if (voices
[voice
].env_phase
>= 5)
572 /* Envelope finished. Shoot the voice down */
573 gus_voice_init(voice
);
576 prev_vol
= voices
[voice
].current_volume
;
577 phase
= ++voices
[voice
].env_phase
;
578 compute_volume(voice
, voices
[voice
].midi_volume
);
579 vol
= voices
[voice
].initial_volume
* voices
[voice
].env_offset
[phase
] / 255;
580 rate
= voices
[voice
].env_rate
[phase
];
584 gus_select_voice(voice
);
586 gus_voice_volume(prev_vol
);
589 gus_write8(0x06, rate
); /* Ramping rate */
591 voices
[voice
].volume_irq_mode
= VMODE_ENVELOPE
;
593 if (((vol
- prev_vol
) / 64) == 0) /* No significant volume change */
595 restore_flags(flags
);
596 step_envelope(voice
); /* Continue the envelope on the next step */
601 if (vol
>= (4096 - 64))
603 gus_ramp_range(0, vol
);
604 gus_rampon(0x20); /* Increasing volume, with IRQ */
610 gus_ramp_range(vol
, 4030);
611 gus_rampon(0x60); /* Decreasing volume, with IRQ */
613 voices
[voice
].current_volume
= vol
;
614 restore_flags(flags
);
617 static void init_envelope(int voice
)
619 voices
[voice
].env_phase
= -1;
620 voices
[voice
].current_volume
= 64;
622 step_envelope(voice
);
625 static void start_release(int voice
, long int flags
)
627 if (gus_read8(0x00) & 0x03)
628 return; /* Voice already stopped */
630 voices
[voice
].env_phase
= 2; /* Will be incremented by step_envelope */
632 voices
[voice
].current_volume
= voices
[voice
].initial_volume
=
633 gus_read16(0x09) >> 4; /* Get current volume */
635 voices
[voice
].mode
&= ~WAVE_SUSTAIN_ON
;
637 restore_flags(flags
);
638 step_envelope(voice
);
641 static void gus_voice_fade(int voice
)
643 int instr_no
= sample_map
[voice
], is16bits
;
648 gus_select_voice(voice
);
650 if (instr_no
< 0 || instr_no
> MAX_SAMPLE
)
652 gus_write8(0x00, 0x03); /* Hard stop */
653 voice_alloc
->map
[voice
] = 0;
654 restore_flags(flags
);
657 is16bits
= (samples
[instr_no
].mode
& WAVE_16_BITS
) ? 1 : 0; /* 8 or 16 bits */
659 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
661 start_release(voice
, flags
);
662 restore_flags(flags
);
666 * Ramp the volume down but not too quickly.
668 if ((int) (gus_read16(0x09) >> 4) < 100) /* Get current volume */
672 gus_voice_init(voice
);
673 restore_flags(flags
);
676 gus_ramp_range(65, 4030);
678 gus_rampon(0x40 | 0x20); /* Down, once, with IRQ */
679 voices
[voice
].volume_irq_mode
= VMODE_HALT
;
680 restore_flags(flags
);
683 static void gus_reset(void)
687 gus_select_max_voices(24);
690 volume_method
= VOL_METHOD_ADAGIO
;
692 for (i
= 0; i
< 32; i
++)
694 gus_voice_init(i
); /* Turn voice off */
699 static void gus_initialize(void)
702 unsigned char dma_image
, irq_image
, tmp
;
704 static unsigned char gus_irq_map
[16] = {
705 0, 0, 0, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7
708 static unsigned char gus_dma_map
[8] = {
709 0, 1, 0, 2, 0, 3, 4, 5
714 gus_write8(0x4c, 0); /* Reset GF1 */
718 gus_write8(0x4c, 1); /* Release Reset */
723 * Clear all interrupts
726 gus_write8(0x41, 0); /* DMA control */
727 gus_write8(0x45, 0); /* Timer control */
728 gus_write8(0x49, 0); /* Sample control */
730 gus_select_max_voices(24);
732 inb(u_Status
); /* Touch the status register */
734 gus_look8(0x41); /* Clear any pending DMA IRQs */
735 gus_look8(0x49); /* Clear any pending sample IRQs */
736 gus_read8(0x0f); /* Clear pending IRQs */
738 gus_reset(); /* Resets all voices */
740 gus_look8(0x41); /* Clear any pending DMA IRQs */
741 gus_look8(0x49); /* Clear any pending sample IRQs */
742 gus_read8(0x0f); /* Clear pending IRQs */
744 gus_write8(0x4c, 7); /* Master reset | DAC enable | IRQ enable */
747 * Set up for Digital ASIC
750 outb((0x05), gus_base
+ 0x0f);
752 mix_image
|= 0x02; /* Disable line out (for a moment) */
753 outb((mix_image
), u_Mixer
);
755 outb((0x00), u_IRQDMAControl
);
757 outb((0x00), gus_base
+ 0x0f);
760 * Now set up the DMA and IRQ interface
762 * The GUS supports two IRQs and two DMAs.
764 * Just one DMA channel is used. This prevents simultaneous ADC and DAC.
765 * Adding this support requires significant changes to the dmabuf.c, dsp.c
770 tmp
= gus_irq_map
[gus_irq
];
771 if (!gus_pnp_flag
&& !tmp
)
772 printk(KERN_WARNING
"Warning! GUS IRQ not selected\n");
774 irq_image
|= 0x40; /* Combine IRQ1 (GF1) and IRQ2 (Midi) */
777 if (gus_dma2
== gus_dma
|| gus_dma2
== -1)
780 dma_image
= 0x40; /* Combine DMA1 (DRAM) and IRQ2 (ADC) */
782 tmp
= gus_dma_map
[gus_dma
];
784 printk(KERN_WARNING
"Warning! GUS DMA not selected\n");
790 /* Setup dual DMA channel mode for GUS MAX */
792 dma_image
= gus_dma_map
[gus_dma
];
794 printk(KERN_WARNING
"Warning! GUS DMA not selected\n");
796 tmp
= gus_dma_map
[gus_dma2
] << 3;
799 printk(KERN_WARNING
"Warning! Invalid GUS MAX DMA\n");
800 tmp
= 0x40; /* Combine DMA channels */
807 * For some reason the IRQ and DMA addresses must be written twice
811 * Doing it first time
814 outb((mix_image
), u_Mixer
); /* Select DMA control */
815 outb((dma_image
| 0x80), u_IRQDMAControl
); /* Set DMA address */
817 outb((mix_image
| 0x40), u_Mixer
); /* Select IRQ control */
818 outb((irq_image
), u_IRQDMAControl
); /* Set IRQ address */
821 * Doing it second time
824 outb((mix_image
), u_Mixer
); /* Select DMA control */
825 outb((dma_image
), u_IRQDMAControl
); /* Set DMA address */
827 outb((mix_image
| 0x40), u_Mixer
); /* Select IRQ control */
828 outb((irq_image
), u_IRQDMAControl
); /* Set IRQ address */
830 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
832 mix_image
&= ~0x02; /* Enable line out */
833 mix_image
|= 0x08; /* Enable IRQ */
834 outb((mix_image
), u_Mixer
); /*
835 * Turn mixer channels on
836 * Note! Mic in is left off.
839 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
841 gusintr(gus_irq
, (void *)gus_hw_config
, NULL
); /* Serve pending interrupts */
843 inb(u_Status
); /* Touch the status register */
845 gus_look8(0x41); /* Clear any pending DMA IRQs */
846 gus_look8(0x49); /* Clear any pending sample IRQs */
848 gus_read8(0x0f); /* Clear pending IRQs */
851 gus_write8(0x19, gus_read8(0x19) | 0x01);
852 restore_flags(flags
);
856 static void pnp_mem_init(void)
859 #define CHUNK_SIZE (256*1024)
860 #define BANK_SIZE (4*1024*1024)
861 #define CHUNKS_PER_BANK (BANK_SIZE/CHUNK_SIZE)
863 int bank
, chunk
, addr
, total
= 0;
865 int i
, j
, bits
= -1, testbits
= -1, nbanks
= 0;
868 * This routine determines what kind of RAM is installed in each of the four
869 * SIMM banks and configures the DRAM address decode logic accordingly.
873 * Place the chip into enhanced mode
875 gus_write8(0x19, gus_read8(0x19) | 0x01);
876 gus_write8(0x53, gus_look8(0x53) & ~0x02); /* Select DRAM I/O access */
879 * Set memory configuration to 4 DRAM banks of 4M in each (16M total).
882 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | 0x000c);
885 * Perform the DRAM size detection for each bank individually.
887 for (bank
= 0; bank
< 4; bank
++)
891 addr
= bank
* BANK_SIZE
;
893 /* Clean check points of each chunk */
894 for (chunk
= 0; chunk
< CHUNKS_PER_BANK
; chunk
++)
896 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 0L, 0x00);
897 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 1L, 0x00);
900 /* Write a value to each chunk point and verify the result */
901 for (chunk
= 0; chunk
< CHUNKS_PER_BANK
; chunk
++)
903 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 0L, 0x55);
904 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 1L, 0xAA);
906 if (gus_peek(addr
+ chunk
* CHUNK_SIZE
+ 0L) == 0x55 &&
907 gus_peek(addr
+ chunk
* CHUNK_SIZE
+ 1L) == 0xAA)
909 /* OK. There is RAM. Now check for possible shadows */
912 for (chunk2
= 0; ok
&& chunk2
< chunk
; chunk2
++)
913 if (gus_peek(addr
+ chunk2
* CHUNK_SIZE
+ 0L) ||
914 gus_peek(addr
+ chunk2
* CHUNK_SIZE
+ 1L))
915 ok
= 0; /* Addressing wraps */
918 size
= (chunk
+ 1) * CHUNK_SIZE
;
920 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 0L, 0x00);
921 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 1L, 0x00);
923 bank_sizes
[bank
] = size
;
926 DDB(printk("Interwave: Bank %d, size=%dk\n", bank
, size
/ 1024));
929 if (nbanks
== 0) /* No RAM - Give up */
931 printk(KERN_ERR
"Sound: An Interwave audio chip detected but no DRAM\n");
932 printk(KERN_ERR
"Sound: Unable to work with this card.\n");
933 gus_write8(0x19, gus_read8(0x19) & ~0x01);
939 * Now we know how much DRAM there is in each bank. The next step is
940 * to find a DRAM size encoding (0 to 12) which is best for the combination
943 * First try if any of the possible alternatives matches exactly the amount
947 for (i
= 0; bits
== -1 && i
< 13; i
++)
951 for (j
= 0; bits
!= -1 && j
< 4; j
++)
952 if (mem_decode
[i
][j
] != bank_sizes
[j
])
953 bits
= -1; /* No hit */
957 * If necessary, try to find a combination where other than the last
958 * bank matches our configuration and the last bank is left oversized.
959 * In this way we don't leave holes in the middle of memory.
962 if (bits
== -1) /* No luck yet */
964 for (i
= 0; bits
== -1 && i
< 13; i
++)
968 for (j
= 0; bits
!= -1 && j
< nbanks
- 1; j
++)
969 if (mem_decode
[i
][j
] != bank_sizes
[j
])
970 bits
= -1; /* No hit */
971 if (mem_decode
[i
][nbanks
- 1] < bank_sizes
[nbanks
- 1])
972 bits
= -1; /* The last bank is too small */
976 * The last resort is to search for a combination where the banks are
977 * smaller than the actual SIMMs. This leaves some memory in the banks
978 * unused but doesn't leave holes in the DRAM address space.
980 if (bits
== -1) /* No luck yet */
982 for (i
= 0; i
< 13; i
++)
985 for (j
= 0; testbits
!= -1 && j
< nbanks
- 1; j
++)
986 if (mem_decode
[i
][j
] > bank_sizes
[j
]) {
989 if(testbits
> bits
) bits
= testbits
;
993 printk(KERN_INFO
"Interwave: Can't use all installed RAM.\n");
994 printk(KERN_INFO
"Interwave: Try reordering SIMMS.\n");
996 printk(KERN_INFO
"Interwave: Can't find working DRAM encoding.\n");
997 printk(KERN_INFO
"Interwave: Defaulting to 256k. Try reordering SIMMS.\n");
1000 DDB(printk("Interwave: Selecting DRAM addressing mode %d\n", bits
));
1002 for (bank
= 0; bank
< 4; bank
++)
1004 DDB(printk(" Bank %d, mem=%dk (limit %dk)\n", bank
, bank_sizes
[bank
] / 1024, mem_decode
[bits
][bank
] / 1024));
1006 if (bank_sizes
[bank
] > mem_decode
[bits
][bank
])
1007 total
+= mem_decode
[bits
][bank
];
1009 total
+= bank_sizes
[bank
];
1012 DDB(printk("Total %dk of DRAM (enhanced mode)\n", total
/ 1024));
1015 * Set the memory addressing mode.
1017 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | bits
);
1019 /* Leave the chip into enhanced mode. Disable LFO */
1020 gus_mem_size
= total
;
1022 gus_write8(0x19, (gus_read8(0x19) | 0x01) & ~0x02);
1025 int gus_wave_detect(int baseaddr
)
1027 unsigned long i
, max_mem
= 1024L;
1031 gus_base
= baseaddr
;
1033 gus_write8(0x4c, 0); /* Reset GF1 */
1037 gus_write8(0x4c, 1); /* Release Reset */
1041 #ifdef GUSPNP_AUTODETECT
1042 val
= gus_look8(0x5b); /* Version number register */
1043 gus_write8(0x5b, ~val
); /* Invert all bits */
1045 if ((gus_look8(0x5b) & 0xf0) == (val
& 0xf0)) /* No change */
1047 if ((gus_look8(0x5b) & 0x0f) == ((~val
) & 0x0f)) /* Change */
1049 DDB(printk("Interwave chip version %d detected\n", (val
& 0xf0) >> 4));
1054 DDB(printk("Not an Interwave chip (%x)\n", gus_look8(0x5b)));
1058 gus_write8(0x5b, val
); /* Restore all bits */
1066 /* See if there is first block there.... */
1068 if (gus_peek(0L) != 0xaa)
1071 /* Now zero it out so that I can check for mirroring .. */
1073 for (i
= 1L; i
< max_mem
; i
++)
1077 /* check for mirroring ... */
1078 if (gus_peek(0L) != 0)
1082 for (n
= loc
- 1, failed
= 0; n
<= loc
; n
++)
1084 gus_poke(loc
, 0xaa);
1085 if (gus_peek(loc
) != 0xaa)
1087 gus_poke(loc
, 0x55);
1088 if (gus_peek(loc
) != 0x55)
1094 gus_mem_size
= i
<< 10;
1098 static int guswave_ioctl(int dev
, unsigned int cmd
, caddr_t arg
)
1103 case SNDCTL_SYNTH_INFO
:
1104 gus_info
.nr_voices
= nr_voices
;
1105 if (copy_to_user(arg
, &gus_info
, sizeof(gus_info
)))
1109 case SNDCTL_SEQ_RESETSAMPLES
:
1110 reset_sample_memory();
1113 case SNDCTL_SEQ_PERCMODE
:
1116 case SNDCTL_SYNTH_MEMAVL
:
1117 return (gus_mem_size
== 0) ? 0 : gus_mem_size
- free_mem_ptr
- 32;
1124 static int guswave_set_instr(int dev
, int voice
, int instr_no
)
1128 if (instr_no
< 0 || instr_no
> MAX_PATCH
)
1129 instr_no
= 0; /* Default to acoustic piano */
1131 if (voice
< 0 || voice
> 31)
1134 if (voices
[voice
].volume_irq_mode
== VMODE_START_NOTE
)
1136 voices
[voice
].sample_pending
= instr_no
;
1139 sample_no
= patch_table
[instr_no
];
1140 patch_map
[voice
] = -1;
1142 if (sample_no
== NOT_SAMPLE
)
1144 /* printk("GUS: Undefined patch %d for voice %d\n", instr_no, voice);*/
1145 return -EINVAL
; /* Patch not defined */
1147 if (sample_ptrs
[sample_no
] == -1) /* Sample not loaded */
1149 /* printk("GUS: Sample #%d not loaded for patch %d (voice %d)\n", sample_no, instr_no, voice);*/
1152 sample_map
[voice
] = sample_no
;
1153 patch_map
[voice
] = instr_no
;
1157 static int guswave_kill_note(int dev
, int voice
, int note
, int velocity
)
1159 unsigned long flags
;
1163 /* voice_alloc->map[voice] = 0xffff; */
1164 if (voices
[voice
].volume_irq_mode
== VMODE_START_NOTE
)
1166 voices
[voice
].kill_pending
= 1;
1167 restore_flags(flags
);
1171 restore_flags(flags
);
1172 gus_voice_fade(voice
);
1178 static void guswave_aftertouch(int dev
, int voice
, int pressure
)
1182 static void guswave_panning(int dev
, int voice
, int value
)
1184 if (voice
>= 0 || voice
< 32)
1185 voices
[voice
].panning
= value
;
1188 static void guswave_volume_method(int dev
, int mode
)
1190 if (mode
== VOL_METHOD_LINEAR
|| mode
== VOL_METHOD_ADAGIO
)
1191 volume_method
= mode
;
1194 static void compute_volume(int voice
, int volume
)
1197 voices
[voice
].midi_volume
= volume
;
1199 switch (volume_method
)
1201 case VOL_METHOD_ADAGIO
:
1202 voices
[voice
].initial_volume
=
1203 gus_adagio_vol(voices
[voice
].midi_volume
, voices
[voice
].main_vol
,
1204 voices
[voice
].expression_vol
,
1205 voices
[voice
].patch_vol
);
1208 case VOL_METHOD_LINEAR
: /* Totally ignores patch-volume and expression */
1209 voices
[voice
].initial_volume
= gus_linear_vol(volume
, voices
[voice
].main_vol
);
1213 voices
[voice
].initial_volume
= volume_base
+
1214 (voices
[voice
].midi_volume
* volume_scale
);
1217 if (voices
[voice
].initial_volume
> 4030)
1218 voices
[voice
].initial_volume
= 4030;
1221 static void compute_and_set_volume(int voice
, int volume
, int ramp_time
)
1223 int curr
, target
, rate
;
1224 unsigned long flags
;
1226 compute_volume(voice
, volume
);
1227 voices
[voice
].current_volume
= voices
[voice
].initial_volume
;
1232 * CAUTION! Interrupts disabled. Enable them before returning
1235 gus_select_voice(voice
);
1237 curr
= gus_read16(0x09) >> 4;
1238 target
= voices
[voice
].initial_volume
;
1240 if (ramp_time
== INSTANT_RAMP
)
1243 gus_voice_volume(target
);
1244 restore_flags(flags
);
1247 if (ramp_time
== FAST_RAMP
)
1251 gus_ramp_rate(0, rate
);
1253 if ((target
- curr
) / 64 == 0) /* Close enough to target. */
1256 gus_voice_volume(target
);
1257 restore_flags(flags
);
1262 if (target
> (4095 - 65))
1264 gus_ramp_range(curr
, target
);
1265 gus_rampon(0x00); /* Ramp up, once, no IRQ */
1272 gus_ramp_range(target
, curr
);
1273 gus_rampon(0x40); /* Ramp down, once, no irq */
1275 restore_flags(flags
);
1278 static void dynamic_volume_change(int voice
)
1280 unsigned char status
;
1281 unsigned long flags
;
1285 gus_select_voice(voice
);
1286 status
= gus_read8(0x00); /* Get voice status */
1287 restore_flags(flags
);
1290 return; /* Voice was not running */
1292 if (!(voices
[voice
].mode
& WAVE_ENVELOPES
))
1294 compute_and_set_volume(voice
, voices
[voice
].midi_volume
, 1);
1299 * Voice is running and has envelopes.
1304 gus_select_voice(voice
);
1305 status
= gus_read8(0x0d); /* Ramping status */
1306 restore_flags(flags
);
1308 if (status
& 0x03) /* Sustain phase? */
1310 compute_and_set_volume(voice
, voices
[voice
].midi_volume
, 1);
1313 if (voices
[voice
].env_phase
< 0)
1316 compute_volume(voice
, voices
[voice
].midi_volume
);
1320 static void guswave_controller(int dev
, int voice
, int ctrl_num
, int value
)
1322 unsigned long flags
;
1325 if (voice
< 0 || voice
> 31)
1330 case CTRL_PITCH_BENDER
:
1331 voices
[voice
].bender
= value
;
1333 if (voices
[voice
].volume_irq_mode
!= VMODE_START_NOTE
)
1335 freq
= compute_finetune(voices
[voice
].orig_freq
, value
, voices
[voice
].bender_range
, 0);
1336 voices
[voice
].current_freq
= freq
;
1340 gus_select_voice(voice
);
1341 gus_voice_freq(freq
);
1342 restore_flags(flags
);
1346 case CTRL_PITCH_BENDER_RANGE
:
1347 voices
[voice
].bender_range
= value
;
1349 case CTL_EXPRESSION
:
1351 case CTRL_EXPRESSION
:
1352 if (volume_method
== VOL_METHOD_ADAGIO
)
1354 voices
[voice
].expression_vol
= value
;
1355 if (voices
[voice
].volume_irq_mode
!= VMODE_START_NOTE
)
1356 dynamic_volume_change(voice
);
1361 voices
[voice
].panning
= (value
* 2) - 128;
1364 case CTL_MAIN_VOLUME
:
1365 value
= (value
* 100) / 16383;
1367 case CTRL_MAIN_VOLUME
:
1368 voices
[voice
].main_vol
= value
;
1369 if (voices
[voice
].volume_irq_mode
!= VMODE_START_NOTE
)
1370 dynamic_volume_change(voice
);
1378 static int guswave_start_note2(int dev
, int voice
, int note_num
, int volume
)
1380 int sample
, best_sample
, best_delta
, delta_freq
;
1381 int is16bits
, samplep
, patch
, pan
;
1382 unsigned long note_freq
, base_note
, freq
, flags
;
1383 unsigned char mode
= 0;
1385 if (voice
< 0 || voice
> 31)
1387 /* printk("GUS: Invalid voice\n");*/
1390 if (note_num
== 255)
1392 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
1394 voices
[voice
].midi_volume
= volume
;
1395 dynamic_volume_change(voice
);
1398 compute_and_set_volume(voice
, volume
, 1);
1401 if ((patch
= patch_map
[voice
]) == -1)
1403 if ((samplep
= patch_table
[patch
]) == NOT_SAMPLE
)
1407 note_freq
= note_to_freq(note_num
);
1410 * Find a sample within a patch so that the note_freq is between low_note
1415 best_sample
= samplep
;
1416 best_delta
= 1000000;
1417 while (samplep
!= 0 && samplep
!= NOT_SAMPLE
&& sample
== -1)
1419 delta_freq
= note_freq
- samples
[samplep
].base_note
;
1421 delta_freq
= -delta_freq
;
1422 if (delta_freq
< best_delta
)
1424 best_sample
= samplep
;
1425 best_delta
= delta_freq
;
1427 if (samples
[samplep
].low_note
<= note_freq
&&
1428 note_freq
<= samples
[samplep
].high_note
)
1433 samplep
= samples
[samplep
].key
; /* Link to next sample */
1436 sample
= best_sample
;
1440 /* printk("GUS: Patch %d not defined for note %d\n", patch, note_num);*/
1441 return 0; /* Should play default patch ??? */
1443 is16bits
= (samples
[sample
].mode
& WAVE_16_BITS
) ? 1 : 0;
1444 voices
[voice
].mode
= samples
[sample
].mode
;
1445 voices
[voice
].patch_vol
= samples
[sample
].volume
;
1448 gus_write8(0x15, 0x00); /* RAM, Reset voice deactivate bit of SMSI */
1450 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
1454 for (i
= 0; i
< 6; i
++)
1456 voices
[voice
].env_rate
[i
] = samples
[sample
].env_rate
[i
];
1457 voices
[voice
].env_offset
[i
] = samples
[sample
].env_offset
[i
];
1460 sample_map
[voice
] = sample
;
1462 if (voices
[voice
].fixed_pitch
) /* Fixed pitch */
1464 freq
= samples
[sample
].base_freq
;
1468 base_note
= samples
[sample
].base_note
/ 100;
1471 freq
= samples
[sample
].base_freq
* note_freq
/ base_note
;
1474 voices
[voice
].orig_freq
= freq
;
1477 * Since the pitch bender may have been set before playing the note, we
1478 * have to calculate the bending now.
1481 freq
= compute_finetune(voices
[voice
].orig_freq
, voices
[voice
].bender
,
1482 voices
[voice
].bender_range
, 0);
1483 voices
[voice
].current_freq
= freq
;
1485 pan
= (samples
[sample
].panning
+ voices
[voice
].panning
) / 32;
1492 if (samples
[sample
].mode
& WAVE_16_BITS
)
1494 mode
|= 0x04; /* 16 bits */
1495 if ((sample_ptrs
[sample
] / GUS_BANK_SIZE
) !=
1496 ((sample_ptrs
[sample
] + samples
[sample
].len
) / GUS_BANK_SIZE
))
1497 printk(KERN_ERR
"GUS: Sample address error\n");
1499 /*************************************************************************
1500 * CAUTION! Interrupts disabled. Don't return before enabling
1501 *************************************************************************/
1505 gus_select_voice(voice
);
1509 restore_flags(flags
);
1511 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
1513 compute_volume(voice
, volume
);
1514 init_envelope(voice
);
1518 compute_and_set_volume(voice
, volume
, 0);
1523 gus_select_voice(voice
);
1525 if (samples
[sample
].mode
& WAVE_LOOP_BACK
)
1526 gus_write_addr(0x0a, sample_ptrs
[sample
] + samples
[sample
].len
-
1527 voices
[voice
].offset_pending
, 0, is16bits
); /* start=end */
1529 gus_write_addr(0x0a, sample_ptrs
[sample
] + voices
[voice
].offset_pending
, 0, is16bits
); /* Sample start=begin */
1531 if (samples
[sample
].mode
& WAVE_LOOPING
)
1535 if (samples
[sample
].mode
& WAVE_BIDIR_LOOP
)
1538 if (samples
[sample
].mode
& WAVE_LOOP_BACK
)
1540 gus_write_addr(0x0a, sample_ptrs
[sample
] + samples
[sample
].loop_end
-
1541 voices
[voice
].offset_pending
,
1542 (samples
[sample
].fractions
>> 4) & 0x0f, is16bits
);
1545 gus_write_addr(0x02, sample_ptrs
[sample
] + samples
[sample
].loop_start
,
1546 samples
[sample
].fractions
& 0x0f, is16bits
); /* Loop start location */
1547 gus_write_addr(0x04, sample_ptrs
[sample
] + samples
[sample
].loop_end
,
1548 (samples
[sample
].fractions
>> 4) & 0x0f, is16bits
); /* Loop end location */
1552 mode
|= 0x20; /* Loop IRQ at the end */
1553 voices
[voice
].loop_irq_mode
= LMODE_FINISH
; /* Ramp down at the end */
1554 voices
[voice
].loop_irq_parm
= 1;
1555 gus_write_addr(0x02, sample_ptrs
[sample
], 0, is16bits
); /* Loop start location */
1556 gus_write_addr(0x04, sample_ptrs
[sample
] + samples
[sample
].len
- 1,
1557 (samples
[sample
].fractions
>> 4) & 0x0f, is16bits
); /* Loop end location */
1559 gus_voice_freq(freq
);
1560 gus_voice_balance(pan
);
1562 restore_flags(flags
);
1568 * New guswave_start_note by Andrew J. Robinson attempts to minimize clicking
1569 * when the note playing on the voice is changed. It uses volume
1573 static int guswave_start_note(int dev
, int voice
, int note_num
, int volume
)
1581 if (note_num
== 255)
1583 if (voices
[voice
].volume_irq_mode
== VMODE_START_NOTE
)
1585 voices
[voice
].volume_pending
= volume
;
1589 ret_val
= guswave_start_note2(dev
, voice
, note_num
, volume
);
1594 gus_select_voice(voice
);
1595 mode
= gus_read8(0x00);
1597 gus_write8(0x00, mode
& 0xdf); /* No interrupt! */
1599 voices
[voice
].offset_pending
= 0;
1600 voices
[voice
].kill_pending
= 0;
1601 voices
[voice
].volume_irq_mode
= 0;
1602 voices
[voice
].loop_irq_mode
= 0;
1604 if (voices
[voice
].sample_pending
>= 0)
1606 restore_flags(flags
); /* Run temporarily with interrupts enabled */
1607 guswave_set_instr(voices
[voice
].dev_pending
, voice
, voices
[voice
].sample_pending
);
1608 voices
[voice
].sample_pending
= -1;
1611 gus_select_voice(voice
); /* Reselect the voice (just to be sure) */
1613 if ((mode
& 0x01) || (int) ((gus_read16(0x09) >> 4) < (unsigned) 2065))
1615 ret_val
= guswave_start_note2(dev
, voice
, note_num
, volume
);
1619 voices
[voice
].dev_pending
= dev
;
1620 voices
[voice
].note_pending
= note_num
;
1621 voices
[voice
].volume_pending
= volume
;
1622 voices
[voice
].volume_irq_mode
= VMODE_START_NOTE
;
1625 gus_ramp_range(2000, 4065);
1626 gus_ramp_rate(0, 63); /* Fastest possible rate */
1627 gus_rampon(0x20 | 0x40); /* Ramp down, once, irq */
1630 restore_flags(flags
);
1634 static void guswave_reset(int dev
)
1638 for (i
= 0; i
< 32; i
++)
1645 static int guswave_open(int dev
, int mode
)
1652 voice_alloc
->timestamp
= 0;
1654 if (gus_no_wave_dma
) {
1657 if ((err
= DMAbuf_open_dma(gus_devnum
)) < 0)
1659 /* printk( "GUS: Loading samples without DMA\n"); */
1660 gus_no_dma
= 1; /* Upload samples using PIO */
1666 init_waitqueue_head(&dram_sleeper
);
1668 active_device
= GUS_DEV_WAVE
;
1670 gusintr(gus_irq
, (void *)gus_hw_config
, NULL
); /* Serve pending interrupts */
1673 gusintr(gus_irq
, (void *)gus_hw_config
, NULL
); /* Serve pending interrupts */
1678 static void guswave_close(int dev
)
1685 DMAbuf_close_dma(gus_devnum
);
1688 static int guswave_load_patch(int dev
, int format
, const char *addr
,
1689 int offs
, int count
, int pmgr_flag
)
1691 struct patch_info patch
;
1695 unsigned long blk_sz
, blk_end
, left
, src_offs
, target
;
1697 sizeof_patch
= (long) &patch
.data
[0] - (long) &patch
; /* Header size */
1699 if (format
!= GUS_PATCH
)
1701 /* printk("GUS Error: Invalid patch format (key) 0x%x\n", format);*/
1704 if (count
< sizeof_patch
)
1706 /* printk("GUS Error: Patch header too short\n");*/
1709 count
-= sizeof_patch
;
1711 if (free_sample
>= MAX_SAMPLE
)
1713 /* printk("GUS: Sample table full\n");*/
1717 * Copy the header from user space but ignore the first bytes which have
1718 * been transferred already.
1721 copy_from_user(&((char *) &patch
)[offs
], &(addr
)[offs
], sizeof_patch
- offs
);
1723 if (patch
.mode
& WAVE_ROM
)
1725 if (gus_mem_size
== 0)
1728 instr
= patch
.instr_no
;
1730 if (instr
< 0 || instr
> MAX_PATCH
)
1732 /* printk(KERN_ERR "GUS: Invalid patch number %d\n", instr);*/
1735 if (count
< patch
.len
)
1737 /* printk(KERN_ERR "GUS Warning: Patch record too short (%d<%d)\n", count, (int) patch.len);*/
1740 if (patch
.len
<= 0 || patch
.len
> gus_mem_size
)
1742 /* printk(KERN_ERR "GUS: Invalid sample length %d\n", (int) patch.len);*/
1745 if (patch
.mode
& WAVE_LOOPING
)
1747 if (patch
.loop_start
< 0 || patch
.loop_start
>= patch
.len
)
1749 /* printk(KERN_ERR "GUS: Invalid loop start\n");*/
1752 if (patch
.loop_end
< patch
.loop_start
|| patch
.loop_end
> patch
.len
)
1754 /* printk(KERN_ERR "GUS: Invalid loop end\n");*/
1758 free_mem_ptr
= (free_mem_ptr
+ 31) & ~31; /* 32 byte alignment */
1760 if (patch
.mode
& WAVE_16_BITS
)
1763 * 16 bit samples must fit one 256k bank.
1765 if (patch
.len
>= GUS_BANK_SIZE
)
1767 /* printk("GUS: Sample (16 bit) too long %d\n", (int) patch.len);*/
1770 if ((free_mem_ptr
/ GUS_BANK_SIZE
) !=
1771 ((free_mem_ptr
+ patch
.len
) / GUS_BANK_SIZE
))
1773 unsigned long tmp_mem
=
1775 ((free_mem_ptr
/ GUS_BANK_SIZE
) + 1) * GUS_BANK_SIZE
;
1777 if ((tmp_mem
+ patch
.len
) > gus_mem_size
)
1780 free_mem_ptr
= tmp_mem
; /* This leaves unusable memory */
1783 if ((free_mem_ptr
+ patch
.len
) > gus_mem_size
)
1786 sample_ptrs
[free_sample
] = free_mem_ptr
;
1789 * Tremolo is not possible with envelopes
1792 if (patch
.mode
& WAVE_ENVELOPES
)
1793 patch
.mode
&= ~WAVE_TREMOLO
;
1795 if (!(patch
.mode
& WAVE_FRACTIONS
))
1797 patch
.fractions
= 0;
1799 memcpy((char *) &samples
[free_sample
], &patch
, sizeof_patch
);
1802 * Link this_one sample to the list of samples for patch 'instr'.
1805 samples
[free_sample
].key
= patch_table
[instr
];
1806 patch_table
[instr
] = free_sample
;
1809 * Use DMA to transfer the wave data to the DRAM
1814 target
= free_mem_ptr
;
1816 while (left
) /* Not completely transferred yet */
1818 blk_sz
= audio_devs
[gus_devnum
]->dmap_out
->bytes_in_use
;
1823 * DMA cannot cross bank (256k) boundaries. Check for that.
1826 blk_end
= target
+ blk_sz
;
1828 if ((target
/ GUS_BANK_SIZE
) != (blk_end
/ GUS_BANK_SIZE
))
1830 /* Split the block */
1831 blk_end
&= ~(GUS_BANK_SIZE
- 1);
1832 blk_sz
= blk_end
- target
;
1837 * For some reason the DMA is not possible. We have to use PIO.
1842 for (i
= 0; i
< blk_sz
; i
++)
1844 get_user(*(unsigned char *) &data
, (unsigned char *) &((addr
)[sizeof_patch
+ i
]));
1845 if (patch
.mode
& WAVE_UNSIGNED
)
1846 if (!(patch
.mode
& WAVE_16_BITS
) || (i
& 0x01))
1847 data
^= 0x80; /* Convert to signed */
1848 gus_poke(target
+ i
, data
);
1853 unsigned long address
, hold_address
;
1854 unsigned char dma_command
;
1855 unsigned long flags
;
1857 if (audio_devs
[gus_devnum
]->dmap_out
->raw_buf
== NULL
)
1859 printk(KERN_ERR
"GUS: DMA buffer == NULL\n");
1863 * OK, move now. First in and then out.
1866 copy_from_user(audio_devs
[gus_devnum
]->dmap_out
->raw_buf
, &(addr
)[sizeof_patch
+ src_offs
], blk_sz
);
1870 /******** INTERRUPTS DISABLED NOW ********/
1871 gus_write8(0x41, 0); /* Disable GF1 DMA */
1872 DMAbuf_start_dma(gus_devnum
, audio_devs
[gus_devnum
]->dmap_out
->raw_buf_phys
,
1873 blk_sz
, DMA_MODE_WRITE
);
1876 * Set the DRAM address for the wave data
1881 /* Different address translation in enhanced mode */
1886 address
= target
>> 1; /* Convert to 16 bit word address */
1890 hi
= (unsigned char) ((address
>> 16) & 0xf0);
1891 hi
+= (unsigned char) (address
& 0x0f);
1893 gus_write16(0x42, (address
>> 4) & 0xffff); /* DMA address (low) */
1894 gus_write8(0x50, hi
);
1899 if (audio_devs
[gus_devnum
]->dmap_out
->dma
> 3)
1901 hold_address
= address
;
1902 address
= address
>> 1;
1903 address
&= 0x0001ffffL
;
1904 address
|= (hold_address
& 0x000c0000L
);
1906 gus_write16(0x42, (address
>> 4) & 0xffff); /* DRAM DMA address */
1910 * Start the DMA transfer
1913 dma_command
= 0x21; /* IRQ enable, DMA start */
1914 if (patch
.mode
& WAVE_UNSIGNED
)
1915 dma_command
|= 0x80; /* Invert MSB */
1916 if (patch
.mode
& WAVE_16_BITS
)
1917 dma_command
|= 0x40; /* 16 bit _DATA_ */
1918 if (audio_devs
[gus_devnum
]->dmap_out
->dma
> 3)
1919 dma_command
|= 0x04; /* 16 bit DMA _channel_ */
1921 gus_write8(0x41, dma_command
); /* Lets go luteet (=bugs) */
1924 * Sleep here until the DRAM DMA done interrupt is served
1926 active_device
= GUS_DEV_WAVE
;
1928 if (!interruptible_sleep_on_timeout(&dram_sleeper
, HZ
))
1929 printk("GUS: DMA Transfer timed out\n");
1930 restore_flags(flags
);
1941 gus_write8(0x41, 0); /* Stop DMA */
1944 free_mem_ptr
+= patch
.len
;
1949 static void guswave_hw_control(int dev
, unsigned char *event_rec
)
1952 unsigned short p1
, p2
;
1957 voice
= event_rec
[3];
1958 p1
= *(unsigned short *) &event_rec
[4];
1959 p2
= *(unsigned short *) &event_rec
[6];
1960 plong
= *(unsigned int *) &event_rec
[4];
1962 if ((voices
[voice
].volume_irq_mode
== VMODE_START_NOTE
) &&
1963 (cmd
!= _GUS_VOICESAMPLE
) && (cmd
!= _GUS_VOICE_POS
))
1964 do_volume_irq(voice
);
1968 case _GUS_NUMVOICES
:
1971 gus_select_voice(voice
);
1972 gus_select_max_voices(p1
);
1973 restore_flags(flags
);
1976 case _GUS_VOICESAMPLE
:
1977 guswave_set_instr(dev
, voice
, p1
);
1983 gus_select_voice(voice
);
1984 p1
&= ~0x20; /* Don't allow interrupts */
1986 restore_flags(flags
);
1992 gus_select_voice(voice
);
1994 restore_flags(flags
);
1997 case _GUS_VOICEFADE
:
1998 gus_voice_fade(voice
);
2001 case _GUS_VOICEMODE
:
2004 gus_select_voice(voice
);
2005 p1
&= ~0x20; /* Don't allow interrupts */
2007 restore_flags(flags
);
2010 case _GUS_VOICEBALA
:
2013 gus_select_voice(voice
);
2014 gus_voice_balance(p1
);
2015 restore_flags(flags
);
2018 case _GUS_VOICEFREQ
:
2021 gus_select_voice(voice
);
2022 gus_voice_freq(plong
);
2023 restore_flags(flags
);
2029 gus_select_voice(voice
);
2030 gus_voice_volume(p1
);
2031 restore_flags(flags
);
2034 case _GUS_VOICEVOL2
: /* Just update the software voice level */
2035 voices
[voice
].initial_volume
= voices
[voice
].current_volume
= p1
;
2038 case _GUS_RAMPRANGE
:
2039 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
2043 gus_select_voice(voice
);
2044 gus_ramp_range(p1
, p2
);
2045 restore_flags(flags
);
2049 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
2050 break; /* NJET-NJET */
2053 gus_select_voice(voice
);
2054 gus_ramp_rate(p1
, p2
);
2055 restore_flags(flags
);
2059 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
2063 gus_select_voice(voice
);
2064 p1
&= ~0x20; /* Don't allow interrupts */
2066 restore_flags(flags
);
2070 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
2074 gus_select_voice(voice
);
2075 p1
&= ~0x20; /* Don't allow interrupts */
2077 restore_flags(flags
);
2081 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
2082 break; /* NEJ-NEJ */
2085 gus_select_voice(voice
);
2087 restore_flags(flags
);
2090 case _GUS_VOLUME_SCALE
:
2095 case _GUS_VOICE_POS
:
2098 gus_select_voice(voice
);
2099 gus_set_voice_pos(voice
, plong
);
2100 restore_flags(flags
);
2107 static int gus_audio_set_speed(int speed
)
2110 speed
= gus_audio_speed
;
2118 gus_audio_speed
= speed
;
2120 if (only_read_access
)
2122 /* Compute nearest valid recording speed and return it */
2124 /* speed = (9878400 / (gus_audio_speed + 2)) / 16; */
2125 speed
= (((9878400 + gus_audio_speed
/ 2) / (gus_audio_speed
+ 2)) + 8) / 16;
2126 speed
= (9878400 / (speed
* 16)) - 2;
2131 static int gus_audio_set_channels(int channels
)
2134 return gus_audio_channels
;
2139 gus_audio_channels
= channels
;
2143 static int gus_audio_set_bits(int bits
)
2146 return gus_audio_bits
;
2148 if (bits
!= 8 && bits
!= 16)
2154 gus_audio_bits
= bits
;
2158 static int gus_audio_ioctl(int dev
, unsigned int cmd
, caddr_t arg
)
2164 case SOUND_PCM_WRITE_RATE
:
2165 if (get_user(val
, (int *)arg
))
2167 val
= gus_audio_set_speed(val
);
2170 case SOUND_PCM_READ_RATE
:
2171 val
= gus_audio_speed
;
2174 case SNDCTL_DSP_STEREO
:
2175 if (get_user(val
, (int *)arg
))
2177 val
= gus_audio_set_channels(val
+ 1) - 1;
2180 case SOUND_PCM_WRITE_CHANNELS
:
2181 if (get_user(val
, (int *)arg
))
2183 val
= gus_audio_set_channels(val
);
2186 case SOUND_PCM_READ_CHANNELS
:
2187 val
= gus_audio_channels
;
2190 case SNDCTL_DSP_SETFMT
:
2191 if (get_user(val
, (int *)arg
))
2193 val
= gus_audio_set_bits(val
);
2196 case SOUND_PCM_READ_BITS
:
2197 val
= gus_audio_bits
;
2200 case SOUND_PCM_WRITE_FILTER
: /* NOT POSSIBLE */
2201 case SOUND_PCM_READ_FILTER
:
2207 return put_user(val
, (int *)arg
);
2210 static void gus_audio_reset(int dev
)
2212 if (recording_active
)
2214 gus_write8(0x49, 0x00); /* Halt recording */
2215 set_input_volumes();
2219 static int saved_iw_mode
; /* A hack hack hack */
2221 static int gus_audio_open(int dev
, int mode
)
2226 if (gus_pnp_flag
&& mode
& OPEN_READ
)
2228 /* printk(KERN_ERR "GUS: Audio device #%d is playback only.\n", dev);*/
2236 saved_iw_mode
= iw_mode
;
2239 /* There are some problems with audio in enhanced mode so disable it */
2240 gus_write8(0x19, gus_read8(0x19) & ~0x01); /* Disable enhanced mode */
2245 reset_sample_memory();
2246 gus_select_max_voices(14);
2251 if (mode
& OPEN_READ
)
2253 recording_active
= 1;
2254 set_input_volumes();
2256 only_read_access
= !(mode
& OPEN_WRITE
);
2257 only_8_bits
= mode
& OPEN_READ
;
2259 audio_devs
[dev
]->format_mask
= AFMT_U8
;
2261 audio_devs
[dev
]->format_mask
= AFMT_U8
| AFMT_S16_LE
;
2266 static void gus_audio_close(int dev
)
2268 iw_mode
= saved_iw_mode
;
2274 if (recording_active
)
2276 gus_write8(0x49, 0x00); /* Halt recording */
2277 set_input_volumes();
2279 recording_active
= 0;
2282 static void gus_audio_update_volume(void)
2284 unsigned long flags
;
2287 if (pcm_active
&& pcm_opened
)
2288 for (voice
= 0; voice
< gus_audio_channels
; voice
++)
2292 gus_select_voice(voice
);
2294 gus_voice_volume(1530 + (25 * gus_pcm_volume
));
2295 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume
));
2296 restore_flags(flags
);
2300 static void play_next_pcm_block(void)
2302 unsigned long flags
;
2303 int speed
= gus_audio_speed
;
2304 int this_one
, is16bits
, chn
;
2305 unsigned long dram_loc
;
2306 unsigned char mode
[2], ramp_mode
[2];
2311 this_one
= pcm_head
;
2313 for (chn
= 0; chn
< gus_audio_channels
; chn
++)
2316 ramp_mode
[chn
] = 0x03; /* Ramping and rollover off */
2320 mode
[chn
] |= 0x20; /* Loop IRQ */
2321 voices
[chn
].loop_irq_mode
= LMODE_PCM
;
2323 if (gus_audio_bits
!= 8)
2326 mode
[chn
] |= 0x04; /* 16 bit data */
2331 dram_loc
= this_one
* pcm_bsize
;
2332 dram_loc
+= chn
* pcm_banksize
;
2334 if (this_one
== (pcm_nblk
- 1)) /* Last fragment of the DRAM buffer */
2336 mode
[chn
] |= 0x08; /* Enable loop */
2337 ramp_mode
[chn
] = 0x03; /* Disable rollover bit */
2342 ramp_mode
[chn
] = 0x04; /* Enable rollover bit */
2346 gus_select_voice(chn
);
2347 gus_voice_freq(speed
);
2349 if (gus_audio_channels
== 1)
2350 gus_voice_balance(7); /* mono */
2352 gus_voice_balance(0); /* left */
2354 gus_voice_balance(15); /* right */
2356 if (!pcm_active
) /* Playback not already active */
2359 * The playback was not started yet (or there has been a pause).
2360 * Start the voice (again) and ask for a rollover irq at the end of
2361 * this_one block. If this_one one is last of the buffers, use just
2362 * the normal loop with irq.
2367 gus_voice_volume(1530 + (25 * gus_pcm_volume
));
2368 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume
));
2370 gus_write_addr(0x0a, chn
* pcm_banksize
, 0, is16bits
); /* Starting position */
2371 gus_write_addr(0x02, chn
* pcm_banksize
, 0, is16bits
); /* Loop start */
2374 gus_write_addr(0x04, pcm_banksize
+ (pcm_bsize
* pcm_nblk
) - 1,
2375 0, is16bits
); /* Loop end location */
2378 gus_write_addr(0x04, dram_loc
+ pcm_bsize
- 1,
2379 0, is16bits
); /* Loop end location */
2381 mode
[chn
] |= 0x08; /* Enable looping */
2382 restore_flags(flags
);
2384 for (chn
= 0; chn
< gus_audio_channels
; chn
++)
2388 gus_select_voice(chn
);
2389 gus_write8(0x0d, ramp_mode
[chn
]);
2391 gus_write8(0x15, 0x00); /* Reset voice deactivate bit of SMSI */
2392 gus_voice_on(mode
[chn
]);
2393 restore_flags(flags
);
2398 static void gus_transfer_output_block(int dev
, unsigned long buf
,
2399 int total_count
, int intrflag
, int chn
)
2402 * This routine transfers one block of audio data to the DRAM. In mono mode
2403 * it's called just once. When in stereo mode, this_one routine is called
2404 * once for both channels.
2406 * The left/mono channel data is transferred to the beginning of dram and the
2407 * right data to the area pointed by gus_page_size.
2410 int this_one
, count
;
2411 unsigned long flags
;
2412 unsigned char dma_command
;
2413 unsigned long address
, hold_address
;
2418 count
= total_count
/ gus_audio_channels
;
2422 if (pcm_qlen
>= pcm_nblk
)
2423 printk(KERN_WARNING
"GUS Warning: PCM buffers out of sync\n");
2425 this_one
= pcm_current_block
= pcm_tail
;
2427 pcm_tail
= (pcm_tail
+ 1) % pcm_nblk
;
2428 pcm_datasize
[this_one
] = count
;
2431 this_one
= pcm_current_block
;
2433 gus_write8(0x41, 0); /* Disable GF1 DMA */
2434 DMAbuf_start_dma(dev
, buf
+ (chn
* count
), count
, DMA_MODE_WRITE
);
2436 address
= this_one
* pcm_bsize
;
2437 address
+= chn
* pcm_banksize
;
2439 if (audio_devs
[dev
]->dmap_out
->dma
> 3)
2441 hold_address
= address
;
2442 address
= address
>> 1;
2443 address
&= 0x0001ffffL
;
2444 address
|= (hold_address
& 0x000c0000L
);
2446 gus_write16(0x42, (address
>> 4) & 0xffff); /* DRAM DMA address */
2448 dma_command
= 0x21; /* IRQ enable, DMA start */
2450 if (gus_audio_bits
!= 8)
2451 dma_command
|= 0x40; /* 16 bit _DATA_ */
2453 dma_command
|= 0x80; /* Invert MSB */
2455 if (audio_devs
[dev
]->dmap_out
->dma
> 3)
2456 dma_command
|= 0x04; /* 16 bit DMA channel */
2458 gus_write8(0x41, dma_command
); /* Kick start */
2460 if (chn
== (gus_audio_channels
- 1)) /* Last channel */
2463 * Last (right or mono) channel data
2465 dma_active
= 1; /* DMA started. There is a unacknowledged buffer */
2466 active_device
= GUS_DEV_PCM_DONE
;
2467 if (!pcm_active
&& (pcm_qlen
> 1 || count
< pcm_bsize
))
2469 play_next_pcm_block();
2475 * Left channel data. The right channel
2476 * is transferred after DMA interrupt
2478 active_device
= GUS_DEV_PCM_CONTINUE
;
2481 restore_flags(flags
);
2484 static void gus_uninterleave8(char *buf
, int l
)
2486 /* This routine uninterleaves 8 bit stereo output (LRLRLR->LLLRRR) */
2487 int i
, p
= 0, halfsize
= l
/ 2;
2488 char *buf2
= buf
+ halfsize
, *src
= bounce_buf
;
2490 memcpy(bounce_buf
, buf
, l
);
2492 for (i
= 0; i
< halfsize
; i
++)
2494 buf
[i
] = src
[p
++]; /* Left channel */
2495 buf2
[i
] = src
[p
++]; /* Right channel */
2499 static void gus_uninterleave16(short *buf
, int l
)
2501 /* This routine uninterleaves 16 bit stereo output (LRLRLR->LLLRRR) */
2502 int i
, p
= 0, halfsize
= l
/ 2;
2503 short *buf2
= buf
+ halfsize
, *src
= (short *) bounce_buf
;
2505 memcpy(bounce_buf
, (char *) buf
, l
* 2);
2507 for (i
= 0; i
< halfsize
; i
++)
2509 buf
[i
] = src
[p
++]; /* Left channel */
2510 buf2
[i
] = src
[p
++]; /* Right channel */
2514 static void gus_audio_output_block(int dev
, unsigned long buf
, int total_count
,
2517 struct dma_buffparms
*dmap
= audio_devs
[dev
]->dmap_out
;
2519 dmap
->flags
|= DMA_NODMA
| DMA_NOTIMEOUT
;
2521 pcm_current_buf
= buf
;
2522 pcm_current_count
= total_count
;
2523 pcm_current_intrflag
= intrflag
;
2524 pcm_current_dev
= dev
;
2525 if (gus_audio_channels
== 2)
2527 char *b
= dmap
->raw_buf
+ (buf
- dmap
->raw_buf_phys
);
2529 if (gus_audio_bits
== 8)
2530 gus_uninterleave8(b
, total_count
);
2532 gus_uninterleave16((short *) b
, total_count
/ 2);
2534 gus_transfer_output_block(dev
, buf
, total_count
, intrflag
, 0);
2537 static void gus_audio_start_input(int dev
, unsigned long buf
, int count
,
2540 unsigned long flags
;
2546 DMAbuf_start_dma(dev
, buf
, count
, DMA_MODE_READ
);
2547 mode
= 0xa0; /* DMA IRQ enabled, invert MSB */
2549 if (audio_devs
[dev
]->dmap_in
->dma
> 3)
2550 mode
|= 0x04; /* 16 bit DMA channel */
2551 if (gus_audio_channels
> 1)
2552 mode
|= 0x02; /* Stereo */
2553 mode
|= 0x01; /* DMA enable */
2555 gus_write8(0x49, mode
);
2556 restore_flags(flags
);
2559 static int gus_audio_prepare_for_input(int dev
, int bsize
, int bcount
)
2563 gus_audio_bsize
= bsize
;
2564 audio_devs
[dev
]->dmap_in
->flags
|= DMA_NODMA
;
2565 rate
= (((9878400 + gus_audio_speed
/ 2) / (gus_audio_speed
+ 2)) + 8) / 16;
2567 gus_write8(0x48, rate
& 0xff); /* Set sampling rate */
2569 if (gus_audio_bits
!= 8)
2571 /* printk("GUS Error: 16 bit recording not supported\n");*/
2577 static int gus_audio_prepare_for_output(int dev
, int bsize
, int bcount
)
2581 long mem_ptr
, mem_size
;
2583 audio_devs
[dev
]->dmap_out
->flags
|= DMA_NODMA
| DMA_NOTIMEOUT
;
2585 mem_size
= gus_mem_size
/ gus_audio_channels
;
2587 if (mem_size
> (256 * 1024))
2588 mem_size
= 256 * 1024;
2590 pcm_bsize
= bsize
/ gus_audio_channels
;
2591 pcm_head
= pcm_tail
= pcm_qlen
= 0;
2593 pcm_nblk
= 2; /* MAX_PCM_BUFFERS; */
2594 if ((pcm_bsize
* pcm_nblk
) > mem_size
)
2595 pcm_nblk
= mem_size
/ pcm_bsize
;
2597 for (i
= 0; i
< pcm_nblk
; i
++)
2598 pcm_datasize
[i
] = 0;
2600 pcm_banksize
= pcm_nblk
* pcm_bsize
;
2602 if (gus_audio_bits
!= 8 && pcm_banksize
== (256 * 1024))
2604 gus_write8(0x41, 0); /* Disable GF1 DMA */
2608 static int gus_local_qlen(int dev
)
2614 static struct audio_driver gus_audio_driver
=
2618 gus_audio_output_block
,
2619 gus_audio_start_input
,
2621 gus_audio_prepare_for_input
,
2622 gus_audio_prepare_for_output
,
2628 static void guswave_setup_voice(int dev
, int voice
, int chn
)
2630 struct channel_info
*info
= &synth_devs
[dev
]->chn_info
[chn
];
2632 guswave_set_instr(dev
, voice
, info
->pgm_num
);
2633 voices
[voice
].expression_vol
= info
->controllers
[CTL_EXPRESSION
]; /* Just MSB */
2634 voices
[voice
].main_vol
= (info
->controllers
[CTL_MAIN_VOLUME
] * 100) / (unsigned) 128;
2635 voices
[voice
].panning
= (info
->controllers
[CTL_PAN
] * 2) - 128;
2636 voices
[voice
].bender
= 0;
2637 voices
[voice
].bender_range
= info
->bender_range
;
2640 voices
[voice
].fixed_pitch
= 1;
2643 static void guswave_bender(int dev
, int voice
, int value
)
2646 unsigned long flags
;
2648 voices
[voice
].bender
= value
- 8192;
2649 freq
= compute_finetune(voices
[voice
].orig_freq
, value
- 8192, voices
[voice
].bender_range
, 0);
2650 voices
[voice
].current_freq
= freq
;
2654 gus_select_voice(voice
);
2655 gus_voice_freq(freq
);
2656 restore_flags(flags
);
2659 static int guswave_alloc(int dev
, int chn
, int note
, struct voice_alloc_info
*alloc
)
2661 int i
, p
, best
= -1, best_time
= 0x7fffffff;
2665 * First look for a completely stopped voice
2668 for (i
= 0; i
< alloc
->max_voice
; i
++)
2670 if (alloc
->map
[p
] == 0)
2675 if (alloc
->alloc_times
[p
] < best_time
)
2678 best_time
= alloc
->alloc_times
[p
];
2680 p
= (p
+ 1) % alloc
->max_voice
;
2684 * Then look for a releasing voice
2687 for (i
= 0; i
< alloc
->max_voice
; i
++)
2689 if (alloc
->map
[p
] == 0xffff)
2694 p
= (p
+ 1) % alloc
->max_voice
;
2703 static struct synth_operations guswave_operations
=
2722 guswave_volume_method
,
2728 static void set_input_volumes(void)
2730 unsigned long flags
;
2731 unsigned char mask
= 0xff & ~0x06; /* Just line out enabled */
2733 if (have_gus_max
) /* Don't disturb GUS MAX */
2740 * Enable channels having vol > 10%
2741 * Note! bit 0x01 means the line in DISABLED while 0x04 means
2742 * the mic in ENABLED.
2744 if (gus_line_vol
> 10)
2746 if (gus_mic_vol
> 10)
2749 if (recording_active
)
2752 * Disable channel, if not selected for recording
2754 if (!(gus_recmask
& SOUND_MASK_LINE
))
2756 if (!(gus_recmask
& SOUND_MASK_MIC
))
2760 mix_image
|= mask
& 0x07;
2761 outb((mix_image
), u_Mixer
);
2763 restore_flags(flags
);
2766 #define MIX_DEVS (SOUND_MASK_MIC|SOUND_MASK_LINE| \
2767 SOUND_MASK_SYNTH|SOUND_MASK_PCM)
2769 int gus_default_mixer_ioctl(int dev
, unsigned int cmd
, caddr_t arg
)
2773 if (((cmd
>> 8) & 0xff) != 'M')
2776 if (!access_ok(VERIFY_WRITE
, (int *)arg
, sizeof(int)))
2779 if (_SIOC_DIR(cmd
) & _SIOC_WRITE
)
2781 if (__get_user(val
, (int *) arg
))
2786 case SOUND_MIXER_RECSRC
:
2787 gus_recmask
= val
& MIX_DEVS
;
2788 if (!(gus_recmask
& (SOUND_MASK_MIC
| SOUND_MASK_LINE
)))
2789 gus_recmask
= SOUND_MASK_MIC
;
2790 /* Note! Input volumes are updated during next open for recording */
2794 case SOUND_MIXER_MIC
:
2801 set_input_volumes();
2802 val
= vol
| (vol
<< 8);
2805 case SOUND_MIXER_LINE
:
2812 set_input_volumes();
2813 val
= vol
| (vol
<< 8);
2816 case SOUND_MIXER_PCM
:
2817 gus_pcm_volume
= val
& 0xff;
2818 if (gus_pcm_volume
< 0)
2820 if (gus_pcm_volume
> 100)
2821 gus_pcm_volume
= 100;
2822 gus_audio_update_volume();
2823 val
= gus_pcm_volume
| (gus_pcm_volume
<< 8);
2826 case SOUND_MIXER_SYNTH
:
2827 gus_wave_volume
= val
& 0xff;
2828 if (gus_wave_volume
< 0)
2829 gus_wave_volume
= 0;
2830 if (gus_wave_volume
> 100)
2831 gus_wave_volume
= 100;
2832 if (active_device
== GUS_DEV_WAVE
)
2835 for (voice
= 0; voice
< nr_voices
; voice
++)
2836 dynamic_volume_change(voice
); /* Apply the new vol */
2838 val
= gus_wave_volume
| (gus_wave_volume
<< 8);
2852 case SOUND_MIXER_RECSRC
:
2856 case SOUND_MIXER_DEVMASK
:
2860 case SOUND_MIXER_STEREODEVS
:
2864 case SOUND_MIXER_RECMASK
:
2865 val
= SOUND_MASK_MIC
| SOUND_MASK_LINE
;
2868 case SOUND_MIXER_CAPS
:
2872 case SOUND_MIXER_MIC
:
2873 val
= gus_mic_vol
| (gus_mic_vol
<< 8);
2876 case SOUND_MIXER_LINE
:
2877 val
= gus_line_vol
| (gus_line_vol
<< 8);
2880 case SOUND_MIXER_PCM
:
2881 val
= gus_pcm_volume
| (gus_pcm_volume
<< 8);
2884 case SOUND_MIXER_SYNTH
:
2885 val
= gus_wave_volume
| (gus_wave_volume
<< 8);
2892 return __put_user(val
, (int *)arg
);
2895 static struct mixer_operations gus_mixer_operations
=
2898 "Gravis Ultrasound",
2899 gus_default_mixer_ioctl
2902 static int gus_default_mixer_init(void)
2906 if ((n
= sound_alloc_mixerdev()) != -1)
2909 * Don't install if there is another
2912 mixer_devs
[n
] = &gus_mixer_operations
;
2917 * Enable all mixer channels on the GF1 side. Otherwise recording will
2918 * not be possible using GUS MAX.
2921 mix_image
|= 0x04; /* All channels enabled */
2922 outb((mix_image
), u_Mixer
);
2927 void gus_wave_init(struct address_info
*hw_config
)
2929 unsigned long flags
;
2931 char *model_num
= "2.4";
2932 char tmp
[64], tmp2
[64];
2933 int gus_type
= 0x24; /* 2.4 */
2935 int irq
= hw_config
->irq
, dma
= hw_config
->dma
, dma2
= hw_config
->dma2
;
2938 hw_config
->slots
[0] = -1; /* No wave */
2939 hw_config
->slots
[1] = -1; /* No ad1848 */
2940 hw_config
->slots
[4] = -1; /* No audio */
2941 hw_config
->slots
[5] = -1; /* No mixer */
2945 if (irq
< 0 || irq
> 15)
2947 printk(KERN_ERR
"ERROR! Invalid IRQ#%d. GUS Disabled", irq
);
2952 if (dma
< 0 || dma
> 7 || dma
== 4)
2954 printk(KERN_ERR
"ERROR! Invalid DMA#%d. GUS Disabled", dma
);
2960 gus_hw_config
= hw_config
;
2966 * Try to identify the GUS model.
2968 * Versions < 3.6 don't have the digital ASIC. Try to probe it first.
2973 outb((0x20), gus_base
+ 0x0f);
2974 val
= inb(gus_base
+ 0x0f);
2975 restore_flags(flags
);
2977 if (gus_pnp_flag
|| (val
!= 0xff && (val
& 0x06))) /* Should be 0x02?? */
2982 ad_flags
= 0x12345678; /* Interwave "magic" */
2984 * It has the digital ASIC so the card is at least v3.4.
2985 * Next try to detect the true model.
2988 if (gus_pnp_flag
) /* Hack hack hack */
2991 val
= inb(u_MixSelect
);
2994 * Value 255 means pre-3.7 which don't have mixer.
2995 * Values 5 thru 9 mean v3.7 which has a ICS2101 mixer.
2996 * 10 and above is GUS MAX which has the CS4231 codec/mixer.
3000 if (val
== 255 || val
< 5)
3009 mixer_type
= ICS2101
;
3010 request_region(u_MixSelect
, 1, "GUS mixer");
3016 mixer_type
= CS4231
;
3017 #ifdef CONFIG_SOUND_GUSMAX
3019 unsigned char max_config
= 0x40; /* Codec enable */
3025 max_config
|= 0x10; /* 16 bit capture DMA */
3028 max_config
|= 0x20; /* 16 bit playback DMA */
3030 max_config
|= (gus_base
>> 4) & 0x0f; /* Extract the X from 2X0 */
3032 outb((max_config
), gus_base
+ 0x106); /* UltraMax control */
3035 if (ad1848_detect(gus_base
+ 0x10c, &ad_flags
, hw_config
->osp
))
3037 char *name
= "GUS MAX";
3038 int old_num_mixers
= num_mixers
;
3043 gus_mic_vol
= gus_line_vol
= gus_pcm_volume
= 100;
3044 gus_wave_volume
= 90;
3046 if (hw_config
->name
)
3047 name
= hw_config
->name
;
3049 hw_config
->slots
[1] = ad1848_init(name
, gus_base
+ 0x10c,
3050 -irq
, gus_dma2
, /* Playback DMA */
3051 gus_dma
, /* Capture DMA */
3052 1, /* Share DMA channels with GF1 */
3055 if (num_mixers
> old_num_mixers
)
3057 /* GUS has it's own mixer map */
3058 AD1848_REROUTE(SOUND_MIXER_LINE1
, SOUND_MIXER_SYNTH
);
3059 AD1848_REROUTE(SOUND_MIXER_LINE2
, SOUND_MIXER_CD
);
3060 AD1848_REROUTE(SOUND_MIXER_LINE3
, SOUND_MIXER_LINE
);
3064 printk(KERN_WARNING
"GUS: No CS4231 ??");
3066 printk(KERN_ERR
"GUS MAX found, but not compiled in\n");
3073 * ASIC not detected so the card must be 2.2 or 2.4.
3074 * There could still be the 16-bit/mixer daughter card.
3078 if (hw_config
->name
)
3080 strncpy(tmp
, hw_config
->name
, 45);
3082 sprintf(tmp2
, "%s (%dk)", tmp
, (int) gus_mem_size
/ 1024);
3083 tmp2
[sizeof(tmp2
) - 1] = 0;
3085 else if (gus_pnp_flag
)
3087 sprintf(tmp2
, "Gravis UltraSound PnP (%dk)",
3088 (int) gus_mem_size
/ 1024);
3091 sprintf(tmp2
, "Gravis UltraSound %s (%dk)", model_num
, (int) gus_mem_size
/ 1024);
3094 samples
= (struct patch_info
*)vmalloc((MAX_SAMPLE
+ 1) * sizeof(*samples
));
3095 if (samples
== NULL
)
3097 printk(KERN_WARNING
"gus_init: Cant allocate memory for instrument tables\n");
3100 conf_printf(tmp2
, hw_config
);
3101 tmp2
[sizeof(gus_info
.name
) - 1] = 0;
3102 strcpy(gus_info
.name
, tmp2
);
3104 if ((sdev
= sound_alloc_synthdev()) == -1)
3105 printk(KERN_WARNING
"gus_init: Too many synthesizers\n");
3108 voice_alloc
= &guswave_operations
.alloc
;
3110 guswave_operations
.id
= "IWAVE";
3111 hw_config
->slots
[0] = sdev
;
3112 synth_devs
[sdev
] = &guswave_operations
;
3114 gus_tmr_install(gus_base
+ 8);
3117 reset_sample_memory();
3121 if ((gus_mem_size
> 0) & !gus_no_wave_dma
)
3123 hw_config
->slots
[4] = -1;
3124 if ((gus_devnum
= sound_install_audiodrv(AUDIO_DRIVER_VERSION
,
3127 sizeof(struct audio_driver
),
3129 ((!iw_mode
&& dma2
!= dma
&& dma2
!= -1) ?
3131 AFMT_U8
| AFMT_S16_LE
,
3132 NULL
, dma
, dma2
)) < 0)
3137 hw_config
->slots
[4] = gus_devnum
;
3138 audio_devs
[gus_devnum
]->min_fragment
= 9; /* 512k */
3139 audio_devs
[gus_devnum
]->max_fragment
= 11; /* 8k (must match size of bounce_buf */
3140 audio_devs
[gus_devnum
]->mixer_dev
= -1; /* Next mixer# */
3141 audio_devs
[gus_devnum
]->flags
|= DMA_HARDSTOP
;
3145 * Mixer dependent initialization.
3151 gus_mic_vol
= gus_line_vol
= gus_pcm_volume
= 100;
3152 gus_wave_volume
= 90;
3153 request_region(u_MixSelect
, 1, "GUS mixer");
3154 hw_config
->slots
[5] = ics2101_mixer_init();
3155 audio_devs
[gus_devnum
]->mixer_dev
= hw_config
->slots
[5]; /* Next mixer# */
3159 /* Initialized elsewhere (ad1848.c) */
3161 hw_config
->slots
[5] = gus_default_mixer_init();
3162 audio_devs
[gus_devnum
]->mixer_dev
= hw_config
->slots
[5]; /* Next mixer# */
3167 void gus_wave_unload(struct address_info
*hw_config
)
3169 #ifdef CONFIG_SOUND_GUSMAX
3172 ad1848_unload(gus_base
+ 0x10c,
3174 gus_dma2
, /* Playback DMA */
3175 gus_dma
, /* Capture DMA */
3176 1); /* Share DMA channels with GF1 */
3180 if (mixer_type
== ICS2101
)
3182 release_region(u_MixSelect
, 1);
3184 if (hw_config
->slots
[0] != -1)
3185 sound_unload_synthdev(hw_config
->slots
[0]);
3186 if (hw_config
->slots
[1] != -1)
3187 sound_unload_audiodev(hw_config
->slots
[1]);
3188 if (hw_config
->slots
[2] != -1)
3189 sound_unload_mididev(hw_config
->slots
[2]);
3190 if (hw_config
->slots
[4] != -1)
3191 sound_unload_audiodev(hw_config
->slots
[4]);
3192 if (hw_config
->slots
[5] != -1)
3193 sound_unload_mixerdev(hw_config
->slots
[5]);
3200 static void do_loop_irq(int voice
)
3204 unsigned long flags
;
3208 gus_select_voice(voice
);
3210 tmp
= gus_read8(0x00);
3212 * Disable wave IRQ for this_one voice
3214 gus_write8(0x00, tmp
);
3216 if (tmp
& 0x03) /* Voice stopped */
3217 voice_alloc
->map
[voice
] = 0;
3219 mode
= voices
[voice
].loop_irq_mode
;
3220 voices
[voice
].loop_irq_mode
= 0;
3221 parm
= voices
[voice
].loop_irq_parm
;
3225 case LMODE_FINISH
: /*
3226 * Final loop finished, shoot volume down
3229 if ((int) (gus_read16(0x09) >> 4) < 100) /*
3230 * Get current volume
3235 gus_voice_init(voice
);
3238 gus_ramp_range(65, 4065);
3239 gus_ramp_rate(0, 63); /*
3240 * Fastest possible rate
3242 gus_rampon(0x20 | 0x40); /*
3243 * Ramp down, once, irq
3245 voices
[voice
].volume_irq_mode
= VMODE_HALT
;
3248 case LMODE_PCM_STOP
:
3249 pcm_active
= 0; /* Signal to the play_next_pcm_block routine */
3253 pcm_head
= (pcm_head
+ 1) % pcm_nblk
;
3254 if (pcm_qlen
&& pcm_active
)
3256 play_next_pcm_block();
3260 /* Underrun. Just stop the voice */
3261 gus_select_voice(0); /* Left channel */
3264 gus_select_voice(1); /* Right channel */
3271 * If the queue was full before this interrupt, the DMA transfer was
3272 * suspended. Let it continue now.
3275 if (audio_devs
[gus_devnum
]->dmap_out
->qlen
> 0)
3276 DMAbuf_outputintr(gus_devnum
, 0);
3282 restore_flags(flags
);
3285 static void do_volume_irq(int voice
)
3289 unsigned long flags
;
3294 gus_select_voice(voice
);
3295 tmp
= gus_read8(0x0d);
3297 * Disable volume ramp IRQ
3299 gus_write8(0x0d, tmp
);
3301 mode
= voices
[voice
].volume_irq_mode
;
3302 voices
[voice
].volume_irq_mode
= 0;
3303 parm
= voices
[voice
].volume_irq_parm
;
3307 case VMODE_HALT
: /* Decay phase finished */
3309 gus_write8(0x15, 0x02); /* Set voice deactivate bit of SMSI */
3310 restore_flags(flags
);
3311 gus_voice_init(voice
);
3314 case VMODE_ENVELOPE
:
3316 restore_flags(flags
);
3317 step_envelope(voice
);
3320 case VMODE_START_NOTE
:
3321 restore_flags(flags
);
3322 guswave_start_note2(voices
[voice
].dev_pending
, voice
,
3323 voices
[voice
].note_pending
, voices
[voice
].volume_pending
);
3324 if (voices
[voice
].kill_pending
)
3325 guswave_kill_note(voices
[voice
].dev_pending
, voice
,
3326 voices
[voice
].note_pending
, 0);
3328 if (voices
[voice
].sample_pending
>= 0)
3330 guswave_set_instr(voices
[voice
].dev_pending
, voice
,
3331 voices
[voice
].sample_pending
);
3332 voices
[voice
].sample_pending
= -1;
3337 restore_flags(flags
);
3339 restore_flags(flags
);
3342 void gus_voice_irq(void)
3344 unsigned long wave_ignore
= 0, volume_ignore
= 0;
3345 unsigned long voice_bit
;
3347 unsigned char src
, voice
;
3351 src
= gus_read8(0x0f); /*
3357 if (src
== (0x80 | 0x40))
3362 voice_bit
= 1 << voice
;
3364 if (!(src
& 0x80)) /*
3367 if (!(wave_ignore
& voice_bit
) && (int) voice
< nr_voices
) /*
3372 wave_ignore
|= voice_bit
;
3375 if (!(src
& 0x40)) /*
3376 * Volume IRQ pending
3378 if (!(volume_ignore
& voice_bit
) && (int) voice
< nr_voices
) /*
3383 volume_ignore
|= voice_bit
;
3384 do_volume_irq(voice
);
3389 void guswave_dma_irq(void)
3391 unsigned char status
;
3393 status
= gus_look8(0x41); /* Get DMA IRQ Status */
3394 if (status
& 0x40) /* DMA interrupt pending */
3395 switch (active_device
)
3398 wake_up(&dram_sleeper
);
3401 case GUS_DEV_PCM_CONTINUE
: /* Left channel data transferred */
3402 gus_write8(0x41, 0); /* Disable GF1 DMA */
3403 gus_transfer_output_block(pcm_current_dev
, pcm_current_buf
,
3405 pcm_current_intrflag
, 1);
3408 case GUS_DEV_PCM_DONE
: /* Right or mono channel data transferred */
3409 gus_write8(0x41, 0); /* Disable GF1 DMA */
3410 if (pcm_qlen
< pcm_nblk
)
3415 if (audio_devs
[gus_devnum
]->dmap_out
->qlen
> 0)
3416 DMAbuf_outputintr(gus_devnum
, 0);
3423 status
= gus_look8(0x49); /*
3424 * Get Sampling IRQ Status
3426 if (status
& 0x40) /*
3427 * Sampling Irq pending
3430 DMAbuf_inputintr(gus_devnum
);
3438 static volatile int select_addr
, data_addr
;
3439 static volatile int curr_timer
= 0;
3441 void gus_timer_command(unsigned int addr
, unsigned int val
)
3445 outb(((unsigned char) (addr
& 0xff)), select_addr
);
3447 for (i
= 0; i
< 2; i
++)
3450 outb(((unsigned char) (val
& 0xff)), data_addr
);
3452 for (i
= 0; i
< 2; i
++)
3456 static void arm_timer(int timer
, unsigned int interval
)
3462 gus_write8(0x46, 256 - interval
); /* Set counter for timer 1 */
3463 gus_write8(0x45, 0x04); /* Enable timer 1 IRQ */
3464 gus_timer_command(0x04, 0x01); /* Start timer 1 */
3468 gus_write8(0x47, 256 - interval
); /* Set counter for timer 2 */
3469 gus_write8(0x45, 0x08); /* Enable timer 2 IRQ */
3470 gus_timer_command(0x04, 0x02); /* Start timer 2 */
3473 gus_timer_enabled
= 1;
3476 static unsigned int gus_tmr_start(int dev
, unsigned int usecs_per_tick
)
3478 int timer_no
, resolution
;
3481 if (usecs_per_tick
> (256 * 80))
3484 resolution
= 320; /* usec */
3489 resolution
= 80; /* usec */
3491 divisor
= (usecs_per_tick
+ (resolution
/ 2)) / resolution
;
3492 arm_timer(timer_no
, divisor
);
3494 return divisor
* resolution
;
3497 static void gus_tmr_disable(int dev
)
3499 gus_write8(0x45, 0); /* Disable both timers */
3500 gus_timer_enabled
= 0;
3503 static void gus_tmr_restart(int dev
)
3505 if (curr_timer
== 1)
3506 gus_write8(0x45, 0x04); /* Start timer 1 again */
3508 gus_write8(0x45, 0x08); /* Start timer 2 again */
3509 gus_timer_enabled
= 1;
3512 static struct sound_lowlev_timer gus_tmr
=
3521 static void gus_tmr_install(int io_base
)
3523 struct sound_lowlev_timer
*tmr
;
3525 select_addr
= io_base
;
3526 data_addr
= io_base
+ 1;
3530 #ifdef THIS_GETS_FIXED
3531 sound_timer_init(&gus_tmr
, "GUS");