This source file includes following definitions.
- reset_sample_memory
- gus_delay
- gus_poke
- gus_peek
- gus_write8
- gus_read8
- gus_look8
- gus_write16
- gus_read16
- gus_write_addr
- gus_select_voice
- gus_select_max_voices
- gus_voice_on
- gus_voice_off
- gus_voice_mode
- gus_voice_freq
- gus_voice_volume
- gus_voice_balance
- gus_ramp_range
- gus_ramp_rate
- gus_rampon
- gus_ramp_mode
- gus_rampoff
- gus_voice_init
- step_envelope
- init_envelope
- start_release
- gus_voice_fade
- gus_reset
- gus_initialize
- gus_wave_detect
- guswave_ioctl
- guswave_set_instr
- guswave_kill_note
- guswave_aftertouch
- guswave_panning
- compute_volume
- compute_and_set_volume
- dynamic_volume_change
- guswave_controller
- guswave_start_note
- guswave_reset
- guswave_open
- guswave_close
- guswave_load_patch
- guswave_hw_control
- gus_sampling_set_speed
- gus_sampling_set_channels
- gus_sampling_set_bits
- gus_sampling_ioctl
- gus_sampling_reset
- gus_sampling_open
- gus_sampling_close
- play_next_pcm_block
- gus_transfer_output_block
- gus_sampling_output_block
- gus_sampling_start_input
- gus_sampling_prepare_for_input
- gus_sampling_prepare_for_output
- gus_has_output_drained
- gus_copy_from_user
- guswave_patchmgr
- gus_wave_init
- do_loop_irq
- do_volume_irq
- gus_voice_irq
- guswave_dma_irq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 #include "sound_config.h"
34 #include <linux/ultrasound.h>
35 #include "gus_hw.h"
36
37 #if defined(CONFIGURE_SOUNDCARD) && !defined(EXCLUDE_GUS)
38
39 #define MAX_SAMPLE 256
40 #define MAX_PATCH 256
41
42 struct voice_info
43 {
44 unsigned long orig_freq;
45 unsigned long current_freq;
46 unsigned long mode;
47 int bender;
48 int bender_range;
49 int panning;
50 int midi_volume;
51 unsigned int initial_volume;
52 unsigned int current_volume;
53 int loop_irq_mode, loop_irq_parm;
54 #define LMODE_FINISH 1
55 #define LMODE_PCM 2
56 #define LMODE_PCM_STOP 3
57 int volume_irq_mode, volume_irq_parm;
58 #define VMODE_HALT 1
59 #define VMODE_ENVELOPE 2
60
61 int env_phase;
62 unsigned char env_rate[6];
63 unsigned char env_offset[6];
64
65
66
67
68 int main_vol, expression_vol, patch_vol;
69
70 };
71
72 extern int gus_base;
73 extern int gus_irq, gus_dma;
74 extern char *snd_raw_buf[MAX_DSP_DEV][DSP_BUFFCOUNT];
75 extern unsigned long snd_raw_buf_phys[MAX_DSP_DEV][DSP_BUFFCOUNT];
76 extern int snd_raw_count[MAX_DSP_DEV];
77 static long gus_mem_size = 0;
78 static long free_mem_ptr = 0;
79 static int gus_busy = 0;
80 static int nr_voices = 0;
81 static int gus_devnum = 0;
82 static int volume_base, volume_scale, volume_method;
83
84 #define VOL_METHOD_ADAGIO 1
85 int gus_wave_volume = 60;
86 static unsigned char mix_image = 0x00;
87
88
89
90
91
92 static int active_device = 0;
93
94 #define GUS_DEV_WAVE 1
95 #define GUS_DEV_PCM_DONE 2
96 #define GUS_DEV_PCM_CONTINUE 3
97
98
99 static int gus_sampling_speed;
100 static int gus_sampling_channels;
101 static int gus_sampling_bits;
102
103 DEFINE_WAIT_QUEUE (dram_sleeper, dram_sleep_flag);
104
105
106
107
108 #define MAX_PCM_BUFFERS (32*MAX_REALTIME_FACTOR)
109
110 static int pcm_bsize,
111 pcm_nblk,
112 pcm_banksize;
113 static int pcm_datasize[MAX_PCM_BUFFERS];
114 static volatile int pcm_head, pcm_tail, pcm_qlen;
115 static volatile int pcm_active;
116 static int pcm_current_dev;
117 static int pcm_current_block;
118 static unsigned long pcm_current_buf;
119 static int pcm_current_count;
120 static int pcm_current_intrflag;
121
122 struct voice_info voices[32];
123
124 static int freq_div_table[] =
125 {
126 44100,
127 41160,
128 38587,
129 36317,
130 34300,
131 32494,
132 30870,
133 29400,
134 28063,
135 26843,
136 25725,
137 24696,
138 23746,
139 22866,
140 22050,
141 21289,
142 20580,
143 19916,
144 19293
145 };
146
147 static struct patch_info samples[MAX_SAMPLE + 1];
148 static long sample_ptrs[MAX_SAMPLE + 1];
149 static int sample_map[32];
150 static int free_sample;
151
152
153 static int patch_table[MAX_PATCH];
154 static int patch_map[32];
155
156 static struct synth_info gus_info =
157 {"Gravis UltraSound", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_GUS, 0, 16, 0, MAX_PATCH};
158
159 static void gus_poke (long addr, unsigned char data);
160 static void compute_and_set_volume (int voice, int volume, int ramp_time);
161 extern unsigned short gus_adagio_vol (int vel, int mainv, int xpn, int voicev);
162 static void compute_volume (int voice, int volume);
163
164 #define INSTANT_RAMP -1
165 #define FAST_RAMP 0
166
167 static void
168 reset_sample_memory (void)
169 {
170 int i;
171
172 for (i = 0; i <= MAX_SAMPLE; i++)
173 sample_ptrs[i] = -1;
174 for (i = 0; i < 32; i++)
175 sample_map[i] = -1;
176 for (i = 0; i < 32; i++)
177 patch_map[i] = -1;
178
179 gus_poke (0, 0);
180 gus_poke (1, 0);
181
182 free_mem_ptr = 2;
183 free_sample = 0;
184
185 for (i = 0; i < MAX_PATCH; i++)
186 patch_table[i] = -1;
187 }
188
189 void
190 gus_delay (void)
191 {
192 int i;
193
194 for (i = 0; i < 7; i++)
195 INB (u_DRAMIO);
196 }
197
198 static void
199 gus_poke (long addr, unsigned char data)
200 {
201 unsigned long flags;
202
203 DISABLE_INTR (flags);
204 OUTB (0x43, u_Command);
205 OUTB (addr & 0xff, u_DataLo);
206 OUTB ((addr >> 8) & 0xff, u_DataHi);
207
208 OUTB (0x44, u_Command);
209 OUTB ((addr >> 16) & 0xff, u_DataHi);
210 OUTB (data, u_DRAMIO);
211 RESTORE_INTR (flags);
212 }
213
214 static unsigned char
215 gus_peek (long addr)
216 {
217 unsigned long flags;
218 unsigned char tmp;
219
220 DISABLE_INTR (flags);
221 OUTB (0x43, u_Command);
222 OUTB (addr & 0xff, u_DataLo);
223 OUTB ((addr >> 8) & 0xff, u_DataHi);
224
225 OUTB (0x44, u_Command);
226 OUTB ((addr >> 16) & 0xff, u_DataHi);
227 tmp = INB (u_DRAMIO);
228 RESTORE_INTR (flags);
229
230 return tmp;
231 }
232
233 void
234 gus_write8 (int reg, unsigned char data)
235 {
236 unsigned long flags;
237
238 DISABLE_INTR (flags);
239
240 OUTB (reg, u_Command);
241 OUTB (data, u_DataHi);
242
243 RESTORE_INTR (flags);
244 }
245
246 unsigned char
247 gus_read8 (int reg)
248 {
249 unsigned long flags;
250 unsigned char val;
251
252 DISABLE_INTR (flags);
253 OUTB (reg | 0x80, u_Command);
254 val = INB (u_DataHi);
255 RESTORE_INTR (flags);
256
257 return val;
258 }
259
260 unsigned char
261 gus_look8 (int reg)
262 {
263 unsigned long flags;
264 unsigned char val;
265
266 DISABLE_INTR (flags);
267 OUTB (reg, u_Command);
268 val = INB (u_DataHi);
269 RESTORE_INTR (flags);
270
271 return val;
272 }
273
274 void
275 gus_write16 (int reg, unsigned short data)
276 {
277 unsigned long flags;
278
279 DISABLE_INTR (flags);
280
281 OUTB (reg, u_Command);
282
283 OUTB (data & 0xff, u_DataLo);
284 OUTB ((data >> 8) & 0xff, u_DataHi);
285
286 RESTORE_INTR (flags);
287 }
288
289 unsigned short
290 gus_read16 (int reg)
291 {
292 unsigned long flags;
293 unsigned char hi, lo;
294
295 DISABLE_INTR (flags);
296
297 OUTB (reg | 0x80, u_Command);
298
299 lo = INB (u_DataLo);
300 hi = INB (u_DataHi);
301
302 RESTORE_INTR (flags);
303
304 return ((hi << 8) & 0xff00) | lo;
305 }
306
307 void
308 gus_write_addr (int reg, unsigned long address, int is16bit)
309 {
310 unsigned long hold_address;
311
312 if (is16bit)
313 {
314
315
316
317
318 hold_address = address;
319 address = address >> 1;
320 address &= 0x0001ffffL;
321 address |= (hold_address & 0x000c0000L);
322 }
323
324 gus_write16 (reg, (unsigned short) ((address >> 7) & 0xffff));
325 gus_write16 (reg + 1, (unsigned short) ((address << 9) & 0xffff));
326 }
327
328 static void
329 gus_select_voice (int voice)
330 {
331 if (voice < 0 || voice > 31)
332 return;
333
334 OUTB (voice, u_Voice);
335 }
336
337 static void
338 gus_select_max_voices (int nvoices)
339 {
340 if (nvoices < 14)
341 nvoices = 14;
342 if (nvoices > 32)
343 nvoices = 32;
344
345 nr_voices = nvoices;
346
347 gus_write8 (0x0e, (nvoices - 1) | 0xc0);
348 }
349
350 static void
351 gus_voice_on (unsigned char mode)
352 {
353 gus_write8 (0x00, mode & 0xfc);
354 gus_delay ();
355 gus_write8 (0x00, mode & 0xfc);
356 }
357
358 static void
359 gus_voice_off (void)
360 {
361 gus_write8 (0x00, gus_read8 (0x00) | 0x03);
362 }
363
364 static void
365 gus_voice_mode (unsigned char mode)
366 {
367 gus_write8 (0x00, (gus_read8 (0x00) & 0x03) | (mode & 0xfc));
368
369 gus_delay ();
370 gus_write8 (0x00, (gus_read8 (0x00) & 0x03) | (mode & 0xfc));
371 }
372
373 static void
374 gus_voice_freq (unsigned long freq)
375 {
376 unsigned long divisor = freq_div_table[nr_voices - 14];
377 unsigned short fc;
378
379 fc = (unsigned short) (((freq << 9) + (divisor >> 1)) / divisor);
380 fc = fc << 1;
381
382 gus_write16 (0x01, fc);
383 }
384
385 static void
386 gus_voice_volume (unsigned short vol)
387 {
388 gus_write8 (0x0d, 0x03);
389 gus_write16 (0x09, vol << 4);
390 }
391
392 static void
393 gus_voice_balance (unsigned char balance)
394 {
395 gus_write8 (0x0c, balance);
396 }
397
398 static void
399 gus_ramp_range (unsigned short low, unsigned short high)
400 {
401 gus_write8 (0x07, (low >> 4) & 0xff);
402 gus_write8 (0x08, (high >> 4) & 0xff);
403 }
404
405 static void
406 gus_ramp_rate (unsigned char scale, unsigned char rate)
407 {
408 gus_write8 (0x06, ((scale & 0x03) << 6) | (rate & 0x3f));
409 }
410
411 static void
412 gus_rampon (unsigned char mode)
413 {
414 gus_write8 (0x0d, mode & 0xfc);
415 gus_delay ();
416 gus_write8 (0x0d, mode & 0xfc);
417 }
418
419 static void
420 gus_ramp_mode (unsigned char mode)
421 {
422 gus_write8 (0x0d, (gus_read8 (0x0d) & 0x03) | (mode & 0xfc));
423
424 gus_delay ();
425 gus_write8 (0x0d, (gus_read8 (0x0d) & 0x03) | (mode & 0xfc));
426 }
427
428 static void
429 gus_rampoff (void)
430 {
431 gus_write8 (0x0d, 0x03);
432 }
433
434 static void
435 gus_voice_init (int voice)
436 {
437 unsigned long flags;
438
439 DISABLE_INTR (flags);
440 gus_select_voice (voice);
441 gus_voice_volume (0);
442 gus_write_addr (0x0a, 0, 0);
443 gus_write8 (0x00, 0x03);
444 gus_write8 (0x0d, 0x03);
445 RESTORE_INTR (flags);
446
447 voices[voice].panning = 0;
448 voices[voice].mode = 0;
449 voices[voice].orig_freq = 20000;
450 voices[voice].current_freq = 20000;
451 voices[voice].bender = 0;
452 voices[voice].bender_range = 200;
453 voices[voice].initial_volume = 0;
454 voices[voice].current_volume = 0;
455 voices[voice].loop_irq_mode = 0;
456 voices[voice].loop_irq_parm = 0;
457 voices[voice].volume_irq_mode = 0;
458 voices[voice].volume_irq_parm = 0;
459 voices[voice].env_phase = 0;
460 voices[voice].main_vol = 127;
461 voices[voice].patch_vol = 127;
462 voices[voice].expression_vol = 127;
463 }
464
465 static void
466 step_envelope (int voice)
467 {
468 unsigned vol, prev_vol, phase;
469 unsigned char rate;
470
471 if (voices[voice].mode & WAVE_SUSTAIN_ON && voices[voice].env_phase == 2)
472 {
473 gus_rampoff ();
474 return;
475 }
476
477 if (voices[voice].env_phase >= 5)
478 {
479
480
481
482
483 gus_voice_init (voice);
484 return;
485 }
486
487 prev_vol = voices[voice].current_volume;
488 gus_voice_volume (prev_vol);
489 phase = ++voices[voice].env_phase;
490
491 compute_volume (voice, voices[voice].midi_volume);
492
493 vol = voices[voice].initial_volume * voices[voice].env_offset[phase] / 255;
494 rate = voices[voice].env_rate[phase];
495 gus_write8 (0x06, rate);
496
497 voices[voice].volume_irq_mode = VMODE_ENVELOPE;
498
499 if (((vol - prev_vol) / 64) == 0)
500 {
501 step_envelope (voice);
502 return;
503 }
504
505 if (vol > prev_vol)
506 {
507 if (vol >= (4096 - 64))
508 vol = 4096 - 65;
509 gus_ramp_range (0, vol);
510 gus_rampon (0x20);
511 }
512 else
513 {
514 if (vol <= 64)
515 vol = 65;
516 gus_ramp_range (vol, 4095);
517 gus_rampon (0x60);
518 }
519 voices[voice].current_volume = vol;
520 }
521
522 static void
523 init_envelope (int voice)
524 {
525 voices[voice].env_phase = -1;
526 voices[voice].current_volume = 64;
527
528 step_envelope (voice);
529 }
530
531 static void
532 start_release (int voice)
533 {
534 if (gus_read8 (0x00) & 0x03)
535 return;
536
537 voices[voice].env_phase = 2;
538
539 voices[voice].current_volume =
540 voices[voice].initial_volume =
541 gus_read16 (0x09) >> 4;
542
543 voices[voice].mode &= ~WAVE_SUSTAIN_ON;
544 gus_rampoff ();
545 step_envelope (voice);
546 }
547
548 static void
549 gus_voice_fade (int voice)
550 {
551 int instr_no = sample_map[voice], is16bits;
552
553 if (instr_no < 0 || instr_no > MAX_SAMPLE)
554 {
555 gus_write8 (0x00, 0x03);
556 return;
557 }
558
559 is16bits = (samples[instr_no].mode & WAVE_16_BITS) ? 1 : 0;
560
561 if (voices[voice].mode & WAVE_ENVELOPES)
562 {
563 start_release (voice);
564 return;
565 }
566
567
568
569
570 if ((gus_read16 (0x09) >> 4) < 100)
571 {
572 gus_voice_off ();
573 gus_rampoff ();
574 gus_voice_init (voice);
575 return;
576 }
577
578 gus_ramp_range (65, 4095);
579 gus_ramp_rate (2, 4);
580 gus_rampon (0x40 | 0x20);
581 voices[voice].volume_irq_mode = VMODE_HALT;
582 }
583
584 static void
585 gus_reset (void)
586 {
587 int i;
588
589 gus_select_max_voices (24);
590 volume_base = 3071;
591 volume_scale = 4;
592 volume_method = VOL_METHOD_ADAGIO;
593
594 for (i = 0; i < 32; i++)
595 {
596 gus_voice_init (i);
597 }
598
599 INB (u_Status);
600
601 gus_look8 (0x41);
602 gus_look8 (0x49);
603 gus_read8 (0x0f);
604
605 }
606
607 static void
608 gus_initialize (void)
609 {
610 unsigned long flags;
611 unsigned char dma_image, irq_image, tmp;
612
613 static unsigned char gus_irq_map[16] =
614 {0, 0, 1, 3, 0, 2, 0, 4, 0, 0, 0, 5, 6, 0, 0, 7};
615
616 static unsigned char gus_dma_map[8] =
617 {0, 1, 0, 2, 0, 3, 4, 5};
618
619 DISABLE_INTR (flags);
620
621 gus_write8 (0x4c, 0);
622 gus_delay ();
623 gus_delay ();
624
625 gus_write8 (0x4c, 1);
626 gus_delay ();
627 gus_delay ();
628
629
630
631
632
633 gus_write8 (0x41, 0);
634 gus_write8 (0x45, 0);
635 gus_write8 (0x49, 0);
636
637 gus_select_max_voices (24);
638
639 INB (u_Status);
640
641 gus_look8 (0x41);
642 gus_look8 (0x49);
643 gus_read8 (0x0f);
644
645 gus_reset ();
646
647 gus_look8 (0x41);
648 gus_look8 (0x49);
649 gus_read8 (0x0f);
650
651 gus_write8 (0x4c, 7);
652
653
654
655
656
657 OUTB (0x05, gus_base + 0x0f);
658
659 mix_image |= 0x02;
660 OUTB (mix_image, u_Mixer);
661
662 OUTB (0x00, u_IRQDMAControl);
663
664 OUTB (0x00, gus_base + 0x0f);
665
666
667
668
669
670
671
672
673
674
675
676 irq_image = 0;
677 tmp = gus_irq_map[gus_irq];
678 if (!tmp)
679 printk ("Warning! GUS IRQ not selected\n");
680 irq_image |= tmp;
681 irq_image |= 0x40;
682
683 dma_image = 0x40;
684 tmp = gus_dma_map[gus_dma];
685 if (!tmp)
686 printk ("Warning! GUS DMA not selected\n");
687 dma_image |= tmp;
688
689
690
691
692
693
694
695 OUTB (mix_image, u_Mixer);
696 OUTB (dma_image | 0x80, u_IRQDMAControl);
697
698 OUTB (mix_image | 0x40, u_Mixer);
699 OUTB (irq_image, u_IRQDMAControl);
700
701
702
703 OUTB (mix_image, u_Mixer);
704 OUTB (dma_image, u_IRQDMAControl);
705
706 OUTB (mix_image | 0x40, u_Mixer);
707 OUTB (irq_image, u_IRQDMAControl);
708
709 gus_select_voice (0);
710
711 mix_image &= ~0x02;
712 mix_image |= 0x08;
713 OUTB (mix_image, u_Mixer);
714
715 gus_select_voice (0);
716
717 gusintr (0);
718 RESTORE_INTR (flags);
719 }
720
721 int
722 gus_wave_detect (int baseaddr)
723 {
724 gus_base = baseaddr;
725
726 gus_write8 (0x4c, 0);
727 gus_delay ();
728 gus_delay ();
729
730 gus_write8 (0x4c, 1);
731 gus_delay ();
732 gus_delay ();
733
734 gus_poke (0x000, 0xaa);
735 gus_poke (0x100, 0x55);
736
737 if (gus_peek (0x000) != 0xaa)
738 return 0;
739 if (gus_peek (0x100) != 0x55)
740 return 0;
741
742 gus_mem_size = 0x40000;
743 gus_poke (0x40000, 0xaa);
744 if (gus_peek (0x40000) != 0xaa)
745 return 1;
746
747 gus_mem_size = 0x80000;
748 gus_poke (0x80000, 0xaa);
749 if (gus_peek (0x80000) != 0xaa)
750 return 1;
751
752 gus_mem_size = 0xc0000;
753 gus_poke (0xc0000, 0xaa);
754 if (gus_peek (0xc0000) != 0xaa)
755 return 1;
756
757 gus_mem_size = 0x100000;
758
759 return 1;
760 }
761
762 static int
763 guswave_ioctl (int dev,
764 unsigned int cmd, unsigned int arg)
765 {
766
767 switch (cmd)
768 {
769 case SNDCTL_SYNTH_INFO:
770 gus_info.nr_voices = nr_voices;
771 IOCTL_TO_USER ((char *) arg, 0, &gus_info, sizeof (gus_info));
772 return 0;
773 break;
774
775 case SNDCTL_SEQ_RESETSAMPLES:
776 reset_sample_memory ();
777 return 0;
778 break;
779
780 case SNDCTL_SEQ_PERCMODE:
781 return 0;
782 break;
783
784 case SNDCTL_SYNTH_MEMAVL:
785 return gus_mem_size - free_mem_ptr - 32;
786
787 default:
788 return RET_ERROR (EINVAL);
789 }
790 }
791
792 static int
793 guswave_set_instr (int dev, int voice, int instr_no)
794 {
795 int sample_no;
796
797 if (instr_no < 0 || instr_no > MAX_PATCH)
798 return RET_ERROR (EINVAL);
799
800 if (voice < 0 || voice > 31)
801 return RET_ERROR (EINVAL);
802
803 sample_no = patch_table[instr_no];
804 patch_map[voice] = -1;
805
806 if (sample_no < 0)
807 {
808 printk ("GUS: Undefined patch %d for voice %d\n", instr_no, voice);
809 return RET_ERROR (EINVAL);
810 }
811
812 if (sample_ptrs[sample_no] == -1)
813 {
814 printk ("GUS: Sample #%d not loaded for patch %d (voice %d)\n", sample_no, instr_no, voice);
815 return RET_ERROR (EINVAL);
816 }
817
818 sample_map[voice] = sample_no;
819 patch_map[voice] = instr_no;
820 return 0;
821 }
822
823 static int
824 guswave_kill_note (int dev, int voice, int velocity)
825 {
826 unsigned long flags;
827
828 DISABLE_INTR (flags);
829 gus_select_voice (voice);
830 gus_voice_fade (voice);
831 RESTORE_INTR (flags);
832
833 return 0;
834 }
835
836 static void
837 guswave_aftertouch (int dev, int voice, int pressure)
838 {
839 short lo_limit, hi_limit;
840 unsigned long flags;
841
842 return;
843
844 if (voice < 0 || voice > 31)
845 return;
846
847 if (voices[voice].mode & WAVE_ENVELOPES && voices[voice].env_phase != 2)
848 return;
849
850 if (pressure < 32)
851 {
852 DISABLE_INTR (flags);
853 gus_select_voice (voice);
854 gus_rampoff ();
855 compute_and_set_volume (voice, 255, 0);
856 RESTORE_INTR (flags);
857 return;
858 }
859
860 hi_limit = voices[voice].current_volume;
861 lo_limit = hi_limit * 99 / 100;
862 if (lo_limit < 65)
863 lo_limit = 65;
864
865 DISABLE_INTR (flags);
866 gus_select_voice (voice);
867 if (hi_limit > (4095 - 65))
868 {
869 hi_limit = 4095 - 65;
870 gus_voice_volume (hi_limit);
871 }
872 gus_ramp_range (lo_limit, hi_limit);
873 gus_ramp_rate (3, 8);
874 gus_rampon (0x58);
875 RESTORE_INTR (flags);
876 }
877
878 static void
879 guswave_panning (int dev, int voice, int value)
880 {
881 if (voice >= 0 || voice < 32)
882 voices[voice].panning = value;
883 }
884
885 static void
886 compute_volume (int voice, int volume)
887 {
888 if (volume < 128)
889 {
890 voices[voice].midi_volume = volume;
891
892 switch (volume_method)
893 {
894 case VOL_METHOD_ADAGIO:
895 voices[voice].initial_volume =
896 gus_adagio_vol (volume, voices[voice].main_vol,
897 voices[voice].expression_vol,
898 voices[voice].patch_vol);
899 break;
900
901 default:
902 voices[voice].initial_volume = volume_base + (volume * volume_scale);
903 }
904 }
905
906 if (voices[voice].initial_volume > 4095)
907 voices[voice].initial_volume = 4095;
908 }
909
910 static void
911 compute_and_set_volume (int voice, int volume, int ramp_time)
912 {
913 int current, target, rate;
914
915 compute_volume (voice, volume);
916 voices[voice].current_volume = voices[voice].initial_volume;
917
918 current = gus_read16 (0x09) >> 4;
919 target = voices[voice].initial_volume;
920
921 if (ramp_time == INSTANT_RAMP)
922 {
923 gus_rampoff ();
924 gus_voice_volume (target);
925 return;
926 }
927
928 if (ramp_time == FAST_RAMP)
929 rate = 63;
930 else
931 rate = 16;
932 gus_ramp_rate (0, rate);
933
934 if ((target - current) / 64 == 0)
935 {
936 gus_rampoff ();
937 gus_voice_volume (target);
938 return;
939 }
940
941 if (target > current)
942 {
943 if (target > (4095 - 65))
944 target = 4095 - 65;
945 gus_ramp_range (current, target);
946 gus_rampon (0x00);
947 }
948 else
949 {
950 if (target < 65)
951 target = 65;
952
953 gus_ramp_range (target, current);
954 gus_rampon (0x40);
955 }
956 }
957
958 static void
959 dynamic_volume_change (int voice)
960 {
961 unsigned char status;
962 unsigned long flags;
963
964 DISABLE_INTR (flags);
965 gus_select_voice (voice);
966 status = gus_read8 (0x00);
967 RESTORE_INTR (flags);
968
969 if (status & 0x03)
970 return;
971
972 if (!(voices[voice].mode & WAVE_ENVELOPES))
973 {
974 compute_and_set_volume (voice, voices[voice].midi_volume, 1);
975 return;
976 }
977
978
979
980
981
982 DISABLE_INTR (flags);
983 gus_select_voice (voice);
984 status = gus_read8 (0x0d);
985 RESTORE_INTR (flags);
986
987 if (status & 0x03)
988 {
989 compute_and_set_volume (voice, voices[voice].midi_volume, 1);
990 return;
991 }
992
993 if (voices[voice].env_phase < 0)
994 return;
995
996 compute_volume (voice, voices[voice].midi_volume);
997
998 #if 0
999 voices[voice].current_volume =
1000 gus_read16 (0x09) >> 4;
1001
1002 voices[voice].env_phase--;
1003 step_envelope (voice);
1004 #endif
1005 }
1006
1007 static void
1008 guswave_controller (int dev, int voice, int ctrl_num, int value)
1009 {
1010 unsigned long flags;
1011 unsigned long freq;
1012
1013 if (voice < 0 || voice > 31)
1014 return;
1015
1016 switch (ctrl_num)
1017 {
1018 case CTRL_PITCH_BENDER:
1019 voices[voice].bender = value;
1020 freq = compute_finetune (voices[voice].orig_freq, value, voices[voice].bender_range);
1021 voices[voice].current_freq = freq;
1022
1023 DISABLE_INTR (flags);
1024 gus_select_voice (voice);
1025 gus_voice_freq (freq);
1026 RESTORE_INTR (flags);
1027 break;
1028
1029 case CTRL_PITCH_BENDER_RANGE:
1030 voices[voice].bender_range = value;
1031 break;
1032
1033 case CTRL_EXPRESSION:
1034 volume_method = VOL_METHOD_ADAGIO;
1035 voices[voice].expression_vol = value;
1036 dynamic_volume_change (voice);
1037 break;
1038
1039 case CTRL_MAIN_VOLUME:
1040 volume_method = VOL_METHOD_ADAGIO;
1041 voices[voice].main_vol = value;
1042 dynamic_volume_change (voice);
1043 break;
1044
1045 default:
1046 break;
1047 }
1048 }
1049
1050 static int
1051 guswave_start_note (int dev, int voice, int note_num, int volume)
1052 {
1053 int sample, best_sample, best_delta, delta_freq;
1054 int is16bits, samplep, patch, pan;
1055 unsigned long note_freq, base_note, freq, flags;
1056 unsigned char mode = 0;
1057
1058 if (voice < 0 || voice > 31)
1059 {
1060 printk ("GUS: Invalid voice\n");
1061 return RET_ERROR (EINVAL);
1062 }
1063
1064 if (note_num == 255)
1065 {
1066 if (voices[voice].mode & WAVE_ENVELOPES)
1067 {
1068 voices[voice].midi_volume = volume;
1069 dynamic_volume_change (voice);
1070 return 0;
1071 }
1072
1073 compute_and_set_volume (voice, volume, 1);
1074 return 0;
1075 }
1076
1077 if ((patch = patch_map[voice]) == -1)
1078 {
1079 return RET_ERROR (EINVAL);
1080 }
1081
1082 if ((samplep = patch_table[patch]) == -1)
1083 {
1084 return RET_ERROR (EINVAL);
1085 }
1086
1087 note_freq = note_to_freq (note_num);
1088
1089
1090
1091
1092
1093 sample = -1;
1094
1095 best_sample = samplep;
1096 best_delta = 1000000;
1097 while (samplep >= 0 && sample == -1)
1098 {
1099 delta_freq = note_freq - samples[samplep].base_note;
1100 if (delta_freq < 0)
1101 delta_freq = -delta_freq;
1102 if (delta_freq < best_delta)
1103 {
1104 best_sample = samplep;
1105 best_delta = delta_freq;
1106 }
1107 if (samples[samplep].low_note <= note_freq && note_freq <= samples[samplep].high_note)
1108 sample = samplep;
1109 else
1110 samplep = samples[samplep].key;
1111 }
1112 if (sample == -1)
1113 sample = best_sample;
1114
1115 if (sample == -1)
1116 {
1117 printk ("GUS: Patch %d not defined for note %d\n", patch, note_num);
1118 return 0;
1119 }
1120
1121 is16bits = (samples[sample].mode & WAVE_16_BITS) ? 1 : 0;
1122 voices[voice].mode = samples[sample].mode;
1123 voices[voice].patch_vol = samples[sample].volume;
1124
1125 if (voices[voice].mode & WAVE_ENVELOPES)
1126 {
1127 int i;
1128
1129 for (i = 0; i < 6; i++)
1130 {
1131 voices[voice].env_rate[i] = samples[sample].env_rate[i];
1132 voices[voice].env_offset[i] = samples[sample].env_offset[i];
1133 }
1134 }
1135
1136 sample_map[voice] = sample;
1137
1138 base_note = samples[sample].base_note / 100;
1139 note_freq /= 100;
1140
1141 freq = samples[sample].base_freq * note_freq / base_note;
1142
1143 voices[voice].orig_freq = freq;
1144
1145
1146
1147
1148
1149
1150 freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);
1151 voices[voice].current_freq = freq;
1152
1153 pan = (samples[sample].panning + voices[voice].panning) / 32;
1154 pan += 7;
1155 if (pan < 0)
1156 pan = 0;
1157 if (pan > 15)
1158 pan = 15;
1159
1160 if (samples[sample].mode & WAVE_16_BITS)
1161 {
1162 mode |= 0x04;
1163 if ((sample_ptrs[sample] >> 18) !=
1164 ((sample_ptrs[sample] + samples[sample].len) >> 18))
1165 printk ("GUS: Sample address error\n");
1166 }
1167
1168
1169
1170
1171
1172 DISABLE_INTR (flags);
1173 gus_select_voice (voice);
1174 gus_voice_off ();
1175 gus_rampoff ();
1176 if (voices[voice].mode & WAVE_ENVELOPES)
1177 {
1178 compute_volume (voice, volume);
1179 init_envelope (voice);
1180 }
1181 else
1182 compute_and_set_volume (voice, volume, 0);
1183
1184 if (samples[sample].mode & WAVE_LOOP_BACK)
1185 gus_write_addr (0x0a, sample_ptrs[sample] + samples[sample].len, is16bits);
1186 else
1187 gus_write_addr (0x0a, sample_ptrs[sample], is16bits);
1188
1189 if (samples[sample].mode & WAVE_LOOPING)
1190 {
1191 mode |= 0x08;
1192
1193 if (samples[sample].mode & WAVE_BIDIR_LOOP)
1194 mode |= 0x10;
1195
1196 if (samples[sample].mode & WAVE_LOOP_BACK)
1197 {
1198 gus_write_addr (0x0a,
1199 sample_ptrs[sample] + samples[sample].loop_end, is16bits);
1200 mode |= 0x40;
1201 }
1202
1203 gus_write_addr (0x02, sample_ptrs[sample] + samples[sample].loop_start, is16bits);
1204 gus_write_addr (0x04, sample_ptrs[sample] + samples[sample].loop_end, is16bits);
1205 }
1206 else
1207 {
1208 mode |= 0x20;
1209 voices[voice].loop_irq_mode = LMODE_FINISH;
1210
1211 voices[voice].loop_irq_parm = 1;
1212 gus_write_addr (0x02, sample_ptrs[sample], is16bits);
1213 gus_write_addr (0x04, sample_ptrs[sample] + samples[sample].len, is16bits);
1214 }
1215 gus_voice_freq (freq);
1216 gus_voice_balance (pan);
1217 gus_voice_on (mode);
1218 RESTORE_INTR (flags);
1219
1220 return 0;
1221 }
1222
1223 static void
1224 guswave_reset (int dev)
1225 {
1226 int i;
1227
1228 for (i = 0; i < 32; i++)
1229 gus_voice_init (i);
1230 }
1231
1232 static int
1233 guswave_open (int dev, int mode)
1234 {
1235 int err;
1236
1237 if (gus_busy)
1238 return RET_ERROR (EBUSY);
1239
1240 if ((err = DMAbuf_open_dma (gus_devnum)))
1241 return err;
1242
1243 RESET_WAIT_QUEUE (dram_sleeper, dram_sleep_flag);
1244 gus_busy = 1;
1245 active_device = GUS_DEV_WAVE;
1246
1247 gus_reset ();
1248
1249 return 0;
1250 }
1251
1252 static void
1253 guswave_close (int dev)
1254 {
1255 gus_busy = 0;
1256 active_device = 0;
1257 gus_reset ();
1258
1259 DMAbuf_close_dma (gus_devnum);
1260 }
1261
1262 static int
1263 guswave_load_patch (int dev, int format, snd_rw_buf * addr,
1264 int offs, int count, int pmgr_flag)
1265 {
1266 struct patch_info patch;
1267 int instr;
1268 long sizeof_patch;
1269
1270 unsigned long blk_size, blk_end, left, src_offs, target;
1271
1272 sizeof_patch = (long) &patch.data[0] - (long) &patch;
1273
1274
1275 if (format != GUS_PATCH)
1276 {
1277 printk ("GUS Error: Invalid patch format (key) 0x%04x\n", format);
1278 return RET_ERROR (EINVAL);
1279 }
1280
1281 if (count < sizeof_patch)
1282 {
1283 printk ("GUS Error: Patch header too short\n");
1284 return RET_ERROR (EINVAL);
1285 }
1286
1287 count -= sizeof_patch;
1288
1289 if (free_sample >= MAX_SAMPLE)
1290 {
1291 printk ("GUS: Sample table full\n");
1292 return RET_ERROR (ENOSPC);
1293 }
1294
1295
1296
1297
1298
1299
1300 COPY_FROM_USER (&((char *) &patch)[offs], addr, offs, sizeof_patch - offs);
1301
1302 instr = patch.instr_no;
1303
1304 if (instr < 0 || instr > MAX_PATCH)
1305 {
1306 printk ("GUS: Invalid patch number %d\n", instr);
1307 return RET_ERROR (EINVAL);
1308 }
1309
1310 if (count < patch.len)
1311 {
1312 printk ("GUS Warning: Patch record too short (%d<%lu)\n",
1313 count, patch.len);
1314 patch.len = count;
1315 }
1316
1317 if (patch.len <= 0 || patch.len > gus_mem_size)
1318 {
1319 printk ("GUS: Invalid sample length %lu\n", patch.len);
1320 return RET_ERROR (EINVAL);
1321 }
1322
1323 if (patch.mode & WAVE_LOOPING)
1324 {
1325 if (patch.loop_start < 0 || patch.loop_start >= patch.len)
1326 {
1327 printk ("GUS: Invalid loop start\n");
1328 return RET_ERROR (EINVAL);
1329 }
1330
1331 if (patch.loop_end < patch.loop_start || patch.loop_end > patch.len)
1332 {
1333 printk ("GUS: Invalid loop end\n");
1334 return RET_ERROR (EINVAL);
1335 }
1336 }
1337
1338 free_mem_ptr = (free_mem_ptr + 31) & ~31;
1339
1340 #define GUS_BANK_SIZE (256*1024)
1341
1342 if (patch.mode & WAVE_16_BITS)
1343 {
1344
1345
1346
1347 if (patch.len >= GUS_BANK_SIZE)
1348 {
1349 printk ("GUS: Sample (16 bit) too long %lu\n", patch.len);
1350 return RET_ERROR (ENOSPC);
1351 }
1352
1353 if ((free_mem_ptr / GUS_BANK_SIZE) !=
1354 ((free_mem_ptr + patch.len) / GUS_BANK_SIZE))
1355 {
1356 unsigned long tmp_mem =
1357 ((free_mem_ptr / GUS_BANK_SIZE) + 1) * GUS_BANK_SIZE;
1358
1359 if ((tmp_mem + patch.len) > gus_mem_size)
1360 return RET_ERROR (ENOSPC);
1361
1362 free_mem_ptr = tmp_mem;
1363 }
1364 }
1365
1366 if ((free_mem_ptr + patch.len) > gus_mem_size)
1367 return RET_ERROR (ENOSPC);
1368
1369 sample_ptrs[free_sample] = free_mem_ptr;
1370
1371
1372
1373 if (patch.mode & WAVE_ENVELOPES)
1374 patch.mode &= ~WAVE_TREMOLO;
1375
1376 memcpy ((char *) &samples[free_sample], &patch, sizeof_patch);
1377
1378
1379
1380
1381
1382 samples[free_sample].key = patch_table[instr];
1383 patch_table[instr] = free_sample;
1384
1385
1386
1387
1388
1389 left = patch.len;
1390 src_offs = 0;
1391 target = free_mem_ptr;
1392
1393 while (left)
1394 {
1395 blk_size = sound_buffsizes[gus_devnum];
1396 if (blk_size > left)
1397 blk_size = left;
1398
1399
1400
1401
1402 blk_end = target + blk_size;
1403
1404 if ((target >> 18) != (blk_end >> 18))
1405 {
1406
1407 blk_end &= ~(256 * 1024 - 1);
1408 blk_size = blk_end - target;
1409 }
1410
1411 #if defined(GUS_NO_DMA) || defined(GUS_PATCH_NO_DMA)
1412
1413
1414
1415 {
1416 long i;
1417 unsigned char data;
1418
1419 for (i = 0; i < blk_size; i++)
1420 {
1421 GET_BYTE_FROM_USER (data, addr, sizeof_patch + i);
1422 if (patch.mode & WAVE_UNSIGNED)
1423
1424 if (!(patch.mode & WAVE_16_BITS) || (i & 0x01))
1425 data ^= 0x80;
1426 gus_poke (target + i, data);
1427 }
1428 }
1429 #else
1430 {
1431 unsigned long address, hold_address;
1432 unsigned char dma_command;
1433
1434
1435
1436
1437
1438 COPY_FROM_USER (snd_raw_buf[gus_devnum][0],
1439 addr, sizeof_patch + src_offs,
1440 blk_size);
1441
1442 gus_write8 (0x41, 0);
1443 DMAbuf_start_dma (gus_devnum, snd_raw_buf_phys[gus_devnum][0],
1444 blk_size, DMA_MODE_WRITE);
1445
1446
1447
1448
1449
1450 address = target;
1451
1452 if (sound_dsp_dmachan[gus_devnum] > 3)
1453 {
1454 hold_address = address;
1455 address = address >> 1;
1456 address &= 0x0001ffffL;
1457 address |= (hold_address & 0x000c0000L);
1458 }
1459
1460 gus_write16 (0x42, (address >> 4) & 0xffff);
1461
1462
1463
1464
1465
1466 dma_command = 0x21;
1467 if (patch.mode & WAVE_UNSIGNED)
1468 dma_command |= 0x80;
1469 if (patch.mode & WAVE_16_BITS)
1470 dma_command |= 0x40;
1471 if (sound_dsp_dmachan[gus_devnum] > 3)
1472 dma_command |= 0x04;
1473
1474 gus_write8 (0x41, dma_command);
1475
1476
1477
1478
1479 active_device = GUS_DEV_WAVE;
1480
1481 DO_SLEEP (dram_sleeper, dram_sleep_flag, HZ);
1482 if (TIMED_OUT (dram_sleeper, dram_sleep_flag))
1483 printk ("GUS: DMA Transfer timed out\n");
1484 }
1485 #endif
1486
1487
1488
1489
1490
1491 left -= blk_size;
1492 src_offs += blk_size;
1493 target += blk_size;
1494
1495 gus_write8 (0x41, 0);
1496 }
1497
1498 free_mem_ptr += patch.len;
1499
1500 if (!pmgr_flag)
1501 pmgr_inform (dev, PM_E_PATCH_LOADED, instr, free_sample, 0, 0);
1502 free_sample++;
1503 return 0;
1504 }
1505
1506 static void
1507 guswave_hw_control (int dev, unsigned char *event)
1508 {
1509 int voice, cmd;
1510 unsigned short p1, p2;
1511 unsigned long plong, flags;
1512
1513 cmd = event[2];
1514 voice = event[3];
1515 p1 = *(unsigned short *) &event[4];
1516 p2 = *(unsigned short *) &event[6];
1517 plong = *(unsigned long *) &event[4];
1518
1519 switch (cmd)
1520 {
1521
1522 case _GUS_NUMVOICES:
1523 DISABLE_INTR (flags);
1524 gus_select_voice (voice);
1525 gus_select_max_voices (p1);
1526 RESTORE_INTR (flags);
1527 break;
1528
1529 case _GUS_VOICESAMPLE:
1530 guswave_set_instr (dev, voice, p1);
1531 break;
1532
1533 case _GUS_VOICEON:
1534 DISABLE_INTR (flags);
1535 gus_select_voice (voice);
1536 p1 &= ~0x20;
1537 gus_voice_on (p1);
1538 RESTORE_INTR (flags);
1539 break;
1540
1541 case _GUS_VOICEOFF:
1542 DISABLE_INTR (flags);
1543 gus_select_voice (voice);
1544 gus_voice_off ();
1545 RESTORE_INTR (flags);
1546 break;
1547
1548 case _GUS_VOICEFADE:
1549 DISABLE_INTR (flags);
1550 gus_select_voice (voice);
1551 gus_voice_fade (voice);
1552 RESTORE_INTR (flags);
1553 break;
1554
1555 case _GUS_VOICEMODE:
1556 DISABLE_INTR (flags);
1557 gus_select_voice (voice);
1558 p1 &= ~0x20;
1559 gus_voice_mode (p1);
1560 RESTORE_INTR (flags);
1561 break;
1562
1563 case _GUS_VOICEBALA:
1564 DISABLE_INTR (flags);
1565 gus_select_voice (voice);
1566 gus_voice_balance (p1);
1567 RESTORE_INTR (flags);
1568 break;
1569
1570 case _GUS_VOICEFREQ:
1571 DISABLE_INTR (flags);
1572 gus_select_voice (voice);
1573 gus_voice_freq (plong);
1574 RESTORE_INTR (flags);
1575 break;
1576
1577 case _GUS_VOICEVOL:
1578 DISABLE_INTR (flags);
1579 gus_select_voice (voice);
1580 gus_voice_volume (p1);
1581 RESTORE_INTR (flags);
1582 break;
1583
1584 case _GUS_VOICEVOL2:
1585 voices[voice].initial_volume =
1586 voices[voice].current_volume = p1;
1587 break;
1588
1589 case _GUS_RAMPRANGE:
1590 if (voices[voice].mode & WAVE_ENVELOPES)
1591 break;
1592 DISABLE_INTR (flags);
1593 gus_select_voice (voice);
1594 gus_ramp_range (p1, p2);
1595 RESTORE_INTR (flags);
1596 break;
1597
1598 case _GUS_RAMPRATE:
1599 if (voices[voice].mode & WAVE_ENVELOPES)
1600 break;
1601 DISABLE_INTR (flags);
1602 gus_select_voice (voice);
1603 gus_ramp_rate (p1, p2);
1604 RESTORE_INTR (flags);
1605 break;
1606
1607 case _GUS_RAMPMODE:
1608 if (voices[voice].mode & WAVE_ENVELOPES)
1609 break;
1610 DISABLE_INTR (flags);
1611 gus_select_voice (voice);
1612 p1 &= ~0x20;
1613 gus_ramp_mode (p1);
1614 RESTORE_INTR (flags);
1615 break;
1616
1617 case _GUS_RAMPON:
1618 if (voices[voice].mode & WAVE_ENVELOPES)
1619 break;
1620 DISABLE_INTR (flags);
1621 gus_select_voice (voice);
1622 p1 &= ~0x20;
1623 gus_rampon (p1);
1624 RESTORE_INTR (flags);
1625 break;
1626
1627 case _GUS_RAMPOFF:
1628 if (voices[voice].mode & WAVE_ENVELOPES)
1629 break;
1630 DISABLE_INTR (flags);
1631 gus_select_voice (voice);
1632 gus_rampoff ();
1633 RESTORE_INTR (flags);
1634 break;
1635
1636 case _GUS_VOLUME_SCALE:
1637 volume_base = p1;
1638 volume_scale = p2;
1639 break;
1640
1641 default:;
1642 }
1643 }
1644
1645 static int
1646 gus_sampling_set_speed (int speed)
1647 {
1648 if (speed <= 0)
1649 return gus_sampling_speed;
1650
1651 if (speed > 44100)
1652 speed = 44100;
1653
1654 gus_sampling_speed = speed;
1655 return speed;
1656 }
1657
1658 static int
1659 gus_sampling_set_channels (int channels)
1660 {
1661 if (!channels)
1662 return gus_sampling_channels;
1663 if (channels > 2)
1664 channels = 2;
1665 if (channels < 1)
1666 channels = 1;
1667 gus_sampling_channels = channels;
1668 return channels;
1669 }
1670
1671 static int
1672 gus_sampling_set_bits (int bits)
1673 {
1674 if (!bits)
1675 return gus_sampling_bits;
1676
1677 if (bits != 8 && bits != 16)
1678 bits = 8;
1679
1680 gus_sampling_bits = bits;
1681 return bits;
1682 }
1683
1684 static int
1685 gus_sampling_ioctl (int dev, unsigned int cmd, unsigned int arg, int local)
1686 {
1687 switch (cmd)
1688 {
1689 case SOUND_PCM_WRITE_RATE:
1690 if (local)
1691 return gus_sampling_set_speed (arg);
1692 return IOCTL_OUT (arg, gus_sampling_set_speed (IOCTL_IN (arg)));
1693 break;
1694
1695 case SOUND_PCM_READ_RATE:
1696 if (local)
1697 return gus_sampling_speed;
1698 return IOCTL_OUT (arg, gus_sampling_speed);
1699 break;
1700
1701 case SNDCTL_DSP_STEREO:
1702 if (local)
1703 return gus_sampling_set_channels (arg + 1) - 1;
1704 return IOCTL_OUT (arg, gus_sampling_set_channels (IOCTL_IN (arg) + 1) - 1);
1705 break;
1706
1707 case SOUND_PCM_WRITE_CHANNELS:
1708 if (local) return gus_sampling_set_channels(arg);
1709 return IOCTL_OUT (arg, gus_sampling_set_channels (IOCTL_IN (arg)));
1710 break;
1711
1712 case SOUND_PCM_READ_CHANNELS:
1713 if (local)
1714 return gus_sampling_channels;
1715 return IOCTL_OUT (arg, gus_sampling_channels);
1716 break;
1717
1718 case SNDCTL_DSP_SAMPLESIZE:
1719 if (local)
1720 return gus_sampling_set_bits (arg);
1721 return IOCTL_OUT (arg, gus_sampling_set_bits (IOCTL_IN (arg)));
1722 break;
1723
1724 case SOUND_PCM_READ_BITS:
1725 if (local)
1726 return gus_sampling_bits;
1727 return IOCTL_OUT (arg, gus_sampling_bits);
1728
1729 case SOUND_PCM_WRITE_FILTER:
1730 return IOCTL_OUT (arg, RET_ERROR (EINVAL));
1731 break;
1732
1733 case SOUND_PCM_READ_FILTER:
1734 return IOCTL_OUT (arg, RET_ERROR (EINVAL));
1735 break;
1736
1737 default:
1738 return RET_ERROR (EINVAL);
1739 }
1740 return RET_ERROR (EINVAL);
1741 }
1742
1743 static void
1744 gus_sampling_reset (int dev)
1745 {
1746 }
1747
1748 static int
1749 gus_sampling_open (int dev, int mode)
1750 {
1751 #ifdef GUS_NO_DMA
1752 printk ("GUS: DMA mode not enabled. Device not supported\n");
1753 return RET_ERROR (ENXIO);
1754 #endif
1755
1756 if (gus_busy)
1757 return RET_ERROR (EBUSY);
1758
1759 gus_busy = 1;
1760 active_device = 0;
1761
1762 gus_reset ();
1763 reset_sample_memory ();
1764 gus_select_max_voices (14);
1765
1766 pcm_active = 0;
1767
1768 return 0;
1769 }
1770
1771 static void
1772 gus_sampling_close (int dev)
1773 {
1774 gus_reset ();
1775 gus_busy = 0;
1776 active_device = 0;
1777 }
1778
1779 static void
1780 play_next_pcm_block (void)
1781 {
1782 unsigned long flags;
1783 int speed = gus_sampling_speed;
1784 int this_one, is16bits, chn;
1785 unsigned long dram_loc;
1786 unsigned char mode[2], ramp_mode[2];
1787
1788 if (!pcm_qlen)
1789 return;
1790
1791 this_one = pcm_head;
1792
1793 for (chn = 0; chn < gus_sampling_channels; chn++)
1794 {
1795 mode[chn] = 0x00;
1796 ramp_mode[chn] = 0x03;
1797
1798 if (chn == 0)
1799 {
1800 mode[chn] |= 0x20;
1801 voices[chn].loop_irq_mode = LMODE_PCM;
1802 }
1803
1804 if (gus_sampling_bits != 8)
1805 {
1806 is16bits = 1;
1807 mode[chn] |= 0x04;
1808 }
1809 else
1810 is16bits = 0;
1811
1812 dram_loc = this_one * pcm_bsize;
1813 dram_loc += chn * pcm_banksize;
1814
1815 if (this_one == (pcm_nblk - 1))
1816 {
1817 mode[chn] |= 0x08;
1818 ramp_mode[chn] = 0x03;
1819 }
1820 else
1821 {
1822 if (chn == 0)
1823 ramp_mode[chn] = 0x04;
1824 }
1825
1826 DISABLE_INTR (flags);
1827 gus_select_voice (chn);
1828 gus_voice_freq (speed);
1829
1830 if (gus_sampling_channels == 1)
1831 gus_voice_balance (7);
1832 else if (chn == 0)
1833 gus_voice_balance (0);
1834 else
1835 gus_voice_balance (15);
1836
1837 if (!pcm_active)
1838 {
1839
1840
1841
1842
1843
1844
1845
1846 gus_voice_off ();
1847 gus_rampoff ();
1848 gus_voice_volume (4000);
1849 gus_ramp_range (65, 4030);
1850
1851 gus_write_addr (0x0a, dram_loc, is16bits);
1852 gus_write_addr (0x02, chn * pcm_banksize, is16bits);
1853
1854 if (chn != 0)
1855 gus_write_addr (0x04, pcm_banksize + (pcm_bsize * pcm_nblk),
1856 is16bits);
1857 }
1858
1859 if (chn == 0)
1860 gus_write_addr (0x04, dram_loc + pcm_datasize[this_one], is16bits);
1861 else
1862 mode[chn] |= 0x08;
1863
1864 if (pcm_datasize[this_one] != pcm_bsize)
1865 {
1866
1867 if (chn == 0)
1868 {
1869 mode[chn] &= ~0x08;
1870 mode[chn] |= 0x20;
1871 voices[0].loop_irq_mode = LMODE_PCM_STOP;
1872 ramp_mode[chn] = 0x03;
1873 }
1874 else
1875 {
1876 gus_write_addr (0x04, dram_loc + pcm_datasize[this_one], is16bits);
1877 mode[chn] &= ~0x08;
1878 }
1879 }
1880
1881 RESTORE_INTR (flags);
1882 }
1883
1884 for (chn = 0; chn < gus_sampling_channels; chn++)
1885 {
1886 DISABLE_INTR (flags);
1887 gus_select_voice (chn);
1888 gus_write8 (0x0d, ramp_mode[chn]);
1889 gus_voice_on (mode[chn]);
1890 RESTORE_INTR (flags);
1891 }
1892
1893 pcm_active = 1;
1894 }
1895
1896 static void
1897 gus_transfer_output_block (int dev, unsigned long buf,
1898 int total_count, int intrflag, int chn)
1899 {
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909 int this_one, count;
1910 unsigned long flags;
1911 unsigned char dma_command;
1912 unsigned long address, hold_address;
1913
1914 DISABLE_INTR (flags);
1915
1916 count = total_count / gus_sampling_channels;
1917
1918 if (chn == 0)
1919 {
1920 if (pcm_qlen >= pcm_nblk)
1921 printk ("GUS Warning: PCM buffers out of sync\n");
1922
1923 this_one = pcm_current_block = pcm_tail;
1924 pcm_qlen++;
1925 pcm_tail = (pcm_tail + 1) % pcm_nblk;
1926 pcm_datasize[this_one] = count;
1927 }
1928 else
1929 this_one = pcm_current_block;
1930
1931 gus_write8 (0x41, 0);
1932 DMAbuf_start_dma (dev, buf + (chn * count), count, DMA_MODE_WRITE);
1933
1934 address = this_one * pcm_bsize;
1935 address += chn * pcm_banksize;
1936
1937 if (sound_dsp_dmachan[dev] > 3)
1938 {
1939 hold_address = address;
1940 address = address >> 1;
1941 address &= 0x0001ffffL;
1942 address |= (hold_address & 0x000c0000L);
1943 }
1944
1945 gus_write16 (0x42, (address >> 4) & 0xffff);
1946
1947 dma_command = 0x21;
1948
1949 if (gus_sampling_bits != 8)
1950 dma_command |= 0x40;
1951 else
1952 dma_command |= 0x80;
1953
1954 if (sound_dsp_dmachan[dev] > 3)
1955 dma_command |= 0x04;
1956
1957 gus_write8 (0x41, dma_command);
1958
1959 if (chn == (gus_sampling_channels - 1))
1960 {
1961
1962 active_device = GUS_DEV_PCM_DONE;
1963 if (!pcm_active && (pcm_qlen > 2 || count < pcm_bsize))
1964 {
1965 play_next_pcm_block ();
1966 }
1967 }
1968 else
1969
1970 active_device = GUS_DEV_PCM_CONTINUE;
1971
1972 RESTORE_INTR (flags);
1973 }
1974
1975 static void
1976 gus_sampling_output_block (int dev, unsigned long buf, int total_count,
1977 int intrflag, int restart_dma)
1978 {
1979 pcm_current_buf = buf;
1980 pcm_current_count = total_count;
1981 pcm_current_intrflag = intrflag;
1982 pcm_current_dev = dev;
1983 gus_transfer_output_block (dev, buf, total_count, intrflag, 0);
1984 }
1985
1986 static void
1987 gus_sampling_start_input (int dev, unsigned long buf, int count,
1988 int intrflag, int restart_dma)
1989 {
1990 unsigned long flags;
1991 unsigned char mode;
1992
1993 DISABLE_INTR (flags);
1994
1995 DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ);
1996
1997 mode = 0xa0;
1998
1999 if (sound_dsp_dmachan[dev] > 3)
2000 mode |= 0x04;
2001 if (gus_sampling_channels > 1)
2002 mode |= 0x02;
2003 mode |= 0x01;
2004
2005 gus_write8 (0x49, mode);
2006
2007 RESTORE_INTR (flags);
2008 }
2009
2010 static int
2011 gus_sampling_prepare_for_input (int dev, int bsize, int bcount)
2012 {
2013 unsigned int rate;
2014
2015 rate = (9878400 / (gus_sampling_speed + 2)) / 16;
2016
2017 gus_write8 (0x48, rate & 0xff);
2018
2019 if (gus_sampling_bits != 8)
2020 {
2021 printk ("GUS Error: 16 bit recording not supported\n");
2022 return RET_ERROR (EINVAL);
2023 }
2024
2025 return 0;
2026 }
2027
2028 static int
2029 gus_sampling_prepare_for_output (int dev, int bsize, int bcount)
2030 {
2031 int i;
2032
2033 long mem_ptr, mem_size;
2034
2035 mem_ptr = 0;
2036 mem_size = gus_mem_size / gus_sampling_channels;
2037
2038 if (mem_size > (256 * 1024))
2039 mem_size = 256 * 1024;
2040
2041 pcm_bsize = bsize / gus_sampling_channels;
2042 pcm_head = pcm_tail = pcm_qlen = 0;
2043
2044 pcm_nblk = MAX_PCM_BUFFERS;
2045 if ((pcm_bsize * pcm_nblk) > mem_size)
2046 pcm_nblk = mem_size / pcm_bsize;
2047
2048 for (i = 0; i < pcm_nblk; i++)
2049 pcm_datasize[i] = 0;
2050
2051 pcm_banksize = pcm_nblk * pcm_bsize;
2052
2053 if (gus_sampling_bits != 8 && pcm_banksize == (256 * 1024))
2054 pcm_nblk--;
2055
2056 return 0;
2057 }
2058
2059 static int
2060 gus_has_output_drained (int dev)
2061 {
2062 return !pcm_qlen;
2063 }
2064
2065 static void
2066 gus_copy_from_user (int dev, char *localbuf, int localoffs,
2067 snd_rw_buf * userbuf, int useroffs, int len)
2068 {
2069 if (gus_sampling_channels == 1)
2070 {
2071 COPY_FROM_USER (&localbuf[localoffs], userbuf, useroffs, len);
2072 }
2073 else if (gus_sampling_bits == 8)
2074 {
2075 int in_left = useroffs;
2076 int in_right = useroffs + 1;
2077 char *out_left, *out_right;
2078 int i;
2079
2080 len /= 2;
2081 localoffs /= 2;
2082 out_left = &localbuf[localoffs];
2083 out_right = out_left + pcm_bsize;
2084
2085 for (i = 0; i < len; i++)
2086 {
2087 GET_BYTE_FROM_USER (*out_left++, userbuf, in_left);
2088 in_left += 2;
2089 GET_BYTE_FROM_USER (*out_right++, userbuf, in_right);
2090 in_right += 2;
2091 }
2092 }
2093 else
2094 {
2095 int in_left = useroffs;
2096 int in_right = useroffs + 1;
2097 short *out_left, *out_right;
2098 int i;
2099
2100 len /= 4;
2101 localoffs /= 4;
2102
2103 out_left = (short *) &localbuf[localoffs];
2104 out_right = out_left + (pcm_bsize / 2);
2105
2106 for (i = 0; i < len; i++)
2107 {
2108 GET_SHORT_FROM_USER (*out_left++, (short *) userbuf, in_left);
2109 in_left += 2;
2110 GET_SHORT_FROM_USER (*out_right++, (short *) userbuf, in_right);
2111 in_right += 2;
2112 }
2113 }
2114 }
2115
2116 static struct audio_operations gus_sampling_operations =
2117 {
2118 "Gravis UltraSound",
2119 gus_sampling_open,
2120 gus_sampling_close,
2121 gus_sampling_output_block,
2122 gus_sampling_start_input,
2123 gus_sampling_ioctl,
2124 gus_sampling_prepare_for_input,
2125 gus_sampling_prepare_for_output,
2126 gus_sampling_reset,
2127 gus_sampling_reset,
2128 gus_has_output_drained,
2129 gus_copy_from_user
2130 };
2131
2132 static int
2133 guswave_patchmgr (int dev, struct patmgr_info *rec)
2134 {
2135 int i, n;
2136
2137 switch (rec->command)
2138 {
2139 case PM_GET_DEVTYPE:
2140 rec->parm1 = PMTYPE_WAVE;
2141 return 0;
2142 break;
2143
2144 case PM_GET_NRPGM:
2145 rec->parm1 = MAX_PATCH;
2146 return 0;
2147 break;
2148
2149 case PM_GET_PGMMAP:
2150 rec->parm1 = MAX_PATCH;
2151
2152 for (i = 0; i < MAX_PATCH; i++)
2153 {
2154 int ptr = patch_table[i];
2155
2156 rec->data.data8[i] = 0;
2157
2158 while (ptr >= 0 && ptr < free_sample)
2159 {
2160 rec->data.data8[i]++;
2161 ptr = samples[ptr].key;
2162 }
2163 }
2164 return 0;
2165 break;
2166
2167 case PM_GET_PGM_PATCHES:
2168 {
2169 int ptr = patch_table[rec->parm1];
2170
2171 n = 0;
2172
2173 while (ptr >= 0 && ptr < free_sample)
2174 {
2175 rec->data.data32[n++] = ptr;
2176 ptr = samples[ptr].key;
2177 }
2178 }
2179 rec->parm1 = n;
2180 return 0;
2181 break;
2182
2183 case PM_GET_PATCH:
2184 {
2185 int ptr = rec->parm1;
2186 struct patch_info *pat;
2187
2188 if (ptr < 0 || ptr >= free_sample)
2189 return RET_ERROR (EINVAL);
2190
2191 memcpy (rec->data.data8, (char *) &samples[ptr],
2192 sizeof (struct patch_info));
2193
2194 pat = (struct patch_info *) rec->data.data8;
2195
2196 pat->key = GUS_PATCH;
2197 rec->parm1 = sample_ptrs[ptr];
2198 rec->parm2 = sizeof (struct patch_info);
2199 }
2200 return 0;
2201 break;
2202
2203 case PM_SET_PATCH:
2204 {
2205 int ptr = rec->parm1;
2206 struct patch_info *pat;
2207
2208 if (ptr < 0 || ptr >= free_sample)
2209 return RET_ERROR (EINVAL);
2210
2211 pat = (struct patch_info *) rec->data.data8;
2212
2213 if (pat->len > samples[ptr].len)
2214 return RET_ERROR (EINVAL);
2215
2216 pat->key = samples[ptr].key;
2217
2218 memcpy ((char *) &samples[ptr], rec->data.data8,
2219 sizeof (struct patch_info));
2220
2221 pat->key = GUS_PATCH;
2222 }
2223 return 0;
2224 break;
2225
2226 case PM_READ_PATCH:
2227 {
2228 int sample = rec->parm1;
2229 int n;
2230 long offs = rec->parm2;
2231 int l = rec->parm3;
2232
2233 if (sample < 0 || sample >= free_sample)
2234 return RET_ERROR (EINVAL);
2235
2236 if (offs < 0 || offs >= samples[sample].len)
2237 return RET_ERROR (EINVAL);
2238
2239 n = samples[sample].len - offs;
2240
2241 if (l > n)
2242 l = n;
2243
2244 if (l > sizeof (rec->data.data8))
2245 l = sizeof (rec->data.data8);
2246
2247 if (l <= 0)
2248 return RET_ERROR (EINVAL);
2249
2250 offs += sample_ptrs[sample];
2251
2252 for (n = 0; n < l; n++)
2253 rec->data.data8[n] = gus_peek (offs++);
2254 rec->parm1 = n;
2255 }
2256 return 0;
2257 break;
2258
2259 case PM_WRITE_PATCH:
2260 {
2261 int sample = rec->parm1;
2262 int n;
2263 long offs = rec->parm2;
2264 int l = rec->parm3;
2265
2266 if (sample < 0 || sample >= free_sample)
2267 return RET_ERROR (EINVAL);
2268
2269 if (offs < 0 || offs >= samples[sample].len)
2270 return RET_ERROR (EINVAL);
2271
2272 n = samples[sample].len - offs;
2273
2274 if (l > n)
2275 l = n;
2276
2277 if (l > sizeof (rec->data.data8))
2278 l = sizeof (rec->data.data8);
2279
2280 if (l <= 0)
2281 return RET_ERROR (EINVAL);
2282
2283 offs += sample_ptrs[sample];
2284
2285 for (n = 0; n < l; n++)
2286 gus_poke (offs++, rec->data.data8[n]);
2287 rec->parm1 = n;
2288 }
2289 return 0;
2290 break;
2291
2292 default:
2293 return RET_ERROR (EINVAL);
2294 }
2295 }
2296
2297 static struct synth_operations guswave_operations =
2298 {
2299 &gus_info,
2300 SYNTH_TYPE_SAMPLE,
2301 SAMPLE_TYPE_GUS,
2302 guswave_open,
2303 guswave_close,
2304 guswave_ioctl,
2305 guswave_kill_note,
2306 guswave_start_note,
2307 guswave_set_instr,
2308 guswave_reset,
2309 guswave_hw_control,
2310 guswave_load_patch,
2311 guswave_aftertouch,
2312 guswave_controller,
2313 guswave_panning,
2314 guswave_patchmgr
2315 };
2316
2317 long
2318 gus_wave_init (long mem_start, int irq, int dma)
2319 {
2320 printk (" <Gravis UltraSound %luk>", gus_mem_size / 1024);
2321
2322 if (irq < 0 || irq > 15)
2323 {
2324 printk ("ERROR! Invalid IRQ#%d. GUS Disabled", irq);
2325 return mem_start;
2326 }
2327
2328 if (dma < 0 || dma > 7)
2329 {
2330 printk ("ERROR! Invalid DMA#%d. GUS Disabled", dma);
2331 return mem_start;
2332 }
2333
2334 gus_irq = irq;
2335 gus_dma = dma;
2336
2337 if (num_synths >= MAX_SYNTH_DEV)
2338 printk ("GUS Error: Too many synthesizers\n");
2339 else
2340 synth_devs[num_synths++] = &guswave_operations;
2341
2342 reset_sample_memory ();
2343
2344 gus_initialize ();
2345
2346 if (num_dspdevs < MAX_DSP_DEV)
2347 {
2348 dsp_devs[gus_devnum = num_dspdevs++] = &gus_sampling_operations;
2349 sound_dsp_dmachan[gus_devnum] = dma;
2350 sound_buffcounts[gus_devnum] = 1;
2351 sound_buffsizes[gus_devnum] = DSP_BUFFSIZE;
2352 sound_dma_automode[gus_devnum] = 0;
2353 }
2354 else
2355 printk ("GUS: Too many PCM devices available\n");
2356
2357 return mem_start;
2358 }
2359
2360 static void
2361 do_loop_irq (int voice)
2362 {
2363 unsigned char tmp;
2364 int mode, parm;
2365 unsigned long flags;
2366
2367 DISABLE_INTR (flags);
2368 gus_select_voice (voice);
2369
2370 tmp = gus_read8 (0x00);
2371 tmp &= ~0x20;
2372 gus_write8 (0x00, tmp);
2373
2374 mode = voices[voice].loop_irq_mode;
2375 voices[voice].loop_irq_mode = 0;
2376 parm = voices[voice].loop_irq_parm;
2377
2378 switch (mode)
2379 {
2380
2381 case LMODE_FINISH:
2382
2383 if ((gus_read16 (0x09) >> 4) < 100)
2384 {
2385 gus_voice_off ();
2386 gus_rampoff ();
2387 gus_voice_init (voice);
2388 return;
2389 }
2390 gus_ramp_range (65, 4065);
2391 gus_ramp_rate (0, 63);
2392 gus_rampon (0x20 | 0x40);
2393 voices[voice].volume_irq_mode = VMODE_HALT;
2394 break;
2395
2396 case LMODE_PCM_STOP:
2397 pcm_active = 0;
2398 case LMODE_PCM:
2399 {
2400 int orig_qlen = pcm_qlen;
2401
2402 pcm_qlen--;
2403 pcm_head = (pcm_head + 1) % pcm_nblk;
2404 if (pcm_qlen)
2405 {
2406 play_next_pcm_block ();
2407 }
2408 else
2409 {
2410 gus_voice_off ();
2411 gus_rampoff ();
2412 pcm_active = 0;
2413 }
2414
2415 if (orig_qlen == pcm_nblk)
2416 {
2417 DMAbuf_outputintr (gus_devnum, 0);
2418 }
2419 }
2420 break;
2421
2422 default:;
2423 }
2424 RESTORE_INTR (flags);
2425 }
2426
2427 static void
2428 do_volume_irq (int voice)
2429 {
2430 unsigned char tmp;
2431 int mode, parm;
2432 unsigned long flags;
2433
2434 DISABLE_INTR (flags);
2435
2436 gus_select_voice (voice);
2437
2438 tmp = gus_read8 (0x0d);
2439 tmp &= ~0x20;
2440 gus_write8 (0x0d, tmp);
2441
2442 mode = voices[voice].volume_irq_mode;
2443 voices[voice].volume_irq_mode = 0;
2444 parm = voices[voice].volume_irq_parm;
2445
2446 switch (mode)
2447 {
2448 case VMODE_HALT:
2449 gus_voice_init (voice);
2450 break;
2451
2452 case VMODE_ENVELOPE:
2453 gus_rampoff ();
2454 step_envelope (voice);
2455 break;
2456
2457 default:;
2458 }
2459
2460 RESTORE_INTR (flags);
2461 }
2462
2463 void
2464 gus_voice_irq (void)
2465 {
2466 unsigned long wave_ignore = 0, volume_ignore = 0;
2467 unsigned long voice_bit;
2468
2469 unsigned char src, voice;
2470
2471 while (1)
2472 {
2473 src = gus_read8 (0x0f);
2474 voice = src & 0x1f;
2475 src &= 0xc0;
2476
2477 if (src == (0x80 | 0x40))
2478 return;
2479
2480 voice_bit = 1 << voice;
2481
2482 if (!(src & 0x80))
2483 if (!(wave_ignore & voice_bit) && voice < nr_voices)
2484 {
2485 wave_ignore |= voice_bit;
2486 do_loop_irq (voice);
2487 }
2488
2489 if (!(src & 0x40))
2490 if (!(volume_ignore & voice_bit) && voice < nr_voices)
2491 {
2492 volume_ignore |= voice_bit;
2493 do_volume_irq (voice);
2494 }
2495 }
2496 }
2497
2498 void
2499 guswave_dma_irq (void)
2500 {
2501 unsigned char status;
2502
2503 status = gus_look8 (0x41);
2504 if (status & 0x40)
2505 switch (active_device)
2506 {
2507 case GUS_DEV_WAVE:
2508 if (SOMEONE_WAITING (dram_sleep_flag))
2509 WAKE_UP (dram_sleeper, dram_sleep_flag);
2510 break;
2511
2512 case GUS_DEV_PCM_CONTINUE:
2513 gus_transfer_output_block (pcm_current_dev, pcm_current_buf,
2514 pcm_current_count,
2515 pcm_current_intrflag, 1);
2516 break;
2517
2518 case GUS_DEV_PCM_DONE:
2519 if (pcm_qlen < pcm_nblk)
2520 {
2521 DMAbuf_outputintr (gus_devnum, pcm_qlen == 0);
2522 }
2523 break;
2524
2525 default:;
2526 }
2527
2528 status = gus_look8 (0x49);
2529 if (status & 0x40)
2530 {
2531 DMAbuf_inputintr (gus_devnum);
2532 }
2533
2534 }
2535
2536 #endif