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)
16 #include <linux/config.h>
19 #define GUSPNP_AUTODETECT
21 #include "sound_config.h"
22 #include <linux/ultrasound.h>
25 #if defined(CONFIG_GUSHW) || defined(MODULE)
27 #define GUS_BANK_SIZE (((iw_mode) ? 256*1024*1024 : 256*1024))
29 #define MAX_SAMPLE 150
32 #define NOT_SAMPLE 0xffff
36 unsigned long orig_freq
;
37 unsigned long current_freq
;
44 unsigned int initial_volume
;
45 unsigned int current_volume
;
46 int loop_irq_mode
, loop_irq_parm
;
47 #define LMODE_FINISH 1
49 #define LMODE_PCM_STOP 3
50 int volume_irq_mode
, volume_irq_parm
;
52 #define VMODE_ENVELOPE 2
53 #define VMODE_START_NOTE 3
56 unsigned char env_rate
[6];
57 unsigned char env_offset
[6];
60 * Volume computation parameters for gus_adagio_vol()
62 int main_vol
, expression_vol
, patch_vol
;
64 /* Variables for "Ultraclick" removal */
65 int dev_pending
, note_pending
, volume_pending
,
72 static struct voice_alloc_info
*voice_alloc
;
75 extern int gus_irq
, gus_dma
;
76 extern int gus_pnp_flag
;
77 static int gus_dma2
= -1;
78 static int dual_dma_mode
= 0;
79 static long gus_mem_size
= 0;
80 static long free_mem_ptr
= 0;
81 static int gus_busy
= 0;
82 static int gus_no_dma
= 0;
83 static int nr_voices
= 0;
84 static int gus_devnum
= 0;
85 static int volume_base
, volume_scale
, volume_method
;
86 static int gus_recmask
= SOUND_MASK_MIC
;
87 static int recording_active
= 0;
88 static int only_read_access
= 0;
89 static int only_8_bits
= 0;
92 int gus_wave_volume
= 60;
93 int gus_pcm_volume
= 80;
95 static int gus_line_vol
= 100, gus_mic_vol
= 0;
96 static unsigned char mix_image
= 0x00;
98 int gus_timer_enabled
= 0;
101 * Current version of this driver doesn't allow synth and PCM functions
102 * at the same time. The active_device specifies the active driver
105 static int active_device
= 0;
107 #define GUS_DEV_WAVE 1 /* Wave table synth */
108 #define GUS_DEV_PCM_DONE 2 /* PCM device, transfer done */
109 #define GUS_DEV_PCM_CONTINUE 3 /* PCM device, transfer done ch. 1/2 */
111 static int gus_audio_speed
;
112 static int gus_audio_channels
;
113 static int gus_audio_bits
;
114 static int gus_audio_bsize
;
115 static char bounce_buf
[8 * 1024]; /* Must match value set to max_fragment */
117 static struct wait_queue
*dram_sleeper
= NULL
;
118 static volatile struct snd_wait dram_sleep_flag
= {
123 * Variables and buffers for PCM output
126 #define MAX_PCM_BUFFERS (128*MAX_REALTIME_FACTOR) /* Don't change */
128 static int pcm_bsize
, pcm_nblk
, pcm_banksize
;
129 static int pcm_datasize
[MAX_PCM_BUFFERS
];
130 static volatile int pcm_head
, pcm_tail
, pcm_qlen
;
131 static volatile int pcm_active
;
132 static volatile int dma_active
;
133 static int pcm_opened
= 0;
134 static int pcm_current_dev
;
135 static int pcm_current_block
;
136 static unsigned long pcm_current_buf
;
137 static int pcm_current_count
;
138 static int pcm_current_intrflag
;
142 static struct voice_info voices
[32];
144 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() ? Lets 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
);
510 static void gus_voice_init(int voice
)
516 gus_select_voice(voice
);
519 gus_write_addr(0x0a, 0, 0, 0); /* Set current position to 0 */
520 gus_write8(0x00, 0x03); /* Voice off */
521 gus_write8(0x0d, 0x03); /* Ramping off */
522 voice_alloc
->map
[voice
] = 0;
523 voice_alloc
->alloc_times
[voice
] = 0;
524 restore_flags(flags
);
528 static void gus_voice_init2(int voice
)
530 voices
[voice
].panning
= 0;
531 voices
[voice
].mode
= 0;
532 voices
[voice
].orig_freq
= 20000;
533 voices
[voice
].current_freq
= 20000;
534 voices
[voice
].bender
= 0;
535 voices
[voice
].bender_range
= 200;
536 voices
[voice
].initial_volume
= 0;
537 voices
[voice
].current_volume
= 0;
538 voices
[voice
].loop_irq_mode
= 0;
539 voices
[voice
].loop_irq_parm
= 0;
540 voices
[voice
].volume_irq_mode
= 0;
541 voices
[voice
].volume_irq_parm
= 0;
542 voices
[voice
].env_phase
= 0;
543 voices
[voice
].main_vol
= 127;
544 voices
[voice
].patch_vol
= 127;
545 voices
[voice
].expression_vol
= 127;
546 voices
[voice
].sample_pending
= -1;
547 voices
[voice
].fixed_pitch
= 0;
550 static void step_envelope(int voice
)
552 unsigned vol
, prev_vol
, phase
;
556 if (voices
[voice
].mode
& WAVE_SUSTAIN_ON
&& voices
[voice
].env_phase
== 2)
560 gus_select_voice(voice
);
562 restore_flags(flags
);
565 * Sustain phase begins. Continue envelope after receiving note off.
568 if (voices
[voice
].env_phase
>= 5)
570 /* Envelope finished. Shoot the voice down */
571 gus_voice_init(voice
);
574 prev_vol
= voices
[voice
].current_volume
;
575 phase
= ++voices
[voice
].env_phase
;
576 compute_volume(voice
, voices
[voice
].midi_volume
);
577 vol
= voices
[voice
].initial_volume
* voices
[voice
].env_offset
[phase
] / 255;
578 rate
= voices
[voice
].env_rate
[phase
];
582 gus_select_voice(voice
);
584 gus_voice_volume(prev_vol
);
587 gus_write8(0x06, rate
); /* Ramping rate */
589 voices
[voice
].volume_irq_mode
= VMODE_ENVELOPE
;
591 if (((vol
- prev_vol
) / 64) == 0) /* No significant volume change */
593 restore_flags(flags
);
594 step_envelope(voice
); /* Continue the envelope on the next step */
599 if (vol
>= (4096 - 64))
601 gus_ramp_range(0, vol
);
602 gus_rampon(0x20); /* Increasing volume, with IRQ */
608 gus_ramp_range(vol
, 4030);
609 gus_rampon(0x60); /* Decreasing volume, with IRQ */
611 voices
[voice
].current_volume
= vol
;
612 restore_flags(flags
);
615 static void init_envelope(int voice
)
617 voices
[voice
].env_phase
= -1;
618 voices
[voice
].current_volume
= 64;
620 step_envelope(voice
);
623 static void start_release(int voice
, long int flags
)
625 if (gus_read8(0x00) & 0x03)
626 return; /* Voice already stopped */
628 voices
[voice
].env_phase
= 2; /* Will be incremented by step_envelope */
630 voices
[voice
].current_volume
= voices
[voice
].initial_volume
=
631 gus_read16(0x09) >> 4; /* Get current volume */
633 voices
[voice
].mode
&= ~WAVE_SUSTAIN_ON
;
635 restore_flags(flags
);
636 step_envelope(voice
);
639 static void gus_voice_fade(int voice
)
641 int instr_no
= sample_map
[voice
], is16bits
;
646 gus_select_voice(voice
);
648 if (instr_no
< 0 || instr_no
> MAX_SAMPLE
)
650 gus_write8(0x00, 0x03); /* Hard stop */
651 voice_alloc
->map
[voice
] = 0;
652 restore_flags(flags
);
655 is16bits
= (samples
[instr_no
].mode
& WAVE_16_BITS
) ? 1 : 0; /* 8 or 16 bits */
657 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
659 start_release(voice
, flags
);
660 restore_flags(flags
);
664 * Ramp the volume down but not too quickly.
666 if ((int) (gus_read16(0x09) >> 4) < 100) /* Get current volume */
670 gus_voice_init(voice
);
671 restore_flags(flags
);
674 gus_ramp_range(65, 4030);
676 gus_rampon(0x40 | 0x20); /* Down, once, with IRQ */
677 voices
[voice
].volume_irq_mode
= VMODE_HALT
;
678 restore_flags(flags
);
681 static void gus_reset(void)
685 gus_select_max_voices(24);
688 volume_method
= VOL_METHOD_ADAGIO
;
690 for (i
= 0; i
< 32; i
++)
692 gus_voice_init(i
); /* Turn voice off */
697 static void gus_initialize(void)
700 unsigned char dma_image
, irq_image
, tmp
;
702 static unsigned char gus_irq_map
[16] = {
703 0, 0, 0, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7
706 static unsigned char gus_dma_map
[8] = {
707 0, 1, 0, 2, 0, 3, 4, 5
712 gus_write8(0x4c, 0); /* Reset GF1 */
716 gus_write8(0x4c, 1); /* Release Reset */
721 * Clear all interrupts
724 gus_write8(0x41, 0); /* DMA control */
725 gus_write8(0x45, 0); /* Timer control */
726 gus_write8(0x49, 0); /* Sample control */
728 gus_select_max_voices(24);
730 inb(u_Status
); /* Touch the status register */
732 gus_look8(0x41); /* Clear any pending DMA IRQs */
733 gus_look8(0x49); /* Clear any pending sample IRQs */
734 gus_read8(0x0f); /* Clear pending IRQs */
736 gus_reset(); /* Resets all voices */
738 gus_look8(0x41); /* Clear any pending DMA IRQs */
739 gus_look8(0x49); /* Clear any pending sample IRQs */
740 gus_read8(0x0f); /* Clear pending IRQs */
742 gus_write8(0x4c, 7); /* Master reset | DAC enable | IRQ enable */
745 * Set up for Digital ASIC
748 outb((0x05), gus_base
+ 0x0f);
750 mix_image
|= 0x02; /* Disable line out (for a moment) */
751 outb((mix_image
), u_Mixer
);
753 outb((0x00), u_IRQDMAControl
);
755 outb((0x00), gus_base
+ 0x0f);
758 * Now set up the DMA and IRQ interface
760 * The GUS supports two IRQs and two DMAs.
762 * Just one DMA channel is used. This prevents simultaneous ADC and DAC.
763 * Adding this support requires significant changes to the dmabuf.c, dsp.c
768 tmp
= gus_irq_map
[gus_irq
];
769 if (!gus_pnp_flag
&& !tmp
)
770 printk(KERN_WARNING
"Warning! GUS IRQ not selected\n");
772 irq_image
|= 0x40; /* Combine IRQ1 (GF1) and IRQ2 (Midi) */
775 if (gus_dma2
== gus_dma
|| gus_dma2
== -1)
778 dma_image
= 0x40; /* Combine DMA1 (DRAM) and IRQ2 (ADC) */
780 tmp
= gus_dma_map
[gus_dma
];
782 printk(KERN_WARNING
"Warning! GUS DMA not selected\n");
788 /* Setup dual DMA channel mode for GUS MAX */
790 dma_image
= gus_dma_map
[gus_dma
];
792 printk(KERN_WARNING
"Warning! GUS DMA not selected\n");
794 tmp
= gus_dma_map
[gus_dma2
] << 3;
797 printk(KERN_WARNING
"Warning! Invalid GUS MAX DMA\n");
798 tmp
= 0x40; /* Combine DMA channels */
805 * For some reason the IRQ and DMA addresses must be written twice
809 * Doing it first time
812 outb((mix_image
), u_Mixer
); /* Select DMA control */
813 outb((dma_image
| 0x80), u_IRQDMAControl
); /* Set DMA address */
815 outb((mix_image
| 0x40), u_Mixer
); /* Select IRQ control */
816 outb((irq_image
), u_IRQDMAControl
); /* Set IRQ address */
819 * Doing it second time
822 outb((mix_image
), u_Mixer
); /* Select DMA control */
823 outb((dma_image
), u_IRQDMAControl
); /* Set DMA address */
825 outb((mix_image
| 0x40), u_Mixer
); /* Select IRQ control */
826 outb((irq_image
), u_IRQDMAControl
); /* Set IRQ address */
828 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
830 mix_image
&= ~0x02; /* Enable line out */
831 mix_image
|= 0x08; /* Enable IRQ */
832 outb((mix_image
), u_Mixer
); /*
833 * Turn mixer channels on
834 * Note! Mic in is left off.
837 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
839 gusintr(gus_irq
, NULL
, NULL
); /* Serve pending interrupts */
841 inb(u_Status
); /* Touch the status register */
843 gus_look8(0x41); /* Clear any pending DMA IRQs */
844 gus_look8(0x49); /* Clear any pending sample IRQs */
846 gus_read8(0x0f); /* Clear pending IRQs */
849 gus_write8(0x19, gus_read8(0x19) | 0x01);
850 restore_flags(flags
);
854 static void pnp_mem_init(void)
857 #define CHUNK_SIZE (256*1024)
858 #define BANK_SIZE (4*1024*1024)
859 #define CHUNKS_PER_BANK (BANK_SIZE/CHUNK_SIZE)
861 int bank
, chunk
, addr
, total
= 0;
863 int i
, j
, bits
= -1, nbanks
= 0;
866 * This routine determines what kind of RAM is installed in each of the four
867 * SIMM banks and configures the DRAM address decode logic accordingly.
871 * Place the chip into enhanced mode
873 gus_write8(0x19, gus_read8(0x19) | 0x01);
874 gus_write8(0x53, gus_look8(0x53) & ~0x02); /* Select DRAM I/O access */
877 * Set memory configuration to 4 DRAM banks of 4M in each (16M total).
880 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | 0x000c);
883 * Perform the DRAM size detection for each bank individually.
885 for (bank
= 0; bank
< 4; bank
++)
889 addr
= bank
* BANK_SIZE
;
891 /* Clean check points of each chunk */
892 for (chunk
= 0; chunk
< CHUNKS_PER_BANK
; chunk
++)
894 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 0L, 0x00);
895 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 1L, 0x00);
898 /* Write a value to each chunk point and verify the result */
899 for (chunk
= 0; chunk
< CHUNKS_PER_BANK
; chunk
++)
901 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 0L, 0x55);
902 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 1L, 0xAA);
904 if (gus_peek(addr
+ chunk
* CHUNK_SIZE
+ 0L) == 0x55 &&
905 gus_peek(addr
+ chunk
* CHUNK_SIZE
+ 1L) == 0xAA)
907 /* OK. There is RAM. Now check for possible shadows */
910 for (chunk2
= 0; ok
&& chunk2
< chunk
; chunk2
++)
911 if (gus_peek(addr
+ chunk2
* CHUNK_SIZE
+ 0L) ||
912 gus_peek(addr
+ chunk2
* CHUNK_SIZE
+ 1L))
913 ok
= 0; /* Addressing wraps */
916 size
= (chunk
+ 1) * CHUNK_SIZE
;
918 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 0L, 0x00);
919 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 1L, 0x00);
921 bank_sizes
[bank
] = size
;
924 DDB(printk("Interwave: Bank %d, size=%dk\n", bank
, size
/ 1024));
927 if (nbanks
== 0) /* No RAM - Give up */
929 printk(KERN_ERR
"Sound: An Interwave audio chip detected but no DRAM\n");
930 printk(KERN_ERR
"Sound: Unable to work with this card.\n");
931 gus_write8(0x19, gus_read8(0x19) & ~0x01);
937 * Now we know how much DRAM there is in each bank. The next step is
938 * to find a DRAM size encoding (0 to 12) which is best for the combination
941 * First try if any of the possible alternatives matches exactly the amount
945 for (i
= 0; bits
== -1 && i
< 13; i
++)
949 for (j
= 0; bits
!= -1 && j
< 4; j
++)
950 if (mem_decode
[i
][j
] != bank_sizes
[j
])
951 bits
= -1; /* No hit */
955 * If necessary, try to find a combination where other than the last
956 * bank matches our configuration and the last bank is left oversized.
957 * In this way we don't leave holes in the middle of memory.
960 if (bits
== -1) /* No luck yet */
962 for (i
= 0; bits
== -1 && i
< 13; i
++)
966 for (j
= 0; bits
!= -1 && j
< nbanks
- 1; j
++)
967 if (mem_decode
[i
][j
] != bank_sizes
[j
])
968 bits
= -1; /* No hit */
969 if (mem_decode
[i
][nbanks
- 1] < bank_sizes
[nbanks
- 1])
970 bits
= -1; /* The last bank is too small */
974 * The last resort is to search for a combination where the last bank is
975 * smaller than the actual SIMM. This leaves some memory in the last bank
976 * unused but doesn't leave holes in the DRAM address space.
978 if (bits
== -1) /* No luck yet */
980 for (i
= 0; bits
== -1 && i
< 13; i
++)
984 for (j
= 0; bits
!= -1 && j
< nbanks
- 1; j
++)
985 if (mem_decode
[i
][j
] != bank_sizes
[j
])
986 bits
= -1; /* No hit */
990 printk(KERN_INFO
"Interwave: Can't use all installed RAM.\n");
991 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 return copy_to_user(arg
, &gus_info
, sizeof(gus_info
));
1107 case SNDCTL_SEQ_RESETSAMPLES
:
1108 reset_sample_memory();
1111 case SNDCTL_SEQ_PERCMODE
:
1114 case SNDCTL_SYNTH_MEMAVL
:
1115 return (gus_mem_size
== 0) ? 0 : gus_mem_size
- free_mem_ptr
- 32;
1122 static int guswave_set_instr(int dev
, int voice
, int instr_no
)
1126 if (instr_no
< 0 || instr_no
> MAX_PATCH
)
1127 instr_no
= 0; /* Default to acoustic piano */
1129 if (voice
< 0 || voice
> 31)
1132 if (voices
[voice
].volume_irq_mode
== VMODE_START_NOTE
)
1134 voices
[voice
].sample_pending
= instr_no
;
1137 sample_no
= patch_table
[instr_no
];
1138 patch_map
[voice
] = -1;
1140 if (sample_no
== NOT_SAMPLE
)
1142 /* printk("GUS: Undefined patch %d for voice %d\n", instr_no, voice);*/
1143 return -EINVAL
; /* Patch not defined */
1145 if (sample_ptrs
[sample_no
] == -1) /* Sample not loaded */
1147 /* printk("GUS: Sample #%d not loaded for patch %d (voice %d)\n", sample_no, instr_no, voice);*/
1150 sample_map
[voice
] = sample_no
;
1151 patch_map
[voice
] = instr_no
;
1155 static int guswave_kill_note(int dev
, int voice
, int note
, int velocity
)
1157 unsigned long flags
;
1161 /* voice_alloc->map[voice] = 0xffff; */
1162 if (voices
[voice
].volume_irq_mode
== VMODE_START_NOTE
)
1164 voices
[voice
].kill_pending
= 1;
1165 restore_flags(flags
);
1169 restore_flags(flags
);
1170 gus_voice_fade(voice
);
1173 restore_flags(flags
);
1177 static void guswave_aftertouch(int dev
, int voice
, int pressure
)
1181 static void guswave_panning(int dev
, int voice
, int value
)
1183 if (voice
>= 0 || voice
< 32)
1184 voices
[voice
].panning
= value
;
1187 static void guswave_volume_method(int dev
, int mode
)
1189 if (mode
== VOL_METHOD_LINEAR
|| mode
== VOL_METHOD_ADAGIO
)
1190 volume_method
= mode
;
1193 static void compute_volume(int voice
, int volume
)
1196 voices
[voice
].midi_volume
= volume
;
1198 switch (volume_method
)
1200 case VOL_METHOD_ADAGIO
:
1201 voices
[voice
].initial_volume
=
1202 gus_adagio_vol(voices
[voice
].midi_volume
, voices
[voice
].main_vol
,
1203 voices
[voice
].expression_vol
,
1204 voices
[voice
].patch_vol
);
1207 case VOL_METHOD_LINEAR
: /* Totally ignores patch-volume and expression */
1208 voices
[voice
].initial_volume
= gus_linear_vol(volume
, voices
[voice
].main_vol
);
1212 voices
[voice
].initial_volume
= volume_base
+
1213 (voices
[voice
].midi_volume
* volume_scale
);
1216 if (voices
[voice
].initial_volume
> 4030)
1217 voices
[voice
].initial_volume
= 4030;
1220 static void compute_and_set_volume(int voice
, int volume
, int ramp_time
)
1222 int curr
, target
, rate
;
1223 unsigned long flags
;
1225 compute_volume(voice
, volume
);
1226 voices
[voice
].current_volume
= voices
[voice
].initial_volume
;
1231 * CAUTION! Interrupts disabled. Enable them before returning
1234 gus_select_voice(voice
);
1236 curr
= gus_read16(0x09) >> 4;
1237 target
= voices
[voice
].initial_volume
;
1239 if (ramp_time
== INSTANT_RAMP
)
1242 gus_voice_volume(target
);
1243 restore_flags(flags
);
1246 if (ramp_time
== FAST_RAMP
)
1250 gus_ramp_rate(0, rate
);
1252 if ((target
- curr
) / 64 == 0) /* Close enough to target. */
1255 gus_voice_volume(target
);
1256 restore_flags(flags
);
1261 if (target
> (4095 - 65))
1263 gus_ramp_range(curr
, target
);
1264 gus_rampon(0x00); /* Ramp up, once, no IRQ */
1271 gus_ramp_range(target
, curr
);
1272 gus_rampon(0x40); /* Ramp down, once, no irq */
1274 restore_flags(flags
);
1277 static void dynamic_volume_change(int voice
)
1279 unsigned char status
;
1280 unsigned long flags
;
1284 gus_select_voice(voice
);
1285 status
= gus_read8(0x00); /* Get voice status */
1286 restore_flags(flags
);
1289 return; /* Voice was not running */
1291 if (!(voices
[voice
].mode
& WAVE_ENVELOPES
))
1293 compute_and_set_volume(voice
, voices
[voice
].midi_volume
, 1);
1298 * Voice is running and has envelopes.
1303 gus_select_voice(voice
);
1304 status
= gus_read8(0x0d); /* Ramping status */
1305 restore_flags(flags
);
1307 if (status
& 0x03) /* Sustain phase? */
1309 compute_and_set_volume(voice
, voices
[voice
].midi_volume
, 1);
1312 if (voices
[voice
].env_phase
< 0)
1315 compute_volume(voice
, voices
[voice
].midi_volume
);
1319 static void guswave_controller(int dev
, int voice
, int ctrl_num
, int value
)
1321 unsigned long flags
;
1324 if (voice
< 0 || voice
> 31)
1329 case CTRL_PITCH_BENDER
:
1330 voices
[voice
].bender
= value
;
1332 if (voices
[voice
].volume_irq_mode
!= VMODE_START_NOTE
)
1334 freq
= compute_finetune(voices
[voice
].orig_freq
, value
, voices
[voice
].bender_range
, 0);
1335 voices
[voice
].current_freq
= freq
;
1339 gus_select_voice(voice
);
1340 gus_voice_freq(freq
);
1341 restore_flags(flags
);
1345 case CTRL_PITCH_BENDER_RANGE
:
1346 voices
[voice
].bender_range
= value
;
1348 case CTL_EXPRESSION
:
1350 case CTRL_EXPRESSION
:
1351 if (volume_method
== VOL_METHOD_ADAGIO
)
1353 voices
[voice
].expression_vol
= value
;
1354 if (voices
[voice
].volume_irq_mode
!= VMODE_START_NOTE
)
1355 dynamic_volume_change(voice
);
1360 voices
[voice
].panning
= (value
* 2) - 128;
1363 case CTL_MAIN_VOLUME
:
1364 value
= (value
* 100) / 16383;
1366 case CTRL_MAIN_VOLUME
:
1367 voices
[voice
].main_vol
= value
;
1368 if (voices
[voice
].volume_irq_mode
!= VMODE_START_NOTE
)
1369 dynamic_volume_change(voice
);
1377 static int guswave_start_note2(int dev
, int voice
, int note_num
, int volume
)
1379 int sample
, best_sample
, best_delta
, delta_freq
;
1380 int is16bits
, samplep
, patch
, pan
;
1381 unsigned long note_freq
, base_note
, freq
, flags
;
1382 unsigned char mode
= 0;
1384 if (voice
< 0 || voice
> 31)
1386 /* printk("GUS: Invalid voice\n");*/
1389 if (note_num
== 255)
1391 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
1393 voices
[voice
].midi_volume
= volume
;
1394 dynamic_volume_change(voice
);
1397 compute_and_set_volume(voice
, volume
, 1);
1400 if ((patch
= patch_map
[voice
]) == -1)
1402 if ((samplep
= patch_table
[patch
]) == NOT_SAMPLE
)
1406 note_freq
= note_to_freq(note_num
);
1409 * Find a sample within a patch so that the note_freq is between low_note
1414 best_sample
= samplep
;
1415 best_delta
= 1000000;
1416 while (samplep
!= 0 && samplep
!= NOT_SAMPLE
&& sample
== -1)
1418 delta_freq
= note_freq
- samples
[samplep
].base_note
;
1420 delta_freq
= -delta_freq
;
1421 if (delta_freq
< best_delta
)
1423 best_sample
= samplep
;
1424 best_delta
= delta_freq
;
1426 if (samples
[samplep
].low_note
<= note_freq
&&
1427 note_freq
<= samples
[samplep
].high_note
)
1432 samplep
= samples
[samplep
].key
; /* Link to next sample */
1435 sample
= best_sample
;
1439 /* printk("GUS: Patch %d not defined for note %d\n", patch, note_num);*/
1440 return 0; /* Should play default patch ??? */
1442 is16bits
= (samples
[sample
].mode
& WAVE_16_BITS
) ? 1 : 0;
1443 voices
[voice
].mode
= samples
[sample
].mode
;
1444 voices
[voice
].patch_vol
= samples
[sample
].volume
;
1447 gus_write8(0x15, 0x00); /* RAM, Reset voice deactivate bit of SMSI */
1449 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
1453 for (i
= 0; i
< 6; i
++)
1455 voices
[voice
].env_rate
[i
] = samples
[sample
].env_rate
[i
];
1456 voices
[voice
].env_offset
[i
] = samples
[sample
].env_offset
[i
];
1459 sample_map
[voice
] = sample
;
1461 if (voices
[voice
].fixed_pitch
) /* Fixed pitch */
1463 freq
= samples
[sample
].base_freq
;
1467 base_note
= samples
[sample
].base_note
/ 100;
1470 freq
= samples
[sample
].base_freq
* note_freq
/ base_note
;
1473 voices
[voice
].orig_freq
= freq
;
1476 * Since the pitch bender may have been set before playing the note, we
1477 * have to calculate the bending now.
1480 freq
= compute_finetune(voices
[voice
].orig_freq
, voices
[voice
].bender
,
1481 voices
[voice
].bender_range
, 0);
1482 voices
[voice
].current_freq
= freq
;
1484 pan
= (samples
[sample
].panning
+ voices
[voice
].panning
) / 32;
1491 if (samples
[sample
].mode
& WAVE_16_BITS
)
1493 mode
|= 0x04; /* 16 bits */
1494 if ((sample_ptrs
[sample
] / GUS_BANK_SIZE
) !=
1495 ((sample_ptrs
[sample
] + samples
[sample
].len
) / GUS_BANK_SIZE
))
1496 printk(KERN_ERR
"GUS: Sample address error\n");
1498 /*************************************************************************
1499 * CAUTION! Interrupts disabled. Don't return before enabling
1500 *************************************************************************/
1504 gus_select_voice(voice
);
1508 restore_flags(flags
);
1510 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
1512 compute_volume(voice
, volume
);
1513 init_envelope(voice
);
1517 compute_and_set_volume(voice
, volume
, 0);
1522 gus_select_voice(voice
);
1524 if (samples
[sample
].mode
& WAVE_LOOP_BACK
)
1525 gus_write_addr(0x0a, sample_ptrs
[sample
] + samples
[sample
].len
-
1526 voices
[voice
].offset_pending
, 0, is16bits
); /* start=end */
1528 gus_write_addr(0x0a, sample_ptrs
[sample
] + voices
[voice
].offset_pending
, 0, is16bits
); /* Sample start=begin */
1530 if (samples
[sample
].mode
& WAVE_LOOPING
)
1534 if (samples
[sample
].mode
& WAVE_BIDIR_LOOP
)
1537 if (samples
[sample
].mode
& WAVE_LOOP_BACK
)
1539 gus_write_addr(0x0a, sample_ptrs
[sample
] + samples
[sample
].loop_end
-
1540 voices
[voice
].offset_pending
,
1541 (samples
[sample
].fractions
>> 4) & 0x0f, is16bits
);
1544 gus_write_addr(0x02, sample_ptrs
[sample
] + samples
[sample
].loop_start
,
1545 samples
[sample
].fractions
& 0x0f, is16bits
); /* Loop start location */
1546 gus_write_addr(0x04, sample_ptrs
[sample
] + samples
[sample
].loop_end
,
1547 (samples
[sample
].fractions
>> 4) & 0x0f, is16bits
); /* Loop end location */
1551 mode
|= 0x20; /* Loop IRQ at the end */
1552 voices
[voice
].loop_irq_mode
= LMODE_FINISH
; /* Ramp down at the end */
1553 voices
[voice
].loop_irq_parm
= 1;
1554 gus_write_addr(0x02, sample_ptrs
[sample
], 0, is16bits
); /* Loop start location */
1555 gus_write_addr(0x04, sample_ptrs
[sample
] + samples
[sample
].len
- 1,
1556 (samples
[sample
].fractions
>> 4) & 0x0f, is16bits
); /* Loop end location */
1558 gus_voice_freq(freq
);
1559 gus_voice_balance(pan
);
1561 restore_flags(flags
);
1567 * New guswave_start_note by Andrew J. Robinson attempts to minimize clicking
1568 * when the note playing on the voice is changed. It uses volume
1572 static int guswave_start_note(int dev
, int voice
, int note_num
, int volume
)
1580 if (note_num
== 255)
1582 if (voices
[voice
].volume_irq_mode
== VMODE_START_NOTE
)
1584 voices
[voice
].volume_pending
= volume
;
1588 ret_val
= guswave_start_note2(dev
, voice
, note_num
, volume
);
1593 gus_select_voice(voice
);
1594 mode
= gus_read8(0x00);
1596 gus_write8(0x00, mode
& 0xdf); /* No interrupt! */
1598 voices
[voice
].offset_pending
= 0;
1599 voices
[voice
].kill_pending
= 0;
1600 voices
[voice
].volume_irq_mode
= 0;
1601 voices
[voice
].loop_irq_mode
= 0;
1603 if (voices
[voice
].sample_pending
>= 0)
1605 restore_flags(flags
); /* Run temporarily with interrupts enabled */
1606 guswave_set_instr(voices
[voice
].dev_pending
, voice
, voices
[voice
].sample_pending
);
1607 voices
[voice
].sample_pending
= -1;
1610 gus_select_voice(voice
); /* Reselect the voice (just to be sure) */
1612 if ((mode
& 0x01) || (int) ((gus_read16(0x09) >> 4) < (unsigned) 2065))
1614 ret_val
= guswave_start_note2(dev
, voice
, note_num
, volume
);
1618 voices
[voice
].dev_pending
= dev
;
1619 voices
[voice
].note_pending
= note_num
;
1620 voices
[voice
].volume_pending
= volume
;
1621 voices
[voice
].volume_irq_mode
= VMODE_START_NOTE
;
1624 gus_ramp_range(2000, 4065);
1625 gus_ramp_rate(0, 63); /* Fastest possible rate */
1626 gus_rampon(0x20 | 0x40); /* Ramp down, once, irq */
1629 restore_flags(flags
);
1633 static void guswave_reset(int dev
)
1637 for (i
= 0; i
< 32; i
++)
1644 static int guswave_open(int dev
, int mode
)
1651 voice_alloc
->timestamp
= 0;
1653 if ((err
= DMAbuf_open_dma(gus_devnum
)) < 0)
1655 /* printk( "GUS: Loading samples without DMA\n"); */
1656 gus_no_dma
= 1; /* Upload samples using PIO */
1661 dram_sleep_flag
.opts
= WK_NONE
;
1663 active_device
= GUS_DEV_WAVE
;
1665 gusintr(gus_irq
, NULL
, NULL
); /* Serve pending interrupts */
1668 gusintr(gus_irq
, NULL
, NULL
); /* Serve pending interrupts */
1673 static void guswave_close(int dev
)
1680 DMAbuf_close_dma(gus_devnum
);
1683 static int guswave_load_patch(int dev
, int format
, const char *addr
,
1684 int offs
, int count
, int pmgr_flag
)
1686 struct patch_info patch
;
1690 unsigned long blk_sz
, blk_end
, left
, src_offs
, target
;
1692 sizeof_patch
= (long) &patch
.data
[0] - (long) &patch
; /* Header size */
1694 if (format
!= GUS_PATCH
)
1696 /* printk("GUS Error: Invalid patch format (key) 0x%x\n", format);*/
1699 if (count
< sizeof_patch
)
1701 /* printk("GUS Error: Patch header too short\n");*/
1704 count
-= sizeof_patch
;
1706 if (free_sample
>= MAX_SAMPLE
)
1708 /* printk("GUS: Sample table full\n");*/
1712 * Copy the header from user space but ignore the first bytes which have
1713 * been transferred already.
1716 copy_from_user(&((char *) &patch
)[offs
], &(addr
)[offs
], sizeof_patch
- offs
);
1718 if (patch
.mode
& WAVE_ROM
)
1720 if (gus_mem_size
== 0)
1723 instr
= patch
.instr_no
;
1725 if (instr
< 0 || instr
> MAX_PATCH
)
1727 /* printk(KERN_ERR "GUS: Invalid patch number %d\n", instr);*/
1730 if (count
< patch
.len
)
1732 /* printk(KERN_ERR "GUS Warning: Patch record too short (%d<%d)\n", count, (int) patch.len);*/
1735 if (patch
.len
<= 0 || patch
.len
> gus_mem_size
)
1737 /* printk(KERN_ERR "GUS: Invalid sample length %d\n", (int) patch.len);*/
1740 if (patch
.mode
& WAVE_LOOPING
)
1742 if (patch
.loop_start
< 0 || patch
.loop_start
>= patch
.len
)
1744 /* printk(KERN_ERR "GUS: Invalid loop start\n");*/
1747 if (patch
.loop_end
< patch
.loop_start
|| patch
.loop_end
> patch
.len
)
1749 /* printk(KERN_ERR "GUS: Invalid loop end\n");*/
1753 free_mem_ptr
= (free_mem_ptr
+ 31) & ~31; /* 32 byte alignment */
1755 if (patch
.mode
& WAVE_16_BITS
)
1758 * 16 bit samples must fit one 256k bank.
1760 if (patch
.len
>= GUS_BANK_SIZE
)
1762 /* printk("GUS: Sample (16 bit) too long %d\n", (int) patch.len);*/
1765 if ((free_mem_ptr
/ GUS_BANK_SIZE
) !=
1766 ((free_mem_ptr
+ patch
.len
) / GUS_BANK_SIZE
))
1768 unsigned long tmp_mem
=
1770 ((free_mem_ptr
/ GUS_BANK_SIZE
) + 1) * GUS_BANK_SIZE
;
1772 if ((tmp_mem
+ patch
.len
) > gus_mem_size
)
1775 free_mem_ptr
= tmp_mem
; /* This leaves unusable memory */
1778 if ((free_mem_ptr
+ patch
.len
) > gus_mem_size
)
1781 sample_ptrs
[free_sample
] = free_mem_ptr
;
1784 * Tremolo is not possible with envelopes
1787 if (patch
.mode
& WAVE_ENVELOPES
)
1788 patch
.mode
&= ~WAVE_TREMOLO
;
1790 if (!(patch
.mode
& WAVE_FRACTIONS
))
1792 patch
.fractions
= 0;
1794 memcpy((char *) &samples
[free_sample
], &patch
, sizeof_patch
);
1797 * Link this_one sample to the list of samples for patch 'instr'.
1800 samples
[free_sample
].key
= patch_table
[instr
];
1801 patch_table
[instr
] = free_sample
;
1804 * Use DMA to transfer the wave data to the DRAM
1809 target
= free_mem_ptr
;
1811 while (left
) /* Not completely transferred yet */
1813 blk_sz
= audio_devs
[gus_devnum
]->dmap_out
->bytes_in_use
;
1818 * DMA cannot cross bank (256k) boundaries. Check for that.
1821 blk_end
= target
+ blk_sz
;
1823 if ((target
/ GUS_BANK_SIZE
) != (blk_end
/ GUS_BANK_SIZE
))
1825 /* Split the block */
1826 blk_end
&= ~(GUS_BANK_SIZE
- 1);
1827 blk_sz
= blk_end
- target
;
1832 * For some reason the DMA is not possible. We have to use PIO.
1837 for (i
= 0; i
< blk_sz
; i
++)
1839 get_user(*(unsigned char *) &data
, (unsigned char *) &((addr
)[sizeof_patch
+ i
]));
1840 if (patch
.mode
& WAVE_UNSIGNED
)
1841 if (!(patch
.mode
& WAVE_16_BITS
) || (i
& 0x01))
1842 data
^= 0x80; /* Convert to signed */
1843 gus_poke(target
+ i
, data
);
1848 unsigned long address
, hold_address
;
1849 unsigned char dma_command
;
1850 unsigned long flags
;
1851 unsigned long tlimit
;
1853 if (audio_devs
[gus_devnum
]->dmap_out
->raw_buf
== NULL
)
1855 printk(KERN_ERR
"GUS: DMA buffer == NULL\n");
1859 * OK, move now. First in and then out.
1862 copy_from_user(audio_devs
[gus_devnum
]->dmap_out
->raw_buf
, &(addr
)[sizeof_patch
+ src_offs
], blk_sz
);
1866 /******** INTERRUPTS DISABLED NOW ********/
1867 gus_write8(0x41, 0); /* Disable GF1 DMA */
1868 DMAbuf_start_dma(gus_devnum
, audio_devs
[gus_devnum
]->dmap_out
->raw_buf_phys
,
1869 blk_sz
, DMA_MODE_WRITE
);
1872 * Set the DRAM address for the wave data
1877 /* Different address translation in enhanced mode */
1882 address
= target
>> 1; /* Convert to 16 bit word address */
1886 hi
= (unsigned char) ((address
>> 16) & 0xf0);
1887 hi
+= (unsigned char) (address
& 0x0f);
1889 gus_write16(0x42, (address
>> 4) & 0xffff); /* DMA address (low) */
1890 gus_write8(0x50, hi
);
1895 if (audio_devs
[gus_devnum
]->dmap_out
->dma
> 3)
1897 hold_address
= address
;
1898 address
= address
>> 1;
1899 address
&= 0x0001ffffL
;
1900 address
|= (hold_address
& 0x000c0000L
);
1902 gus_write16(0x42, (address
>> 4) & 0xffff); /* DRAM DMA address */
1906 * Start the DMA transfer
1909 dma_command
= 0x21; /* IRQ enable, DMA start */
1910 if (patch
.mode
& WAVE_UNSIGNED
)
1911 dma_command
|= 0x80; /* Invert MSB */
1912 if (patch
.mode
& WAVE_16_BITS
)
1913 dma_command
|= 0x40; /* 16 bit _DATA_ */
1914 if (audio_devs
[gus_devnum
]->dmap_out
->dma
> 3)
1915 dma_command
|= 0x04; /* 16 bit DMA _channel_ */
1917 gus_write8(0x41, dma_command
); /* Lets go luteet (=bugs) */
1920 * Sleep here until the DRAM DMA done interrupt is served
1922 active_device
= GUS_DEV_WAVE
;
1924 current
->timeout
= tlimit
= jiffies
+ HZ
;
1925 dram_sleep_flag
.opts
= WK_SLEEP
;
1926 interruptible_sleep_on(&dram_sleeper
);
1927 if (!(dram_sleep_flag
.opts
& WK_WAKEUP
))
1929 if (jiffies
>= tlimit
)
1930 dram_sleep_flag
.opts
|= WK_TIMEOUT
;
1932 dram_sleep_flag
.opts
&= ~WK_SLEEP
;
1933 if ((dram_sleep_flag
.opts
& WK_TIMEOUT
))
1934 printk(KERN_WARNING
"GUS: DMA Transfer timed out\n");
1935 restore_flags(flags
);
1946 gus_write8(0x41, 0); /* Stop DMA */
1949 free_mem_ptr
+= patch
.len
;
1954 static void guswave_hw_control(int dev
, unsigned char *event_rec
)
1957 unsigned short p1
, p2
;
1962 voice
= event_rec
[3];
1963 p1
= *(unsigned short *) &event_rec
[4];
1964 p2
= *(unsigned short *) &event_rec
[6];
1965 plong
= *(unsigned int *) &event_rec
[4];
1967 if ((voices
[voice
].volume_irq_mode
== VMODE_START_NOTE
) &&
1968 (cmd
!= _GUS_VOICESAMPLE
) && (cmd
!= _GUS_VOICE_POS
))
1969 do_volume_irq(voice
);
1973 case _GUS_NUMVOICES
:
1976 gus_select_voice(voice
);
1977 gus_select_max_voices(p1
);
1978 restore_flags(flags
);
1981 case _GUS_VOICESAMPLE
:
1982 guswave_set_instr(dev
, voice
, p1
);
1988 gus_select_voice(voice
);
1989 p1
&= ~0x20; /* Don't allow interrupts */
1991 restore_flags(flags
);
1997 gus_select_voice(voice
);
1999 restore_flags(flags
);
2002 case _GUS_VOICEFADE
:
2003 gus_voice_fade(voice
);
2006 case _GUS_VOICEMODE
:
2009 gus_select_voice(voice
);
2010 p1
&= ~0x20; /* Don't allow interrupts */
2012 restore_flags(flags
);
2015 case _GUS_VOICEBALA
:
2018 gus_select_voice(voice
);
2019 gus_voice_balance(p1
);
2020 restore_flags(flags
);
2023 case _GUS_VOICEFREQ
:
2026 gus_select_voice(voice
);
2027 gus_voice_freq(plong
);
2028 restore_flags(flags
);
2034 gus_select_voice(voice
);
2035 gus_voice_volume(p1
);
2036 restore_flags(flags
);
2039 case _GUS_VOICEVOL2
: /* Just update the software voice level */
2040 voices
[voice
].initial_volume
= voices
[voice
].current_volume
= p1
;
2043 case _GUS_RAMPRANGE
:
2044 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
2048 gus_select_voice(voice
);
2049 gus_ramp_range(p1
, p2
);
2050 restore_flags(flags
);
2054 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
2055 break; /* NJET-NJET */
2058 gus_select_voice(voice
);
2059 gus_ramp_rate(p1
, p2
);
2060 restore_flags(flags
);
2064 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
2068 gus_select_voice(voice
);
2069 p1
&= ~0x20; /* Don't allow interrupts */
2071 restore_flags(flags
);
2075 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
2079 gus_select_voice(voice
);
2080 p1
&= ~0x20; /* Don't allow interrupts */
2082 restore_flags(flags
);
2086 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
2087 break; /* NEJ-NEJ */
2090 gus_select_voice(voice
);
2092 restore_flags(flags
);
2095 case _GUS_VOLUME_SCALE
:
2100 case _GUS_VOICE_POS
:
2103 gus_select_voice(voice
);
2104 gus_set_voice_pos(voice
, plong
);
2105 restore_flags(flags
);
2112 static int gus_audio_set_speed(int speed
)
2115 speed
= gus_audio_speed
;
2123 gus_audio_speed
= speed
;
2125 if (only_read_access
)
2127 /* Compute nearest valid recording speed and return it */
2129 /* speed = (9878400 / (gus_audio_speed + 2)) / 16; */
2130 speed
= (((9878400 + gus_audio_speed
/ 2) / (gus_audio_speed
+ 2)) + 8) / 16;
2131 speed
= (9878400 / (speed
* 16)) - 2;
2136 static int gus_audio_set_channels(int channels
)
2139 return gus_audio_channels
;
2144 gus_audio_channels
= channels
;
2148 static int gus_audio_set_bits(int bits
)
2151 return gus_audio_bits
;
2153 if (bits
!= 8 && bits
!= 16)
2159 gus_audio_bits
= bits
;
2163 static int gus_audio_ioctl(int dev
, unsigned int cmd
, caddr_t arg
)
2169 case SOUND_PCM_WRITE_RATE
:
2170 if (get_user(val
, (int *)arg
))
2172 val
= gus_audio_set_speed(val
);
2175 case SOUND_PCM_READ_RATE
:
2176 val
= gus_audio_speed
;
2179 case SNDCTL_DSP_STEREO
:
2180 if (get_user(val
, (int *)arg
))
2182 val
= gus_audio_set_channels(val
+ 1) - 1;
2185 case SOUND_PCM_WRITE_CHANNELS
:
2186 if (get_user(val
, (int *)arg
))
2188 val
= gus_audio_set_channels(val
);
2191 case SOUND_PCM_READ_CHANNELS
:
2192 val
= gus_audio_channels
;
2195 case SNDCTL_DSP_SETFMT
:
2196 if (get_user(val
, (int *)arg
))
2198 val
= gus_audio_set_bits(val
);
2201 case SOUND_PCM_READ_BITS
:
2202 val
= gus_audio_bits
;
2205 case SOUND_PCM_WRITE_FILTER
: /* NOT POSSIBLE */
2206 case SOUND_PCM_READ_FILTER
:
2212 return put_user(val
, (int *)arg
);
2215 static void gus_audio_reset(int dev
)
2217 if (recording_active
)
2219 gus_write8(0x49, 0x00); /* Halt recording */
2220 set_input_volumes();
2224 static int saved_iw_mode
; /* A hack hack hack */
2226 static int gus_audio_open(int dev
, int mode
)
2231 if (gus_pnp_flag
&& mode
& OPEN_READ
)
2233 /* printk(KERN_ERR "GUS: Audio device #%d is playback only.\n", dev);*/
2242 reset_sample_memory();
2243 gus_select_max_voices(14);
2244 saved_iw_mode
= iw_mode
;
2247 /* There are some problems with audio in enhanced mode so disable it */
2248 gus_write8(0x19, gus_read8(0x19) & ~0x01); /* Disable enhanced mode */
2254 if (mode
& OPEN_READ
)
2256 recording_active
= 1;
2257 set_input_volumes();
2259 only_read_access
= !(mode
& OPEN_WRITE
);
2260 only_8_bits
= mode
& OPEN_READ
;
2262 audio_devs
[dev
]->format_mask
= AFMT_U8
;
2264 audio_devs
[dev
]->format_mask
= AFMT_U8
| AFMT_S16_LE
;
2269 static void gus_audio_close(int dev
)
2271 iw_mode
= saved_iw_mode
;
2277 if (recording_active
)
2279 gus_write8(0x49, 0x00); /* Halt recording */
2280 set_input_volumes();
2282 recording_active
= 0;
2285 static void gus_audio_update_volume(void)
2287 unsigned long flags
;
2290 if (pcm_active
&& pcm_opened
)
2291 for (voice
= 0; voice
< gus_audio_channels
; voice
++)
2295 gus_select_voice(voice
);
2297 gus_voice_volume(1530 + (25 * gus_pcm_volume
));
2298 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume
));
2299 restore_flags(flags
);
2303 static void play_next_pcm_block(void)
2305 unsigned long flags
;
2306 int speed
= gus_audio_speed
;
2307 int this_one
, is16bits
, chn
;
2308 unsigned long dram_loc
;
2309 unsigned char mode
[2], ramp_mode
[2];
2314 this_one
= pcm_head
;
2316 for (chn
= 0; chn
< gus_audio_channels
; chn
++)
2319 ramp_mode
[chn
] = 0x03; /* Ramping and rollover off */
2323 mode
[chn
] |= 0x20; /* Loop IRQ */
2324 voices
[chn
].loop_irq_mode
= LMODE_PCM
;
2326 if (gus_audio_bits
!= 8)
2329 mode
[chn
] |= 0x04; /* 16 bit data */
2334 dram_loc
= this_one
* pcm_bsize
;
2335 dram_loc
+= chn
* pcm_banksize
;
2337 if (this_one
== (pcm_nblk
- 1)) /* Last fragment of the DRAM buffer */
2339 mode
[chn
] |= 0x08; /* Enable loop */
2340 ramp_mode
[chn
] = 0x03; /* Disable rollover bit */
2345 ramp_mode
[chn
] = 0x04; /* Enable rollover bit */
2349 gus_select_voice(chn
);
2350 gus_voice_freq(speed
);
2352 if (gus_audio_channels
== 1)
2353 gus_voice_balance(7); /* mono */
2355 gus_voice_balance(0); /* left */
2357 gus_voice_balance(15); /* right */
2359 if (!pcm_active
) /* Playback not already active */
2362 * The playback was not started yet (or there has been a pause).
2363 * Start the voice (again) and ask for a rollover irq at the end of
2364 * this_one block. If this_one one is last of the buffers, use just
2365 * the normal loop with irq.
2370 gus_voice_volume(1530 + (25 * gus_pcm_volume
));
2371 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume
));
2373 gus_write_addr(0x0a, chn
* pcm_banksize
, 0, is16bits
); /* Starting position */
2374 gus_write_addr(0x02, chn
* pcm_banksize
, 0, is16bits
); /* Loop start */
2377 gus_write_addr(0x04, pcm_banksize
+ (pcm_bsize
* pcm_nblk
) - 1,
2378 0, is16bits
); /* Loop end location */
2381 gus_write_addr(0x04, dram_loc
+ pcm_bsize
- 1,
2382 0, is16bits
); /* Loop end location */
2384 mode
[chn
] |= 0x08; /* Enable looping */
2385 restore_flags(flags
);
2387 for (chn
= 0; chn
< gus_audio_channels
; chn
++)
2391 gus_select_voice(chn
);
2392 gus_write8(0x0d, ramp_mode
[chn
]);
2394 gus_write8(0x15, 0x00); /* Reset voice deactivate bit of SMSI */
2395 gus_voice_on(mode
[chn
]);
2396 restore_flags(flags
);
2401 static void gus_transfer_output_block(int dev
, unsigned long buf
,
2402 int total_count
, int intrflag
, int chn
)
2405 * This routine transfers one block of audio data to the DRAM. In mono mode
2406 * it's called just once. When in stereo mode, this_one routine is called
2407 * once for both channels.
2409 * The left/mono channel data is transferred to the beginning of dram and the
2410 * right data to the area pointed by gus_page_size.
2413 int this_one
, count
;
2414 unsigned long flags
;
2415 unsigned char dma_command
;
2416 unsigned long address
, hold_address
;
2421 count
= total_count
/ gus_audio_channels
;
2425 if (pcm_qlen
>= pcm_nblk
)
2426 printk(KERN_WARNING
"GUS Warning: PCM buffers out of sync\n");
2428 this_one
= pcm_current_block
= pcm_tail
;
2430 pcm_tail
= (pcm_tail
+ 1) % pcm_nblk
;
2431 pcm_datasize
[this_one
] = count
;
2434 this_one
= pcm_current_block
;
2436 gus_write8(0x41, 0); /* Disable GF1 DMA */
2437 DMAbuf_start_dma(dev
, buf
+ (chn
* count
), count
, DMA_MODE_WRITE
);
2439 address
= this_one
* pcm_bsize
;
2440 address
+= chn
* pcm_banksize
;
2442 if (audio_devs
[dev
]->dmap_out
->dma
> 3)
2444 hold_address
= address
;
2445 address
= address
>> 1;
2446 address
&= 0x0001ffffL
;
2447 address
|= (hold_address
& 0x000c0000L
);
2449 gus_write16(0x42, (address
>> 4) & 0xffff); /* DRAM DMA address */
2451 dma_command
= 0x21; /* IRQ enable, DMA start */
2453 if (gus_audio_bits
!= 8)
2454 dma_command
|= 0x40; /* 16 bit _DATA_ */
2456 dma_command
|= 0x80; /* Invert MSB */
2458 if (audio_devs
[dev
]->dmap_out
->dma
> 3)
2459 dma_command
|= 0x04; /* 16 bit DMA channel */
2461 gus_write8(0x41, dma_command
); /* Kick start */
2463 if (chn
== (gus_audio_channels
- 1)) /* Last channel */
2466 * Last (right or mono) channel data
2468 dma_active
= 1; /* DMA started. There is a unacknowledged buffer */
2469 active_device
= GUS_DEV_PCM_DONE
;
2470 if (!pcm_active
&& (pcm_qlen
> 1 || count
< pcm_bsize
))
2472 play_next_pcm_block();
2478 * Left channel data. The right channel
2479 * is transferred after DMA interrupt
2481 active_device
= GUS_DEV_PCM_CONTINUE
;
2484 restore_flags(flags
);
2487 static void gus_uninterleave8(char *buf
, int l
)
2489 /* This routine uninterleaves 8 bit stereo output (LRLRLR->LLLRRR) */
2490 int i
, p
= 0, halfsize
= l
/ 2;
2491 char *buf2
= buf
+ halfsize
, *src
= bounce_buf
;
2493 memcpy(bounce_buf
, buf
, l
);
2495 for (i
= 0; i
< halfsize
; i
++)
2497 buf
[i
] = src
[p
++]; /* Left channel */
2498 buf2
[i
] = src
[p
++]; /* Right channel */
2502 static void gus_uninterleave16(short *buf
, int l
)
2504 /* This routine uninterleaves 16 bit stereo output (LRLRLR->LLLRRR) */
2505 int i
, p
= 0, halfsize
= l
/ 2;
2506 short *buf2
= buf
+ halfsize
, *src
= (short *) bounce_buf
;
2508 memcpy(bounce_buf
, (char *) buf
, l
* 2);
2510 for (i
= 0; i
< halfsize
; i
++)
2512 buf
[i
] = src
[p
++]; /* Left channel */
2513 buf2
[i
] = src
[p
++]; /* Right channel */
2517 static void gus_audio_output_block(int dev
, unsigned long buf
, int total_count
,
2520 struct dma_buffparms
*dmap
= audio_devs
[dev
]->dmap_out
;
2522 dmap
->flags
|= DMA_NODMA
| DMA_NOTIMEOUT
;
2524 pcm_current_buf
= buf
;
2525 pcm_current_count
= total_count
;
2526 pcm_current_intrflag
= intrflag
;
2527 pcm_current_dev
= dev
;
2528 if (gus_audio_channels
== 2)
2530 char *b
= dmap
->raw_buf
+ (buf
- dmap
->raw_buf_phys
);
2532 if (gus_audio_bits
== 8)
2533 gus_uninterleave8(b
, total_count
);
2535 gus_uninterleave16((short *) b
, total_count
/ 2);
2537 gus_transfer_output_block(dev
, buf
, total_count
, intrflag
, 0);
2540 static void gus_audio_start_input(int dev
, unsigned long buf
, int count
,
2543 unsigned long flags
;
2549 DMAbuf_start_dma(dev
, buf
, count
, DMA_MODE_READ
);
2550 mode
= 0xa0; /* DMA IRQ enabled, invert MSB */
2552 if (audio_devs
[dev
]->dmap_in
->dma
> 3)
2553 mode
|= 0x04; /* 16 bit DMA channel */
2554 if (gus_audio_channels
> 1)
2555 mode
|= 0x02; /* Stereo */
2556 mode
|= 0x01; /* DMA enable */
2558 gus_write8(0x49, mode
);
2559 restore_flags(flags
);
2562 static int gus_audio_prepare_for_input(int dev
, int bsize
, int bcount
)
2566 gus_audio_bsize
= bsize
;
2567 audio_devs
[dev
]->dmap_in
->flags
|= DMA_NODMA
;
2568 rate
= (((9878400 + gus_audio_speed
/ 2) / (gus_audio_speed
+ 2)) + 8) / 16;
2570 gus_write8(0x48, rate
& 0xff); /* Set sampling rate */
2572 if (gus_audio_bits
!= 8)
2574 /* printk("GUS Error: 16 bit recording not supported\n");*/
2580 static int gus_audio_prepare_for_output(int dev
, int bsize
, int bcount
)
2584 long mem_ptr
, mem_size
;
2586 audio_devs
[dev
]->dmap_out
->flags
|= DMA_NODMA
| DMA_NOTIMEOUT
;
2588 mem_size
= gus_mem_size
/ gus_audio_channels
;
2590 if (mem_size
> (256 * 1024))
2591 mem_size
= 256 * 1024;
2593 pcm_bsize
= bsize
/ gus_audio_channels
;
2594 pcm_head
= pcm_tail
= pcm_qlen
= 0;
2596 pcm_nblk
= 2; /* MAX_PCM_BUFFERS; */
2597 if ((pcm_bsize
* pcm_nblk
) > mem_size
)
2598 pcm_nblk
= mem_size
/ pcm_bsize
;
2600 for (i
= 0; i
< pcm_nblk
; i
++)
2601 pcm_datasize
[i
] = 0;
2603 pcm_banksize
= pcm_nblk
* pcm_bsize
;
2605 if (gus_audio_bits
!= 8 && pcm_banksize
== (256 * 1024))
2607 gus_write8(0x41, 0); /* Disable GF1 DMA */
2611 static int gus_local_qlen(int dev
)
2617 static struct audio_driver gus_audio_driver
=
2621 gus_audio_output_block
,
2622 gus_audio_start_input
,
2624 gus_audio_prepare_for_input
,
2625 gus_audio_prepare_for_output
,
2631 static void guswave_setup_voice(int dev
, int voice
, int chn
)
2633 struct channel_info
*info
= &synth_devs
[dev
]->chn_info
[chn
];
2635 guswave_set_instr(dev
, voice
, info
->pgm_num
);
2636 voices
[voice
].expression_vol
= info
->controllers
[CTL_EXPRESSION
]; /* Just MSB */
2637 voices
[voice
].main_vol
= (info
->controllers
[CTL_MAIN_VOLUME
] * 100) / (unsigned) 128;
2638 voices
[voice
].panning
= (info
->controllers
[CTL_PAN
] * 2) - 128;
2639 voices
[voice
].bender
= 0;
2640 voices
[voice
].bender_range
= info
->bender_range
;
2643 voices
[voice
].fixed_pitch
= 1;
2646 static void guswave_bender(int dev
, int voice
, int value
)
2649 unsigned long flags
;
2651 voices
[voice
].bender
= value
- 8192;
2652 freq
= compute_finetune(voices
[voice
].orig_freq
, value
- 8192, voices
[voice
].bender_range
, 0);
2653 voices
[voice
].current_freq
= freq
;
2657 gus_select_voice(voice
);
2658 gus_voice_freq(freq
);
2659 restore_flags(flags
);
2662 static int guswave_alloc(int dev
, int chn
, int note
, struct voice_alloc_info
*alloc
)
2664 int i
, p
, best
= -1, best_time
= 0x7fffffff;
2668 * First look for a completely stopped voice
2671 for (i
= 0; i
< alloc
->max_voice
; i
++)
2673 if (alloc
->map
[p
] == 0)
2678 if (alloc
->alloc_times
[p
] < best_time
)
2681 best_time
= alloc
->alloc_times
[p
];
2683 p
= (p
+ 1) % alloc
->max_voice
;
2687 * Then look for a releasing voice
2690 for (i
= 0; i
< alloc
->max_voice
; i
++)
2692 if (alloc
->map
[p
] == 0xffff)
2697 p
= (p
+ 1) % alloc
->max_voice
;
2706 static struct synth_operations guswave_operations
=
2725 guswave_volume_method
,
2731 static void set_input_volumes(void)
2733 unsigned long flags
;
2734 unsigned char mask
= 0xff & ~0x06; /* Just line out enabled */
2736 if (have_gus_max
) /* Don't disturb GUS MAX */
2743 * Enable channels having vol > 10%
2744 * Note! bit 0x01 means the line in DISABLED while 0x04 means
2745 * the mic in ENABLED.
2747 if (gus_line_vol
> 10)
2749 if (gus_mic_vol
> 10)
2752 if (recording_active
)
2755 * Disable channel, if not selected for recording
2757 if (!(gus_recmask
& SOUND_MASK_LINE
))
2759 if (!(gus_recmask
& SOUND_MASK_MIC
))
2763 mix_image
|= mask
& 0x07;
2764 outb((mix_image
), u_Mixer
);
2766 restore_flags(flags
);
2769 #define MIX_DEVS (SOUND_MASK_MIC|SOUND_MASK_LINE| \
2770 SOUND_MASK_SYNTH|SOUND_MASK_PCM)
2772 int gus_default_mixer_ioctl(int dev
, unsigned int cmd
, caddr_t arg
)
2776 if (((cmd
>> 8) & 0xff) != 'M')
2779 if (!access_ok(VERIFY_WRITE
, (int *)arg
, sizeof(int)))
2782 if (_SIOC_DIR(cmd
) & _SIOC_WRITE
)
2784 if (__get_user(val
, (int *) arg
))
2789 case SOUND_MIXER_RECSRC
:
2790 gus_recmask
= val
& MIX_DEVS
;
2791 if (!(gus_recmask
& (SOUND_MASK_MIC
| SOUND_MASK_LINE
)))
2792 gus_recmask
= SOUND_MASK_MIC
;
2793 /* Note! Input volumes are updated during next open for recording */
2797 case SOUND_MIXER_MIC
:
2804 set_input_volumes();
2805 val
= vol
| (vol
<< 8);
2808 case SOUND_MIXER_LINE
:
2815 set_input_volumes();
2816 val
= vol
| (vol
<< 8);
2819 case SOUND_MIXER_PCM
:
2820 gus_pcm_volume
= val
& 0xff;
2821 if (gus_pcm_volume
< 0)
2823 if (gus_pcm_volume
> 100)
2824 gus_pcm_volume
= 100;
2825 gus_audio_update_volume();
2826 val
= gus_pcm_volume
| (gus_pcm_volume
<< 8);
2829 case SOUND_MIXER_SYNTH
:
2830 gus_wave_volume
= val
& 0xff;
2831 if (gus_wave_volume
< 0)
2832 gus_wave_volume
= 0;
2833 if (gus_wave_volume
> 100)
2834 gus_wave_volume
= 100;
2835 if (active_device
== GUS_DEV_WAVE
)
2838 for (voice
= 0; voice
< nr_voices
; voice
++)
2839 dynamic_volume_change(voice
); /* Apply the new vol */
2841 val
= gus_wave_volume
| (gus_wave_volume
<< 8);
2855 case SOUND_MIXER_RECSRC
:
2859 case SOUND_MIXER_DEVMASK
:
2863 case SOUND_MIXER_STEREODEVS
:
2867 case SOUND_MIXER_RECMASK
:
2868 val
= SOUND_MASK_MIC
| SOUND_MASK_LINE
;
2871 case SOUND_MIXER_CAPS
:
2875 case SOUND_MIXER_MIC
:
2876 val
= gus_mic_vol
| (gus_mic_vol
<< 8);
2879 case SOUND_MIXER_LINE
:
2880 val
= gus_line_vol
| (gus_line_vol
<< 8);
2883 case SOUND_MIXER_PCM
:
2884 val
= gus_pcm_volume
| (gus_pcm_volume
<< 8);
2887 case SOUND_MIXER_SYNTH
:
2888 val
= gus_wave_volume
| (gus_wave_volume
<< 8);
2895 return __put_user(val
, (int *)arg
);
2898 static struct mixer_operations gus_mixer_operations
=
2901 "Gravis Ultrasound",
2902 gus_default_mixer_ioctl
2905 static int gus_default_mixer_init(void)
2909 if ((n
= sound_alloc_mixerdev()) != -1)
2912 * Don't install if there is another
2915 mixer_devs
[n
] = &gus_mixer_operations
;
2920 * Enable all mixer channels on the GF1 side. Otherwise recording will
2921 * not be possible using GUS MAX.
2924 mix_image
|= 0x04; /* All channels enabled */
2925 outb((mix_image
), u_Mixer
);
2930 void gus_wave_init(struct address_info
*hw_config
)
2932 unsigned long flags
;
2934 char *model_num
= "2.4";
2935 char tmp
[64], tmp2
[64];
2936 int gus_type
= 0x24; /* 2.4 */
2938 int irq
= hw_config
->irq
, dma
= hw_config
->dma
, dma2
= hw_config
->dma2
;
2942 hw_config
->slots
[0] = -1; /* No wave */
2943 hw_config
->slots
[1] = -1; /* No ad1848 */
2944 hw_config
->slots
[4] = -1; /* No audio */
2945 hw_config
->slots
[5] = -1; /* No mixer */
2949 if (irq
< 0 || irq
> 15)
2951 printk(KERN_ERR
"ERROR! Invalid IRQ#%d. GUS Disabled", irq
);
2956 if (dma
< 0 || dma
> 7 || dma
== 4)
2958 printk(KERN_ERR
"ERROR! Invalid DMA#%d. GUS Disabled", dma
);
2969 * Try to identify the GUS model.
2971 * Versions < 3.6 don't have the digital ASIC. Try to probe it first.
2976 outb((0x20), gus_base
+ 0x0f);
2977 val
= inb(gus_base
+ 0x0f);
2978 restore_flags(flags
);
2980 if (gus_pnp_flag
|| (val
!= 0xff && (val
& 0x06))) /* Should be 0x02?? */
2985 ad_flags
= 0x12345678; /* Interwave "magic" */
2987 * It has the digital ASIC so the card is at least v3.4.
2988 * Next try to detect the true model.
2991 if (gus_pnp_flag
) /* Hack hack hack */
2994 val
= inb(u_MixSelect
);
2997 * Value 255 means pre-3.7 which don't have mixer.
2998 * Values 5 thru 9 mean v3.7 which has a ICS2101 mixer.
2999 * 10 and above is GUS MAX which has the CS4231 codec/mixer.
3003 if (val
== 255 || val
< 5)
3012 mixer_type
= ICS2101
;
3013 request_region(u_MixSelect
, 1, "GUS mixer");
3019 mixer_type
= CS4231
;
3020 #ifdef CONFIG_GUSMAX
3022 unsigned char max_config
= 0x40; /* Codec enable */
3028 max_config
|= 0x10; /* 16 bit capture DMA */
3031 max_config
|= 0x20; /* 16 bit playback DMA */
3033 max_config
|= (gus_base
>> 4) & 0x0f; /* Extract the X from 2X0 */
3035 outb((max_config
), gus_base
+ 0x106); /* UltraMax control */
3038 if (ad1848_detect(gus_base
+ 0x10c, &ad_flags
, hw_config
->osp
))
3040 char *name
= "GUS MAX";
3041 int old_num_mixers
= num_mixers
;
3046 gus_mic_vol
= gus_line_vol
= gus_pcm_volume
= 100;
3047 gus_wave_volume
= 90;
3049 if (hw_config
->name
)
3050 name
= hw_config
->name
;
3052 hw_config
->slots
[1] = ad1848_init(name
, gus_base
+ 0x10c,
3053 -irq
, gus_dma2
, /* Playback DMA */
3054 gus_dma
, /* Capture DMA */
3055 1, /* Share DMA channels with GF1 */
3058 if (num_mixers
> old_num_mixers
)
3060 /* GUS has it's own mixer map */
3061 AD1848_REROUTE(SOUND_MIXER_LINE1
, SOUND_MIXER_SYNTH
);
3062 AD1848_REROUTE(SOUND_MIXER_LINE2
, SOUND_MIXER_CD
);
3063 AD1848_REROUTE(SOUND_MIXER_LINE3
, SOUND_MIXER_LINE
);
3067 printk(KERN_WARNING
"GUS: No CS4231 ??");
3069 printk(KERN_ERR
"GUS MAX found, but not compiled in\n");
3076 * ASIC not detected so the card must be 2.2 or 2.4.
3077 * There could still be the 16-bit/mixer daughter card.
3081 if (hw_config
->name
)
3083 strncpy(tmp
, hw_config
->name
, 45);
3085 sprintf(tmp2
, "%s (%dk)", tmp
, (int) gus_mem_size
/ 1024);
3086 tmp2
[sizeof(tmp2
) - 1] = 0;
3088 else if (gus_pnp_flag
)
3090 sprintf(tmp2
, "Gravis UltraSound PnP (%dk)",
3091 (int) gus_mem_size
/ 1024);
3094 sprintf(tmp2
, "Gravis UltraSound %s (%dk)", model_num
, (int) gus_mem_size
/ 1024);
3097 samples
= (struct patch_info
*)vmalloc((MAX_SAMPLE
+ 1) * sizeof(*samples
));
3098 if (samples
== NULL
)
3100 printk(KERN_WARNING
"gus_init: Cant allocate memory for instrument tables\n");
3103 conf_printf(tmp2
, hw_config
);
3104 tmp2
[sizeof(gus_info
.name
) - 1] = 0;
3105 strcpy(gus_info
.name
, tmp2
);
3107 if ((sdev
= sound_alloc_synthdev()) == -1)
3108 printk(KERN_WARNING
"gus_init: Too many synthesizers\n");
3111 voice_alloc
= &guswave_operations
.alloc
;
3113 guswave_operations
.id
= "IWAVE";
3114 hw_config
->slots
[0] = sdev
;
3115 synth_devs
[sdev
] = &guswave_operations
;
3117 #if defined(CONFIG_SEQUENCER) || defined(MODULE)
3118 gus_tmr_install(gus_base
+ 8);
3122 reset_sample_memory();
3126 if (gus_mem_size
> 0)
3128 if ((dev
= sound_alloc_audiodev()) != -1)
3130 hw_config
->slots
[4] = dev
;
3131 if ((gus_devnum
= sound_install_audiodrv(AUDIO_DRIVER_VERSION
,
3134 sizeof(struct audio_driver
),
3136 ((!iw_mode
&& dma2
!= dma
&& dma2
!= -1) ?
3138 AFMT_U8
| AFMT_S16_LE
,
3146 audio_devs
[gus_devnum
]->min_fragment
= 9; /* 512k */
3147 audio_devs
[gus_devnum
]->max_fragment
= 11; /* 8k (must match size of bounce_buf */
3148 audio_devs
[gus_devnum
]->mixer_dev
= -1; /* Next mixer# */
3149 audio_devs
[gus_devnum
]->flags
|= DMA_HARDSTOP
;
3151 printk(KERN_WARNING
"GUS: Too many audio devices available\n");
3155 * Mixer dependent initialization.
3161 gus_mic_vol
= gus_line_vol
= gus_pcm_volume
= 100;
3162 gus_wave_volume
= 90;
3163 request_region(u_MixSelect
, 1, "GUS mixer");
3164 hw_config
->slots
[5] = ics2101_mixer_init();
3165 audio_devs
[gus_devnum
]->mixer_dev
= hw_config
->slots
[5]; /* Next mixer# */
3169 /* Initialized elsewhere (ad1848.c) */
3171 hw_config
->slots
[5] = gus_default_mixer_init();
3172 audio_devs
[gus_devnum
]->mixer_dev
= hw_config
->slots
[5]; /* Next mixer# */
3177 void gus_wave_unload(struct address_info
*hw_config
)
3179 #ifdef CONFIG_GUSMAX
3182 ad1848_unload(gus_base
+ 0x10c,
3184 gus_dma2
, /* Playback DMA */
3185 gus_dma
, /* Capture DMA */
3186 1); /* Share DMA channels with GF1 */
3190 if (mixer_type
== ICS2101
)
3192 release_region(u_MixSelect
, 1);
3194 if (hw_config
->slots
[0] != -1)
3195 sound_unload_synthdev(hw_config
->slots
[0]);
3196 if (hw_config
->slots
[1] != -1)
3197 sound_unload_audiodev(hw_config
->slots
[1]);
3198 if (hw_config
->slots
[2] != -1)
3199 sound_unload_mididev(hw_config
->slots
[2]);
3200 if (hw_config
->slots
[4] != -1)
3201 sound_unload_audiodev(hw_config
->slots
[4]);
3202 if (hw_config
->slots
[5] != -1)
3203 sound_unload_mixerdev(hw_config
->slots
[5]);
3210 static void do_loop_irq(int voice
)
3214 unsigned long flags
;
3218 gus_select_voice(voice
);
3220 tmp
= gus_read8(0x00);
3222 * Disable wave IRQ for this_one voice
3224 gus_write8(0x00, tmp
);
3226 if (tmp
& 0x03) /* Voice stopped */
3227 voice_alloc
->map
[voice
] = 0;
3229 mode
= voices
[voice
].loop_irq_mode
;
3230 voices
[voice
].loop_irq_mode
= 0;
3231 parm
= voices
[voice
].loop_irq_parm
;
3235 case LMODE_FINISH
: /*
3236 * Final loop finished, shoot volume down
3239 if ((int) (gus_read16(0x09) >> 4) < 100) /*
3240 * Get current volume
3245 gus_voice_init(voice
);
3248 gus_ramp_range(65, 4065);
3249 gus_ramp_rate(0, 63); /*
3250 * Fastest possible rate
3252 gus_rampon(0x20 | 0x40); /*
3253 * Ramp down, once, irq
3255 voices
[voice
].volume_irq_mode
= VMODE_HALT
;
3258 case LMODE_PCM_STOP
:
3259 pcm_active
= 0; /* Signal to the play_next_pcm_block routine */
3263 pcm_head
= (pcm_head
+ 1) % pcm_nblk
;
3264 if (pcm_qlen
&& pcm_active
)
3266 play_next_pcm_block();
3270 /* Underrun. Just stop the voice */
3271 gus_select_voice(0); /* Left channel */
3274 gus_select_voice(1); /* Right channel */
3281 * If the queue was full before this interrupt, the DMA transfer was
3282 * suspended. Let it continue now.
3285 if (audio_devs
[gus_devnum
]->dmap_out
->qlen
> 0)
3286 DMAbuf_outputintr(gus_devnum
, 0);
3292 restore_flags(flags
);
3295 static void do_volume_irq(int voice
)
3299 unsigned long flags
;
3304 gus_select_voice(voice
);
3305 tmp
= gus_read8(0x0d);
3307 * Disable volume ramp IRQ
3309 gus_write8(0x0d, tmp
);
3311 mode
= voices
[voice
].volume_irq_mode
;
3312 voices
[voice
].volume_irq_mode
= 0;
3313 parm
= voices
[voice
].volume_irq_parm
;
3317 case VMODE_HALT
: /* Decay phase finished */
3319 gus_write8(0x15, 0x02); /* Set voice deactivate bit of SMSI */
3320 restore_flags(flags
);
3321 gus_voice_init(voice
);
3324 case VMODE_ENVELOPE
:
3326 restore_flags(flags
);
3327 step_envelope(voice
);
3330 case VMODE_START_NOTE
:
3331 restore_flags(flags
);
3332 guswave_start_note2(voices
[voice
].dev_pending
, voice
,
3333 voices
[voice
].note_pending
, voices
[voice
].volume_pending
);
3334 if (voices
[voice
].kill_pending
)
3335 guswave_kill_note(voices
[voice
].dev_pending
, voice
,
3336 voices
[voice
].note_pending
, 0);
3338 if (voices
[voice
].sample_pending
>= 0)
3340 guswave_set_instr(voices
[voice
].dev_pending
, voice
,
3341 voices
[voice
].sample_pending
);
3342 voices
[voice
].sample_pending
= -1;
3347 restore_flags(flags
);
3349 restore_flags(flags
);
3352 void gus_voice_irq(void)
3354 unsigned long wave_ignore
= 0, volume_ignore
= 0;
3355 unsigned long voice_bit
;
3357 unsigned char src
, voice
;
3361 src
= gus_read8(0x0f); /*
3367 if (src
== (0x80 | 0x40))
3372 voice_bit
= 1 << voice
;
3374 if (!(src
& 0x80)) /*
3377 if (!(wave_ignore
& voice_bit
) && (int) voice
< nr_voices
) /*
3382 wave_ignore
|= voice_bit
;
3385 if (!(src
& 0x40)) /*
3386 * Volume IRQ pending
3388 if (!(volume_ignore
& voice_bit
) && (int) voice
< nr_voices
) /*
3393 volume_ignore
|= voice_bit
;
3394 do_volume_irq(voice
);
3399 void guswave_dma_irq(void)
3401 unsigned char status
;
3403 status
= gus_look8(0x41); /* Get DMA IRQ Status */
3404 if (status
& 0x40) /* DMA interrupt pending */
3405 switch (active_device
)
3408 if ((dram_sleep_flag
.opts
& WK_SLEEP
))
3410 dram_sleep_flag
.opts
= WK_WAKEUP
;
3411 wake_up(&dram_sleeper
);
3415 case GUS_DEV_PCM_CONTINUE
: /* Left channel data transferred */
3416 gus_write8(0x41, 0); /* Disable GF1 DMA */
3417 gus_transfer_output_block(pcm_current_dev
, pcm_current_buf
,
3419 pcm_current_intrflag
, 1);
3422 case GUS_DEV_PCM_DONE
: /* Right or mono channel data transferred */
3423 gus_write8(0x41, 0); /* Disable GF1 DMA */
3424 if (pcm_qlen
< pcm_nblk
)
3429 if (audio_devs
[gus_devnum
]->dmap_out
->qlen
> 0)
3430 DMAbuf_outputintr(gus_devnum
, 0);
3437 status
= gus_look8(0x49); /*
3438 * Get Sampling IRQ Status
3440 if (status
& 0x40) /*
3441 * Sampling Irq pending
3444 DMAbuf_inputintr(gus_devnum
);
3448 #if defined(CONFIG_SEQUENCER) || defined(MODULE)
3454 static volatile int select_addr
, data_addr
;
3455 static volatile int curr_timer
= 0;
3457 void gus_timer_command(unsigned int addr
, unsigned int val
)
3461 outb(((unsigned char) (addr
& 0xff)), select_addr
);
3463 for (i
= 0; i
< 2; i
++)
3466 outb(((unsigned char) (val
& 0xff)), data_addr
);
3468 for (i
= 0; i
< 2; i
++)
3472 static void arm_timer(int timer
, unsigned int interval
)
3478 gus_write8(0x46, 256 - interval
); /* Set counter for timer 1 */
3479 gus_write8(0x45, 0x04); /* Enable timer 1 IRQ */
3480 gus_timer_command(0x04, 0x01); /* Start timer 1 */
3484 gus_write8(0x47, 256 - interval
); /* Set counter for timer 2 */
3485 gus_write8(0x45, 0x08); /* Enable timer 2 IRQ */
3486 gus_timer_command(0x04, 0x02); /* Start timer 2 */
3489 gus_timer_enabled
= 1;
3492 static unsigned int gus_tmr_start(int dev
, unsigned int usecs_per_tick
)
3494 int timer_no
, resolution
;
3497 if (usecs_per_tick
> (256 * 80))
3500 resolution
= 320; /* usec */
3505 resolution
= 80; /* usec */
3507 divisor
= (usecs_per_tick
+ (resolution
/ 2)) / resolution
;
3508 arm_timer(timer_no
, divisor
);
3510 return divisor
* resolution
;
3513 static void gus_tmr_disable(int dev
)
3515 gus_write8(0x45, 0); /* Disable both timers */
3516 gus_timer_enabled
= 0;
3519 static void gus_tmr_restart(int dev
)
3521 if (curr_timer
== 1)
3522 gus_write8(0x45, 0x04); /* Start timer 1 again */
3524 gus_write8(0x45, 0x08); /* Start timer 2 again */
3525 gus_timer_enabled
= 1;
3528 static struct sound_lowlev_timer gus_tmr
=
3537 static void gus_tmr_install(int io_base
)
3539 struct sound_lowlev_timer
*tmr
;
3541 select_addr
= io_base
;
3542 data_addr
= io_base
+ 1;
3546 #ifdef THIS_GETS_FIXED
3547 sound_timer_init(&gus_tmr
, "GUS");