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 struct wait_queue
*dram_sleeper
= NULL
;
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
[] =
169 static struct patch_info
*samples
= NULL
;
170 static long sample_ptrs
[MAX_SAMPLE
+ 1];
171 static int sample_map
[32];
172 static int free_sample
;
173 static int mixer_type
= 0;
176 static int patch_table
[MAX_PATCH
];
177 static int patch_map
[32];
179 static struct synth_info gus_info
= {
180 "Gravis UltraSound", 0, SYNTH_TYPE_SAMPLE
, SAMPLE_TYPE_GUS
,
184 static void gus_poke(long addr
, unsigned char data
);
185 static void compute_and_set_volume(int voice
, int volume
, int ramp_time
);
186 extern unsigned short gus_adagio_vol(int vel
, int mainv
, int xpn
, int voicev
);
187 extern unsigned short gus_linear_vol(int vol
, int mainvol
);
188 static void compute_volume(int voice
, int volume
);
189 static void do_volume_irq(int voice
);
190 static void set_input_volumes(void);
191 static void gus_tmr_install(int io_base
);
193 #define INSTANT_RAMP -1 /* Instant change. No ramping */
194 #define FAST_RAMP 0 /* Fastest possible ramp */
196 static void reset_sample_memory(void)
200 for (i
= 0; i
<= MAX_SAMPLE
; i
++)
202 for (i
= 0; i
< 32; i
++)
204 for (i
= 0; i
< 32; i
++)
207 gus_poke(0, 0); /* Put a silent sample to the beginning */
213 for (i
= 0; i
< MAX_PATCH
; i
++)
214 patch_table
[i
] = NOT_SAMPLE
;
221 for (i
= 0; i
< 7; i
++)
225 static void gus_poke(long addr
, unsigned char data
)
226 { /* Writes a byte to the DRAM */
231 outb((0x43), u_Command
);
232 outb((addr
& 0xff), u_DataLo
);
233 outb(((addr
>> 8) & 0xff), u_DataHi
);
235 outb((0x44), u_Command
);
236 outb(((addr
>> 16) & 0xff), u_DataHi
);
237 outb((data
), u_DRAMIO
);
238 restore_flags(flags
);
241 static unsigned char gus_peek(long addr
)
242 { /* Reads a byte from the DRAM */
248 outb((0x43), u_Command
);
249 outb((addr
& 0xff), u_DataLo
);
250 outb(((addr
>> 8) & 0xff), u_DataHi
);
252 outb((0x44), u_Command
);
253 outb(((addr
>> 16) & 0xff), u_DataHi
);
255 restore_flags(flags
);
260 void gus_write8(int reg
, unsigned int data
)
261 { /* Writes to an indirect register (8 bit) */
267 outb((reg
), u_Command
);
268 outb(((unsigned char) (data
& 0xff)), u_DataHi
);
270 restore_flags(flags
);
273 static unsigned char gus_read8(int reg
)
275 /* Reads from an indirect register (8 bit). Offset 0x80. */
281 outb((reg
| 0x80), u_Command
);
283 restore_flags(flags
);
288 static unsigned char gus_look8(int reg
)
290 /* Reads from an indirect register (8 bit). No additional offset. */
296 outb((reg
), u_Command
);
298 restore_flags(flags
);
303 static void gus_write16(int reg
, unsigned int data
)
305 /* Writes to an indirect register (16 bit) */
311 outb((reg
), u_Command
);
313 outb(((unsigned char) (data
& 0xff)), u_DataLo
);
314 outb(((unsigned char) ((data
>> 8) & 0xff)), u_DataHi
);
316 restore_flags(flags
);
319 static unsigned short gus_read16(int reg
)
321 /* Reads from an indirect register (16 bit). Offset 0x80. */
323 unsigned char hi
, lo
;
328 outb((reg
| 0x80), u_Command
);
333 restore_flags(flags
);
335 return ((hi
<< 8) & 0xff00) | lo
;
338 static unsigned short gus_look16(int reg
)
340 /* Reads from an indirect register (16 bit). No additional offset. */
342 unsigned char hi
, lo
;
347 outb((reg
), u_Command
);
352 restore_flags(flags
);
354 return ((hi
<< 8) & 0xff00) | lo
;
357 static void gus_write_addr(int reg
, unsigned long address
, int frac
, int is16bit
)
359 /* Writes an 24 bit memory address */
360 unsigned long hold_address
;
369 /* Interwave spesific address translations */
375 * Special processing required for 16 bit patches
378 hold_address
= address
;
379 address
= address
>> 1;
380 address
&= 0x0001ffffL
;
381 address
|= (hold_address
& 0x000c0000L
);
384 gus_write16(reg
, (unsigned short) ((address
>> 7) & 0xffff));
385 gus_write16(reg
+ 1, (unsigned short) ((address
<< 9) & 0xffff)
387 /* Could writing twice fix problems with GUS_VOICE_POS()? Let's try. */
389 gus_write16(reg
, (unsigned short) ((address
>> 7) & 0xffff));
390 gus_write16(reg
+ 1, (unsigned short) ((address
<< 9) & 0xffff)
392 restore_flags(flags
);
395 static void gus_select_voice(int voice
)
397 if (voice
< 0 || voice
> 31)
399 outb((voice
), u_Voice
);
402 static void gus_select_max_voices(int nvoices
)
411 voice_alloc
->max_voice
= nr_voices
= nvoices
;
412 gus_write8(0x0e, (nvoices
- 1) | 0xc0);
415 static void gus_voice_on(unsigned int mode
)
417 gus_write8(0x00, (unsigned char) (mode
& 0xfc));
419 gus_write8(0x00, (unsigned char) (mode
& 0xfc));
422 static void gus_voice_off(void)
424 gus_write8(0x00, gus_read8(0x00) | 0x03);
427 static void gus_voice_mode(unsigned int m
)
429 unsigned char mode
= (unsigned char) (m
& 0xff);
431 gus_write8(0x00, (gus_read8(0x00) & 0x03) |
432 (mode
& 0xfc)); /* Don't touch last two bits */
434 gus_write8(0x00, (gus_read8(0x00) & 0x03) | (mode
& 0xfc));
437 static void gus_voice_freq(unsigned long freq
)
439 unsigned long divisor
= freq_div_table
[nr_voices
- 14];
442 /* Interwave plays at 44100 Hz with any number of voices */
444 fc
= (unsigned short) (((freq
<< 9) + (44100 >> 1)) / 44100);
446 fc
= (unsigned short) (((freq
<< 9) + (divisor
>> 1)) / divisor
);
449 gus_write16(0x01, fc
);
452 static void gus_voice_volume(unsigned int vol
)
454 gus_write8(0x0d, 0x03); /* Stop ramp before setting volume */
455 gus_write16(0x09, (unsigned short) (vol
<< 4));
458 static void gus_voice_balance(unsigned int balance
)
460 gus_write8(0x0c, (unsigned char) (balance
& 0xff));
463 static void gus_ramp_range(unsigned int low
, unsigned int high
)
465 gus_write8(0x07, (unsigned char) ((low
>> 4) & 0xff));
466 gus_write8(0x08, (unsigned char) ((high
>> 4) & 0xff));
469 static void gus_ramp_rate(unsigned int scale
, unsigned int rate
)
471 gus_write8(0x06, (unsigned char) (((scale
& 0x03) << 6) | (rate
& 0x3f)));
474 static void gus_rampon(unsigned int m
)
476 unsigned char mode
= (unsigned char) (m
& 0xff);
478 gus_write8(0x0d, mode
& 0xfc);
480 gus_write8(0x0d, mode
& 0xfc);
483 static void gus_ramp_mode(unsigned int m
)
485 unsigned char mode
= (unsigned char) (m
& 0xff);
487 gus_write8(0x0d, (gus_read8(0x0d) & 0x03) |
488 (mode
& 0xfc)); /* Leave the last 2 bits alone */
490 gus_write8(0x0d, (gus_read8(0x0d) & 0x03) | (mode
& 0xfc));
493 static void gus_rampoff(void)
495 gus_write8(0x0d, 0x03);
498 static void gus_set_voice_pos(int voice
, long position
)
502 if ((sample_no
= sample_map
[voice
]) != -1) {
503 if (position
< samples
[sample_no
].len
) {
504 if (voices
[voice
].volume_irq_mode
== VMODE_START_NOTE
)
505 voices
[voice
].offset_pending
= position
;
507 gus_write_addr(0x0a, sample_ptrs
[sample_no
] + position
, 0,
508 samples
[sample_no
].mode
& WAVE_16_BITS
);
513 static void gus_voice_init(int voice
)
519 gus_select_voice(voice
);
522 gus_write_addr(0x0a, 0, 0, 0); /* Set current position to 0 */
523 gus_write8(0x00, 0x03); /* Voice off */
524 gus_write8(0x0d, 0x03); /* Ramping off */
525 voice_alloc
->map
[voice
] = 0;
526 voice_alloc
->alloc_times
[voice
] = 0;
527 restore_flags(flags
);
531 static void gus_voice_init2(int voice
)
533 voices
[voice
].panning
= 0;
534 voices
[voice
].mode
= 0;
535 voices
[voice
].orig_freq
= 20000;
536 voices
[voice
].current_freq
= 20000;
537 voices
[voice
].bender
= 0;
538 voices
[voice
].bender_range
= 200;
539 voices
[voice
].initial_volume
= 0;
540 voices
[voice
].current_volume
= 0;
541 voices
[voice
].loop_irq_mode
= 0;
542 voices
[voice
].loop_irq_parm
= 0;
543 voices
[voice
].volume_irq_mode
= 0;
544 voices
[voice
].volume_irq_parm
= 0;
545 voices
[voice
].env_phase
= 0;
546 voices
[voice
].main_vol
= 127;
547 voices
[voice
].patch_vol
= 127;
548 voices
[voice
].expression_vol
= 127;
549 voices
[voice
].sample_pending
= -1;
550 voices
[voice
].fixed_pitch
= 0;
553 static void step_envelope(int voice
)
555 unsigned vol
, prev_vol
, phase
;
559 if (voices
[voice
].mode
& WAVE_SUSTAIN_ON
&& voices
[voice
].env_phase
== 2)
563 gus_select_voice(voice
);
565 restore_flags(flags
);
568 * Sustain phase begins. Continue envelope after receiving note off.
571 if (voices
[voice
].env_phase
>= 5)
573 /* Envelope finished. Shoot the voice down */
574 gus_voice_init(voice
);
577 prev_vol
= voices
[voice
].current_volume
;
578 phase
= ++voices
[voice
].env_phase
;
579 compute_volume(voice
, voices
[voice
].midi_volume
);
580 vol
= voices
[voice
].initial_volume
* voices
[voice
].env_offset
[phase
] / 255;
581 rate
= voices
[voice
].env_rate
[phase
];
585 gus_select_voice(voice
);
587 gus_voice_volume(prev_vol
);
590 gus_write8(0x06, rate
); /* Ramping rate */
592 voices
[voice
].volume_irq_mode
= VMODE_ENVELOPE
;
594 if (((vol
- prev_vol
) / 64) == 0) /* No significant volume change */
596 restore_flags(flags
);
597 step_envelope(voice
); /* Continue the envelope on the next step */
602 if (vol
>= (4096 - 64))
604 gus_ramp_range(0, vol
);
605 gus_rampon(0x20); /* Increasing volume, with IRQ */
611 gus_ramp_range(vol
, 4030);
612 gus_rampon(0x60); /* Decreasing volume, with IRQ */
614 voices
[voice
].current_volume
= vol
;
615 restore_flags(flags
);
618 static void init_envelope(int voice
)
620 voices
[voice
].env_phase
= -1;
621 voices
[voice
].current_volume
= 64;
623 step_envelope(voice
);
626 static void start_release(int voice
, long int flags
)
628 if (gus_read8(0x00) & 0x03)
629 return; /* Voice already stopped */
631 voices
[voice
].env_phase
= 2; /* Will be incremented by step_envelope */
633 voices
[voice
].current_volume
= voices
[voice
].initial_volume
=
634 gus_read16(0x09) >> 4; /* Get current volume */
636 voices
[voice
].mode
&= ~WAVE_SUSTAIN_ON
;
638 restore_flags(flags
);
639 step_envelope(voice
);
642 static void gus_voice_fade(int voice
)
644 int instr_no
= sample_map
[voice
], is16bits
;
649 gus_select_voice(voice
);
651 if (instr_no
< 0 || instr_no
> MAX_SAMPLE
)
653 gus_write8(0x00, 0x03); /* Hard stop */
654 voice_alloc
->map
[voice
] = 0;
655 restore_flags(flags
);
658 is16bits
= (samples
[instr_no
].mode
& WAVE_16_BITS
) ? 1 : 0; /* 8 or 16 bits */
660 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
662 start_release(voice
, flags
);
663 restore_flags(flags
);
667 * Ramp the volume down but not too quickly.
669 if ((int) (gus_read16(0x09) >> 4) < 100) /* Get current volume */
673 gus_voice_init(voice
);
674 restore_flags(flags
);
677 gus_ramp_range(65, 4030);
679 gus_rampon(0x40 | 0x20); /* Down, once, with IRQ */
680 voices
[voice
].volume_irq_mode
= VMODE_HALT
;
681 restore_flags(flags
);
684 static void gus_reset(void)
688 gus_select_max_voices(24);
691 volume_method
= VOL_METHOD_ADAGIO
;
693 for (i
= 0; i
< 32; i
++)
695 gus_voice_init(i
); /* Turn voice off */
700 static void gus_initialize(void)
703 unsigned char dma_image
, irq_image
, tmp
;
705 static unsigned char gus_irq_map
[16] = {
706 0, 0, 0, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7
709 static unsigned char gus_dma_map
[8] = {
710 0, 1, 0, 2, 0, 3, 4, 5
715 gus_write8(0x4c, 0); /* Reset GF1 */
719 gus_write8(0x4c, 1); /* Release Reset */
724 * Clear all interrupts
727 gus_write8(0x41, 0); /* DMA control */
728 gus_write8(0x45, 0); /* Timer control */
729 gus_write8(0x49, 0); /* Sample control */
731 gus_select_max_voices(24);
733 inb(u_Status
); /* Touch the status register */
735 gus_look8(0x41); /* Clear any pending DMA IRQs */
736 gus_look8(0x49); /* Clear any pending sample IRQs */
737 gus_read8(0x0f); /* Clear pending IRQs */
739 gus_reset(); /* Resets all voices */
741 gus_look8(0x41); /* Clear any pending DMA IRQs */
742 gus_look8(0x49); /* Clear any pending sample IRQs */
743 gus_read8(0x0f); /* Clear pending IRQs */
745 gus_write8(0x4c, 7); /* Master reset | DAC enable | IRQ enable */
748 * Set up for Digital ASIC
751 outb((0x05), gus_base
+ 0x0f);
753 mix_image
|= 0x02; /* Disable line out (for a moment) */
754 outb((mix_image
), u_Mixer
);
756 outb((0x00), u_IRQDMAControl
);
758 outb((0x00), gus_base
+ 0x0f);
761 * Now set up the DMA and IRQ interface
763 * The GUS supports two IRQs and two DMAs.
765 * Just one DMA channel is used. This prevents simultaneous ADC and DAC.
766 * Adding this support requires significant changes to the dmabuf.c, dsp.c
771 tmp
= gus_irq_map
[gus_irq
];
772 if (!gus_pnp_flag
&& !tmp
)
773 printk(KERN_WARNING
"Warning! GUS IRQ not selected\n");
775 irq_image
|= 0x40; /* Combine IRQ1 (GF1) and IRQ2 (Midi) */
778 if (gus_dma2
== gus_dma
|| gus_dma2
== -1)
781 dma_image
= 0x40; /* Combine DMA1 (DRAM) and IRQ2 (ADC) */
783 tmp
= gus_dma_map
[gus_dma
];
785 printk(KERN_WARNING
"Warning! GUS DMA not selected\n");
791 /* Setup dual DMA channel mode for GUS MAX */
793 dma_image
= gus_dma_map
[gus_dma
];
795 printk(KERN_WARNING
"Warning! GUS DMA not selected\n");
797 tmp
= gus_dma_map
[gus_dma2
] << 3;
800 printk(KERN_WARNING
"Warning! Invalid GUS MAX DMA\n");
801 tmp
= 0x40; /* Combine DMA channels */
808 * For some reason the IRQ and DMA addresses must be written twice
812 * Doing it first time
815 outb((mix_image
), u_Mixer
); /* Select DMA control */
816 outb((dma_image
| 0x80), u_IRQDMAControl
); /* Set DMA address */
818 outb((mix_image
| 0x40), u_Mixer
); /* Select IRQ control */
819 outb((irq_image
), u_IRQDMAControl
); /* Set IRQ address */
822 * Doing it second time
825 outb((mix_image
), u_Mixer
); /* Select DMA control */
826 outb((dma_image
), u_IRQDMAControl
); /* Set DMA address */
828 outb((mix_image
| 0x40), u_Mixer
); /* Select IRQ control */
829 outb((irq_image
), u_IRQDMAControl
); /* Set IRQ address */
831 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
833 mix_image
&= ~0x02; /* Enable line out */
834 mix_image
|= 0x08; /* Enable IRQ */
835 outb((mix_image
), u_Mixer
); /*
836 * Turn mixer channels on
837 * Note! Mic in is left off.
840 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
842 gusintr(gus_irq
, (void *)gus_hw_config
, NULL
); /* Serve pending interrupts */
844 inb(u_Status
); /* Touch the status register */
846 gus_look8(0x41); /* Clear any pending DMA IRQs */
847 gus_look8(0x49); /* Clear any pending sample IRQs */
849 gus_read8(0x0f); /* Clear pending IRQs */
852 gus_write8(0x19, gus_read8(0x19) | 0x01);
853 restore_flags(flags
);
857 static void pnp_mem_init(void)
860 #define CHUNK_SIZE (256*1024)
861 #define BANK_SIZE (4*1024*1024)
862 #define CHUNKS_PER_BANK (BANK_SIZE/CHUNK_SIZE)
864 int bank
, chunk
, addr
, total
= 0;
866 int i
, j
, bits
= -1, testbits
= -1, nbanks
= 0;
869 * This routine determines what kind of RAM is installed in each of the four
870 * SIMM banks and configures the DRAM address decode logic accordingly.
874 * Place the chip into enhanced mode
876 gus_write8(0x19, gus_read8(0x19) | 0x01);
877 gus_write8(0x53, gus_look8(0x53) & ~0x02); /* Select DRAM I/O access */
880 * Set memory configuration to 4 DRAM banks of 4M in each (16M total).
883 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | 0x000c);
886 * Perform the DRAM size detection for each bank individually.
888 for (bank
= 0; bank
< 4; bank
++)
892 addr
= bank
* BANK_SIZE
;
894 /* Clean check points of each chunk */
895 for (chunk
= 0; chunk
< CHUNKS_PER_BANK
; chunk
++)
897 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 0L, 0x00);
898 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 1L, 0x00);
901 /* Write a value to each chunk point and verify the result */
902 for (chunk
= 0; chunk
< CHUNKS_PER_BANK
; chunk
++)
904 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 0L, 0x55);
905 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 1L, 0xAA);
907 if (gus_peek(addr
+ chunk
* CHUNK_SIZE
+ 0L) == 0x55 &&
908 gus_peek(addr
+ chunk
* CHUNK_SIZE
+ 1L) == 0xAA)
910 /* OK. There is RAM. Now check for possible shadows */
913 for (chunk2
= 0; ok
&& chunk2
< chunk
; chunk2
++)
914 if (gus_peek(addr
+ chunk2
* CHUNK_SIZE
+ 0L) ||
915 gus_peek(addr
+ chunk2
* CHUNK_SIZE
+ 1L))
916 ok
= 0; /* Addressing wraps */
919 size
= (chunk
+ 1) * CHUNK_SIZE
;
921 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 0L, 0x00);
922 gus_poke(addr
+ chunk
* CHUNK_SIZE
+ 1L, 0x00);
924 bank_sizes
[bank
] = size
;
927 DDB(printk("Interwave: Bank %d, size=%dk\n", bank
, size
/ 1024));
930 if (nbanks
== 0) /* No RAM - Give up */
932 printk(KERN_ERR
"Sound: An Interwave audio chip detected but no DRAM\n");
933 printk(KERN_ERR
"Sound: Unable to work with this card.\n");
934 gus_write8(0x19, gus_read8(0x19) & ~0x01);
940 * Now we know how much DRAM there is in each bank. The next step is
941 * to find a DRAM size encoding (0 to 12) which is best for the combination
944 * First try if any of the possible alternatives matches exactly the amount
948 for (i
= 0; bits
== -1 && i
< 13; i
++)
952 for (j
= 0; bits
!= -1 && j
< 4; j
++)
953 if (mem_decode
[i
][j
] != bank_sizes
[j
])
954 bits
= -1; /* No hit */
958 * If necessary, try to find a combination where other than the last
959 * bank matches our configuration and the last bank is left oversized.
960 * In this way we don't leave holes in the middle of memory.
963 if (bits
== -1) /* No luck yet */
965 for (i
= 0; bits
== -1 && i
< 13; i
++)
969 for (j
= 0; bits
!= -1 && j
< nbanks
- 1; j
++)
970 if (mem_decode
[i
][j
] != bank_sizes
[j
])
971 bits
= -1; /* No hit */
972 if (mem_decode
[i
][nbanks
- 1] < bank_sizes
[nbanks
- 1])
973 bits
= -1; /* The last bank is too small */
977 * The last resort is to search for a combination where the banks are
978 * smaller than the actual SIMMs. This leaves some memory in the banks
979 * unused but doesn't leave holes in the DRAM address space.
981 if (bits
== -1) /* No luck yet */
983 for (i
= 0; i
< 13; i
++)
986 for (j
= 0; testbits
!= -1 && j
< nbanks
- 1; j
++)
987 if (mem_decode
[i
][j
] > bank_sizes
[j
]) {
990 if(testbits
> bits
) bits
= testbits
;
994 printk(KERN_INFO
"Interwave: Can't use all installed RAM.\n");
995 printk(KERN_INFO
"Interwave: Try reordering SIMMS.\n");
997 printk(KERN_INFO
"Interwave: Can't find working DRAM encoding.\n");
998 printk(KERN_INFO
"Interwave: Defaulting to 256k. Try reordering SIMMS.\n");
1001 DDB(printk("Interwave: Selecting DRAM addressing mode %d\n", bits
));
1003 for (bank
= 0; bank
< 4; bank
++)
1005 DDB(printk(" Bank %d, mem=%dk (limit %dk)\n", bank
, bank_sizes
[bank
] / 1024, mem_decode
[bits
][bank
] / 1024));
1007 if (bank_sizes
[bank
] > mem_decode
[bits
][bank
])
1008 total
+= mem_decode
[bits
][bank
];
1010 total
+= bank_sizes
[bank
];
1013 DDB(printk("Total %dk of DRAM (enhanced mode)\n", total
/ 1024));
1016 * Set the memory addressing mode.
1018 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | bits
);
1020 /* Leave the chip into enhanced mode. Disable LFO */
1021 gus_mem_size
= total
;
1023 gus_write8(0x19, (gus_read8(0x19) | 0x01) & ~0x02);
1026 int gus_wave_detect(int baseaddr
)
1028 unsigned long i
, max_mem
= 1024L;
1032 gus_base
= baseaddr
;
1034 gus_write8(0x4c, 0); /* Reset GF1 */
1038 gus_write8(0x4c, 1); /* Release Reset */
1042 #ifdef GUSPNP_AUTODETECT
1043 val
= gus_look8(0x5b); /* Version number register */
1044 gus_write8(0x5b, ~val
); /* Invert all bits */
1046 if ((gus_look8(0x5b) & 0xf0) == (val
& 0xf0)) /* No change */
1048 if ((gus_look8(0x5b) & 0x0f) == ((~val
) & 0x0f)) /* Change */
1050 DDB(printk("Interwave chip version %d detected\n", (val
& 0xf0) >> 4));
1055 DDB(printk("Not an Interwave chip (%x)\n", gus_look8(0x5b)));
1059 gus_write8(0x5b, val
); /* Restore all bits */
1067 /* See if there is first block there.... */
1069 if (gus_peek(0L) != 0xaa)
1072 /* Now zero it out so that I can check for mirroring .. */
1074 for (i
= 1L; i
< max_mem
; i
++)
1078 /* check for mirroring ... */
1079 if (gus_peek(0L) != 0)
1083 for (n
= loc
- 1, failed
= 0; n
<= loc
; n
++)
1085 gus_poke(loc
, 0xaa);
1086 if (gus_peek(loc
) != 0xaa)
1088 gus_poke(loc
, 0x55);
1089 if (gus_peek(loc
) != 0x55)
1095 gus_mem_size
= i
<< 10;
1099 static int guswave_ioctl(int dev
, unsigned int cmd
, caddr_t arg
)
1104 case SNDCTL_SYNTH_INFO
:
1105 gus_info
.nr_voices
= nr_voices
;
1106 if (copy_to_user(arg
, &gus_info
, sizeof(gus_info
)))
1110 case SNDCTL_SEQ_RESETSAMPLES
:
1111 reset_sample_memory();
1114 case SNDCTL_SEQ_PERCMODE
:
1117 case SNDCTL_SYNTH_MEMAVL
:
1118 return (gus_mem_size
== 0) ? 0 : gus_mem_size
- free_mem_ptr
- 32;
1125 static int guswave_set_instr(int dev
, int voice
, int instr_no
)
1129 if (instr_no
< 0 || instr_no
> MAX_PATCH
)
1130 instr_no
= 0; /* Default to acoustic piano */
1132 if (voice
< 0 || voice
> 31)
1135 if (voices
[voice
].volume_irq_mode
== VMODE_START_NOTE
)
1137 voices
[voice
].sample_pending
= instr_no
;
1140 sample_no
= patch_table
[instr_no
];
1141 patch_map
[voice
] = -1;
1143 if (sample_no
== NOT_SAMPLE
)
1145 /* printk("GUS: Undefined patch %d for voice %d\n", instr_no, voice);*/
1146 return -EINVAL
; /* Patch not defined */
1148 if (sample_ptrs
[sample_no
] == -1) /* Sample not loaded */
1150 /* printk("GUS: Sample #%d not loaded for patch %d (voice %d)\n", sample_no, instr_no, voice);*/
1153 sample_map
[voice
] = sample_no
;
1154 patch_map
[voice
] = instr_no
;
1158 static int guswave_kill_note(int dev
, int voice
, int note
, int velocity
)
1160 unsigned long flags
;
1164 /* voice_alloc->map[voice] = 0xffff; */
1165 if (voices
[voice
].volume_irq_mode
== VMODE_START_NOTE
)
1167 voices
[voice
].kill_pending
= 1;
1168 restore_flags(flags
);
1172 restore_flags(flags
);
1173 gus_voice_fade(voice
);
1179 static void guswave_aftertouch(int dev
, int voice
, int pressure
)
1183 static void guswave_panning(int dev
, int voice
, int value
)
1185 if (voice
>= 0 || voice
< 32)
1186 voices
[voice
].panning
= value
;
1189 static void guswave_volume_method(int dev
, int mode
)
1191 if (mode
== VOL_METHOD_LINEAR
|| mode
== VOL_METHOD_ADAGIO
)
1192 volume_method
= mode
;
1195 static void compute_volume(int voice
, int volume
)
1198 voices
[voice
].midi_volume
= volume
;
1200 switch (volume_method
)
1202 case VOL_METHOD_ADAGIO
:
1203 voices
[voice
].initial_volume
=
1204 gus_adagio_vol(voices
[voice
].midi_volume
, voices
[voice
].main_vol
,
1205 voices
[voice
].expression_vol
,
1206 voices
[voice
].patch_vol
);
1209 case VOL_METHOD_LINEAR
: /* Totally ignores patch-volume and expression */
1210 voices
[voice
].initial_volume
= gus_linear_vol(volume
, voices
[voice
].main_vol
);
1214 voices
[voice
].initial_volume
= volume_base
+
1215 (voices
[voice
].midi_volume
* volume_scale
);
1218 if (voices
[voice
].initial_volume
> 4030)
1219 voices
[voice
].initial_volume
= 4030;
1222 static void compute_and_set_volume(int voice
, int volume
, int ramp_time
)
1224 int curr
, target
, rate
;
1225 unsigned long flags
;
1227 compute_volume(voice
, volume
);
1228 voices
[voice
].current_volume
= voices
[voice
].initial_volume
;
1233 * CAUTION! Interrupts disabled. Enable them before returning
1236 gus_select_voice(voice
);
1238 curr
= gus_read16(0x09) >> 4;
1239 target
= voices
[voice
].initial_volume
;
1241 if (ramp_time
== INSTANT_RAMP
)
1244 gus_voice_volume(target
);
1245 restore_flags(flags
);
1248 if (ramp_time
== FAST_RAMP
)
1252 gus_ramp_rate(0, rate
);
1254 if ((target
- curr
) / 64 == 0) /* Close enough to target. */
1257 gus_voice_volume(target
);
1258 restore_flags(flags
);
1263 if (target
> (4095 - 65))
1265 gus_ramp_range(curr
, target
);
1266 gus_rampon(0x00); /* Ramp up, once, no IRQ */
1273 gus_ramp_range(target
, curr
);
1274 gus_rampon(0x40); /* Ramp down, once, no irq */
1276 restore_flags(flags
);
1279 static void dynamic_volume_change(int voice
)
1281 unsigned char status
;
1282 unsigned long flags
;
1286 gus_select_voice(voice
);
1287 status
= gus_read8(0x00); /* Get voice status */
1288 restore_flags(flags
);
1291 return; /* Voice was not running */
1293 if (!(voices
[voice
].mode
& WAVE_ENVELOPES
))
1295 compute_and_set_volume(voice
, voices
[voice
].midi_volume
, 1);
1300 * Voice is running and has envelopes.
1305 gus_select_voice(voice
);
1306 status
= gus_read8(0x0d); /* Ramping status */
1307 restore_flags(flags
);
1309 if (status
& 0x03) /* Sustain phase? */
1311 compute_and_set_volume(voice
, voices
[voice
].midi_volume
, 1);
1314 if (voices
[voice
].env_phase
< 0)
1317 compute_volume(voice
, voices
[voice
].midi_volume
);
1321 static void guswave_controller(int dev
, int voice
, int ctrl_num
, int value
)
1323 unsigned long flags
;
1326 if (voice
< 0 || voice
> 31)
1331 case CTRL_PITCH_BENDER
:
1332 voices
[voice
].bender
= value
;
1334 if (voices
[voice
].volume_irq_mode
!= VMODE_START_NOTE
)
1336 freq
= compute_finetune(voices
[voice
].orig_freq
, value
, voices
[voice
].bender_range
, 0);
1337 voices
[voice
].current_freq
= freq
;
1341 gus_select_voice(voice
);
1342 gus_voice_freq(freq
);
1343 restore_flags(flags
);
1347 case CTRL_PITCH_BENDER_RANGE
:
1348 voices
[voice
].bender_range
= value
;
1350 case CTL_EXPRESSION
:
1352 case CTRL_EXPRESSION
:
1353 if (volume_method
== VOL_METHOD_ADAGIO
)
1355 voices
[voice
].expression_vol
= value
;
1356 if (voices
[voice
].volume_irq_mode
!= VMODE_START_NOTE
)
1357 dynamic_volume_change(voice
);
1362 voices
[voice
].panning
= (value
* 2) - 128;
1365 case CTL_MAIN_VOLUME
:
1366 value
= (value
* 100) / 16383;
1368 case CTRL_MAIN_VOLUME
:
1369 voices
[voice
].main_vol
= value
;
1370 if (voices
[voice
].volume_irq_mode
!= VMODE_START_NOTE
)
1371 dynamic_volume_change(voice
);
1379 static int guswave_start_note2(int dev
, int voice
, int note_num
, int volume
)
1381 int sample
, best_sample
, best_delta
, delta_freq
;
1382 int is16bits
, samplep
, patch
, pan
;
1383 unsigned long note_freq
, base_note
, freq
, flags
;
1384 unsigned char mode
= 0;
1386 if (voice
< 0 || voice
> 31)
1388 /* printk("GUS: Invalid voice\n");*/
1391 if (note_num
== 255)
1393 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
1395 voices
[voice
].midi_volume
= volume
;
1396 dynamic_volume_change(voice
);
1399 compute_and_set_volume(voice
, volume
, 1);
1402 if ((patch
= patch_map
[voice
]) == -1)
1404 if ((samplep
= patch_table
[patch
]) == NOT_SAMPLE
)
1408 note_freq
= note_to_freq(note_num
);
1411 * Find a sample within a patch so that the note_freq is between low_note
1416 best_sample
= samplep
;
1417 best_delta
= 1000000;
1418 while (samplep
!= 0 && samplep
!= NOT_SAMPLE
&& sample
== -1)
1420 delta_freq
= note_freq
- samples
[samplep
].base_note
;
1422 delta_freq
= -delta_freq
;
1423 if (delta_freq
< best_delta
)
1425 best_sample
= samplep
;
1426 best_delta
= delta_freq
;
1428 if (samples
[samplep
].low_note
<= note_freq
&&
1429 note_freq
<= samples
[samplep
].high_note
)
1434 samplep
= samples
[samplep
].key
; /* Link to next sample */
1437 sample
= best_sample
;
1441 /* printk("GUS: Patch %d not defined for note %d\n", patch, note_num);*/
1442 return 0; /* Should play default patch ??? */
1444 is16bits
= (samples
[sample
].mode
& WAVE_16_BITS
) ? 1 : 0;
1445 voices
[voice
].mode
= samples
[sample
].mode
;
1446 voices
[voice
].patch_vol
= samples
[sample
].volume
;
1449 gus_write8(0x15, 0x00); /* RAM, Reset voice deactivate bit of SMSI */
1451 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
1455 for (i
= 0; i
< 6; i
++)
1457 voices
[voice
].env_rate
[i
] = samples
[sample
].env_rate
[i
];
1458 voices
[voice
].env_offset
[i
] = samples
[sample
].env_offset
[i
];
1461 sample_map
[voice
] = sample
;
1463 if (voices
[voice
].fixed_pitch
) /* Fixed pitch */
1465 freq
= samples
[sample
].base_freq
;
1469 base_note
= samples
[sample
].base_note
/ 100;
1472 freq
= samples
[sample
].base_freq
* note_freq
/ base_note
;
1475 voices
[voice
].orig_freq
= freq
;
1478 * Since the pitch bender may have been set before playing the note, we
1479 * have to calculate the bending now.
1482 freq
= compute_finetune(voices
[voice
].orig_freq
, voices
[voice
].bender
,
1483 voices
[voice
].bender_range
, 0);
1484 voices
[voice
].current_freq
= freq
;
1486 pan
= (samples
[sample
].panning
+ voices
[voice
].panning
) / 32;
1493 if (samples
[sample
].mode
& WAVE_16_BITS
)
1495 mode
|= 0x04; /* 16 bits */
1496 if ((sample_ptrs
[sample
] / GUS_BANK_SIZE
) !=
1497 ((sample_ptrs
[sample
] + samples
[sample
].len
) / GUS_BANK_SIZE
))
1498 printk(KERN_ERR
"GUS: Sample address error\n");
1500 /*************************************************************************
1501 * CAUTION! Interrupts disabled. Don't return before enabling
1502 *************************************************************************/
1506 gus_select_voice(voice
);
1510 restore_flags(flags
);
1512 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
1514 compute_volume(voice
, volume
);
1515 init_envelope(voice
);
1519 compute_and_set_volume(voice
, volume
, 0);
1524 gus_select_voice(voice
);
1526 if (samples
[sample
].mode
& WAVE_LOOP_BACK
)
1527 gus_write_addr(0x0a, sample_ptrs
[sample
] + samples
[sample
].len
-
1528 voices
[voice
].offset_pending
, 0, is16bits
); /* start=end */
1530 gus_write_addr(0x0a, sample_ptrs
[sample
] + voices
[voice
].offset_pending
, 0, is16bits
); /* Sample start=begin */
1532 if (samples
[sample
].mode
& WAVE_LOOPING
)
1536 if (samples
[sample
].mode
& WAVE_BIDIR_LOOP
)
1539 if (samples
[sample
].mode
& WAVE_LOOP_BACK
)
1541 gus_write_addr(0x0a, sample_ptrs
[sample
] + samples
[sample
].loop_end
-
1542 voices
[voice
].offset_pending
,
1543 (samples
[sample
].fractions
>> 4) & 0x0f, is16bits
);
1546 gus_write_addr(0x02, sample_ptrs
[sample
] + samples
[sample
].loop_start
,
1547 samples
[sample
].fractions
& 0x0f, is16bits
); /* Loop start location */
1548 gus_write_addr(0x04, sample_ptrs
[sample
] + samples
[sample
].loop_end
,
1549 (samples
[sample
].fractions
>> 4) & 0x0f, is16bits
); /* Loop end location */
1553 mode
|= 0x20; /* Loop IRQ at the end */
1554 voices
[voice
].loop_irq_mode
= LMODE_FINISH
; /* Ramp down at the end */
1555 voices
[voice
].loop_irq_parm
= 1;
1556 gus_write_addr(0x02, sample_ptrs
[sample
], 0, is16bits
); /* Loop start location */
1557 gus_write_addr(0x04, sample_ptrs
[sample
] + samples
[sample
].len
- 1,
1558 (samples
[sample
].fractions
>> 4) & 0x0f, is16bits
); /* Loop end location */
1560 gus_voice_freq(freq
);
1561 gus_voice_balance(pan
);
1563 restore_flags(flags
);
1569 * New guswave_start_note by Andrew J. Robinson attempts to minimize clicking
1570 * when the note playing on the voice is changed. It uses volume
1574 static int guswave_start_note(int dev
, int voice
, int note_num
, int volume
)
1582 if (note_num
== 255)
1584 if (voices
[voice
].volume_irq_mode
== VMODE_START_NOTE
)
1586 voices
[voice
].volume_pending
= volume
;
1590 ret_val
= guswave_start_note2(dev
, voice
, note_num
, volume
);
1595 gus_select_voice(voice
);
1596 mode
= gus_read8(0x00);
1598 gus_write8(0x00, mode
& 0xdf); /* No interrupt! */
1600 voices
[voice
].offset_pending
= 0;
1601 voices
[voice
].kill_pending
= 0;
1602 voices
[voice
].volume_irq_mode
= 0;
1603 voices
[voice
].loop_irq_mode
= 0;
1605 if (voices
[voice
].sample_pending
>= 0)
1607 restore_flags(flags
); /* Run temporarily with interrupts enabled */
1608 guswave_set_instr(voices
[voice
].dev_pending
, voice
, voices
[voice
].sample_pending
);
1609 voices
[voice
].sample_pending
= -1;
1612 gus_select_voice(voice
); /* Reselect the voice (just to be sure) */
1614 if ((mode
& 0x01) || (int) ((gus_read16(0x09) >> 4) < (unsigned) 2065))
1616 ret_val
= guswave_start_note2(dev
, voice
, note_num
, volume
);
1620 voices
[voice
].dev_pending
= dev
;
1621 voices
[voice
].note_pending
= note_num
;
1622 voices
[voice
].volume_pending
= volume
;
1623 voices
[voice
].volume_irq_mode
= VMODE_START_NOTE
;
1626 gus_ramp_range(2000, 4065);
1627 gus_ramp_rate(0, 63); /* Fastest possible rate */
1628 gus_rampon(0x20 | 0x40); /* Ramp down, once, irq */
1631 restore_flags(flags
);
1635 static void guswave_reset(int dev
)
1639 for (i
= 0; i
< 32; i
++)
1646 static int guswave_open(int dev
, int mode
)
1653 voice_alloc
->timestamp
= 0;
1655 if (gus_no_wave_dma
) {
1658 if ((err
= DMAbuf_open_dma(gus_devnum
)) < 0)
1660 /* printk( "GUS: Loading samples without DMA\n"); */
1661 gus_no_dma
= 1; /* Upload samples using PIO */
1667 init_waitqueue(&dram_sleeper
);
1669 active_device
= GUS_DEV_WAVE
;
1671 gusintr(gus_irq
, (void *)gus_hw_config
, NULL
); /* Serve pending interrupts */
1674 gusintr(gus_irq
, (void *)gus_hw_config
, NULL
); /* Serve pending interrupts */
1679 static void guswave_close(int dev
)
1686 DMAbuf_close_dma(gus_devnum
);
1689 static int guswave_load_patch(int dev
, int format
, const char *addr
,
1690 int offs
, int count
, int pmgr_flag
)
1692 struct patch_info patch
;
1696 unsigned long blk_sz
, blk_end
, left
, src_offs
, target
;
1698 sizeof_patch
= (long) &patch
.data
[0] - (long) &patch
; /* Header size */
1700 if (format
!= GUS_PATCH
)
1702 /* printk("GUS Error: Invalid patch format (key) 0x%x\n", format);*/
1705 if (count
< sizeof_patch
)
1707 /* printk("GUS Error: Patch header too short\n");*/
1710 count
-= sizeof_patch
;
1712 if (free_sample
>= MAX_SAMPLE
)
1714 /* printk("GUS: Sample table full\n");*/
1718 * Copy the header from user space but ignore the first bytes which have
1719 * been transferred already.
1722 copy_from_user(&((char *) &patch
)[offs
], &(addr
)[offs
], sizeof_patch
- offs
);
1724 if (patch
.mode
& WAVE_ROM
)
1726 if (gus_mem_size
== 0)
1729 instr
= patch
.instr_no
;
1731 if (instr
< 0 || instr
> MAX_PATCH
)
1733 /* printk(KERN_ERR "GUS: Invalid patch number %d\n", instr);*/
1736 if (count
< patch
.len
)
1738 /* printk(KERN_ERR "GUS Warning: Patch record too short (%d<%d)\n", count, (int) patch.len);*/
1741 if (patch
.len
<= 0 || patch
.len
> gus_mem_size
)
1743 /* printk(KERN_ERR "GUS: Invalid sample length %d\n", (int) patch.len);*/
1746 if (patch
.mode
& WAVE_LOOPING
)
1748 if (patch
.loop_start
< 0 || patch
.loop_start
>= patch
.len
)
1750 /* printk(KERN_ERR "GUS: Invalid loop start\n");*/
1753 if (patch
.loop_end
< patch
.loop_start
|| patch
.loop_end
> patch
.len
)
1755 /* printk(KERN_ERR "GUS: Invalid loop end\n");*/
1759 free_mem_ptr
= (free_mem_ptr
+ 31) & ~31; /* 32 byte alignment */
1761 if (patch
.mode
& WAVE_16_BITS
)
1764 * 16 bit samples must fit one 256k bank.
1766 if (patch
.len
>= GUS_BANK_SIZE
)
1768 /* printk("GUS: Sample (16 bit) too long %d\n", (int) patch.len);*/
1771 if ((free_mem_ptr
/ GUS_BANK_SIZE
) !=
1772 ((free_mem_ptr
+ patch
.len
) / GUS_BANK_SIZE
))
1774 unsigned long tmp_mem
=
1776 ((free_mem_ptr
/ GUS_BANK_SIZE
) + 1) * GUS_BANK_SIZE
;
1778 if ((tmp_mem
+ patch
.len
) > gus_mem_size
)
1781 free_mem_ptr
= tmp_mem
; /* This leaves unusable memory */
1784 if ((free_mem_ptr
+ patch
.len
) > gus_mem_size
)
1787 sample_ptrs
[free_sample
] = free_mem_ptr
;
1790 * Tremolo is not possible with envelopes
1793 if (patch
.mode
& WAVE_ENVELOPES
)
1794 patch
.mode
&= ~WAVE_TREMOLO
;
1796 if (!(patch
.mode
& WAVE_FRACTIONS
))
1798 patch
.fractions
= 0;
1800 memcpy((char *) &samples
[free_sample
], &patch
, sizeof_patch
);
1803 * Link this_one sample to the list of samples for patch 'instr'.
1806 samples
[free_sample
].key
= patch_table
[instr
];
1807 patch_table
[instr
] = free_sample
;
1810 * Use DMA to transfer the wave data to the DRAM
1815 target
= free_mem_ptr
;
1817 while (left
) /* Not completely transferred yet */
1819 blk_sz
= audio_devs
[gus_devnum
]->dmap_out
->bytes_in_use
;
1824 * DMA cannot cross bank (256k) boundaries. Check for that.
1827 blk_end
= target
+ blk_sz
;
1829 if ((target
/ GUS_BANK_SIZE
) != (blk_end
/ GUS_BANK_SIZE
))
1831 /* Split the block */
1832 blk_end
&= ~(GUS_BANK_SIZE
- 1);
1833 blk_sz
= blk_end
- target
;
1838 * For some reason the DMA is not possible. We have to use PIO.
1843 for (i
= 0; i
< blk_sz
; i
++)
1845 get_user(*(unsigned char *) &data
, (unsigned char *) &((addr
)[sizeof_patch
+ i
]));
1846 if (patch
.mode
& WAVE_UNSIGNED
)
1847 if (!(patch
.mode
& WAVE_16_BITS
) || (i
& 0x01))
1848 data
^= 0x80; /* Convert to signed */
1849 gus_poke(target
+ i
, data
);
1854 unsigned long address
, hold_address
;
1855 unsigned char dma_command
;
1856 unsigned long flags
;
1858 if (audio_devs
[gus_devnum
]->dmap_out
->raw_buf
== NULL
)
1860 printk(KERN_ERR
"GUS: DMA buffer == NULL\n");
1864 * OK, move now. First in and then out.
1867 copy_from_user(audio_devs
[gus_devnum
]->dmap_out
->raw_buf
, &(addr
)[sizeof_patch
+ src_offs
], blk_sz
);
1871 /******** INTERRUPTS DISABLED NOW ********/
1872 gus_write8(0x41, 0); /* Disable GF1 DMA */
1873 DMAbuf_start_dma(gus_devnum
, audio_devs
[gus_devnum
]->dmap_out
->raw_buf_phys
,
1874 blk_sz
, DMA_MODE_WRITE
);
1877 * Set the DRAM address for the wave data
1882 /* Different address translation in enhanced mode */
1887 address
= target
>> 1; /* Convert to 16 bit word address */
1891 hi
= (unsigned char) ((address
>> 16) & 0xf0);
1892 hi
+= (unsigned char) (address
& 0x0f);
1894 gus_write16(0x42, (address
>> 4) & 0xffff); /* DMA address (low) */
1895 gus_write8(0x50, hi
);
1900 if (audio_devs
[gus_devnum
]->dmap_out
->dma
> 3)
1902 hold_address
= address
;
1903 address
= address
>> 1;
1904 address
&= 0x0001ffffL
;
1905 address
|= (hold_address
& 0x000c0000L
);
1907 gus_write16(0x42, (address
>> 4) & 0xffff); /* DRAM DMA address */
1911 * Start the DMA transfer
1914 dma_command
= 0x21; /* IRQ enable, DMA start */
1915 if (patch
.mode
& WAVE_UNSIGNED
)
1916 dma_command
|= 0x80; /* Invert MSB */
1917 if (patch
.mode
& WAVE_16_BITS
)
1918 dma_command
|= 0x40; /* 16 bit _DATA_ */
1919 if (audio_devs
[gus_devnum
]->dmap_out
->dma
> 3)
1920 dma_command
|= 0x04; /* 16 bit DMA _channel_ */
1922 gus_write8(0x41, dma_command
); /* Lets go luteet (=bugs) */
1925 * Sleep here until the DRAM DMA done interrupt is served
1927 active_device
= GUS_DEV_WAVE
;
1929 if (!interruptible_sleep_on_timeout(&dram_sleeper
, HZ
))
1930 printk("GUS: DMA Transfer timed out\n");
1931 restore_flags(flags
);
1942 gus_write8(0x41, 0); /* Stop DMA */
1945 free_mem_ptr
+= patch
.len
;
1950 static void guswave_hw_control(int dev
, unsigned char *event_rec
)
1953 unsigned short p1
, p2
;
1958 voice
= event_rec
[3];
1959 p1
= *(unsigned short *) &event_rec
[4];
1960 p2
= *(unsigned short *) &event_rec
[6];
1961 plong
= *(unsigned int *) &event_rec
[4];
1963 if ((voices
[voice
].volume_irq_mode
== VMODE_START_NOTE
) &&
1964 (cmd
!= _GUS_VOICESAMPLE
) && (cmd
!= _GUS_VOICE_POS
))
1965 do_volume_irq(voice
);
1969 case _GUS_NUMVOICES
:
1972 gus_select_voice(voice
);
1973 gus_select_max_voices(p1
);
1974 restore_flags(flags
);
1977 case _GUS_VOICESAMPLE
:
1978 guswave_set_instr(dev
, voice
, p1
);
1984 gus_select_voice(voice
);
1985 p1
&= ~0x20; /* Don't allow interrupts */
1987 restore_flags(flags
);
1993 gus_select_voice(voice
);
1995 restore_flags(flags
);
1998 case _GUS_VOICEFADE
:
1999 gus_voice_fade(voice
);
2002 case _GUS_VOICEMODE
:
2005 gus_select_voice(voice
);
2006 p1
&= ~0x20; /* Don't allow interrupts */
2008 restore_flags(flags
);
2011 case _GUS_VOICEBALA
:
2014 gus_select_voice(voice
);
2015 gus_voice_balance(p1
);
2016 restore_flags(flags
);
2019 case _GUS_VOICEFREQ
:
2022 gus_select_voice(voice
);
2023 gus_voice_freq(plong
);
2024 restore_flags(flags
);
2030 gus_select_voice(voice
);
2031 gus_voice_volume(p1
);
2032 restore_flags(flags
);
2035 case _GUS_VOICEVOL2
: /* Just update the software voice level */
2036 voices
[voice
].initial_volume
= voices
[voice
].current_volume
= p1
;
2039 case _GUS_RAMPRANGE
:
2040 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
2044 gus_select_voice(voice
);
2045 gus_ramp_range(p1
, p2
);
2046 restore_flags(flags
);
2050 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
2051 break; /* NJET-NJET */
2054 gus_select_voice(voice
);
2055 gus_ramp_rate(p1
, p2
);
2056 restore_flags(flags
);
2060 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
2064 gus_select_voice(voice
);
2065 p1
&= ~0x20; /* Don't allow interrupts */
2067 restore_flags(flags
);
2071 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
2075 gus_select_voice(voice
);
2076 p1
&= ~0x20; /* Don't allow interrupts */
2078 restore_flags(flags
);
2082 if (voices
[voice
].mode
& WAVE_ENVELOPES
)
2083 break; /* NEJ-NEJ */
2086 gus_select_voice(voice
);
2088 restore_flags(flags
);
2091 case _GUS_VOLUME_SCALE
:
2096 case _GUS_VOICE_POS
:
2099 gus_select_voice(voice
);
2100 gus_set_voice_pos(voice
, plong
);
2101 restore_flags(flags
);
2108 static int gus_audio_set_speed(int speed
)
2111 speed
= gus_audio_speed
;
2119 gus_audio_speed
= speed
;
2121 if (only_read_access
)
2123 /* Compute nearest valid recording speed and return it */
2125 /* speed = (9878400 / (gus_audio_speed + 2)) / 16; */
2126 speed
= (((9878400 + gus_audio_speed
/ 2) / (gus_audio_speed
+ 2)) + 8) / 16;
2127 speed
= (9878400 / (speed
* 16)) - 2;
2132 static int gus_audio_set_channels(int channels
)
2135 return gus_audio_channels
;
2140 gus_audio_channels
= channels
;
2144 static int gus_audio_set_bits(int bits
)
2147 return gus_audio_bits
;
2149 if (bits
!= 8 && bits
!= 16)
2155 gus_audio_bits
= bits
;
2159 static int gus_audio_ioctl(int dev
, unsigned int cmd
, caddr_t arg
)
2165 case SOUND_PCM_WRITE_RATE
:
2166 if (get_user(val
, (int *)arg
))
2168 val
= gus_audio_set_speed(val
);
2171 case SOUND_PCM_READ_RATE
:
2172 val
= gus_audio_speed
;
2175 case SNDCTL_DSP_STEREO
:
2176 if (get_user(val
, (int *)arg
))
2178 val
= gus_audio_set_channels(val
+ 1) - 1;
2181 case SOUND_PCM_WRITE_CHANNELS
:
2182 if (get_user(val
, (int *)arg
))
2184 val
= gus_audio_set_channels(val
);
2187 case SOUND_PCM_READ_CHANNELS
:
2188 val
= gus_audio_channels
;
2191 case SNDCTL_DSP_SETFMT
:
2192 if (get_user(val
, (int *)arg
))
2194 val
= gus_audio_set_bits(val
);
2197 case SOUND_PCM_READ_BITS
:
2198 val
= gus_audio_bits
;
2201 case SOUND_PCM_WRITE_FILTER
: /* NOT POSSIBLE */
2202 case SOUND_PCM_READ_FILTER
:
2208 return put_user(val
, (int *)arg
);
2211 static void gus_audio_reset(int dev
)
2213 if (recording_active
)
2215 gus_write8(0x49, 0x00); /* Halt recording */
2216 set_input_volumes();
2220 static int saved_iw_mode
; /* A hack hack hack */
2222 static int gus_audio_open(int dev
, int mode
)
2227 if (gus_pnp_flag
&& mode
& OPEN_READ
)
2229 /* printk(KERN_ERR "GUS: Audio device #%d is playback only.\n", dev);*/
2238 reset_sample_memory();
2239 gus_select_max_voices(14);
2240 saved_iw_mode
= iw_mode
;
2243 /* There are some problems with audio in enhanced mode so disable it */
2244 gus_write8(0x19, gus_read8(0x19) & ~0x01); /* Disable enhanced mode */
2250 if (mode
& OPEN_READ
)
2252 recording_active
= 1;
2253 set_input_volumes();
2255 only_read_access
= !(mode
& OPEN_WRITE
);
2256 only_8_bits
= mode
& OPEN_READ
;
2258 audio_devs
[dev
]->format_mask
= AFMT_U8
;
2260 audio_devs
[dev
]->format_mask
= AFMT_U8
| AFMT_S16_LE
;
2265 static void gus_audio_close(int dev
)
2267 iw_mode
= saved_iw_mode
;
2273 if (recording_active
)
2275 gus_write8(0x49, 0x00); /* Halt recording */
2276 set_input_volumes();
2278 recording_active
= 0;
2281 static void gus_audio_update_volume(void)
2283 unsigned long flags
;
2286 if (pcm_active
&& pcm_opened
)
2287 for (voice
= 0; voice
< gus_audio_channels
; voice
++)
2291 gus_select_voice(voice
);
2293 gus_voice_volume(1530 + (25 * gus_pcm_volume
));
2294 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume
));
2295 restore_flags(flags
);
2299 static void play_next_pcm_block(void)
2301 unsigned long flags
;
2302 int speed
= gus_audio_speed
;
2303 int this_one
, is16bits
, chn
;
2304 unsigned long dram_loc
;
2305 unsigned char mode
[2], ramp_mode
[2];
2310 this_one
= pcm_head
;
2312 for (chn
= 0; chn
< gus_audio_channels
; chn
++)
2315 ramp_mode
[chn
] = 0x03; /* Ramping and rollover off */
2319 mode
[chn
] |= 0x20; /* Loop IRQ */
2320 voices
[chn
].loop_irq_mode
= LMODE_PCM
;
2322 if (gus_audio_bits
!= 8)
2325 mode
[chn
] |= 0x04; /* 16 bit data */
2330 dram_loc
= this_one
* pcm_bsize
;
2331 dram_loc
+= chn
* pcm_banksize
;
2333 if (this_one
== (pcm_nblk
- 1)) /* Last fragment of the DRAM buffer */
2335 mode
[chn
] |= 0x08; /* Enable loop */
2336 ramp_mode
[chn
] = 0x03; /* Disable rollover bit */
2341 ramp_mode
[chn
] = 0x04; /* Enable rollover bit */
2345 gus_select_voice(chn
);
2346 gus_voice_freq(speed
);
2348 if (gus_audio_channels
== 1)
2349 gus_voice_balance(7); /* mono */
2351 gus_voice_balance(0); /* left */
2353 gus_voice_balance(15); /* right */
2355 if (!pcm_active
) /* Playback not already active */
2358 * The playback was not started yet (or there has been a pause).
2359 * Start the voice (again) and ask for a rollover irq at the end of
2360 * this_one block. If this_one one is last of the buffers, use just
2361 * the normal loop with irq.
2366 gus_voice_volume(1530 + (25 * gus_pcm_volume
));
2367 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume
));
2369 gus_write_addr(0x0a, chn
* pcm_banksize
, 0, is16bits
); /* Starting position */
2370 gus_write_addr(0x02, chn
* pcm_banksize
, 0, is16bits
); /* Loop start */
2373 gus_write_addr(0x04, pcm_banksize
+ (pcm_bsize
* pcm_nblk
) - 1,
2374 0, is16bits
); /* Loop end location */
2377 gus_write_addr(0x04, dram_loc
+ pcm_bsize
- 1,
2378 0, is16bits
); /* Loop end location */
2380 mode
[chn
] |= 0x08; /* Enable looping */
2381 restore_flags(flags
);
2383 for (chn
= 0; chn
< gus_audio_channels
; chn
++)
2387 gus_select_voice(chn
);
2388 gus_write8(0x0d, ramp_mode
[chn
]);
2390 gus_write8(0x15, 0x00); /* Reset voice deactivate bit of SMSI */
2391 gus_voice_on(mode
[chn
]);
2392 restore_flags(flags
);
2397 static void gus_transfer_output_block(int dev
, unsigned long buf
,
2398 int total_count
, int intrflag
, int chn
)
2401 * This routine transfers one block of audio data to the DRAM. In mono mode
2402 * it's called just once. When in stereo mode, this_one routine is called
2403 * once for both channels.
2405 * The left/mono channel data is transferred to the beginning of dram and the
2406 * right data to the area pointed by gus_page_size.
2409 int this_one
, count
;
2410 unsigned long flags
;
2411 unsigned char dma_command
;
2412 unsigned long address
, hold_address
;
2417 count
= total_count
/ gus_audio_channels
;
2421 if (pcm_qlen
>= pcm_nblk
)
2422 printk(KERN_WARNING
"GUS Warning: PCM buffers out of sync\n");
2424 this_one
= pcm_current_block
= pcm_tail
;
2426 pcm_tail
= (pcm_tail
+ 1) % pcm_nblk
;
2427 pcm_datasize
[this_one
] = count
;
2430 this_one
= pcm_current_block
;
2432 gus_write8(0x41, 0); /* Disable GF1 DMA */
2433 DMAbuf_start_dma(dev
, buf
+ (chn
* count
), count
, DMA_MODE_WRITE
);
2435 address
= this_one
* pcm_bsize
;
2436 address
+= chn
* pcm_banksize
;
2438 if (audio_devs
[dev
]->dmap_out
->dma
> 3)
2440 hold_address
= address
;
2441 address
= address
>> 1;
2442 address
&= 0x0001ffffL
;
2443 address
|= (hold_address
& 0x000c0000L
);
2445 gus_write16(0x42, (address
>> 4) & 0xffff); /* DRAM DMA address */
2447 dma_command
= 0x21; /* IRQ enable, DMA start */
2449 if (gus_audio_bits
!= 8)
2450 dma_command
|= 0x40; /* 16 bit _DATA_ */
2452 dma_command
|= 0x80; /* Invert MSB */
2454 if (audio_devs
[dev
]->dmap_out
->dma
> 3)
2455 dma_command
|= 0x04; /* 16 bit DMA channel */
2457 gus_write8(0x41, dma_command
); /* Kick start */
2459 if (chn
== (gus_audio_channels
- 1)) /* Last channel */
2462 * Last (right or mono) channel data
2464 dma_active
= 1; /* DMA started. There is a unacknowledged buffer */
2465 active_device
= GUS_DEV_PCM_DONE
;
2466 if (!pcm_active
&& (pcm_qlen
> 1 || count
< pcm_bsize
))
2468 play_next_pcm_block();
2474 * Left channel data. The right channel
2475 * is transferred after DMA interrupt
2477 active_device
= GUS_DEV_PCM_CONTINUE
;
2480 restore_flags(flags
);
2483 static void gus_uninterleave8(char *buf
, int l
)
2485 /* This routine uninterleaves 8 bit stereo output (LRLRLR->LLLRRR) */
2486 int i
, p
= 0, halfsize
= l
/ 2;
2487 char *buf2
= buf
+ halfsize
, *src
= bounce_buf
;
2489 memcpy(bounce_buf
, buf
, l
);
2491 for (i
= 0; i
< halfsize
; i
++)
2493 buf
[i
] = src
[p
++]; /* Left channel */
2494 buf2
[i
] = src
[p
++]; /* Right channel */
2498 static void gus_uninterleave16(short *buf
, int l
)
2500 /* This routine uninterleaves 16 bit stereo output (LRLRLR->LLLRRR) */
2501 int i
, p
= 0, halfsize
= l
/ 2;
2502 short *buf2
= buf
+ halfsize
, *src
= (short *) bounce_buf
;
2504 memcpy(bounce_buf
, (char *) buf
, l
* 2);
2506 for (i
= 0; i
< halfsize
; i
++)
2508 buf
[i
] = src
[p
++]; /* Left channel */
2509 buf2
[i
] = src
[p
++]; /* Right channel */
2513 static void gus_audio_output_block(int dev
, unsigned long buf
, int total_count
,
2516 struct dma_buffparms
*dmap
= audio_devs
[dev
]->dmap_out
;
2518 dmap
->flags
|= DMA_NODMA
| DMA_NOTIMEOUT
;
2520 pcm_current_buf
= buf
;
2521 pcm_current_count
= total_count
;
2522 pcm_current_intrflag
= intrflag
;
2523 pcm_current_dev
= dev
;
2524 if (gus_audio_channels
== 2)
2526 char *b
= dmap
->raw_buf
+ (buf
- dmap
->raw_buf_phys
);
2528 if (gus_audio_bits
== 8)
2529 gus_uninterleave8(b
, total_count
);
2531 gus_uninterleave16((short *) b
, total_count
/ 2);
2533 gus_transfer_output_block(dev
, buf
, total_count
, intrflag
, 0);
2536 static void gus_audio_start_input(int dev
, unsigned long buf
, int count
,
2539 unsigned long flags
;
2545 DMAbuf_start_dma(dev
, buf
, count
, DMA_MODE_READ
);
2546 mode
= 0xa0; /* DMA IRQ enabled, invert MSB */
2548 if (audio_devs
[dev
]->dmap_in
->dma
> 3)
2549 mode
|= 0x04; /* 16 bit DMA channel */
2550 if (gus_audio_channels
> 1)
2551 mode
|= 0x02; /* Stereo */
2552 mode
|= 0x01; /* DMA enable */
2554 gus_write8(0x49, mode
);
2555 restore_flags(flags
);
2558 static int gus_audio_prepare_for_input(int dev
, int bsize
, int bcount
)
2562 gus_audio_bsize
= bsize
;
2563 audio_devs
[dev
]->dmap_in
->flags
|= DMA_NODMA
;
2564 rate
= (((9878400 + gus_audio_speed
/ 2) / (gus_audio_speed
+ 2)) + 8) / 16;
2566 gus_write8(0x48, rate
& 0xff); /* Set sampling rate */
2568 if (gus_audio_bits
!= 8)
2570 /* printk("GUS Error: 16 bit recording not supported\n");*/
2576 static int gus_audio_prepare_for_output(int dev
, int bsize
, int bcount
)
2580 long mem_ptr
, mem_size
;
2582 audio_devs
[dev
]->dmap_out
->flags
|= DMA_NODMA
| DMA_NOTIMEOUT
;
2584 mem_size
= gus_mem_size
/ gus_audio_channels
;
2586 if (mem_size
> (256 * 1024))
2587 mem_size
= 256 * 1024;
2589 pcm_bsize
= bsize
/ gus_audio_channels
;
2590 pcm_head
= pcm_tail
= pcm_qlen
= 0;
2592 pcm_nblk
= 2; /* MAX_PCM_BUFFERS; */
2593 if ((pcm_bsize
* pcm_nblk
) > mem_size
)
2594 pcm_nblk
= mem_size
/ pcm_bsize
;
2596 for (i
= 0; i
< pcm_nblk
; i
++)
2597 pcm_datasize
[i
] = 0;
2599 pcm_banksize
= pcm_nblk
* pcm_bsize
;
2601 if (gus_audio_bits
!= 8 && pcm_banksize
== (256 * 1024))
2603 gus_write8(0x41, 0); /* Disable GF1 DMA */
2607 static int gus_local_qlen(int dev
)
2613 static struct audio_driver gus_audio_driver
=
2617 gus_audio_output_block
,
2618 gus_audio_start_input
,
2620 gus_audio_prepare_for_input
,
2621 gus_audio_prepare_for_output
,
2627 static void guswave_setup_voice(int dev
, int voice
, int chn
)
2629 struct channel_info
*info
= &synth_devs
[dev
]->chn_info
[chn
];
2631 guswave_set_instr(dev
, voice
, info
->pgm_num
);
2632 voices
[voice
].expression_vol
= info
->controllers
[CTL_EXPRESSION
]; /* Just MSB */
2633 voices
[voice
].main_vol
= (info
->controllers
[CTL_MAIN_VOLUME
] * 100) / (unsigned) 128;
2634 voices
[voice
].panning
= (info
->controllers
[CTL_PAN
] * 2) - 128;
2635 voices
[voice
].bender
= 0;
2636 voices
[voice
].bender_range
= info
->bender_range
;
2639 voices
[voice
].fixed_pitch
= 1;
2642 static void guswave_bender(int dev
, int voice
, int value
)
2645 unsigned long flags
;
2647 voices
[voice
].bender
= value
- 8192;
2648 freq
= compute_finetune(voices
[voice
].orig_freq
, value
- 8192, voices
[voice
].bender_range
, 0);
2649 voices
[voice
].current_freq
= freq
;
2653 gus_select_voice(voice
);
2654 gus_voice_freq(freq
);
2655 restore_flags(flags
);
2658 static int guswave_alloc(int dev
, int chn
, int note
, struct voice_alloc_info
*alloc
)
2660 int i
, p
, best
= -1, best_time
= 0x7fffffff;
2664 * First look for a completely stopped voice
2667 for (i
= 0; i
< alloc
->max_voice
; i
++)
2669 if (alloc
->map
[p
] == 0)
2674 if (alloc
->alloc_times
[p
] < best_time
)
2677 best_time
= alloc
->alloc_times
[p
];
2679 p
= (p
+ 1) % alloc
->max_voice
;
2683 * Then look for a releasing voice
2686 for (i
= 0; i
< alloc
->max_voice
; i
++)
2688 if (alloc
->map
[p
] == 0xffff)
2693 p
= (p
+ 1) % alloc
->max_voice
;
2702 static struct synth_operations guswave_operations
=
2721 guswave_volume_method
,
2727 static void set_input_volumes(void)
2729 unsigned long flags
;
2730 unsigned char mask
= 0xff & ~0x06; /* Just line out enabled */
2732 if (have_gus_max
) /* Don't disturb GUS MAX */
2739 * Enable channels having vol > 10%
2740 * Note! bit 0x01 means the line in DISABLED while 0x04 means
2741 * the mic in ENABLED.
2743 if (gus_line_vol
> 10)
2745 if (gus_mic_vol
> 10)
2748 if (recording_active
)
2751 * Disable channel, if not selected for recording
2753 if (!(gus_recmask
& SOUND_MASK_LINE
))
2755 if (!(gus_recmask
& SOUND_MASK_MIC
))
2759 mix_image
|= mask
& 0x07;
2760 outb((mix_image
), u_Mixer
);
2762 restore_flags(flags
);
2765 #define MIX_DEVS (SOUND_MASK_MIC|SOUND_MASK_LINE| \
2766 SOUND_MASK_SYNTH|SOUND_MASK_PCM)
2768 int gus_default_mixer_ioctl(int dev
, unsigned int cmd
, caddr_t arg
)
2772 if (((cmd
>> 8) & 0xff) != 'M')
2775 if (!access_ok(VERIFY_WRITE
, (int *)arg
, sizeof(int)))
2778 if (_SIOC_DIR(cmd
) & _SIOC_WRITE
)
2780 if (__get_user(val
, (int *) arg
))
2785 case SOUND_MIXER_RECSRC
:
2786 gus_recmask
= val
& MIX_DEVS
;
2787 if (!(gus_recmask
& (SOUND_MASK_MIC
| SOUND_MASK_LINE
)))
2788 gus_recmask
= SOUND_MASK_MIC
;
2789 /* Note! Input volumes are updated during next open for recording */
2793 case SOUND_MIXER_MIC
:
2800 set_input_volumes();
2801 val
= vol
| (vol
<< 8);
2804 case SOUND_MIXER_LINE
:
2811 set_input_volumes();
2812 val
= vol
| (vol
<< 8);
2815 case SOUND_MIXER_PCM
:
2816 gus_pcm_volume
= val
& 0xff;
2817 if (gus_pcm_volume
< 0)
2819 if (gus_pcm_volume
> 100)
2820 gus_pcm_volume
= 100;
2821 gus_audio_update_volume();
2822 val
= gus_pcm_volume
| (gus_pcm_volume
<< 8);
2825 case SOUND_MIXER_SYNTH
:
2826 gus_wave_volume
= val
& 0xff;
2827 if (gus_wave_volume
< 0)
2828 gus_wave_volume
= 0;
2829 if (gus_wave_volume
> 100)
2830 gus_wave_volume
= 100;
2831 if (active_device
== GUS_DEV_WAVE
)
2834 for (voice
= 0; voice
< nr_voices
; voice
++)
2835 dynamic_volume_change(voice
); /* Apply the new vol */
2837 val
= gus_wave_volume
| (gus_wave_volume
<< 8);
2851 case SOUND_MIXER_RECSRC
:
2855 case SOUND_MIXER_DEVMASK
:
2859 case SOUND_MIXER_STEREODEVS
:
2863 case SOUND_MIXER_RECMASK
:
2864 val
= SOUND_MASK_MIC
| SOUND_MASK_LINE
;
2867 case SOUND_MIXER_CAPS
:
2871 case SOUND_MIXER_MIC
:
2872 val
= gus_mic_vol
| (gus_mic_vol
<< 8);
2875 case SOUND_MIXER_LINE
:
2876 val
= gus_line_vol
| (gus_line_vol
<< 8);
2879 case SOUND_MIXER_PCM
:
2880 val
= gus_pcm_volume
| (gus_pcm_volume
<< 8);
2883 case SOUND_MIXER_SYNTH
:
2884 val
= gus_wave_volume
| (gus_wave_volume
<< 8);
2891 return __put_user(val
, (int *)arg
);
2894 static struct mixer_operations gus_mixer_operations
=
2897 "Gravis Ultrasound",
2898 gus_default_mixer_ioctl
2901 static int gus_default_mixer_init(void)
2905 if ((n
= sound_alloc_mixerdev()) != -1)
2908 * Don't install if there is another
2911 mixer_devs
[n
] = &gus_mixer_operations
;
2916 * Enable all mixer channels on the GF1 side. Otherwise recording will
2917 * not be possible using GUS MAX.
2920 mix_image
|= 0x04; /* All channels enabled */
2921 outb((mix_image
), u_Mixer
);
2926 void gus_wave_init(struct address_info
*hw_config
)
2928 unsigned long flags
;
2930 char *model_num
= "2.4";
2931 char tmp
[64], tmp2
[64];
2932 int gus_type
= 0x24; /* 2.4 */
2934 int irq
= hw_config
->irq
, dma
= hw_config
->dma
, dma2
= hw_config
->dma2
;
2937 hw_config
->slots
[0] = -1; /* No wave */
2938 hw_config
->slots
[1] = -1; /* No ad1848 */
2939 hw_config
->slots
[4] = -1; /* No audio */
2940 hw_config
->slots
[5] = -1; /* No mixer */
2944 if (irq
< 0 || irq
> 15)
2946 printk(KERN_ERR
"ERROR! Invalid IRQ#%d. GUS Disabled", irq
);
2951 if (dma
< 0 || dma
> 7 || dma
== 4)
2953 printk(KERN_ERR
"ERROR! Invalid DMA#%d. GUS Disabled", dma
);
2959 gus_hw_config
= hw_config
;
2965 * Try to identify the GUS model.
2967 * Versions < 3.6 don't have the digital ASIC. Try to probe it first.
2972 outb((0x20), gus_base
+ 0x0f);
2973 val
= inb(gus_base
+ 0x0f);
2974 restore_flags(flags
);
2976 if (gus_pnp_flag
|| (val
!= 0xff && (val
& 0x06))) /* Should be 0x02?? */
2981 ad_flags
= 0x12345678; /* Interwave "magic" */
2983 * It has the digital ASIC so the card is at least v3.4.
2984 * Next try to detect the true model.
2987 if (gus_pnp_flag
) /* Hack hack hack */
2990 val
= inb(u_MixSelect
);
2993 * Value 255 means pre-3.7 which don't have mixer.
2994 * Values 5 thru 9 mean v3.7 which has a ICS2101 mixer.
2995 * 10 and above is GUS MAX which has the CS4231 codec/mixer.
2999 if (val
== 255 || val
< 5)
3008 mixer_type
= ICS2101
;
3009 request_region(u_MixSelect
, 1, "GUS mixer");
3015 mixer_type
= CS4231
;
3016 #ifdef CONFIG_GUSMAX
3018 unsigned char max_config
= 0x40; /* Codec enable */
3024 max_config
|= 0x10; /* 16 bit capture DMA */
3027 max_config
|= 0x20; /* 16 bit playback DMA */
3029 max_config
|= (gus_base
>> 4) & 0x0f; /* Extract the X from 2X0 */
3031 outb((max_config
), gus_base
+ 0x106); /* UltraMax control */
3034 if (ad1848_detect(gus_base
+ 0x10c, &ad_flags
, hw_config
->osp
))
3036 char *name
= "GUS MAX";
3037 int old_num_mixers
= num_mixers
;
3042 gus_mic_vol
= gus_line_vol
= gus_pcm_volume
= 100;
3043 gus_wave_volume
= 90;
3045 if (hw_config
->name
)
3046 name
= hw_config
->name
;
3048 hw_config
->slots
[1] = ad1848_init(name
, gus_base
+ 0x10c,
3049 -irq
, gus_dma2
, /* Playback DMA */
3050 gus_dma
, /* Capture DMA */
3051 1, /* Share DMA channels with GF1 */
3054 if (num_mixers
> old_num_mixers
)
3056 /* GUS has it's own mixer map */
3057 AD1848_REROUTE(SOUND_MIXER_LINE1
, SOUND_MIXER_SYNTH
);
3058 AD1848_REROUTE(SOUND_MIXER_LINE2
, SOUND_MIXER_CD
);
3059 AD1848_REROUTE(SOUND_MIXER_LINE3
, SOUND_MIXER_LINE
);
3063 printk(KERN_WARNING
"GUS: No CS4231 ??");
3065 printk(KERN_ERR
"GUS MAX found, but not compiled in\n");
3072 * ASIC not detected so the card must be 2.2 or 2.4.
3073 * There could still be the 16-bit/mixer daughter card.
3077 if (hw_config
->name
)
3079 strncpy(tmp
, hw_config
->name
, 45);
3081 sprintf(tmp2
, "%s (%dk)", tmp
, (int) gus_mem_size
/ 1024);
3082 tmp2
[sizeof(tmp2
) - 1] = 0;
3084 else if (gus_pnp_flag
)
3086 sprintf(tmp2
, "Gravis UltraSound PnP (%dk)",
3087 (int) gus_mem_size
/ 1024);
3090 sprintf(tmp2
, "Gravis UltraSound %s (%dk)", model_num
, (int) gus_mem_size
/ 1024);
3093 samples
= (struct patch_info
*)vmalloc((MAX_SAMPLE
+ 1) * sizeof(*samples
));
3094 if (samples
== NULL
)
3096 printk(KERN_WARNING
"gus_init: Cant allocate memory for instrument tables\n");
3099 conf_printf(tmp2
, hw_config
);
3100 tmp2
[sizeof(gus_info
.name
) - 1] = 0;
3101 strcpy(gus_info
.name
, tmp2
);
3103 if ((sdev
= sound_alloc_synthdev()) == -1)
3104 printk(KERN_WARNING
"gus_init: Too many synthesizers\n");
3107 voice_alloc
= &guswave_operations
.alloc
;
3109 guswave_operations
.id
= "IWAVE";
3110 hw_config
->slots
[0] = sdev
;
3111 synth_devs
[sdev
] = &guswave_operations
;
3113 #ifdef CONFIG_SEQUENCER
3114 gus_tmr_install(gus_base
+ 8);
3118 reset_sample_memory();
3122 if ((gus_mem_size
> 0) & !gus_no_wave_dma
)
3124 hw_config
->slots
[4] = -1;
3125 if ((gus_devnum
= sound_install_audiodrv(AUDIO_DRIVER_VERSION
,
3128 sizeof(struct audio_driver
),
3130 ((!iw_mode
&& dma2
!= dma
&& dma2
!= -1) ?
3132 AFMT_U8
| AFMT_S16_LE
,
3133 NULL
, dma
, dma2
)) < 0)
3138 hw_config
->slots
[4] = gus_devnum
;
3139 audio_devs
[gus_devnum
]->min_fragment
= 9; /* 512k */
3140 audio_devs
[gus_devnum
]->max_fragment
= 11; /* 8k (must match size of bounce_buf */
3141 audio_devs
[gus_devnum
]->mixer_dev
= -1; /* Next mixer# */
3142 audio_devs
[gus_devnum
]->flags
|= DMA_HARDSTOP
;
3146 * Mixer dependent initialization.
3152 gus_mic_vol
= gus_line_vol
= gus_pcm_volume
= 100;
3153 gus_wave_volume
= 90;
3154 request_region(u_MixSelect
, 1, "GUS mixer");
3155 hw_config
->slots
[5] = ics2101_mixer_init();
3156 audio_devs
[gus_devnum
]->mixer_dev
= hw_config
->slots
[5]; /* Next mixer# */
3160 /* Initialized elsewhere (ad1848.c) */
3162 hw_config
->slots
[5] = gus_default_mixer_init();
3163 audio_devs
[gus_devnum
]->mixer_dev
= hw_config
->slots
[5]; /* Next mixer# */
3168 void gus_wave_unload(struct address_info
*hw_config
)
3170 #ifdef CONFIG_GUSMAX
3173 ad1848_unload(gus_base
+ 0x10c,
3175 gus_dma2
, /* Playback DMA */
3176 gus_dma
, /* Capture DMA */
3177 1); /* Share DMA channels with GF1 */
3181 if (mixer_type
== ICS2101
)
3183 release_region(u_MixSelect
, 1);
3185 if (hw_config
->slots
[0] != -1)
3186 sound_unload_synthdev(hw_config
->slots
[0]);
3187 if (hw_config
->slots
[1] != -1)
3188 sound_unload_audiodev(hw_config
->slots
[1]);
3189 if (hw_config
->slots
[2] != -1)
3190 sound_unload_mididev(hw_config
->slots
[2]);
3191 if (hw_config
->slots
[4] != -1)
3192 sound_unload_audiodev(hw_config
->slots
[4]);
3193 if (hw_config
->slots
[5] != -1)
3194 sound_unload_mixerdev(hw_config
->slots
[5]);
3201 static void do_loop_irq(int voice
)
3205 unsigned long flags
;
3209 gus_select_voice(voice
);
3211 tmp
= gus_read8(0x00);
3213 * Disable wave IRQ for this_one voice
3215 gus_write8(0x00, tmp
);
3217 if (tmp
& 0x03) /* Voice stopped */
3218 voice_alloc
->map
[voice
] = 0;
3220 mode
= voices
[voice
].loop_irq_mode
;
3221 voices
[voice
].loop_irq_mode
= 0;
3222 parm
= voices
[voice
].loop_irq_parm
;
3226 case LMODE_FINISH
: /*
3227 * Final loop finished, shoot volume down
3230 if ((int) (gus_read16(0x09) >> 4) < 100) /*
3231 * Get current volume
3236 gus_voice_init(voice
);
3239 gus_ramp_range(65, 4065);
3240 gus_ramp_rate(0, 63); /*
3241 * Fastest possible rate
3243 gus_rampon(0x20 | 0x40); /*
3244 * Ramp down, once, irq
3246 voices
[voice
].volume_irq_mode
= VMODE_HALT
;
3249 case LMODE_PCM_STOP
:
3250 pcm_active
= 0; /* Signal to the play_next_pcm_block routine */
3254 pcm_head
= (pcm_head
+ 1) % pcm_nblk
;
3255 if (pcm_qlen
&& pcm_active
)
3257 play_next_pcm_block();
3261 /* Underrun. Just stop the voice */
3262 gus_select_voice(0); /* Left channel */
3265 gus_select_voice(1); /* Right channel */
3272 * If the queue was full before this interrupt, the DMA transfer was
3273 * suspended. Let it continue now.
3276 if (audio_devs
[gus_devnum
]->dmap_out
->qlen
> 0)
3277 DMAbuf_outputintr(gus_devnum
, 0);
3283 restore_flags(flags
);
3286 static void do_volume_irq(int voice
)
3290 unsigned long flags
;
3295 gus_select_voice(voice
);
3296 tmp
= gus_read8(0x0d);
3298 * Disable volume ramp IRQ
3300 gus_write8(0x0d, tmp
);
3302 mode
= voices
[voice
].volume_irq_mode
;
3303 voices
[voice
].volume_irq_mode
= 0;
3304 parm
= voices
[voice
].volume_irq_parm
;
3308 case VMODE_HALT
: /* Decay phase finished */
3310 gus_write8(0x15, 0x02); /* Set voice deactivate bit of SMSI */
3311 restore_flags(flags
);
3312 gus_voice_init(voice
);
3315 case VMODE_ENVELOPE
:
3317 restore_flags(flags
);
3318 step_envelope(voice
);
3321 case VMODE_START_NOTE
:
3322 restore_flags(flags
);
3323 guswave_start_note2(voices
[voice
].dev_pending
, voice
,
3324 voices
[voice
].note_pending
, voices
[voice
].volume_pending
);
3325 if (voices
[voice
].kill_pending
)
3326 guswave_kill_note(voices
[voice
].dev_pending
, voice
,
3327 voices
[voice
].note_pending
, 0);
3329 if (voices
[voice
].sample_pending
>= 0)
3331 guswave_set_instr(voices
[voice
].dev_pending
, voice
,
3332 voices
[voice
].sample_pending
);
3333 voices
[voice
].sample_pending
= -1;
3338 restore_flags(flags
);
3340 restore_flags(flags
);
3343 void gus_voice_irq(void)
3345 unsigned long wave_ignore
= 0, volume_ignore
= 0;
3346 unsigned long voice_bit
;
3348 unsigned char src
, voice
;
3352 src
= gus_read8(0x0f); /*
3358 if (src
== (0x80 | 0x40))
3363 voice_bit
= 1 << voice
;
3365 if (!(src
& 0x80)) /*
3368 if (!(wave_ignore
& voice_bit
) && (int) voice
< nr_voices
) /*
3373 wave_ignore
|= voice_bit
;
3376 if (!(src
& 0x40)) /*
3377 * Volume IRQ pending
3379 if (!(volume_ignore
& voice_bit
) && (int) voice
< nr_voices
) /*
3384 volume_ignore
|= voice_bit
;
3385 do_volume_irq(voice
);
3390 void guswave_dma_irq(void)
3392 unsigned char status
;
3394 status
= gus_look8(0x41); /* Get DMA IRQ Status */
3395 if (status
& 0x40) /* DMA interrupt pending */
3396 switch (active_device
)
3399 wake_up(&dram_sleeper
);
3402 case GUS_DEV_PCM_CONTINUE
: /* Left channel data transferred */
3403 gus_write8(0x41, 0); /* Disable GF1 DMA */
3404 gus_transfer_output_block(pcm_current_dev
, pcm_current_buf
,
3406 pcm_current_intrflag
, 1);
3409 case GUS_DEV_PCM_DONE
: /* Right or mono channel data transferred */
3410 gus_write8(0x41, 0); /* Disable GF1 DMA */
3411 if (pcm_qlen
< pcm_nblk
)
3416 if (audio_devs
[gus_devnum
]->dmap_out
->qlen
> 0)
3417 DMAbuf_outputintr(gus_devnum
, 0);
3424 status
= gus_look8(0x49); /*
3425 * Get Sampling IRQ Status
3427 if (status
& 0x40) /*
3428 * Sampling Irq pending
3431 DMAbuf_inputintr(gus_devnum
);
3435 #ifdef CONFIG_SEQUENCER
3441 static volatile int select_addr
, data_addr
;
3442 static volatile int curr_timer
= 0;
3444 void gus_timer_command(unsigned int addr
, unsigned int val
)
3448 outb(((unsigned char) (addr
& 0xff)), select_addr
);
3450 for (i
= 0; i
< 2; i
++)
3453 outb(((unsigned char) (val
& 0xff)), data_addr
);
3455 for (i
= 0; i
< 2; i
++)
3459 static void arm_timer(int timer
, unsigned int interval
)
3465 gus_write8(0x46, 256 - interval
); /* Set counter for timer 1 */
3466 gus_write8(0x45, 0x04); /* Enable timer 1 IRQ */
3467 gus_timer_command(0x04, 0x01); /* Start timer 1 */
3471 gus_write8(0x47, 256 - interval
); /* Set counter for timer 2 */
3472 gus_write8(0x45, 0x08); /* Enable timer 2 IRQ */
3473 gus_timer_command(0x04, 0x02); /* Start timer 2 */
3476 gus_timer_enabled
= 1;
3479 static unsigned int gus_tmr_start(int dev
, unsigned int usecs_per_tick
)
3481 int timer_no
, resolution
;
3484 if (usecs_per_tick
> (256 * 80))
3487 resolution
= 320; /* usec */
3492 resolution
= 80; /* usec */
3494 divisor
= (usecs_per_tick
+ (resolution
/ 2)) / resolution
;
3495 arm_timer(timer_no
, divisor
);
3497 return divisor
* resolution
;
3500 static void gus_tmr_disable(int dev
)
3502 gus_write8(0x45, 0); /* Disable both timers */
3503 gus_timer_enabled
= 0;
3506 static void gus_tmr_restart(int dev
)
3508 if (curr_timer
== 1)
3509 gus_write8(0x45, 0x04); /* Start timer 1 again */
3511 gus_write8(0x45, 0x08); /* Start timer 2 again */
3512 gus_timer_enabled
= 1;
3515 static struct sound_lowlev_timer gus_tmr
=
3524 static void gus_tmr_install(int io_base
)
3526 struct sound_lowlev_timer
*tmr
;
3528 select_addr
= io_base
;
3529 data_addr
= io_base
+ 1;
3533 #ifdef THIS_GETS_FIXED
3534 sound_timer_init(&gus_tmr
, "GUS");