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