1 /*
2 * sound/gus_wave.c
3 *
4 * Driver for the Gravis UltraSound wave table synth.
5 *
6 *
7 * Copyright (C) by Hannu Savolainen 1993-1997
8 *
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
11 * for more info.
12 *
13 *
14 * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
15 * Frank van de Pol : Fixed GUS MAX interrupt handling. Enabled simultanious
16 * usage of CS4231A codec, GUS wave and MIDI for GUS MAX.
17 * Bartlomiej Zolnierkiewicz : added some __init/__exit
18 */
19
20 #include <linux/init.h>
21 #include <linux/config.h>
22
23 #define GUSPNP_AUTODETECT
24
25 #include "sound_config.h"
26 #include <linux/ultrasound.h>
27
28 #include "gus.h"
29 #include "gus_hw.h"
30
31 #define GUS_BANK_SIZE (((iw_mode) ? 256*1024*1024 : 256*1024))
32
33 #define MAX_SAMPLE 150
34 #define MAX_PATCH 256
35
36 #define NOT_SAMPLE 0xffff
37
38 struct voice_info
39 {
40 unsigned long orig_freq;
41 unsigned long current_freq;
42 unsigned long mode;
43 int fixed_pitch;
44 int bender;
45 int bender_range;
46 int panning;
47 int midi_volume;
48 unsigned int initial_volume;
49 unsigned int current_volume;
50 int loop_irq_mode, loop_irq_parm;
51 #define LMODE_FINISH 1
52 #define LMODE_PCM 2
53 #define LMODE_PCM_STOP 3
54 int volume_irq_mode, volume_irq_parm;
55 #define VMODE_HALT 1
56 #define VMODE_ENVELOPE 2
57 #define VMODE_START_NOTE 3
58
59 int env_phase;
60 unsigned char env_rate[6];
61 unsigned char env_offset[6];
62
63 /*
64 * Volume computation parameters for gus_adagio_vol()
65 */
66 int main_vol, expression_vol, patch_vol;
67
68 /* Variables for "Ultraclick" removal */
69 int dev_pending, note_pending, volume_pending,
70 sample_pending;
71 char kill_pending;
72 long offset_pending;
73
74 };
75
76 static struct voice_alloc_info *voice_alloc;
77 static struct address_info *gus_hw_config;
78 extern int gus_base;
79 extern int gus_irq, gus_dma;
80 extern int gus_pnp_flag;
81 extern int gus_no_wave_dma;
82 static int gus_dma2 = -1;
83 static int dual_dma_mode = 0;
84 static long gus_mem_size = 0;
85 static long free_mem_ptr = 0;
86 static int gus_busy = 0;
87 static int gus_no_dma = 0;
88 static int nr_voices = 0;
89 static int gus_devnum = 0;
90 static int volume_base, volume_scale, volume_method;
91 static int gus_recmask = SOUND_MASK_MIC;
92 static int recording_active = 0;
93 static int only_read_access = 0;
94 static int only_8_bits = 0;
95
96 int iw_mode = 0;
97 int gus_wave_volume = 60;
98 int gus_pcm_volume = 80;
99 int have_gus_max = 0;
100 static int gus_line_vol = 100, gus_mic_vol = 0;
101 static unsigned char mix_image = 0x00;
102
103 int gus_timer_enabled = 0;
104
105 /*
106 * Current version of this driver doesn't allow synth and PCM functions
107 * at the same time. The active_device specifies the active driver
108 */
109
110 static int active_device = 0;
111
112 #define GUS_DEV_WAVE 1 /* Wave table synth */
113 #define GUS_DEV_PCM_DONE 2 /* PCM device, transfer done */
114 #define GUS_DEV_PCM_CONTINUE 3 /* PCM device, transfer done ch. 1/2 */
115
116 static int gus_audio_speed;
117 static int gus_audio_channels;
118 static int gus_audio_bits;
119 static int gus_audio_bsize;
120 static char bounce_buf[8 * 1024]; /* Must match value set to max_fragment */
121
122 static DECLARE_WAIT_QUEUE_HEAD(dram_sleeper);
123
124 /*
125 * Variables and buffers for PCM output
126 */
127
128 #define MAX_PCM_BUFFERS (128*MAX_REALTIME_FACTOR) /* Don't change */
129
130 static int pcm_bsize, pcm_nblk, pcm_banksize;
131 static int pcm_datasize[MAX_PCM_BUFFERS];
132 static volatile int pcm_head, pcm_tail, pcm_qlen;
133 static volatile int pcm_active;
134 static volatile int dma_active;
135 static int pcm_opened = 0;
136 static int pcm_current_dev;
137 static int pcm_current_block;
138 static unsigned long pcm_current_buf;
139 static int pcm_current_count;
140 static int pcm_current_intrflag;
141
142 extern int *gus_osp;
143
144 static struct voice_info voices[32];
145
146 static int freq_div_table[] =
147 {
148 44100, /* 14 */
149 41160, /* 15 */
150 38587, /* 16 */
151 36317, /* 17 */
152 34300, /* 18 */
153 32494, /* 19 */
154 30870, /* 20 */
155 29400, /* 21 */
156 28063, /* 22 */
157 26843, /* 23 */
158 25725, /* 24 */
159 24696, /* 25 */
160 23746, /* 26 */
161 22866, /* 27 */
162 22050, /* 28 */
163 21289, /* 29 */
164 20580, /* 30 */
165 19916, /* 31 */
166 19293 /* 32 */
167 };
168
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;
174
175
176 static int patch_table[MAX_PATCH];
177 static int patch_map[32];
178
179 static struct synth_info gus_info = {
180 "Gravis UltraSound", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_GUS,
181 0, 16, 0, MAX_PATCH
182 };
183
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);
192
193 #define INSTANT_RAMP -1 /* Instant change. No ramping */
194 #define FAST_RAMP 0 /* Fastest possible ramp */
195
196 static void reset_sample_memory(void)
197 {
198 int i;
199
200 for (i = 0; i <= MAX_SAMPLE; i++)
201 sample_ptrs[i] = -1;
202 for (i = 0; i < 32; i++)
203 sample_map[i] = -1;
204 for (i = 0; i < 32; i++)
205 patch_map[i] = -1;
206
207 gus_poke(0, 0); /* Put a silent sample to the beginning */
208 gus_poke(1, 0);
209 free_mem_ptr = 2;
210
211 free_sample = 0;
212
213 for (i = 0; i < MAX_PATCH; i++)
214 patch_table[i] = NOT_SAMPLE;
215 }
216
217 void gus_delay(void)
218 {
219 int i;
220
221 for (i = 0; i < 7; i++)
222 inb(u_DRAMIO);
223 }
224
225 static void gus_poke(long addr, unsigned char data)
226 { /* Writes a byte to the DRAM */
227 unsigned long flags;
228
229 save_flags(flags);
230 cli();
231 outb((0x43), u_Command);
232 outb((addr & 0xff), u_DataLo);
233 outb(((addr >> 8) & 0xff), u_DataHi);
234
235 outb((0x44), u_Command);
236 outb(((addr >> 16) & 0xff), u_DataHi);
237 outb((data), u_DRAMIO);
238 restore_flags(flags);
239 }
240
241 static unsigned char gus_peek(long addr)
242 { /* Reads a byte from the DRAM */
243 unsigned long flags;
244 unsigned char tmp;
245
246 save_flags(flags);
247 cli();
248 outb((0x43), u_Command);
249 outb((addr & 0xff), u_DataLo);
250 outb(((addr >> 8) & 0xff), u_DataHi);
251
252 outb((0x44), u_Command);
253 outb(((addr >> 16) & 0xff), u_DataHi);
254 tmp = inb(u_DRAMIO);
255 restore_flags(flags);
256
257 return tmp;
258 }
259
260 void gus_write8(int reg, unsigned int data)
261 { /* Writes to an indirect register (8 bit) */
262 unsigned long flags;
263
264 save_flags(flags);
265 cli();
266
267 outb((reg), u_Command);
268 outb(((unsigned char) (data & 0xff)), u_DataHi);
269
270 restore_flags(flags);
271 }
272
273 static unsigned char gus_read8(int reg)
274 {
275 /* Reads from an indirect register (8 bit). Offset 0x80. */
276 unsigned long flags;
277 unsigned char val;
278
279 save_flags(flags);
280 cli();
281 outb((reg | 0x80), u_Command);
282 val = inb(u_DataHi);
283 restore_flags(flags);
284
285 return val;
286 }
287
288 static unsigned char gus_look8(int reg)
289 {
290 /* Reads from an indirect register (8 bit). No additional offset. */
291 unsigned long flags;
292 unsigned char val;
293
294 save_flags(flags);
295 cli();
296 outb((reg), u_Command);
297 val = inb(u_DataHi);
298 restore_flags(flags);
299
300 return val;
301 }
302
303 static void gus_write16(int reg, unsigned int data)
304 {
305 /* Writes to an indirect register (16 bit) */
306 unsigned long flags;
307
308 save_flags(flags);
309 cli();
310
311 outb((reg), u_Command);
312
313 outb(((unsigned char) (data & 0xff)), u_DataLo);
314 outb(((unsigned char) ((data >> 8) & 0xff)), u_DataHi);
315
316 restore_flags(flags);
317 }
318
319 static unsigned short gus_read16(int reg)
320 {
321 /* Reads from an indirect register (16 bit). Offset 0x80. */
322 unsigned long flags;
323 unsigned char hi, lo;
324
325 save_flags(flags);
326 cli();
327
328 outb((reg | 0x80), u_Command);
329
330 lo = inb(u_DataLo);
331 hi = inb(u_DataHi);
332
333 restore_flags(flags);
334
335 return ((hi << 8) & 0xff00) | lo;
336 }
337
338 static unsigned short gus_look16(int reg)
339 {
340 /* Reads from an indirect register (16 bit). No additional offset. */
341 unsigned long flags;
342 unsigned char hi, lo;
343
344 save_flags(flags);
345 cli();
346
347 outb((reg), u_Command);
348
349 lo = inb(u_DataLo);
350 hi = inb(u_DataHi);
351
352 restore_flags(flags);
353
354 return ((hi << 8) & 0xff00) | lo;
355 }
356
357 static void gus_write_addr(int reg, unsigned long address, int frac, int is16bit)
358 {
359 /* Writes an 24 bit memory address */
360 unsigned long hold_address;
361 unsigned long flags;
362
363 save_flags(flags);
364 cli();
365 if (is16bit)
366 {
367 if (iw_mode)
368 {
369 /* Interwave spesific address translations */
370 address >>= 1;
371 }
372 else
373 {
374 /*
375 * Special processing required for 16 bit patches
376 */
377
378 hold_address = address;
379 address = address >> 1;
380 address &= 0x0001ffffL;
381 address |= (hold_address & 0x000c0000L);
382 }
383 }
384 gus_write16(reg, (unsigned short) ((address >> 7) & 0xffff));
385 gus_write16(reg + 1, (unsigned short) ((address << 9) & 0xffff)
386 + (frac << 5));
387 /* Could writing twice fix problems with GUS_VOICE_POS()? Let's try. */
388 gus_delay();
389 gus_write16(reg, (unsigned short) ((address >> 7) & 0xffff));
390 gus_write16(reg + 1, (unsigned short) ((address << 9) & 0xffff)
391 + (frac << 5));
392 restore_flags(flags);
393 }
394
395 static void gus_select_voice(int voice)
396 {
397 if (voice < 0 || voice > 31)
398 return;
399 outb((voice), u_Voice);
400 }
401
402 static void gus_select_max_voices(int nvoices)
403 {
404 if (iw_mode)
405 nvoices = 32;
406 if (nvoices < 14)
407 nvoices = 14;
408 if (nvoices > 32)
409 nvoices = 32;
410
411 voice_alloc->max_voice = nr_voices = nvoices;
412 gus_write8(0x0e, (nvoices - 1) | 0xc0);
413 }
414
415 static void gus_voice_on(unsigned int mode)
416 {
417 gus_write8(0x00, (unsigned char) (mode & 0xfc));
418 gus_delay();
419 gus_write8(0x00, (unsigned char) (mode & 0xfc));
420 }
421
422 static void gus_voice_off(void)
423 {
424 gus_write8(0x00, gus_read8(0x00) | 0x03);
425 }
426
427 static void gus_voice_mode(unsigned int m)
428 {
429 unsigned char mode = (unsigned char) (m & 0xff);
430
431 gus_write8(0x00, (gus_read8(0x00) & 0x03) |
432 (mode & 0xfc)); /* Don't touch last two bits */
433 gus_delay();
434 gus_write8(0x00, (gus_read8(0x00) & 0x03) | (mode & 0xfc));
435 }
436
437 static void gus_voice_freq(unsigned long freq)
438 {
439 unsigned long divisor = freq_div_table[nr_voices - 14];
440 unsigned short fc;
441
442 /* Interwave plays at 44100 Hz with any number of voices */
443 if (iw_mode)
444 fc = (unsigned short) (((freq << 9) + (44100 >> 1)) / 44100);
445 else
446 fc = (unsigned short) (((freq << 9) + (divisor >> 1)) / divisor);
447 fc = fc << 1;
448
449 gus_write16(0x01, fc);
450 }
451
452 static void gus_voice_volume(unsigned int vol)
453 {
454 gus_write8(0x0d, 0x03); /* Stop ramp before setting volume */
455 gus_write16(0x09, (unsigned short) (vol << 4));
456 }
457
458 static void gus_voice_balance(unsigned int balance)
459 {
460 gus_write8(0x0c, (unsigned char) (balance & 0xff));
461 }
462
463 static void gus_ramp_range(unsigned int low, unsigned int high)
464 {
465 gus_write8(0x07, (unsigned char) ((low >> 4) & 0xff));
466 gus_write8(0x08, (unsigned char) ((high >> 4) & 0xff));
467 }
468
469 static void gus_ramp_rate(unsigned int scale, unsigned int rate)
470 {
471 gus_write8(0x06, (unsigned char) (((scale & 0x03) << 6) | (rate & 0x3f)));
472 }
473
474 static void gus_rampon(unsigned int m)
475 {
476 unsigned char mode = (unsigned char) (m & 0xff);
477
478 gus_write8(0x0d, mode & 0xfc);
479 gus_delay();
480 gus_write8(0x0d, mode & 0xfc);
481 }
482
483 static void gus_ramp_mode(unsigned int m)
484 {
485 unsigned char mode = (unsigned char) (m & 0xff);
486
487 gus_write8(0x0d, (gus_read8(0x0d) & 0x03) |
488 (mode & 0xfc)); /* Leave the last 2 bits alone */
489 gus_delay();
490 gus_write8(0x0d, (gus_read8(0x0d) & 0x03) | (mode & 0xfc));
491 }
492
493 static void gus_rampoff(void)
494 {
495 gus_write8(0x0d, 0x03);
496 }
497
498 static void gus_set_voice_pos(int voice, long position)
499 {
500 int sample_no;
501
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;
506 else
507 gus_write_addr(0x0a, sample_ptrs[sample_no] + position, 0,
508 samples[sample_no].mode & WAVE_16_BITS);
509 }
510 }
511 }
512
513 static void gus_voice_init(int voice)
514 {
515 unsigned long flags;
516
517 save_flags(flags);
518 cli();
519 gus_select_voice(voice);
520 gus_voice_volume(0);
521 gus_voice_off();
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);
528
529 }
530
531 static void gus_voice_init2(int voice)
532 {
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;
551 }
552
553 static void step_envelope(int voice)
554 {
555 unsigned vol, prev_vol, phase;
556 unsigned char rate;
557 long int flags;
558
559 if (voices[voice].mode & WAVE_SUSTAIN_ON && voices[voice].env_phase == 2)
560 {
561 save_flags(flags);
562 cli();
563 gus_select_voice(voice);
564 gus_rampoff();
565 restore_flags(flags);
566 return;
567 /*
568 * Sustain phase begins. Continue envelope after receiving note off.
569 */
570 }
571 if (voices[voice].env_phase >= 5)
572 {
573 /* Envelope finished. Shoot the voice down */
574 gus_voice_init(voice);
575 return;
576 }
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];
582
583 save_flags(flags);
584 cli();
585 gus_select_voice(voice);
586
587 gus_voice_volume(prev_vol);
588
589
590 gus_write8(0x06, rate); /* Ramping rate */
591
592 voices[voice].volume_irq_mode = VMODE_ENVELOPE;
593
594 if (((vol - prev_vol) / 64) == 0) /* No significant volume change */
595 {
596 restore_flags(flags);
597 step_envelope(voice); /* Continue the envelope on the next step */
598 return;
599 }
600 if (vol > prev_vol)
601 {
602 if (vol >= (4096 - 64))
603 vol = 4096 - 65;
604 gus_ramp_range(0, vol);
605 gus_rampon(0x20); /* Increasing volume, with IRQ */
606 }
607 else
608 {
609 if (vol <= 64)
610 vol = 65;
611 gus_ramp_range(vol, 4030);
612 gus_rampon(0x60); /* Decreasing volume, with IRQ */
613 }
614 voices[voice].current_volume = vol;
615 restore_flags(flags);
616 }
617
618 static void init_envelope(int voice)
619 {
620 voices[voice].env_phase = -1;
621 voices[voice].current_volume = 64;
622
623 step_envelope(voice);
624 }
625
626 static void start_release(int voice, long int flags)
627 {
628 if (gus_read8(0x00) & 0x03)
629 return; /* Voice already stopped */
630
631 voices[voice].env_phase = 2; /* Will be incremented by step_envelope */
632
633 voices[voice].current_volume = voices[voice].initial_volume =
634 gus_read16(0x09) >> 4; /* Get current volume */
635
636 voices[voice].mode &= ~WAVE_SUSTAIN_ON;
637 gus_rampoff();
638 restore_flags(flags);
639 step_envelope(voice);
640 }
641
642 static void gus_voice_fade(int voice)
643 {
644 int instr_no = sample_map[voice], is16bits;
645 long int flags;
646
647 save_flags(flags);
648 cli();
649 gus_select_voice(voice);
650
651 if (instr_no < 0 || instr_no > MAX_SAMPLE)
652 {
653 gus_write8(0x00, 0x03); /* Hard stop */
654 voice_alloc->map[voice] = 0;
655 restore_flags(flags);
656 return;
657 }
658 is16bits = (samples[instr_no].mode & WAVE_16_BITS) ? 1 : 0; /* 8 or 16 bits */
659
660 if (voices[voice].mode & WAVE_ENVELOPES)
661 {
662 start_release(voice, flags);
663 restore_flags(flags);
664 return;
665 }
666 /*
667 * Ramp the volume down but not too quickly.
668 */
669 if ((int) (gus_read16(0x09) >> 4) < 100) /* Get current volume */
670 {
671 gus_voice_off();
672 gus_rampoff();
673 gus_voice_init(voice);
674 restore_flags(flags);
675 return;
676 }
677 gus_ramp_range(65, 4030);
678 gus_ramp_rate(2, 4);
679 gus_rampon(0x40 | 0x20); /* Down, once, with IRQ */
680 voices[voice].volume_irq_mode = VMODE_HALT;
681 restore_flags(flags);
682 }
683
684 static void gus_reset(void)
685 {
686 int i;
687
688 gus_select_max_voices(24);
689 volume_base = 3071;
690 volume_scale = 4;
691 volume_method = VOL_METHOD_ADAGIO;
692
693 for (i = 0; i < 32; i++)
694 {
695 gus_voice_init(i); /* Turn voice off */
696 gus_voice_init2(i);
697 }
698 }
699
700 static void gus_initialize(void)
701 {
702 unsigned long flags;
703 unsigned char dma_image, irq_image, tmp;
704
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
707 };
708
709 static unsigned char gus_dma_map[8] = {
710 0, 1, 0, 2, 0, 3, 4, 5
711 };
712
713 save_flags(flags);
714 cli();
715 gus_write8(0x4c, 0); /* Reset GF1 */
716 gus_delay();
717 gus_delay();
718
719 gus_write8(0x4c, 1); /* Release Reset */
720 gus_delay();
721 gus_delay();
722
723 /*
724 * Clear all interrupts
725 */
726
727 gus_write8(0x41, 0); /* DMA control */
728 gus_write8(0x45, 0); /* Timer control */
729 gus_write8(0x49, 0); /* Sample control */
730
731 gus_select_max_voices(24);
732
733 inb(u_Status); /* Touch the status register */
734
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 */
738
739 gus_reset(); /* Resets all voices */
740
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 */
744
745 gus_write8(0x4c, 7); /* Master reset | DAC enable | IRQ enable */
746
747 /*
748 * Set up for Digital ASIC
749 */
750
751 outb((0x05), gus_base + 0x0f);
752
753 mix_image |= 0x02; /* Disable line out (for a moment) */
754 outb((mix_image), u_Mixer);
755
756 outb((0x00), u_IRQDMAControl);
757
758 outb((0x00), gus_base + 0x0f);
759
760 /*
761 * Now set up the DMA and IRQ interface
762 *
763 * The GUS supports two IRQs and two DMAs.
764 *
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
767 * and audio.c also.
768 */
769
770 irq_image = 0;
771 tmp = gus_irq_map[gus_irq];
772 if (!gus_pnp_flag && !tmp)
773 printk(KERN_WARNING "Warning! GUS IRQ not selected\n");
774 irq_image |= tmp;
775 irq_image |= 0x40; /* Combine IRQ1 (GF1) and IRQ2 (Midi) */
776
777 dual_dma_mode = 1;
778 if (gus_dma2 == gus_dma || gus_dma2 == -1)
779 {
780 dual_dma_mode = 0;
781 dma_image = 0x40; /* Combine DMA1 (DRAM) and IRQ2 (ADC) */
782
783 tmp = gus_dma_map[gus_dma];
784 if (!tmp)
785 printk(KERN_WARNING "Warning! GUS DMA not selected\n");
786
787 dma_image |= tmp;
788 }
789 else
790 {
791 /* Setup dual DMA channel mode for GUS MAX */
792
793 dma_image = gus_dma_map[gus_dma];
794 if (!dma_image)
795 printk(KERN_WARNING "Warning! GUS DMA not selected\n");
796
797 tmp = gus_dma_map[gus_dma2] << 3;
798 if (!tmp)
799 {
800 printk(KERN_WARNING "Warning! Invalid GUS MAX DMA\n");
801 tmp = 0x40; /* Combine DMA channels */
802 dual_dma_mode = 0;
803 }
804 dma_image |= tmp;
805 }
806
807 /*
808 * For some reason the IRQ and DMA addresses must be written twice
809 */
810
811 /*
812 * Doing it first time
813 */
814
815 outb((mix_image), u_Mixer); /* Select DMA control */
816 outb((dma_image | 0x80), u_IRQDMAControl); /* Set DMA address */
817
818 outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
819 outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
820
821 /*
822 * Doing it second time
823 */
824
825 outb((mix_image), u_Mixer); /* Select DMA control */
826 outb((dma_image), u_IRQDMAControl); /* Set DMA address */
827
828 outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
829 outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
830
831 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
832
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.
838 */
839
840 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
841
842 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
843
844 inb(u_Status); /* Touch the status register */
845
846 gus_look8(0x41); /* Clear any pending DMA IRQs */
847 gus_look8(0x49); /* Clear any pending sample IRQs */
848
849 gus_read8(0x0f); /* Clear pending IRQs */
850
851 if (iw_mode)
852 gus_write8(0x19, gus_read8(0x19) | 0x01);
853 restore_flags(flags);
854 }
855
856
857 static void __init pnp_mem_init(void)
858 {
859 #include "iwmem.h"
860 #define CHUNK_SIZE (256*1024)
861 #define BANK_SIZE (4*1024*1024)
862 #define CHUNKS_PER_BANK (BANK_SIZE/CHUNK_SIZE)
863
864 int bank, chunk, addr, total = 0;
865 int bank_sizes[4];
866 int i, j, bits = -1, testbits = -1, nbanks = 0;
867
868 /*
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.
871 */
872
873 /*
874 * Place the chip into enhanced mode
875 */
876 gus_write8(0x19, gus_read8(0x19) | 0x01);
877 gus_write8(0x53, gus_look8(0x53) & ~0x02); /* Select DRAM I/O access */
878
879 /*
880 * Set memory configuration to 4 DRAM banks of 4M in each (16M total).
881 */
882
883 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | 0x000c);
884
885 /*
886 * Perform the DRAM size detection for each bank individually.
887 */
888 for (bank = 0; bank < 4; bank++)
889 {
890 int size = 0;
891
892 addr = bank * BANK_SIZE;
893
894 /* Clean check points of each chunk */
895 for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
896 {
897 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
898 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
899 }
900
901 /* Write a value to each chunk point and verify the result */
902 for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
903 {
904 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x55);
905 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0xAA);
906
907 if (gus_peek(addr + chunk * CHUNK_SIZE + 0L) == 0x55 &&
908 gus_peek(addr + chunk * CHUNK_SIZE + 1L) == 0xAA)
909 {
910 /* OK. There is RAM. Now check for possible shadows */
911 int ok = 1, chunk2;
912
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 */
917
918 if (ok)
919 size = (chunk + 1) * CHUNK_SIZE;
920 }
921 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
922 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
923 }
924 bank_sizes[bank] = size;
925 if (size)
926 nbanks = bank + 1;
927 DDB(printk("Interwave: Bank %d, size=%dk\n", bank, size / 1024));
928 }
929
930 if (nbanks == 0) /* No RAM - Give up */
931 {
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);
935 gus_mem_size = 0;
936 return;
937 }
938
939 /*
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
942 * we have.
943 *
944 * First try if any of the possible alternatives matches exactly the amount
945 * of memory we have.
946 */
947
948 for (i = 0; bits == -1 && i < 13; i++)
949 {
950 bits = i;
951
952 for (j = 0; bits != -1 && j < 4; j++)
953 if (mem_decode[i][j] != bank_sizes[j])
954 bits = -1; /* No hit */
955 }
956
957 /*
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.
961 */
962
963 if (bits == -1) /* No luck yet */
964 {
965 for (i = 0; bits == -1 && i < 13; i++)
966 {
967 bits = i;
968
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 */
974 }
975 }
976 /*
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.
980 */
981 if (bits == -1) /* No luck yet */
982 {
983 for (i = 0; i < 13; i++)
984 {
985 testbits = i;
986 for (j = 0; testbits != -1 && j < nbanks - 1; j++)
987 if (mem_decode[i][j] > bank_sizes[j]) {
988 testbits = -1;
989 }
990 if(testbits > bits) bits = testbits;
991 }
992 if (bits != -1)
993 {
994 printk(KERN_INFO "Interwave: Can't use all installed RAM.\n");
995 printk(KERN_INFO "Interwave: Try reordering SIMMS.\n");
996 }
997 printk(KERN_INFO "Interwave: Can't find working DRAM encoding.\n");
998 printk(KERN_INFO "Interwave: Defaulting to 256k. Try reordering SIMMS.\n");
999 bits = 0;
1000 }
1001 DDB(printk("Interwave: Selecting DRAM addressing mode %d\n", bits));
1002
1003 for (bank = 0; bank < 4; bank++)
1004 {
1005 DDB(printk(" Bank %d, mem=%dk (limit %dk)\n", bank, bank_sizes[bank] / 1024, mem_decode[bits][bank] / 1024));
1006
1007 if (bank_sizes[bank] > mem_decode[bits][bank])
1008 total += mem_decode[bits][bank];
1009 else
1010 total += bank_sizes[bank];
1011 }
1012
1013 DDB(printk("Total %dk of DRAM (enhanced mode)\n", total / 1024));
1014
1015 /*
1016 * Set the memory addressing mode.
1017 */
1018 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | bits);
1019
1020 /* Leave the chip into enhanced mode. Disable LFO */
1021 gus_mem_size = total;
1022 iw_mode = 1;
1023 gus_write8(0x19, (gus_read8(0x19) | 0x01) & ~0x02);
1024 }
1025
1026 int __init gus_wave_detect(int baseaddr)
1027 {
1028 unsigned long i, max_mem = 1024L;
1029 unsigned long loc;
1030 unsigned char val;
1031
1032 gus_base = baseaddr;
1033
1034 gus_write8(0x4c, 0); /* Reset GF1 */
1035 gus_delay();
1036 gus_delay();
1037
1038 gus_write8(0x4c, 1); /* Release Reset */
1039 gus_delay();
1040 gus_delay();
1041
1042 #ifdef GUSPNP_AUTODETECT
1043 val = gus_look8(0x5b); /* Version number register */
1044 gus_write8(0x5b, ~val); /* Invert all bits */
1045
1046 if ((gus_look8(0x5b) & 0xf0) == (val & 0xf0)) /* No change */
1047 {
1048 if ((gus_look8(0x5b) & 0x0f) == ((~val) & 0x0f)) /* Change */
1049 {
1050 DDB(printk("Interwave chip version %d detected\n", (val & 0xf0) >> 4));
1051 gus_pnp_flag = 1;
1052 }
1053 else
1054 {
1055 DDB(printk("Not an Interwave chip (%x)\n", gus_look8(0x5b)));
1056 gus_pnp_flag = 0;
1057 }
1058 }
1059 gus_write8(0x5b, val); /* Restore all bits */
1060 #endif
1061
1062 if (gus_pnp_flag)
1063 pnp_mem_init();
1064 if (iw_mode)
1065 return 1;
1066
1067 /* See if there is first block there.... */
1068 gus_poke(0L, 0xaa);
1069 if (gus_peek(0L) != 0xaa)
1070 return (0);
1071
1072 /* Now zero it out so that I can check for mirroring .. */
1073 gus_poke(0L, 0x00);
1074 for (i = 1L; i < max_mem; i++)
1075 {
1076 int n, failed;
1077
1078 /* check for mirroring ... */
1079 if (gus_peek(0L) != 0)
1080 break;
1081 loc = i << 10;
1082
1083 for (n = loc - 1, failed = 0; n <= loc; n++)
1084 {
1085 gus_poke(loc, 0xaa);
1086 if (gus_peek(loc) != 0xaa)
1087 failed = 1;
1088 gus_poke(loc, 0x55);
1089 if (gus_peek(loc) != 0x55)
1090 failed = 1;
1091 }
1092 if (failed)
1093 break;
1094 }
1095 gus_mem_size = i << 10;
1096 return 1;
1097 }
1098
1099 static int guswave_ioctl(int dev, unsigned int cmd, caddr_t arg)
1100 {
1101
1102 switch (cmd)
1103 {
1104 case SNDCTL_SYNTH_INFO:
1105 gus_info.nr_voices = nr_voices;
1106 if (copy_to_user(arg, &gus_info, sizeof(gus_info)))
1107 return -EFAULT;
1108 return 0;
1109
1110 case SNDCTL_SEQ_RESETSAMPLES:
1111 reset_sample_memory();
1112 return 0;
1113
1114 case SNDCTL_SEQ_PERCMODE:
1115 return 0;
1116
1117 case SNDCTL_SYNTH_MEMAVL:
1118 return (gus_mem_size == 0) ? 0 : gus_mem_size - free_mem_ptr - 32;
1119
1120 default:
1121 return -EINVAL;
1122 }
1123 }
1124
1125 static int guswave_set_instr(int dev, int voice, int instr_no)
1126 {
1127 int sample_no;
1128
1129 if (instr_no < 0 || instr_no > MAX_PATCH)
1130 instr_no = 0; /* Default to acoustic piano */
1131
1132 if (voice < 0 || voice > 31)
1133 return -EINVAL;
1134
1135 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1136 {
1137 voices[voice].sample_pending = instr_no;
1138 return 0;
1139 }
1140 sample_no = patch_table[instr_no];
1141 patch_map[voice] = -1;
1142
1143 if (sample_no == NOT_SAMPLE)
1144 {
1145 /* printk("GUS: Undefined patch %d for voice %d\n", instr_no, voice);*/
1146 return -EINVAL; /* Patch not defined */
1147 }
1148 if (sample_ptrs[sample_no] == -1) /* Sample not loaded */
1149 {
1150 /* printk("GUS: Sample #%d not loaded for patch %d (voice %d)\n", sample_no, instr_no, voice);*/
1151 return -EINVAL;
1152 }
1153 sample_map[voice] = sample_no;
1154 patch_map[voice] = instr_no;
1155 return 0;
1156 }
1157
1158 static int guswave_kill_note(int dev, int voice, int note, int velocity)
1159 {
1160 unsigned long flags;
1161
1162 save_flags(flags);
1163 cli();
1164 /* voice_alloc->map[voice] = 0xffff; */
1165 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1166 {
1167 voices[voice].kill_pending = 1;
1168 restore_flags(flags);
1169 }
1170 else
1171 {
1172 restore_flags(flags);
1173 gus_voice_fade(voice);
1174 }
1175
1176 return 0;
1177 }
1178
1179 static void guswave_aftertouch(int dev, int voice, int pressure)
1180 {
1181 }
1182
1183 static void guswave_panning(int dev, int voice, int value)
1184 {
1185 if (voice >= 0 || voice < 32)
1186 voices[voice].panning = value;
1187 }
1188
1189 static void guswave_volume_method(int dev, int mode)
1190 {
1191 if (mode == VOL_METHOD_LINEAR || mode == VOL_METHOD_ADAGIO)
1192 volume_method = mode;
1193 }
1194
1195 static void compute_volume(int voice, int volume)
1196 {
1197 if (volume < 128)
1198 voices[voice].midi_volume = volume;
1199
1200 switch (volume_method)
1201 {
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);
1207 break;
1208
1209 case VOL_METHOD_LINEAR: /* Totally ignores patch-volume and expression */
1210 voices[voice].initial_volume = gus_linear_vol(volume, voices[voice].main_vol);
1211 break;
1212
1213 default:
1214 voices[voice].initial_volume = volume_base +
1215 (voices[voice].midi_volume * volume_scale);
1216 }
1217
1218 if (voices[voice].initial_volume > 4030)
1219 voices[voice].initial_volume = 4030;
1220 }
1221
1222 static void compute_and_set_volume(int voice, int volume, int ramp_time)
1223 {
1224 int curr, target, rate;
1225 unsigned long flags;
1226
1227 compute_volume(voice, volume);
1228 voices[voice].current_volume = voices[voice].initial_volume;
1229
1230 save_flags(flags);
1231 cli();
1232 /*
1233 * CAUTION! Interrupts disabled. Enable them before returning
1234 */
1235
1236 gus_select_voice(voice);
1237
1238 curr = gus_read16(0x09) >> 4;
1239 target = voices[voice].initial_volume;
1240
1241 if (ramp_time == INSTANT_RAMP)
1242 {
1243 gus_rampoff();
1244 gus_voice_volume(target);
1245 restore_flags(flags);
1246 return;
1247 }
1248 if (ramp_time == FAST_RAMP)
1249 rate = 63;
1250 else
1251 rate = 16;
1252 gus_ramp_rate(0, rate);
1253
1254 if ((target - curr) / 64 == 0) /* Close enough to target. */
1255 {
1256 gus_rampoff();
1257 gus_voice_volume(target);
1258 restore_flags(flags);
1259 return;
1260 }
1261 if (target > curr)
1262 {
1263 if (target > (4095 - 65))
1264 target = 4095 - 65;
1265 gus_ramp_range(curr, target);
1266 gus_rampon(0x00); /* Ramp up, once, no IRQ */
1267 }
1268 else
1269 {
1270 if (target < 65)
1271 target = 65;
1272
1273 gus_ramp_range(target, curr);
1274 gus_rampon(0x40); /* Ramp down, once, no irq */
1275 }
1276 restore_flags(flags);
1277 }
1278
1279 static void dynamic_volume_change(int voice)
1280 {
1281 unsigned char status;
1282 unsigned long flags;
1283
1284 save_flags(flags);
1285 cli();
1286 gus_select_voice(voice);
1287 status = gus_read8(0x00); /* Get voice status */
1288 restore_flags(flags);
1289
1290 if (status & 0x03)
1291 return; /* Voice was not running */
1292
1293 if (!(voices[voice].mode & WAVE_ENVELOPES))
1294 {
1295 compute_and_set_volume(voice, voices[voice].midi_volume, 1);
1296 return;
1297 }
1298
1299 /*
1300 * Voice is running and has envelopes.
1301 */
1302
1303 save_flags(flags);
1304 cli();
1305 gus_select_voice(voice);
1306 status = gus_read8(0x0d); /* Ramping status */
1307 restore_flags(flags);
1308
1309 if (status & 0x03) /* Sustain phase? */
1310 {
1311 compute_and_set_volume(voice, voices[voice].midi_volume, 1);
1312 return;
1313 }
1314 if (voices[voice].env_phase < 0)
1315 return;
1316
1317 compute_volume(voice, voices[voice].midi_volume);
1318
1319 }
1320
1321 static void guswave_controller(int dev, int voice, int ctrl_num, int value)
1322 {
1323 unsigned long flags;
1324 unsigned long freq;
1325
1326 if (voice < 0 || voice > 31)
1327 return;
1328
1329 switch (ctrl_num)
1330 {
1331 case CTRL_PITCH_BENDER:
1332 voices[voice].bender = value;
1333
1334 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1335 {
1336 freq = compute_finetune(voices[voice].orig_freq, value, voices[voice].bender_range, 0);
1337 voices[voice].current_freq = freq;
1338
1339 save_flags(flags);
1340 cli();
1341 gus_select_voice(voice);
1342 gus_voice_freq(freq);
1343 restore_flags(flags);
1344 }
1345 break;
1346
1347 case CTRL_PITCH_BENDER_RANGE:
1348 voices[voice].bender_range = value;
1349 break;
1350 case CTL_EXPRESSION:
1351 value /= 128;
1352 case CTRL_EXPRESSION:
1353 if (volume_method == VOL_METHOD_ADAGIO)
1354 {
1355 voices[voice].expression_vol = value;
1356 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1357 dynamic_volume_change(voice);
1358 }
1359 break;
1360
1361 case CTL_PAN:
1362 voices[voice].panning = (value * 2) - 128;
1363 break;
1364
1365 case CTL_MAIN_VOLUME:
1366 value = (value * 100) / 16383;
1367
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);
1372 break;
1373
1374 default:
1375 break;
1376 }
1377 }
1378
1379 static int guswave_start_note2(int dev, int voice, int note_num, int volume)
1380 {
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;
1385
1386 if (voice < 0 || voice > 31)
1387 {
1388 /* printk("GUS: Invalid voice\n");*/
1389 return -EINVAL;
1390 }
1391 if (note_num == 255)
1392 {
1393 if (voices[voice].mode & WAVE_ENVELOPES)
1394 {
1395 voices[voice].midi_volume = volume;
1396 dynamic_volume_change(voice);
1397 return 0;
1398 }
1399 compute_and_set_volume(voice, volume, 1);
1400 return 0;
1401 }
1402 if ((patch = patch_map[voice]) == -1)
1403 return -EINVAL;
1404 if ((samplep = patch_table[patch]) == NOT_SAMPLE)
1405 {
1406 return -EINVAL;
1407 }
1408 note_freq = note_to_freq(note_num);
1409
1410 /*
1411 * Find a sample within a patch so that the note_freq is between low_note
1412 * and high_note.
1413 */
1414 sample = -1;
1415
1416 best_sample = samplep;
1417 best_delta = 1000000;
1418 while (samplep != 0 && samplep != NOT_SAMPLE && sample == -1)
1419 {
1420 delta_freq = note_freq - samples[samplep].base_note;
1421 if (delta_freq < 0)
1422 delta_freq = -delta_freq;
1423 if (delta_freq < best_delta)
1424 {
1425 best_sample = samplep;
1426 best_delta = delta_freq;
1427 }
1428 if (samples[samplep].low_note <= note_freq &&
1429 note_freq <= samples[samplep].high_note)
1430 {
1431 sample = samplep;
1432 }
1433 else
1434 samplep = samples[samplep].key; /* Link to next sample */
1435 }
1436 if (sample == -1)
1437 sample = best_sample;
1438
1439 if (sample == -1)
1440 {
1441 /* printk("GUS: Patch %d not defined for note %d\n", patch, note_num);*/
1442 return 0; /* Should play default patch ??? */
1443 }
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;
1447
1448 if (iw_mode)
1449 gus_write8(0x15, 0x00); /* RAM, Reset voice deactivate bit of SMSI */
1450
1451 if (voices[voice].mode & WAVE_ENVELOPES)
1452 {
1453 int i;
1454
1455 for (i = 0; i < 6; i++)
1456 {
1457 voices[voice].env_rate[i] = samples[sample].env_rate[i];
1458 voices[voice].env_offset[i] = samples[sample].env_offset[i];
1459 }
1460 }
1461 sample_map[voice] = sample;
1462
1463 if (voices[voice].fixed_pitch) /* Fixed pitch */
1464 {
1465 freq = samples[sample].base_freq;
1466 }
1467 else
1468 {
1469 base_note = samples[sample].base_note / 100;
1470 note_freq /= 100;
1471
1472 freq = samples[sample].base_freq * note_freq / base_note;
1473 }
1474
1475 voices[voice].orig_freq = freq;
1476
1477 /*
1478 * Since the pitch bender may have been set before playing the note, we
1479 * have to calculate the bending now.
1480 */
1481
1482 freq = compute_finetune(voices[voice].orig_freq, voices[voice].bender,
1483 voices[voice].bender_range, 0);
1484 voices[voice].current_freq = freq;
1485
1486 pan = (samples[sample].panning + voices[voice].panning) / 32;
1487 pan += 7;
1488 if (pan < 0)
1489 pan = 0;
1490 if (pan > 15)
1491 pan = 15;
1492
1493 if (samples[sample].mode & WAVE_16_BITS)
1494 {
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");
1499 }
1500 /*************************************************************************
1501 * CAUTION! Interrupts disabled. Don't return before enabling
1502 *************************************************************************/
1503
1504 save_flags(flags);
1505 cli();
1506 gus_select_voice(voice);
1507 gus_voice_off();
1508 gus_rampoff();
1509
1510 restore_flags(flags);
1511
1512 if (voices[voice].mode & WAVE_ENVELOPES)
1513 {
1514 compute_volume(voice, volume);
1515 init_envelope(voice);
1516 }
1517 else
1518 {
1519 compute_and_set_volume(voice, volume, 0);
1520 }
1521
1522 save_flags(flags);
1523 cli();
1524 gus_select_voice(voice);
1525
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 */
1529 else
1530 gus_write_addr(0x0a, sample_ptrs[sample] + voices[voice].offset_pending, 0, is16bits); /* Sample start=begin */
1531
1532 if (samples[sample].mode & WAVE_LOOPING)
1533 {
1534 mode |= 0x08;
1535
1536 if (samples[sample].mode & WAVE_BIDIR_LOOP)
1537 mode |= 0x10;
1538
1539 if (samples[sample].mode & WAVE_LOOP_BACK)
1540 {
1541 gus_write_addr(0x0a, sample_ptrs[sample] + samples[sample].loop_end -
1542 voices[voice].offset_pending,
1543 (samples[sample].fractions >> 4) & 0x0f, is16bits);
1544 mode |= 0x40;
1545 }
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 */
1550 }
1551 else
1552 {
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 */
1559 }
1560 gus_voice_freq(freq);
1561 gus_voice_balance(pan);
1562 gus_voice_on(mode);
1563 restore_flags(flags);
1564
1565 return 0;
1566 }
1567
1568 /*
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
1571 * ramping.
1572 */
1573
1574 static int guswave_start_note(int dev, int voice, int note_num, int volume)
1575 {
1576 long int flags;
1577 int mode;
1578 int ret_val = 0;
1579
1580 save_flags(flags);
1581 cli();
1582 if (note_num == 255)
1583 {
1584 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1585 {
1586 voices[voice].volume_pending = volume;
1587 }
1588 else
1589 {
1590 ret_val = guswave_start_note2(dev, voice, note_num, volume);
1591 }
1592 }
1593 else
1594 {
1595 gus_select_voice(voice);
1596 mode = gus_read8(0x00);
1597 if (mode & 0x20)
1598 gus_write8(0x00, mode & 0xdf); /* No interrupt! */
1599
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;
1604
1605 if (voices[voice].sample_pending >= 0)
1606 {
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;
1610 save_flags(flags);
1611 cli();
1612 gus_select_voice(voice); /* Reselect the voice (just to be sure) */
1613 }
1614 if ((mode & 0x01) || (int) ((gus_read16(0x09) >> 4) < (unsigned) 2065))
1615 {
1616 ret_val = guswave_start_note2(dev, voice, note_num, volume);
1617 }
1618 else
1619 {
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;
1624
1625 gus_rampoff();
1626 gus_ramp_range(2000, 4065);
1627 gus_ramp_rate(0, 63); /* Fastest possible rate */
1628 gus_rampon(0x20 | 0x40); /* Ramp down, once, irq */
1629 }
1630 }
1631 restore_flags(flags);
1632 return ret_val;
1633 }
1634
1635 static void guswave_reset(int dev)
1636 {
1637 int i;
1638
1639 for (i = 0; i < 32; i++)
1640 {
1641 gus_voice_init(i);
1642 gus_voice_init2(i);
1643 }
1644 }
1645
1646 static int guswave_open(int dev, int mode)
1647 {
1648 int err;
1649
1650 if (gus_busy)
1651 return -EBUSY;
1652
1653 voice_alloc->timestamp = 0;
1654
1655 if (gus_no_wave_dma) {
1656 gus_no_dma = 1;
1657 } else {
1658 if ((err = DMAbuf_open_dma(gus_devnum)) < 0)
1659 {
1660 /* printk( "GUS: Loading samples without DMA\n"); */
1661 gus_no_dma = 1; /* Upload samples using PIO */
1662 }
1663 else
1664 gus_no_dma = 0;
1665 }
1666
1667 init_waitqueue_head(&dram_sleeper);
1668 gus_busy = 1;
1669 active_device = GUS_DEV_WAVE;
1670
1671 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
1672 gus_initialize();
1673 gus_reset();
1674 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
1675
1676 return 0;
1677 }
1678
1679 static void guswave_close(int dev)
1680 {
1681 gus_busy = 0;
1682 active_device = 0;
1683 gus_reset();
1684
1685 if (!gus_no_dma)
1686 DMAbuf_close_dma(gus_devnum);
1687 }
1688
1689 static int guswave_load_patch(int dev, int format, const char *addr,
1690 int offs, int count, int pmgr_flag)
1691 {
1692 struct patch_info patch;
1693 int instr;
1694 long sizeof_patch;
1695
1696 unsigned long blk_sz, blk_end, left, src_offs, target;
1697
1698 sizeof_patch = (long) &patch.data[0] - (long) &patch; /* Header size */
1699
1700 if (format != GUS_PATCH)
1701 {
1702 /* printk("GUS Error: Invalid patch format (key) 0x%x\n", format);*/
1703 return -EINVAL;
1704 }
1705 if (count < sizeof_patch)
1706 {
1707 /* printk("GUS Error: Patch header too short\n");*/
1708 return -EINVAL;
1709 }
1710 count -= sizeof_patch;
1711
1712 if (free_sample >= MAX_SAMPLE)
1713 {
1714 /* printk("GUS: Sample table full\n");*/
1715 return -ENOSPC;
1716 }
1717 /*
1718 * Copy the header from user space but ignore the first bytes which have
1719 * been transferred already.
1720 */
1721
1722 copy_from_user(&((char *) &patch)[offs], &(addr)[offs], sizeof_patch - offs);
1723
1724 if (patch.mode & WAVE_ROM)
1725 return -EINVAL;
1726 if (gus_mem_size == 0)
1727 return -ENOSPC;
1728
1729 instr = patch.instr_no;
1730
1731 if (instr < 0 || instr > MAX_PATCH)
1732 {
1733 /* printk(KERN_ERR "GUS: Invalid patch number %d\n", instr);*/
1734 return -EINVAL;
1735 }
1736 if (count < patch.len)
1737 {
1738 /* printk(KERN_ERR "GUS Warning: Patch record too short (%d<%d)\n", count, (int) patch.len);*/
1739 patch.len = count;
1740 }
1741 if (patch.len <= 0 || patch.len > gus_mem_size)
1742 {
1743 /* printk(KERN_ERR "GUS: Invalid sample length %d\n", (int) patch.len);*/
1744 return -EINVAL;
1745 }
1746 if (patch.mode & WAVE_LOOPING)
1747 {
1748 if (patch.loop_start < 0 || patch.loop_start >= patch.len)
1749 {
1750 /* printk(KERN_ERR "GUS: Invalid loop start\n");*/
1751 return -EINVAL;
1752 }
1753 if (patch.loop_end < patch.loop_start || patch.loop_end > patch.len)
1754 {
1755 /* printk(KERN_ERR "GUS: Invalid loop end\n");*/
1756 return -EINVAL;
1757 }
1758 }
1759 free_mem_ptr = (free_mem_ptr + 31) & ~31; /* 32 byte alignment */
1760
1761 if (patch.mode & WAVE_16_BITS)
1762 {
1763 /*
1764 * 16 bit samples must fit one 256k bank.
1765 */
1766 if (patch.len >= GUS_BANK_SIZE)
1767 {
1768 /* printk("GUS: Sample (16 bit) too long %d\n", (int) patch.len);*/
1769 return -ENOSPC;
1770 }
1771 if ((free_mem_ptr / GUS_BANK_SIZE) !=
1772 ((free_mem_ptr + patch.len) / GUS_BANK_SIZE))
1773 {
1774 unsigned long tmp_mem =
1775 /* Align to 256K */
1776 ((free_mem_ptr / GUS_BANK_SIZE) + 1) * GUS_BANK_SIZE;
1777
1778 if ((tmp_mem + patch.len) > gus_mem_size)
1779 return -ENOSPC;
1780
1781 free_mem_ptr = tmp_mem; /* This leaves unusable memory */
1782 }
1783 }
1784 if ((free_mem_ptr + patch.len) > gus_mem_size)
1785 return -ENOSPC;
1786
1787 sample_ptrs[free_sample] = free_mem_ptr;
1788
1789 /*
1790 * Tremolo is not possible with envelopes
1791 */
1792
1793 if (patch.mode & WAVE_ENVELOPES)
1794 patch.mode &= ~WAVE_TREMOLO;
1795
1796 if (!(patch.mode & WAVE_FRACTIONS))
1797 {
1798 patch.fractions = 0;
1799 }
1800 memcpy((char *) &samples[free_sample], &patch, sizeof_patch);
1801
1802 /*
1803 * Link this_one sample to the list of samples for patch 'instr'.
1804 */
1805
1806 samples[free_sample].key = patch_table[instr];
1807 patch_table[instr] = free_sample;
1808
1809 /*
1810 * Use DMA to transfer the wave data to the DRAM
1811 */
1812
1813 left = patch.len;
1814 src_offs = 0;
1815 target = free_mem_ptr;
1816
1817 while (left) /* Not completely transferred yet */
1818 {
1819 blk_sz = audio_devs[gus_devnum]->dmap_out->bytes_in_use;
1820 if (blk_sz > left)
1821 blk_sz = left;
1822
1823 /*
1824 * DMA cannot cross bank (256k) boundaries. Check for that.
1825 */
1826
1827 blk_end = target + blk_sz;
1828
1829 if ((target / GUS_BANK_SIZE) != (blk_end / GUS_BANK_SIZE))
1830 {
1831 /* Split the block */
1832 blk_end &= ~(GUS_BANK_SIZE - 1);
1833 blk_sz = blk_end - target;
1834 }
1835 if (gus_no_dma)
1836 {
1837 /*
1838 * For some reason the DMA is not possible. We have to use PIO.
1839 */
1840 long i;
1841 unsigned char data;
1842
1843 for (i = 0; i < blk_sz; i++)
1844 {
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);
1850 }
1851 }
1852 else
1853 {
1854 unsigned long address, hold_address;
1855 unsigned char dma_command;
1856 unsigned long flags;
1857
1858 if (audio_devs[gus_devnum]->dmap_out->raw_buf == NULL)
1859 {
1860 printk(KERN_ERR "GUS: DMA buffer == NULL\n");
1861 return -ENOSPC;
1862 }
1863 /*
1864 * OK, move now. First in and then out.
1865 */
1866
1867 copy_from_user(audio_devs[gus_devnum]->dmap_out->raw_buf, &(addr)[sizeof_patch + src_offs], blk_sz);
1868
1869 save_flags(flags);
1870 cli();
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);
1875
1876 /*
1877 * Set the DRAM address for the wave data
1878 */
1879
1880 if (iw_mode)
1881 {
1882 /* Different address translation in enhanced mode */
1883
1884 unsigned char hi;
1885
1886 if (gus_dma > 4)
1887 address = target >> 1; /* Convert to 16 bit word address */
1888 else
1889 address = target;
1890
1891 hi = (unsigned char) ((address >> 16) & 0xf0);
1892 hi += (unsigned char) (address & 0x0f);
1893
1894 gus_write16(0x42, (address >> 4) & 0xffff); /* DMA address (low) */
1895 gus_write8(0x50, hi);
1896 }
1897 else
1898 {
1899 address = target;
1900 if (audio_devs[gus_devnum]->dmap_out->dma > 3)
1901 {
1902 hold_address = address;
1903 address = address >> 1;
1904 address &= 0x0001ffffL;
1905 address |= (hold_address & 0x000c0000L);
1906 }
1907 gus_write16(0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
1908 }
1909
1910 /*
1911 * Start the DMA transfer
1912 */
1913
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_ */
1921
1922 gus_write8(0x41, dma_command); /* Lets go luteet (=bugs) */
1923
1924 /*
1925 * Sleep here until the DRAM DMA done interrupt is served
1926 */
1927 active_device = GUS_DEV_WAVE;
1928
1929 if (!interruptible_sleep_on_timeout(&dram_sleeper, HZ))
1930 printk("GUS: DMA Transfer timed out\n");
1931 restore_flags(flags);
1932 }
1933
1934 /*
1935 * Now the next part
1936 */
1937
1938 left -= blk_sz;
1939 src_offs += blk_sz;
1940 target += blk_sz;
1941
1942 gus_write8(0x41, 0); /* Stop DMA */
1943 }
1944
1945 free_mem_ptr += patch.len;
1946 free_sample++;
1947 return 0;
1948 }
1949
1950 static void guswave_hw_control(int dev, unsigned char *event_rec)
1951 {
1952 int voice, cmd;
1953 unsigned short p1, p2;
1954 unsigned int plong;
1955 unsigned flags;
1956
1957 cmd = event_rec[2];
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];
1962
1963 if ((voices[voice].volume_irq_mode == VMODE_START_NOTE) &&
1964 (cmd != _GUS_VOICESAMPLE) && (cmd != _GUS_VOICE_POS))
1965 do_volume_irq(voice);
1966
1967 switch (cmd)
1968 {
1969 case _GUS_NUMVOICES:
1970 save_flags(flags);
1971 cli();
1972 gus_select_voice(voice);
1973 gus_select_max_voices(p1);
1974 restore_flags(flags);
1975 break;
1976
1977 case _GUS_VOICESAMPLE:
1978 guswave_set_instr(dev, voice, p1);
1979 break;
1980
1981 case _GUS_VOICEON:
1982 save_flags(flags);
1983 cli();
1984 gus_select_voice(voice);
1985 p1 &= ~0x20; /* Don't allow interrupts */
1986 gus_voice_on(p1);
1987 restore_flags(flags);
1988 break;
1989
1990 case _GUS_VOICEOFF:
1991 save_flags(flags);
1992 cli();
1993 gus_select_voice(voice);
1994 gus_voice_off();
1995 restore_flags(flags);
1996 break;
1997
1998 case _GUS_VOICEFADE:
1999 gus_voice_fade(voice);
2000 break;
2001
2002 case _GUS_VOICEMODE:
2003 save_flags(flags);
2004 cli();
2005 gus_select_voice(voice);
2006 p1 &= ~0x20; /* Don't allow interrupts */
2007 gus_voice_mode(p1);
2008 restore_flags(flags);
2009 break;
2010
2011 case _GUS_VOICEBALA:
2012 save_flags(flags);
2013 cli();
2014 gus_select_voice(voice);
2015 gus_voice_balance(p1);
2016 restore_flags(flags);
2017 break;
2018
2019 case _GUS_VOICEFREQ:
2020 save_flags(flags);
2021 cli();
2022 gus_select_voice(voice);
2023 gus_voice_freq(plong);
2024 restore_flags(flags);
2025 break;
2026
2027 case _GUS_VOICEVOL:
2028 save_flags(flags);
2029 cli();
2030 gus_select_voice(voice);
2031 gus_voice_volume(p1);
2032 restore_flags(flags);
2033 break;
2034
2035 case _GUS_VOICEVOL2: /* Just update the software voice level */
2036 voices[voice].initial_volume = voices[voice].current_volume = p1;
2037 break;
2038
2039 case _GUS_RAMPRANGE:
2040 if (voices[voice].mode & WAVE_ENVELOPES)
2041 break; /* NO-NO */
2042 save_flags(flags);
2043 cli();
2044 gus_select_voice(voice);
2045 gus_ramp_range(p1, p2);
2046 restore_flags(flags);
2047 break;
2048
2049 case _GUS_RAMPRATE:
2050 if (voices[voice].mode & WAVE_ENVELOPES)
2051 break; /* NJET-NJET */
2052 save_flags(flags);
2053 cli();
2054 gus_select_voice(voice);
2055 gus_ramp_rate(p1, p2);
2056 restore_flags(flags);
2057 break;
2058
2059 case _GUS_RAMPMODE:
2060 if (voices[voice].mode & WAVE_ENVELOPES)
2061 break; /* NO-NO */
2062 save_flags(flags);
2063 cli();
2064 gus_select_voice(voice);
2065 p1 &= ~0x20; /* Don't allow interrupts */
2066 gus_ramp_mode(p1);
2067 restore_flags(flags);
2068 break;
2069
2070 case _GUS_RAMPON:
2071 if (voices[voice].mode & WAVE_ENVELOPES)
2072 break; /* EI-EI */
2073 save_flags(flags);
2074 cli();
2075 gus_select_voice(voice);
2076 p1 &= ~0x20; /* Don't allow interrupts */
2077 gus_rampon(p1);
2078 restore_flags(flags);
2079 break;
2080
2081 case _GUS_RAMPOFF:
2082 if (voices[voice].mode & WAVE_ENVELOPES)
2083 break; /* NEJ-NEJ */
2084 save_flags(flags);
2085 cli();
2086 gus_select_voice(voice);
2087 gus_rampoff();
2088 restore_flags(flags);
2089 break;
2090
2091 case _GUS_VOLUME_SCALE:
2092 volume_base = p1;
2093 volume_scale = p2;
2094 break;
2095
2096 case _GUS_VOICE_POS:
2097 save_flags(flags);
2098 cli();
2099 gus_select_voice(voice);
2100 gus_set_voice_pos(voice, plong);
2101 restore_flags(flags);
2102 break;
2103
2104 default:
2105 }
2106 }
2107
2108 static int gus_audio_set_speed(int speed)
2109 {
2110 if (speed <= 0)
2111 speed = gus_audio_speed;
2112
2113 if (speed < 4000)
2114 speed = 4000;
2115
2116 if (speed > 44100)
2117 speed = 44100;
2118
2119 gus_audio_speed = speed;
2120
2121 if (only_read_access)
2122 {
2123 /* Compute nearest valid recording speed and return it */
2124
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;
2128 }
2129 return speed;
2130 }
2131
2132 static int gus_audio_set_channels(int channels)
2133 {
2134 if (!channels)
2135 return gus_audio_channels;
2136 if (channels > 2)
2137 channels = 2;
2138 if (channels < 1)
2139 channels = 1;
2140 gus_audio_channels = channels;
2141 return channels;
2142 }
2143
2144 static int gus_audio_set_bits(int bits)
2145 {
2146 if (!bits)
2147 return gus_audio_bits;
2148
2149 if (bits != 8 && bits != 16)
2150 bits = 8;
2151
2152 if (only_8_bits)
2153 bits = 8;
2154
2155 gus_audio_bits = bits;
2156 return bits;
2157 }
2158
2159 static int gus_audio_ioctl(int dev, unsigned int cmd, caddr_t arg)
2160 {
2161 int val;
2162
2163 switch (cmd)
2164 {
2165 case SOUND_PCM_WRITE_RATE:
2166 if (get_user(val, (int *)arg))
2167 return -EFAULT;
2168 val = gus_audio_set_speed(val);
2169 break;
2170
2171 case SOUND_PCM_READ_RATE:
2172 val = gus_audio_speed;
2173 break;
2174
2175 case SNDCTL_DSP_STEREO:
2176 if (get_user(val, (int *)arg))
2177 return -EFAULT;
2178 val = gus_audio_set_channels(val + 1) - 1;
2179 break;
2180
2181 case SOUND_PCM_WRITE_CHANNELS:
2182 if (get_user(val, (int *)arg))
2183 return -EFAULT;
2184 val = gus_audio_set_channels(val);
2185 break;
2186
2187 case SOUND_PCM_READ_CHANNELS:
2188 val = gus_audio_channels;
2189 break;
2190
2191 case SNDCTL_DSP_SETFMT:
2192 if (get_user(val, (int *)arg))
2193 return -EFAULT;
2194 val = gus_audio_set_bits(val);
2195 break;
2196
2197 case SOUND_PCM_READ_BITS:
2198 val = gus_audio_bits;
2199 break;
2200
2201 case SOUND_PCM_WRITE_FILTER: /* NOT POSSIBLE */
2202 case SOUND_PCM_READ_FILTER:
2203 val = -EINVAL;
2204 break;
2205 default:
2206 return -EINVAL;
2207 }
2208 return put_user(val, (int *)arg);
2209 }
2210
2211 static void gus_audio_reset(int dev)
2212 {
2213 if (recording_active)
2214 {
2215 gus_write8(0x49, 0x00); /* Halt recording */
2216 set_input_volumes();
2217 }
2218 }
2219
2220 static int saved_iw_mode; /* A hack hack hack */
2221
2222 static int gus_audio_open(int dev, int mode)
2223 {
2224 if (gus_busy)
2225 return -EBUSY;
2226
2227 if (gus_pnp_flag && mode & OPEN_READ)
2228 {
2229 /* printk(KERN_ERR "GUS: Audio device #%d is playback only.\n", dev);*/
2230 return -EIO;
2231 }
2232 gus_initialize();
2233
2234 gus_busy = 1;
2235 active_device = 0;
2236
2237 saved_iw_mode = iw_mode;
2238 if (iw_mode)
2239 {
2240 /* There are some problems with audio in enhanced mode so disable it */
2241 gus_write8(0x19, gus_read8(0x19) & ~0x01); /* Disable enhanced mode */
2242 iw_mode = 0;
2243 }
2244
2245 gus_reset();
2246 reset_sample_memory();
2247 gus_select_max_voices(14);
2248
2249 pcm_active = 0;
2250 dma_active = 0;
2251 pcm_opened = 1;
2252 if (mode & OPEN_READ)
2253 {
2254 recording_active = 1;
2255 set_input_volumes();
2256 }
2257 only_read_access = !(mode & OPEN_WRITE);
2258 only_8_bits = mode & OPEN_READ;
2259 if (only_8_bits)
2260 audio_devs[dev]->format_mask = AFMT_U8;
2261 else
2262 audio_devs[dev]->format_mask = AFMT_U8 | AFMT_S16_LE;
2263
2264 return 0;
2265 }
2266
2267 static void gus_audio_close(int dev)
2268 {
2269 iw_mode = saved_iw_mode;
2270 gus_reset();
2271 gus_busy = 0;
2272 pcm_opened = 0;
2273 active_device = 0;
2274
2275 if (recording_active)
2276 {
2277 gus_write8(0x49, 0x00); /* Halt recording */
2278 set_input_volumes();
2279 }
2280 recording_active = 0;
2281 }
2282
2283 static void gus_audio_update_volume(void)
2284 {
2285 unsigned long flags;
2286 int voice;
2287
2288 if (pcm_active && pcm_opened)
2289 for (voice = 0; voice < gus_audio_channels; voice++)
2290 {
2291 save_flags(flags);
2292 cli();
2293 gus_select_voice(voice);
2294 gus_rampoff();
2295 gus_voice_volume(1530 + (25 * gus_pcm_volume));
2296 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume));
2297 restore_flags(flags);
2298 }
2299 }
2300
2301 static void play_next_pcm_block(void)
2302 {
2303 unsigned long flags;
2304 int speed = gus_audio_speed;
2305 int this_one, is16bits, chn;
2306 unsigned long dram_loc;
2307 unsigned char mode[2], ramp_mode[2];
2308
2309 if (!pcm_qlen)
2310 return;
2311
2312 this_one = pcm_head;
2313
2314 for (chn = 0; chn < gus_audio_channels; chn++)
2315 {
2316 mode[chn] = 0x00;
2317 ramp_mode[chn] = 0x03; /* Ramping and rollover off */
2318
2319 if (chn == 0)
2320 {
2321 mode[chn] |= 0x20; /* Loop IRQ */
2322 voices[chn].loop_irq_mode = LMODE_PCM;
2323 }
2324 if (gus_audio_bits != 8)
2325 {
2326 is16bits = 1;
2327 mode[chn] |= 0x04; /* 16 bit data */
2328 }
2329 else
2330 is16bits = 0;
2331
2332 dram_loc = this_one * pcm_bsize;
2333 dram_loc += chn * pcm_banksize;
2334
2335 if (this_one == (pcm_nblk - 1)) /* Last fragment of the DRAM buffer */
2336 {
2337 mode[chn] |= 0x08; /* Enable loop */
2338 ramp_mode[chn] = 0x03; /* Disable rollover bit */
2339 }
2340 else
2341 {
2342 if (chn == 0)
2343 ramp_mode[chn] = 0x04; /* Enable rollover bit */
2344 }
2345 save_flags(flags);
2346 cli();
2347 gus_select_voice(chn);
2348 gus_voice_freq(speed);
2349
2350 if (gus_audio_channels == 1)
2351 gus_voice_balance(7); /* mono */
2352 else if (chn == 0)
2353 gus_voice_balance(0); /* left */
2354 else
2355 gus_voice_balance(15); /* right */
2356
2357 if (!pcm_active) /* Playback not already active */
2358 {
2359 /*
2360 * The playback was not started yet (or there has been a pause).
2361 * Start the voice (again) and ask for a rollover irq at the end of
2362 * this_one block. If this_one one is last of the buffers, use just
2363 * the normal loop with irq.
2364 */
2365
2366 gus_voice_off();
2367 gus_rampoff();
2368 gus_voice_volume(1530 + (25 * gus_pcm_volume));
2369 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume));
2370
2371 gus_write_addr(0x0a, chn * pcm_banksize, 0, is16bits); /* Starting position */
2372 gus_write_addr(0x02, chn * pcm_banksize, 0, is16bits); /* Loop start */
2373
2374 if (chn != 0)
2375 gus_write_addr(0x04, pcm_banksize + (pcm_bsize * pcm_nblk) - 1,
2376 0, is16bits); /* Loop end location */
2377 }
2378 if (chn == 0)
2379 gus_write_addr(0x04, dram_loc + pcm_bsize - 1,
2380 0, is16bits); /* Loop end location */
2381 else
2382 mode[chn] |= 0x08; /* Enable looping */
2383 restore_flags(flags);
2384 }
2385 for (chn = 0; chn < gus_audio_channels; chn++)
2386 {
2387 save_flags(flags);
2388 cli();
2389 gus_select_voice(chn);
2390 gus_write8(0x0d, ramp_mode[chn]);
2391 if (iw_mode)
2392 gus_write8(0x15, 0x00); /* Reset voice deactivate bit of SMSI */
2393 gus_voice_on(mode[chn]);
2394 restore_flags(flags);
2395 }
2396 pcm_active = 1;
2397 }
2398
2399 static void gus_transfer_output_block(int dev, unsigned long buf,
2400 int total_count, int intrflag, int chn)
2401 {
2402 /*
2403 * This routine transfers one block of audio data to the DRAM. In mono mode
2404 * it's called just once. When in stereo mode, this_one routine is called
2405 * once for both channels.
2406 *
2407 * The left/mono channel data is transferred to the beginning of dram and the
2408 * right data to the area pointed by gus_page_size.
2409 */
2410
2411 int this_one, count;
2412 unsigned long flags;
2413 unsigned char dma_command;
2414 unsigned long address, hold_address;
2415
2416 save_flags(flags);
2417 cli();
2418
2419 count = total_count / gus_audio_channels;
2420
2421 if (chn == 0)
2422 {
2423 if (pcm_qlen >= pcm_nblk)
2424 printk(KERN_WARNING "GUS Warning: PCM buffers out of sync\n");
2425
2426 this_one = pcm_current_block = pcm_tail;
2427 pcm_qlen++;
2428 pcm_tail = (pcm_tail + 1) % pcm_nblk;
2429 pcm_datasize[this_one] = count;
2430 }
2431 else
2432 this_one = pcm_current_block;
2433
2434 gus_write8(0x41, 0); /* Disable GF1 DMA */
2435 DMAbuf_start_dma(dev, buf + (chn * count), count, DMA_MODE_WRITE);
2436
2437 address = this_one * pcm_bsize;
2438 address += chn * pcm_banksize;
2439
2440 if (audio_devs[dev]->dmap_out->dma > 3)
2441 {
2442 hold_address = address;
2443 address = address >> 1;
2444 address &= 0x0001ffffL;
2445 address |= (hold_address & 0x000c0000L);
2446 }
2447 gus_write16(0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
2448
2449 dma_command = 0x21; /* IRQ enable, DMA start */
2450
2451 if (gus_audio_bits != 8)
2452 dma_command |= 0x40; /* 16 bit _DATA_ */
2453 else
2454 dma_command |= 0x80; /* Invert MSB */
2455
2456 if (audio_devs[dev]->dmap_out->dma > 3)
2457 dma_command |= 0x04; /* 16 bit DMA channel */
2458
2459 gus_write8(0x41, dma_command); /* Kick start */
2460
2461 if (chn == (gus_audio_channels - 1)) /* Last channel */
2462 {
2463 /*
2464 * Last (right or mono) channel data
2465 */
2466 dma_active = 1; /* DMA started. There is a unacknowledged buffer */
2467 active_device = GUS_DEV_PCM_DONE;
2468 if (!pcm_active && (pcm_qlen > 1 || count < pcm_bsize))
2469 {
2470 play_next_pcm_block();
2471 }
2472 }
2473 else
2474 {
2475 /*
2476 * Left channel data. The right channel
2477 * is transferred after DMA interrupt
2478 */
2479 active_device = GUS_DEV_PCM_CONTINUE;
2480 }
2481
2482 restore_flags(flags);
2483 }
2484
2485 static void gus_uninterleave8(char *buf, int l)
2486 {
2487 /* This routine uninterleaves 8 bit stereo output (LRLRLR->LLLRRR) */
2488 int i, p = 0, halfsize = l / 2;
2489 char *buf2 = buf + halfsize, *src = bounce_buf;
2490
2491 memcpy(bounce_buf, buf, l);
2492
2493 for (i = 0; i < halfsize; i++)
2494 {
2495 buf[i] = src[p++]; /* Left channel */
2496 buf2[i] = src[p++]; /* Right channel */
2497 }
2498 }
2499
2500 static void gus_uninterleave16(short *buf, int l)
2501 {
2502 /* This routine uninterleaves 16 bit stereo output (LRLRLR->LLLRRR) */
2503 int i, p = 0, halfsize = l / 2;
2504 short *buf2 = buf + halfsize, *src = (short *) bounce_buf;
2505
2506 memcpy(bounce_buf, (char *) buf, l * 2);
2507
2508 for (i = 0; i < halfsize; i++)
2509 {
2510 buf[i] = src[p++]; /* Left channel */
2511 buf2[i] = src[p++]; /* Right channel */
2512 }
2513 }
2514
2515 static void gus_audio_output_block(int dev, unsigned long buf, int total_count,
2516 int intrflag)
2517 {
2518 struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
2519
2520 dmap->flags |= DMA_NODMA | DMA_NOTIMEOUT;
2521
2522 pcm_current_buf = buf;
2523 pcm_current_count = total_count;
2524 pcm_current_intrflag = intrflag;
2525 pcm_current_dev = dev;
2526 if (gus_audio_channels == 2)
2527 {
2528 char *b = dmap->raw_buf + (buf - dmap->raw_buf_phys);
2529
2530 if (gus_audio_bits == 8)
2531 gus_uninterleave8(b, total_count);
2532 else
2533 gus_uninterleave16((short *) b, total_count / 2);
2534 }
2535 gus_transfer_output_block(dev, buf, total_count, intrflag, 0);
2536 }
2537
2538 static void gus_audio_start_input(int dev, unsigned long buf, int count,
2539 int intrflag)
2540 {
2541 unsigned long flags;
2542 unsigned char mode;
2543
2544 save_flags(flags);
2545 cli();
2546
2547 DMAbuf_start_dma(dev, buf, count, DMA_MODE_READ);
2548 mode = 0xa0; /* DMA IRQ enabled, invert MSB */
2549
2550 if (audio_devs[dev]->dmap_in->dma > 3)
2551 mode |= 0x04; /* 16 bit DMA channel */
2552 if (gus_audio_channels > 1)
2553 mode |= 0x02; /* Stereo */
2554 mode |= 0x01; /* DMA enable */
2555
2556 gus_write8(0x49, mode);
2557 restore_flags(flags);
2558 }
2559
2560 static int gus_audio_prepare_for_input(int dev, int bsize, int bcount)
2561 {
2562 unsigned int rate;
2563
2564 gus_audio_bsize = bsize;
2565 audio_devs[dev]->dmap_in->flags |= DMA_NODMA;
2566 rate = (((9878400 + gus_audio_speed / 2) / (gus_audio_speed + 2)) + 8) / 16;
2567
2568 gus_write8(0x48, rate & 0xff); /* Set sampling rate */
2569
2570 if (gus_audio_bits != 8)
2571 {
2572 /* printk("GUS Error: 16 bit recording not supported\n");*/
2573 return -EINVAL;
2574 }
2575 return 0;
2576 }
2577
2578 static int gus_audio_prepare_for_output(int dev, int bsize, int bcount)
2579 {
2580 int i;
2581
2582 long mem_ptr, mem_size;
2583
2584 audio_devs[dev]->dmap_out->flags |= DMA_NODMA | DMA_NOTIMEOUT;
2585 mem_ptr = 0;
2586 mem_size = gus_mem_size / gus_audio_channels;
2587
2588 if (mem_size > (256 * 1024))
2589 mem_size = 256 * 1024;
2590
2591 pcm_bsize = bsize / gus_audio_channels;
2592 pcm_head = pcm_tail = pcm_qlen = 0;
2593
2594 pcm_nblk = 2; /* MAX_PCM_BUFFERS; */
2595 if ((pcm_bsize * pcm_nblk) > mem_size)
2596 pcm_nblk = mem_size / pcm_bsize;
2597
2598 for (i = 0; i < pcm_nblk; i++)
2599 pcm_datasize[i] = 0;
2600
2601 pcm_banksize = pcm_nblk * pcm_bsize;
2602
2603 if (gus_audio_bits != 8 && pcm_banksize == (256 * 1024))
2604 pcm_nblk--;
2605 gus_write8(0x41, 0); /* Disable GF1 DMA */
2606 return 0;
2607 }
2608
2609 static int gus_local_qlen(int dev)
2610 {
2611 return pcm_qlen;
2612 }
2613
2614
2615 static struct audio_driver gus_audio_driver =
2616 {
2617 owner: THIS_MODULE,
2618 open: gus_audio_open,
2619 close: gus_audio_close,
2620 output_block: gus_audio_output_block,
2621 start_input: gus_audio_start_input,
2622 ioctl: gus_audio_ioctl,
2623 prepare_for_input: gus_audio_prepare_for_input,
2624 prepare_for_output: gus_audio_prepare_for_output,
2625 halt_io: gus_audio_reset,
2626 local_qlen: gus_local_qlen,
2627 };
2628
2629 static void guswave_setup_voice(int dev, int voice, int chn)
2630 {
2631 struct channel_info *info = &synth_devs[dev]->chn_info[chn];
2632
2633 guswave_set_instr(dev, voice, info->pgm_num);
2634 voices[voice].expression_vol = info->controllers[CTL_EXPRESSION]; /* Just MSB */
2635 voices[voice].main_vol = (info->controllers[CTL_MAIN_VOLUME] * 100) / (unsigned) 128;
2636 voices[voice].panning = (info->controllers[CTL_PAN] * 2) - 128;
2637 voices[voice].bender = 0;
2638 voices[voice].bender_range = info->bender_range;
2639
2640 if (chn == 9)
2641 voices[voice].fixed_pitch = 1;
2642 }
2643
2644 static void guswave_bender(int dev, int voice, int value)
2645 {
2646 int freq;
2647 unsigned long flags;
2648
2649 voices[voice].bender = value - 8192;
2650 freq = compute_finetune(voices[voice].orig_freq, value - 8192, voices[voice].bender_range, 0);
2651 voices[voice].current_freq = freq;
2652
2653 save_flags(flags);
2654 cli();
2655 gus_select_voice(voice);
2656 gus_voice_freq(freq);
2657 restore_flags(flags);
2658 }
2659
2660 static int guswave_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc)
2661 {
2662 int i, p, best = -1, best_time = 0x7fffffff;
2663
2664 p = alloc->ptr;
2665 /*
2666 * First look for a completely stopped voice
2667 */
2668
2669 for (i = 0; i < alloc->max_voice; i++)
2670 {
2671 if (alloc->map[p] == 0)
2672 {
2673 alloc->ptr = p;
2674 return p;
2675 }
2676 if (alloc->alloc_times[p] < best_time)
2677 {
2678 best = p;
2679 best_time = alloc->alloc_times[p];
2680 }
2681 p = (p + 1) % alloc->max_voice;
2682 }
2683
2684 /*
2685 * Then look for a releasing voice
2686 */
2687
2688 for (i = 0; i < alloc->max_voice; i++)
2689 {
2690 if (alloc->map[p] == 0xffff)
2691 {
2692 alloc->ptr = p;
2693 return p;
2694 }
2695 p = (p + 1) % alloc->max_voice;
2696 }
2697 if (best >= 0)
2698 p = best;
2699
2700 alloc->ptr = p;
2701 return p;
2702 }
2703
2704 static struct synth_operations guswave_operations =
2705 {
2706 owner: THIS_MODULE,
2707 id: "GUS",
2708 info: &gus_info,
2709 midi_dev: 0,
2710 synth_type: SYNTH_TYPE_SAMPLE,
2711 synth_subtype: SAMPLE_TYPE_GUS,
2712 open: guswave_open,
2713 close: guswave_close,
2714 ioctl: guswave_ioctl,
2715 kill_note: guswave_kill_note,
2716 start_note: guswave_start_note,
2717 set_instr: guswave_set_instr,
2718 reset: guswave_reset,
2719 hw_control: guswave_hw_control,
2720 load_patch: guswave_load_patch,
2721 aftertouch: guswave_aftertouch,
2722 controller: guswave_controller,
2723 panning: guswave_panning,
2724 volume_method: guswave_volume_method,
2725 bender: guswave_bender,
2726 alloc_voice: guswave_alloc,
2727 setup_voice: guswave_setup_voice
2728 };
2729
2730 static void set_input_volumes(void)
2731 {
2732 unsigned long flags;
2733 unsigned char mask = 0xff & ~0x06; /* Just line out enabled */
2734
2735 if (have_gus_max) /* Don't disturb GUS MAX */
2736 return;
2737
2738 save_flags(flags);
2739 cli();
2740
2741 /*
2742 * Enable channels having vol > 10%
2743 * Note! bit 0x01 means the line in DISABLED while 0x04 means
2744 * the mic in ENABLED.
2745 */
2746 if (gus_line_vol > 10)
2747 mask &= ~0x01;
2748 if (gus_mic_vol > 10)
2749 mask |= 0x04;
2750
2751 if (recording_active)
2752 {
2753 /*
2754 * Disable channel, if not selected for recording
2755 */
2756 if (!(gus_recmask & SOUND_MASK_LINE))
2757 mask |= 0x01;
2758 if (!(gus_recmask & SOUND_MASK_MIC))
2759 mask &= ~0x04;
2760 }
2761 mix_image &= ~0x07;
2762 mix_image |= mask & 0x07;
2763 outb((mix_image), u_Mixer);
2764
2765 restore_flags(flags);
2766 }
2767
2768 #define MIX_DEVS (SOUND_MASK_MIC|SOUND_MASK_LINE| \
2769 SOUND_MASK_SYNTH|SOUND_MASK_PCM)
2770
2771 int gus_default_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg)
2772 {
2773 int vol, val;
2774
2775 if (((cmd >> 8) & 0xff) != 'M')
2776 return -EINVAL;
2777
2778 if (!access_ok(VERIFY_WRITE, (int *)arg, sizeof(int)))
2779 return -EFAULT;
2780
2781 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
2782 {
2783 if (__get_user(val, (int *) arg))
2784 return -EFAULT;
2785
2786 switch (cmd & 0xff)
2787 {
2788 case SOUND_MIXER_RECSRC:
2789 gus_recmask = val & MIX_DEVS;
2790 if (!(gus_recmask & (SOUND_MASK_MIC | SOUND_MASK_LINE)))
2791 gus_recmask = SOUND_MASK_MIC;
2792 /* Note! Input volumes are updated during next open for recording */
2793 val = gus_recmask;
2794 break;
2795
2796 case SOUND_MIXER_MIC:
2797 vol = val & 0xff;
2798 if (vol < 0)
2799 vol = 0;
2800 if (vol > 100)
2801 vol = 100;
2802 gus_mic_vol = vol;
2803 set_input_volumes();
2804 val = vol | (vol << 8);
2805 break;
2806
2807 case SOUND_MIXER_LINE:
2808 vol = val & 0xff;
2809 if (vol < 0)
2810 vol = 0;
2811 if (vol > 100)
2812 vol = 100;
2813 gus_line_vol = vol;
2814 set_input_volumes();
2815 val = vol | (vol << 8);
2816 break;
2817
2818 case SOUND_MIXER_PCM:
2819 gus_pcm_volume = val & 0xff;
2820 if (gus_pcm_volume < 0)
2821 gus_pcm_volume = 0;
2822 if (gus_pcm_volume > 100)
2823 gus_pcm_volume = 100;
2824 gus_audio_update_volume();
2825 val = gus_pcm_volume | (gus_pcm_volume << 8);
2826 break;
2827
2828 case SOUND_MIXER_SYNTH:
2829 gus_wave_volume = val & 0xff;
2830 if (gus_wave_volume < 0)
2831 gus_wave_volume = 0;
2832 if (gus_wave_volume > 100)
2833 gus_wave_volume = 100;
2834 if (active_device == GUS_DEV_WAVE)
2835 {
2836 int voice;
2837 for (voice = 0; voice < nr_voices; voice++)
2838 dynamic_volume_change(voice); /* Apply the new vol */
2839 }
2840 val = gus_wave_volume | (gus_wave_volume << 8);
2841 break;
2842
2843 default:
2844 return -EINVAL;
2845 }
2846 }
2847 else
2848 {
2849 switch (cmd & 0xff)
2850 {
2851 /*
2852 * Return parameters
2853 */
2854 case SOUND_MIXER_RECSRC:
2855 val = gus_recmask;
2856 break;
2857
2858 case SOUND_MIXER_DEVMASK:
2859 val = MIX_DEVS;
2860 break;
2861
2862 case SOUND_MIXER_STEREODEVS:
2863 val = 0;
2864 break;
2865
2866 case SOUND_MIXER_RECMASK:
2867 val = SOUND_MASK_MIC | SOUND_MASK_LINE;
2868 break;
2869
2870 case SOUND_MIXER_CAPS:
2871 val = 0;
2872 break;
2873
2874 case SOUND_MIXER_MIC:
2875 val = gus_mic_vol | (gus_mic_vol << 8);
2876 break;
2877
2878 case SOUND_MIXER_LINE:
2879 val = gus_line_vol | (gus_line_vol << 8);
2880 break;
2881
2882 case SOUND_MIXER_PCM:
2883 val = gus_pcm_volume | (gus_pcm_volume << 8);
2884 break;
2885
2886 case SOUND_MIXER_SYNTH:
2887 val = gus_wave_volume | (gus_wave_volume << 8);
2888 break;
2889
2890 default:
2891 return -EINVAL;
2892 }
2893 }
2894 return __put_user(val, (int *)arg);
2895 }
2896
2897 static struct mixer_operations gus_mixer_operations =
2898 {
2899 owner: THIS_MODULE,
2900 id: "GUS",
2901 name: "Gravis Ultrasound",
2902 ioctl: gus_default_mixer_ioctl
2903 };
2904
2905 static int __init gus_default_mixer_init(void)
2906 {
2907 int n;
2908
2909 if ((n = sound_alloc_mixerdev()) != -1)
2910 {
2911 /*
2912 * Don't install if there is another
2913 * mixer
2914 */
2915 mixer_devs[n] = &gus_mixer_operations;
2916 }
2917 if (have_gus_max)
2918 {
2919 /*
2920 * Enable all mixer channels on the GF1 side. Otherwise recording will
2921 * not be possible using GUS MAX.
2922 */
2923 mix_image &= ~0x07;
2924 mix_image |= 0x04; /* All channels enabled */
2925 outb((mix_image), u_Mixer);
2926 }
2927 return n;
2928 }
2929
2930 void __init gus_wave_init(struct address_info *hw_config)
2931 {
2932 unsigned long flags;
2933 unsigned char val;
2934 char *model_num = "2.4";
2935 char tmp[64], tmp2[64];
2936 int gus_type = 0x24; /* 2.4 */
2937
2938 int irq = hw_config->irq, dma = hw_config->dma, dma2 = hw_config->dma2;
2939 int sdev;
2940
2941 hw_config->slots[0] = -1; /* No wave */
2942 hw_config->slots[1] = -1; /* No ad1848 */
2943 hw_config->slots[4] = -1; /* No audio */
2944 hw_config->slots[5] = -1; /* No mixer */
2945
2946 if (!gus_pnp_flag)
2947 {
2948 if (irq < 0 || irq > 15)
2949 {
2950 printk(KERN_ERR "ERROR! Invalid IRQ#%d. GUS Disabled", irq);
2951 return;
2952 }
2953 }
2954
2955 if (dma < 0 || dma > 7 || dma == 4)
2956 {
2957 printk(KERN_ERR "ERROR! Invalid DMA#%d. GUS Disabled", dma);
2958 return;
2959 }
2960 gus_irq = irq;
2961 gus_dma = dma;
2962 gus_dma2 = dma2;
2963 gus_hw_config = hw_config;
2964
2965 if (gus_dma2 == -1)
2966 gus_dma2 = dma;
2967
2968 /*
2969 * Try to identify the GUS model.
2970 *
2971 * Versions < 3.6 don't have the digital ASIC. Try to probe it first.
2972 */
2973
2974 save_flags(flags);
2975 cli();
2976 outb((0x20), gus_base + 0x0f);
2977 val = inb(gus_base + 0x0f);
2978 restore_flags(flags);
2979
2980 if (gus_pnp_flag || (val != 0xff && (val & 0x06))) /* Should be 0x02?? */
2981 {
2982 int ad_flags = 0;
2983
2984 if (gus_pnp_flag)
2985 ad_flags = 0x12345678; /* Interwave "magic" */
2986 /*
2987 * It has the digital ASIC so the card is at least v3.4.
2988 * Next try to detect the true model.
2989 */
2990
2991 if (gus_pnp_flag) /* Hack hack hack */
2992 val = 10;
2993 else
2994 val = inb(u_MixSelect);
2995
2996 /*
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.
3000 *
3001 */
3002
3003 if (val == 255 || val < 5)
3004 {
3005 model_num = "3.4";
3006 gus_type = 0x34;
3007 }
3008 else if (val < 10)
3009 {
3010 model_num = "3.7";
3011 gus_type = 0x37;
3012 mixer_type = ICS2101;
3013 request_region(u_MixSelect, 1, "GUS mixer");
3014 }
3015 else
3016 {
3017 model_num = "MAX";
3018 gus_type = 0x40;
3019 mixer_type = CS4231;
3020 #ifdef CONFIG_SOUND_GUSMAX
3021 {
3022 unsigned char max_config = 0x40; /* Codec enable */
3023
3024 if (gus_dma2 == -1)
3025 gus_dma2 = gus_dma;
3026
3027 if (gus_dma > 3)
3028 max_config |= 0x10; /* 16 bit capture DMA */
3029
3030 if (gus_dma2 > 3)
3031 max_config |= 0x20; /* 16 bit playback DMA */
3032
3033 max_config |= (gus_base >> 4) & 0x0f; /* Extract the X from 2X0 */
3034
3035 outb((max_config), gus_base + 0x106); /* UltraMax control */
3036 }
3037
3038 if (ad1848_detect(gus_base + 0x10c, &ad_flags, hw_config->osp))
3039 {
3040 char *name = "GUS MAX";
3041 int old_num_mixers = num_mixers;
3042
3043 if (gus_pnp_flag)
3044 name = "GUS PnP";
3045
3046 gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
3047 gus_wave_volume = 90;
3048 have_gus_max = 1;
3049 if (hw_config->name)
3050 name = hw_config->name;
3051
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 */
3056 hw_config->osp,
3057 THIS_MODULE);
3058
3059 if (num_mixers > old_num_mixers)
3060 {
3061 /* GUS has it's own mixer map */
3062 AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_SYNTH);
3063 AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_CD);
3064 AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE);
3065 }
3066 }
3067 else
3068 printk(KERN_WARNING "GUS: No CS4231 ??");
3069 #else
3070 printk(KERN_ERR "GUS MAX found, but not compiled in\n");
3071 #endif
3072 }
3073 }
3074 else
3075 {
3076 /*
3077 * ASIC not detected so the card must be 2.2 or 2.4.
3078 * There could still be the 16-bit/mixer daughter card.
3079 */
3080 }
3081
3082 if (hw_config->name)
3083 {
3084 strncpy(tmp, hw_config->name, 45);
3085 tmp[45] = 0;
3086 sprintf(tmp2, "%s (%dk)", tmp, (int) gus_mem_size / 1024);
3087 tmp2[sizeof(tmp2) - 1] = 0;
3088 }
3089 else if (gus_pnp_flag)
3090 {
3091 sprintf(tmp2, "Gravis UltraSound PnP (%dk)",
3092 (int) gus_mem_size / 1024);
3093 }
3094 else
3095 sprintf(tmp2, "Gravis UltraSound %s (%dk)", model_num, (int) gus_mem_size / 1024);
3096
3097
3098 samples = (struct patch_info *)vmalloc((MAX_SAMPLE + 1) * sizeof(*samples));
3099 if (samples == NULL)
3100 {
3101 printk(KERN_WARNING "gus_init: Cant allocate memory for instrument tables\n");
3102 return;
3103 }
3104 conf_printf(tmp2, hw_config);
3105 tmp2[sizeof(gus_info.name) - 1] = 0;
3106 strcpy(gus_info.name, tmp2);
3107
3108 if ((sdev = sound_alloc_synthdev()) == -1)
3109 printk(KERN_WARNING "gus_init: Too many synthesizers\n");
3110 else
3111 {
3112 voice_alloc = &guswave_operations.alloc;
3113 if (iw_mode)
3114 guswave_operations.id = "IWAVE";
3115 hw_config->slots[0] = sdev;
3116 synth_devs[sdev] = &guswave_operations;
3117 sequencer_init();
3118 gus_tmr_install(gus_base + 8);
3119 }
3120
3121 reset_sample_memory();
3122
3123 gus_initialize();
3124
3125 if ((gus_mem_size > 0) & !gus_no_wave_dma)
3126 {
3127 hw_config->slots[4] = -1;
3128 if ((gus_devnum = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
3129 "Ultrasound",
3130 &gus_audio_driver,
3131 sizeof(struct audio_driver),
3132 NEEDS_RESTART |
3133 ((!iw_mode && dma2 != dma && dma2 != -1) ?
3134 DMA_DUPLEX : 0),
3135 AFMT_U8 | AFMT_S16_LE,
3136 NULL, dma, dma2)) < 0)
3137 {
3138 return;
3139 }
3140
3141 hw_config->slots[4] = gus_devnum;
3142 audio_devs[gus_devnum]->min_fragment = 9; /* 512k */
3143 audio_devs[gus_devnum]->max_fragment = 11; /* 8k (must match size of bounce_buf */
3144 audio_devs[gus_devnum]->mixer_dev = -1; /* Next mixer# */
3145 audio_devs[gus_devnum]->flags |= DMA_HARDSTOP;
3146 }
3147
3148 /*
3149 * Mixer dependent initialization.
3150 */
3151
3152 switch (mixer_type)
3153 {
3154 case ICS2101:
3155 gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
3156 gus_wave_volume = 90;
3157 request_region(u_MixSelect, 1, "GUS mixer");
3158 hw_config->slots[5] = ics2101_mixer_init();
3159 audio_devs[gus_devnum]->mixer_dev = hw_config->slots[5]; /* Next mixer# */
3160 return;
3161
3162 case CS4231:
3163 /* Initialized elsewhere (ad1848.c) */
3164 default:
3165 hw_config->slots[5] = gus_default_mixer_init();
3166 audio_devs[gus_devnum]->mixer_dev = hw_config->slots[5]; /* Next mixer# */
3167 return;
3168 }
3169 }
3170
3171 void __exit gus_wave_unload(struct address_info *hw_config)
3172 {
3173 #ifdef CONFIG_SOUND_GUSMAX
3174 if (have_gus_max)
3175 {
3176 ad1848_unload(gus_base + 0x10c,
3177 -gus_irq,
3178 gus_dma2, /* Playback DMA */
3179 gus_dma, /* Capture DMA */
3180 1); /* Share DMA channels with GF1 */
3181 }
3182 #endif
3183
3184 if (mixer_type == ICS2101)
3185 {
3186 release_region(u_MixSelect, 1);
3187 }
3188 if (hw_config->slots[0] != -1)
3189 sound_unload_synthdev(hw_config->slots[0]);
3190 if (hw_config->slots[1] != -1)
3191 sound_unload_audiodev(hw_config->slots[1]);
3192 if (hw_config->slots[2] != -1)
3193 sound_unload_mididev(hw_config->slots[2]);
3194 if (hw_config->slots[4] != -1)
3195 sound_unload_audiodev(hw_config->slots[4]);
3196 if (hw_config->slots[5] != -1)
3197 sound_unload_mixerdev(hw_config->slots[5]);
3198
3199 if(samples)
3200 vfree(samples);
3201 samples=NULL;
3202 }
3203
3204 static void do_loop_irq(int voice)
3205 {
3206 unsigned char tmp;
3207 int mode, parm;
3208 unsigned long flags;
3209
3210 save_flags(flags);
3211 cli();
3212 gus_select_voice(voice);
3213
3214 tmp = gus_read8(0x00);
3215 tmp &= ~0x20; /*
3216 * Disable wave IRQ for this_one voice
3217 */
3218 gus_write8(0x00, tmp);
3219
3220 if (tmp & 0x03) /* Voice stopped */
3221 voice_alloc->map[voice] = 0;
3222
3223 mode = voices[voice].loop_irq_mode;
3224 voices[voice].loop_irq_mode = 0;
3225 parm = voices[voice].loop_irq_parm;
3226
3227 switch (mode)
3228 {
3229 case LMODE_FINISH: /*
3230 * Final loop finished, shoot volume down
3231 */
3232
3233 if ((int) (gus_read16(0x09) >> 4) < 100) /*
3234 * Get current volume
3235 */
3236 {
3237 gus_voice_off();
3238 gus_rampoff();
3239 gus_voice_init(voice);
3240 break;
3241 }
3242 gus_ramp_range(65, 4065);
3243 gus_ramp_rate(0, 63); /*
3244 * Fastest possible rate
3245 */
3246 gus_rampon(0x20 | 0x40); /*
3247 * Ramp down, once, irq
3248 */
3249 voices[voice].volume_irq_mode = VMODE_HALT;
3250 break;
3251
3252 case LMODE_PCM_STOP:
3253 pcm_active = 0; /* Signal to the play_next_pcm_block routine */
3254 case LMODE_PCM:
3255 {
3256 pcm_qlen--;
3257 pcm_head = (pcm_head + 1) % pcm_nblk;
3258 if (pcm_qlen && pcm_active)
3259 {
3260 play_next_pcm_block();
3261 }
3262 else
3263 {
3264 /* Underrun. Just stop the voice */
3265 gus_select_voice(0); /* Left channel */
3266 gus_voice_off();
3267 gus_rampoff();
3268 gus_select_voice(1); /* Right channel */
3269 gus_voice_off();
3270 gus_rampoff();
3271 pcm_active = 0;
3272 }
3273
3274 /*
3275 * If the queue was full before this interrupt, the DMA transfer was
3276 * suspended. Let it continue now.
3277 */
3278
3279 if (audio_devs[gus_devnum]->dmap_out->qlen > 0)
3280 DMAbuf_outputintr(gus_devnum, 0);
3281 }
3282 break;
3283
3284 default:
3285 }
3286 restore_flags(flags);
3287 }
3288
3289 static void do_volume_irq(int voice)
3290 {
3291 unsigned char tmp;
3292 int mode, parm;
3293 unsigned long flags;
3294
3295 save_flags(flags);
3296 cli();
3297
3298 gus_select_voice(voice);
3299 tmp = gus_read8(0x0d);
3300 tmp &= ~0x20; /*
3301 * Disable volume ramp IRQ
3302 */
3303 gus_write8(0x0d, tmp);
3304
3305 mode = voices[voice].volume_irq_mode;
3306 voices[voice].volume_irq_mode = 0;
3307 parm = voices[voice].volume_irq_parm;
3308
3309 switch (mode)
3310 {
3311 case VMODE_HALT: /* Decay phase finished */
3312 if (iw_mode)
3313 gus_write8(0x15, 0x02); /* Set voice deactivate bit of SMSI */
3314 restore_flags(flags);
3315 gus_voice_init(voice);
3316 break;
3317
3318 case VMODE_ENVELOPE:
3319 gus_rampoff();
3320 restore_flags(flags);
3321 step_envelope(voice);
3322 break;
3323
3324 case VMODE_START_NOTE:
3325 restore_flags(flags);
3326 guswave_start_note2(voices[voice].dev_pending, voice,
3327 voices[voice].note_pending, voices[voice].volume_pending);
3328 if (voices[voice].kill_pending)
3329 guswave_kill_note(voices[voice].dev_pending, voice,
3330 voices[voice].note_pending, 0);
3331
3332 if (voices[voice].sample_pending >= 0)
3333 {
3334 guswave_set_instr(voices[voice].dev_pending, voice,
3335 voices[voice].sample_pending);
3336 voices[voice].sample_pending = -1;
3337 }
3338 break;
3339
3340 default:
3341 restore_flags(flags);
3342 }
3343 restore_flags(flags);
3344 }
3345
3346 void gus_voice_irq(void)
3347 {
3348 unsigned long wave_ignore = 0, volume_ignore = 0;
3349 unsigned long voice_bit;
3350
3351 unsigned char src, voice;
3352
3353 while (1)
3354 {
3355 src = gus_read8(0x0f); /*
3356 * Get source info
3357 */
3358 voice = src & 0x1f;
3359 src &= 0xc0;
3360
3361 if (src == (0x80 | 0x40))
3362 return; /*
3363 * No interrupt
3364 */
3365
3366 voice_bit = 1 << voice;
3367
3368 if (!(src & 0x80)) /*
3369 * Wave IRQ pending
3370 */
3371 if (!(wave_ignore & voice_bit) && (int) voice < nr_voices) /*
3372 * Not done
3373 * yet
3374 */
3375 {
3376 wave_ignore |= voice_bit;
3377 do_loop_irq(voice);
3378 }
3379 if (!(src & 0x40)) /*
3380 * Volume IRQ pending
3381 */
3382 if (!(volume_ignore & voice_bit) && (int) voice < nr_voices) /*
3383 * Not done
3384 * yet
3385 */
3386 {
3387 volume_ignore |= voice_bit;
3388 do_volume_irq(voice);
3389 }
3390 }
3391 }
3392
3393 void guswave_dma_irq(void)
3394 {
3395 unsigned char status;
3396
3397 status = gus_look8(0x41); /* Get DMA IRQ Status */
3398 if (status & 0x40) /* DMA interrupt pending */
3399 switch (active_device)
3400 {
3401 case GUS_DEV_WAVE:
3402 wake_up(&dram_sleeper);
3403 break;
3404
3405 case GUS_DEV_PCM_CONTINUE: /* Left channel data transferred */
3406 gus_write8(0x41, 0); /* Disable GF1 DMA */
3407 gus_transfer_output_block(pcm_current_dev, pcm_current_buf,
3408 pcm_current_count,
3409 pcm_current_intrflag, 1);
3410 break;
3411
3412 case GUS_DEV_PCM_DONE: /* Right or mono channel data transferred */
3413 gus_write8(0x41, 0); /* Disable GF1 DMA */
3414 if (pcm_qlen < pcm_nblk)
3415 {
3416 dma_active = 0;
3417 if (gus_busy)
3418 {
3419 if (audio_devs[gus_devnum]->dmap_out->qlen > 0)
3420 DMAbuf_outputintr(gus_devnum, 0);
3421 }
3422 }
3423 break;
3424
3425 default:
3426 }
3427 status = gus_look8(0x49); /*
3428 * Get Sampling IRQ Status
3429 */
3430 if (status & 0x40) /*
3431 * Sampling Irq pending
3432 */
3433 {
3434 DMAbuf_inputintr(gus_devnum);
3435 }
3436 }
3437
3438 /*
3439 * Timer stuff
3440 */
3441
3442 static volatile int select_addr, data_addr;
3443 static volatile int curr_timer = 0;
3444
3445 void gus_timer_command(unsigned int addr, unsigned int val)
3446 {
3447 int i;
3448
3449 outb(((unsigned char) (addr & 0xff)), select_addr);
3450
3451 for (i = 0; i < 2; i++)
3452 inb(select_addr);
3453
3454 outb(((unsigned char) (val & 0xff)), data_addr);
3455
3456 for (i = 0; i < 2; i++)
3457 inb(select_addr);
3458 }
3459
3460 static void arm_timer(int timer, unsigned int interval)
3461 {
3462 curr_timer = timer;
3463
3464 if (timer == 1)
3465 {
3466 gus_write8(0x46, 256 - interval); /* Set counter for timer 1 */
3467 gus_write8(0x45, 0x04); /* Enable timer 1 IRQ */
3468 gus_timer_command(0x04, 0x01); /* Start timer 1 */
3469 }
3470 else
3471 {
3472 gus_write8(0x47, 256 - interval); /* Set counter for timer 2 */
3473 gus_write8(0x45, 0x08); /* Enable timer 2 IRQ */
3474 gus_timer_command(0x04, 0x02); /* Start timer 2 */
3475 }
3476
3477 gus_timer_enabled = 1;
3478 }
3479
3480 static unsigned int gus_tmr_start(int dev, unsigned int usecs_per_tick)
3481 {
3482 int timer_no, resolution;
3483 int divisor;
3484
3485 if (usecs_per_tick > (256 * 80))
3486 {
3487 timer_no = 2;
3488 resolution = 320; /* usec */
3489 }
3490 else
3491 {
3492 timer_no = 1;
3493 resolution = 80; /* usec */
3494 }
3495 divisor = (usecs_per_tick + (resolution / 2)) / resolution;
3496 arm_timer(timer_no, divisor);
3497
3498 return divisor * resolution;
3499 }
3500
3501 static void gus_tmr_disable(int dev)
3502 {
3503 gus_write8(0x45, 0); /* Disable both timers */
3504 gus_timer_enabled = 0;
3505 }
3506
3507 static void gus_tmr_restart(int dev)
3508 {
3509 if (curr_timer == 1)
3510 gus_write8(0x45, 0x04); /* Start timer 1 again */
3511 else
3512 gus_write8(0x45, 0x08); /* Start timer 2 again */
3513 gus_timer_enabled = 1;
3514 }
3515
3516 static struct sound_lowlev_timer gus_tmr =
3517 {
3518 0,
3519 1,
3520 gus_tmr_start,
3521 gus_tmr_disable,
3522 gus_tmr_restart
3523 };
3524
3525 static void gus_tmr_install(int io_base)
3526 {
3527 struct sound_lowlev_timer *tmr;
3528
3529 select_addr = io_base;
3530 data_addr = io_base + 1;
3531
3532 tmr = &gus_tmr;
3533
3534 #ifdef THIS_GETS_FIXED
3535 sound_timer_init(&gus_tmr, "GUS");
3536 #endif
3537 }
3538
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.