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_set_voice_pos
- gus_voice_init
- gus_voice_init2
- 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
- guswave_volume_method
- compute_volume
- compute_and_set_volume
- dynamic_volume_change
- guswave_controller
- guswave_start_note2
- 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
- gus_sampling_update_volume
- 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_local_qlen
- gus_copy_from_user
- guswave_bender
- guswave_patchmgr
- guswave_alloc
- set_input_volumes
- gus_default_mixer_ioctl
- gus_default_mixer_init
- 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 #include "sound_config.h"
31 #include <linux/ultrasound.h>
32 #include "gus_hw.h"
33
34 #if defined(CONFIGURE_SOUNDCARD) && !defined(EXCLUDE_GUS)
35
36 #define MAX_SAMPLE 128
37 #define MAX_PATCH 256
38
39 struct voice_info
40 {
41 unsigned long orig_freq;
42 unsigned long current_freq;
43 unsigned long mode;
44 int bender;
45 int bender_range;
46 int panning;
47 int midi_volume;
48 unsigned int initial_volume;
49 unsigned int current_volume;
50 int loop_irq_mode, loop_irq_parm;
51 #define LMODE_FINISH 1
52 #define LMODE_PCM 2
53 #define LMODE_PCM_STOP 3
54 int volume_irq_mode, volume_irq_parm;
55 #define VMODE_HALT 1
56 #define VMODE_ENVELOPE 2
57 #define VMODE_START_NOTE 3
58
59 int env_phase;
60 unsigned char env_rate[6];
61 unsigned char env_offset[6];
62
63
64
65
66 int main_vol, expression_vol, patch_vol;
67
68
69 int dev_pending, note_pending, volume_pending, sample_pending;
70 char kill_pending;
71 long offset_pending;
72
73 };
74
75 static struct voice_alloc_info *voice_alloc;
76
77 extern int gus_base;
78 extern int gus_irq, gus_dma;
79 static long gus_mem_size = 0;
80 static long free_mem_ptr = 0;
81 static int gus_busy = 0;
82 static int nr_voices = 0;
83 static int gus_devnum = 0;
84 static int volume_base, volume_scale, volume_method;
85 static int gus_recmask = SOUND_MASK_MIC;
86 static int recording_active = 0;
87
88 int gus_wave_volume = 60;
89 int gus_pcm_volume = 80;
90 int have_gus_max = 0;
91 static int gus_line_vol = 100, gus_mic_vol = 0;
92 static unsigned char mix_image = 0x00;
93
94
95
96
97
98 static int active_device = 0;
99
100 #define GUS_DEV_WAVE 1
101 #define GUS_DEV_PCM_DONE 2
102 #define GUS_DEV_PCM_CONTINUE 3
103
104 static int gus_sampling_speed;
105 static int gus_sampling_channels;
106 static int gus_sampling_bits;
107
108 DEFINE_WAIT_QUEUE (dram_sleeper, dram_sleep_flag);
109
110
111
112
113 #define MAX_PCM_BUFFERS (32*MAX_REALTIME_FACTOR)
114
115 static int pcm_bsize, pcm_nblk, pcm_banksize;
116 static int pcm_datasize[MAX_PCM_BUFFERS];
117 static volatile int pcm_head, pcm_tail, pcm_qlen;
118 static volatile int pcm_active;
119 static volatile int dma_active;
120 static int pcm_opened = 0;
121 static int pcm_current_dev;
122 static int pcm_current_block;
123 static unsigned long pcm_current_buf;
124 static int pcm_current_count;
125 static int pcm_current_intrflag;
126
127 struct voice_info voices[32];
128
129 static int freq_div_table[] =
130 {
131 44100,
132 41160,
133 38587,
134 36317,
135 34300,
136 32494,
137 30870,
138 29400,
139 28063,
140 26843,
141 25725,
142 24696,
143 23746,
144 22866,
145 22050,
146 21289,
147 20580,
148 19916,
149 19293
150 };
151
152 static struct patch_info *samples;
153 static long sample_ptrs[MAX_SAMPLE + 1];
154 static int sample_map[32];
155 static int free_sample;
156
157
158 static int patch_table[MAX_PATCH];
159 static int patch_map[32];
160
161 static struct synth_info gus_info =
162 {"Gravis UltraSound", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_GUS, 0, 16, 0, MAX_PATCH};
163
164 static void gus_poke (long addr, unsigned char data);
165 static void compute_and_set_volume (int voice, int volume, int ramp_time);
166 extern unsigned short gus_adagio_vol (int vel, int mainv, int xpn, int voicev);
167 extern unsigned short gus_linear_vol (int vol, int mainvol);
168 static void compute_volume (int voice, int volume);
169 static void do_volume_irq (int voice);
170 static void set_input_volumes (void);
171
172 #define INSTANT_RAMP -1
173 #define FAST_RAMP 0
174
175 static void
176 reset_sample_memory (void)
177 {
178 int i;
179
180 for (i = 0; i <= MAX_SAMPLE; i++)
181 sample_ptrs[i] = -1;
182 for (i = 0; i < 32; i++)
183 sample_map[i] = -1;
184 for (i = 0; i < 32; i++)
185 patch_map[i] = -1;
186
187 gus_poke (0, 0);
188 gus_poke (1, 0);
189 free_mem_ptr = 2;
190
191 free_sample = 0;
192
193 for (i = 0; i < MAX_PATCH; i++)
194 patch_table[i] = -1;
195 }
196
197 void
198 gus_delay (void)
199 {
200 int i;
201
202 for (i = 0; i < 7; i++)
203 INB (u_DRAMIO);
204 }
205
206 static void
207 gus_poke (long addr, unsigned char data)
208 {
209 unsigned long flags;
210
211 DISABLE_INTR (flags);
212 OUTB (0x43, u_Command);
213 OUTB (addr & 0xff, u_DataLo);
214 OUTB ((addr >> 8) & 0xff, u_DataHi);
215
216 OUTB (0x44, u_Command);
217 OUTB ((addr >> 16) & 0xff, u_DataHi);
218 OUTB (data, u_DRAMIO);
219 RESTORE_INTR (flags);
220 }
221
222 static unsigned char
223 gus_peek (long addr)
224 {
225 unsigned long flags;
226 unsigned char tmp;
227
228 DISABLE_INTR (flags);
229 OUTB (0x43, u_Command);
230 OUTB (addr & 0xff, u_DataLo);
231 OUTB ((addr >> 8) & 0xff, u_DataHi);
232
233 OUTB (0x44, u_Command);
234 OUTB ((addr >> 16) & 0xff, u_DataHi);
235 tmp = INB (u_DRAMIO);
236 RESTORE_INTR (flags);
237
238 return tmp;
239 }
240
241 void
242 gus_write8 (int reg, unsigned int data)
243 {
244 unsigned long flags;
245
246 DISABLE_INTR (flags);
247
248 OUTB (reg, u_Command);
249 OUTB ((unsigned char) (data & 0xff), u_DataHi);
250
251 RESTORE_INTR (flags);
252 }
253
254 unsigned char
255 gus_read8 (int reg)
256 {
257 unsigned long flags;
258 unsigned char val;
259
260 DISABLE_INTR (flags);
261 OUTB (reg | 0x80, u_Command);
262 val = INB (u_DataHi);
263 RESTORE_INTR (flags);
264
265 return val;
266 }
267
268 unsigned char
269 gus_look8 (int reg)
270 {
271 unsigned long flags;
272 unsigned char val;
273
274 DISABLE_INTR (flags);
275 OUTB (reg, u_Command);
276 val = INB (u_DataHi);
277 RESTORE_INTR (flags);
278
279 return val;
280 }
281
282 void
283 gus_write16 (int reg, unsigned int data)
284 {
285 unsigned long flags;
286
287 DISABLE_INTR (flags);
288
289 OUTB (reg, u_Command);
290
291 OUTB ((unsigned char) (data & 0xff), u_DataLo);
292 OUTB ((unsigned char) ((data >> 8) & 0xff), u_DataHi);
293
294 RESTORE_INTR (flags);
295 }
296
297 unsigned short
298 gus_read16 (int reg)
299 {
300 unsigned long flags;
301 unsigned char hi, lo;
302
303 DISABLE_INTR (flags);
304
305 OUTB (reg | 0x80, u_Command);
306
307 lo = INB (u_DataLo);
308 hi = INB (u_DataHi);
309
310 RESTORE_INTR (flags);
311
312 return ((hi << 8) & 0xff00) | lo;
313 }
314
315 void
316 gus_write_addr (int reg, unsigned long address, int is16bit)
317 {
318 unsigned long hold_address;
319 unsigned long flags;
320
321 DISABLE_INTR (flags);
322 if (is16bit)
323 {
324
325
326
327
328 hold_address = address;
329 address = address >> 1;
330 address &= 0x0001ffffL;
331 address |= (hold_address & 0x000c0000L);
332 }
333
334 gus_write16 (reg, (unsigned short) ((address >> 7) & 0xffff));
335 gus_write16 (reg + 1, (unsigned short) ((address << 9) & 0xffff));
336
337 gus_delay ();
338 gus_write16 (reg, (unsigned short) ((address >> 7) & 0xffff));
339 gus_write16 (reg + 1, (unsigned short) ((address << 9) & 0xffff));
340 RESTORE_INTR (flags);
341 }
342
343 static void
344 gus_select_voice (int voice)
345 {
346 if (voice < 0 || voice > 31)
347 return;
348
349 OUTB (voice, u_Voice);
350 }
351
352 static void
353 gus_select_max_voices (int nvoices)
354 {
355 if (nvoices < 14)
356 nvoices = 14;
357 if (nvoices > 32)
358 nvoices = 32;
359
360 voice_alloc->max_voice = nr_voices = nvoices;
361
362 gus_write8 (0x0e, (nvoices - 1) | 0xc0);
363 }
364
365 static void
366 gus_voice_on (unsigned int mode)
367 {
368 gus_write8 (0x00, (unsigned char) (mode & 0xfc));
369 gus_delay ();
370 gus_write8 (0x00, (unsigned char) (mode & 0xfc));
371 }
372
373 static void
374 gus_voice_off (void)
375 {
376 gus_write8 (0x00, gus_read8 (0x00) | 0x03);
377 }
378
379 static void
380 gus_voice_mode (unsigned int m)
381 {
382 unsigned char mode = (unsigned char) (m & 0xff);
383
384 gus_write8 (0x00, (gus_read8 (0x00) & 0x03) |
385 (mode & 0xfc));
386 gus_delay ();
387 gus_write8 (0x00, (gus_read8 (0x00) & 0x03) | (mode & 0xfc));
388 }
389
390 static void
391 gus_voice_freq (unsigned long freq)
392 {
393 unsigned long divisor = freq_div_table[nr_voices - 14];
394 unsigned short fc;
395
396 fc = (unsigned short) (((freq << 9) + (divisor >> 1)) / divisor);
397 fc = fc << 1;
398
399 gus_write16 (0x01, fc);
400 }
401
402 static void
403 gus_voice_volume (unsigned int vol)
404 {
405 gus_write8 (0x0d, 0x03);
406 gus_write16 (0x09, (unsigned short) (vol << 4));
407 }
408
409 static void
410 gus_voice_balance (unsigned int balance)
411 {
412 gus_write8 (0x0c, (unsigned char) (balance & 0xff));
413 }
414
415 static void
416 gus_ramp_range (unsigned int low, unsigned int high)
417 {
418 gus_write8 (0x07, (unsigned char) ((low >> 4) & 0xff));
419 gus_write8 (0x08, (unsigned char) ((high >> 4) & 0xff));
420 }
421
422 static void
423 gus_ramp_rate (unsigned int scale, unsigned int rate)
424 {
425 gus_write8 (0x06, (unsigned char) (((scale & 0x03) << 6) | (rate & 0x3f)));
426 }
427
428 static void
429 gus_rampon (unsigned int m)
430 {
431 unsigned char mode = (unsigned char) (m & 0xff);
432
433 gus_write8 (0x0d, mode & 0xfc);
434 gus_delay ();
435 gus_write8 (0x0d, mode & 0xfc);
436 }
437
438 static void
439 gus_ramp_mode (unsigned int m)
440 {
441 unsigned char mode = (unsigned char) (m & 0xff);
442
443 gus_write8 (0x0d, (gus_read8 (0x0d) & 0x03) |
444 (mode & 0xfc));
445 gus_delay ();
446 gus_write8 (0x0d, (gus_read8 (0x0d) & 0x03) | (mode & 0xfc));
447 }
448
449 static void
450 gus_rampoff (void)
451 {
452 gus_write8 (0x0d, 0x03);
453 }
454
455 static void
456 gus_set_voice_pos (int voice, long position)
457 {
458 int sample_no;
459
460 if ((sample_no = sample_map[voice]) != -1)
461 if (position < samples[sample_no].len)
462 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
463 voices[voice].offset_pending = position;
464 else
465 gus_write_addr (0x0a, sample_ptrs[sample_no] + position,
466 samples[sample_no].mode & WAVE_16_BITS);
467 }
468
469 static void
470 gus_voice_init (int voice)
471 {
472 unsigned long flags;
473
474 DISABLE_INTR (flags);
475 gus_select_voice (voice);
476 gus_voice_volume (0);
477 gus_write_addr (0x0a, 0, 0);
478 gus_write8 (0x00, 0x03);
479 gus_write8 (0x0d, 0x03);
480 voice_alloc->map[voice] = 0;
481 RESTORE_INTR (flags);
482
483 }
484
485 static void
486 gus_voice_init2 (int voice)
487 {
488 voices[voice].panning = 0;
489 voices[voice].mode = 0;
490 voices[voice].orig_freq = 20000;
491 voices[voice].current_freq = 20000;
492 voices[voice].bender = 0;
493 voices[voice].bender_range = 200;
494 voices[voice].initial_volume = 0;
495 voices[voice].current_volume = 0;
496 voices[voice].loop_irq_mode = 0;
497 voices[voice].loop_irq_parm = 0;
498 voices[voice].volume_irq_mode = 0;
499 voices[voice].volume_irq_parm = 0;
500 voices[voice].env_phase = 0;
501 voices[voice].main_vol = 127;
502 voices[voice].patch_vol = 127;
503 voices[voice].expression_vol = 127;
504 voices[voice].sample_pending = -1;
505 }
506
507 static void
508 step_envelope (int voice)
509 {
510 unsigned vol, prev_vol, phase;
511 unsigned char rate;
512 long int flags;
513
514 if (voices[voice].mode & WAVE_SUSTAIN_ON && voices[voice].env_phase == 2)
515 {
516 DISABLE_INTR (flags);
517 gus_select_voice (voice);
518 gus_rampoff ();
519 RESTORE_INTR (flags);
520 return;
521
522
523
524 }
525
526 if (voices[voice].env_phase >= 5)
527 {
528 gus_voice_init (voice);
529 return;
530 }
531
532 prev_vol = voices[voice].current_volume;
533 phase = ++voices[voice].env_phase;
534 compute_volume (voice, voices[voice].midi_volume);
535 vol = voices[voice].initial_volume * voices[voice].env_offset[phase] / 255;
536 rate = voices[voice].env_rate[phase];
537
538 DISABLE_INTR (flags);
539 gus_select_voice (voice);
540
541 gus_voice_volume (prev_vol);
542
543
544 gus_write8 (0x06, rate);
545
546 voices[voice].volume_irq_mode = VMODE_ENVELOPE;
547
548 if (((vol - prev_vol) / 64) == 0)
549 {
550 RESTORE_INTR (flags);
551 step_envelope (voice);
552 return;
553 }
554
555 if (vol > prev_vol)
556 {
557 if (vol >= (4096 - 64))
558 vol = 4096 - 65;
559 gus_ramp_range (0, vol);
560 gus_rampon (0x20);
561 }
562 else
563 {
564 if (vol <= 64)
565 vol = 65;
566 gus_ramp_range (vol, 4030);
567 gus_rampon (0x60);
568 }
569 voices[voice].current_volume = vol;
570 RESTORE_INTR (flags);
571 }
572
573 static void
574 init_envelope (int voice)
575 {
576 voices[voice].env_phase = -1;
577 voices[voice].current_volume = 64;
578
579 step_envelope (voice);
580 }
581
582 static void
583 start_release (int voice, long int flags)
584 {
585 if (gus_read8 (0x00) & 0x03)
586 return;
587
588 voices[voice].env_phase = 2;
589
590 voices[voice].current_volume =
591 voices[voice].initial_volume =
592 gus_read16 (0x09) >> 4;
593
594 voices[voice].mode &= ~WAVE_SUSTAIN_ON;
595 gus_rampoff ();
596 RESTORE_INTR (flags);
597 step_envelope (voice);
598 }
599
600 static void
601 gus_voice_fade (int voice)
602 {
603 int instr_no = sample_map[voice], is16bits;
604 long int flags;
605
606 DISABLE_INTR (flags);
607 gus_select_voice (voice);
608
609 if (instr_no < 0 || instr_no > MAX_SAMPLE)
610 {
611 gus_write8 (0x00, 0x03);
612 voice_alloc->map[voice] = 0;
613 RESTORE_INTR (flags);
614 return;
615 }
616
617 is16bits = (samples[instr_no].mode & WAVE_16_BITS) ? 1 : 0;
618
619 if (voices[voice].mode & WAVE_ENVELOPES)
620 {
621 start_release (voice, flags);
622 return;
623 }
624
625
626
627
628 if ((int) (gus_read16 (0x09) >> 4) < 100)
629 {
630 gus_voice_off ();
631 gus_rampoff ();
632 gus_voice_init (voice);
633 return;
634 }
635
636 gus_ramp_range (65, 4030);
637 gus_ramp_rate (2, 4);
638 gus_rampon (0x40 | 0x20);
639 voices[voice].volume_irq_mode = VMODE_HALT;
640 RESTORE_INTR (flags);
641 }
642
643 static void
644 gus_reset (void)
645 {
646 int i;
647
648 gus_select_max_voices (24);
649 volume_base = 3071;
650 volume_scale = 4;
651 volume_method = VOL_METHOD_ADAGIO;
652
653 for (i = 0; i < 32; i++)
654 {
655 gus_voice_init (i);
656 gus_voice_init2 (i);
657 }
658
659 INB (u_Status);
660
661 gus_look8 (0x41);
662 gus_look8 (0x49);
663
664 gus_read8 (0x0f);
665
666 }
667
668 static void
669 gus_initialize (void)
670 {
671 unsigned long flags;
672 unsigned char dma_image, irq_image, tmp;
673
674 static unsigned char gus_irq_map[16] =
675 {0, 0, 1, 3, 0, 2, 0, 4, 0, 0, 0, 5, 6, 0, 0, 7};
676
677 static unsigned char gus_dma_map[8] =
678 {0, 1, 0, 2, 0, 3, 4, 5};
679
680 DISABLE_INTR (flags);
681 gus_write8 (0x4c, 0);
682 gus_delay ();
683 gus_delay ();
684
685 gus_write8 (0x4c, 1);
686 gus_delay ();
687 gus_delay ();
688
689
690
691
692
693 gus_write8 (0x41, 0);
694 gus_write8 (0x45, 0);
695 gus_write8 (0x49, 0);
696
697 gus_select_max_voices (24);
698
699 INB (u_Status);
700
701 gus_look8 (0x41);
702 gus_look8 (0x49);
703 gus_read8 (0x0f);
704
705 gus_reset ();
706
707 gus_look8 (0x41);
708 gus_look8 (0x49);
709 gus_read8 (0x0f);
710
711 gus_write8 (0x4c, 7);
712
713
714
715
716
717 OUTB (0x05, gus_base + 0x0f);
718
719 mix_image |= 0x02;
720 OUTB (mix_image, u_Mixer);
721
722 OUTB (0x00, u_IRQDMAControl);
723
724 OUTB (0x00, gus_base + 0x0f);
725
726
727
728
729
730
731
732
733
734
735
736 irq_image = 0;
737 tmp = gus_irq_map[gus_irq];
738 if (!tmp)
739 printk ("Warning! GUS IRQ not selected\n");
740 irq_image |= tmp;
741 irq_image |= 0x40;
742
743 dma_image = 0x40;
744 tmp = gus_dma_map[gus_dma];
745 if (!tmp)
746 printk ("Warning! GUS DMA not selected\n");
747 dma_image |= tmp;
748
749
750
751
752
753
754
755
756
757 OUTB (mix_image, u_Mixer);
758 OUTB (dma_image | 0x80, u_IRQDMAControl);
759
760 OUTB (mix_image | 0x40, u_Mixer);
761 OUTB (irq_image, u_IRQDMAControl);
762
763
764
765
766
767 OUTB (mix_image, u_Mixer);
768 OUTB (dma_image, u_IRQDMAControl);
769
770 OUTB (mix_image | 0x40, u_Mixer);
771 OUTB (irq_image, u_IRQDMAControl);
772
773 gus_select_voice (0);
774
775 mix_image &= ~0x02;
776 mix_image |= 0x08;
777 OUTB (mix_image, u_Mixer);
778
779
780
781
782 gus_select_voice (0);
783
784 gusintr (0);
785 RESTORE_INTR (flags);
786 }
787
788 int
789 gus_wave_detect (int baseaddr)
790 {
791 unsigned long i;
792 unsigned long loc;
793
794 gus_base = baseaddr;
795
796 gus_write8 (0x4c, 0);
797 gus_delay ();
798 gus_delay ();
799
800 gus_write8 (0x4c, 1);
801 gus_delay ();
802 gus_delay ();
803
804
805 gus_poke (0L, 0xaa);
806 if (gus_peek (0L) != 0xaa)
807 return (0);
808
809
810 gus_poke (0L, 0x00);
811 for (i = 1L; i < 1024L; i++)
812 {
813 int n, failed;
814
815
816 if (gus_peek (0L) != 0)
817 break;
818 loc = i << 10;
819
820 for (n = loc - 1, failed = 0; n <= loc; n++)
821 {
822 gus_poke (loc, 0xaa);
823 if (gus_peek (loc) != 0xaa)
824 failed = 1;
825
826 gus_poke (loc, 0x55);
827 if (gus_peek (loc) != 0x55)
828 failed = 1;
829 }
830
831 if (failed)
832 break;
833 }
834 gus_mem_size = i << 10;
835 return 1;
836 }
837
838 static int
839 guswave_ioctl (int dev,
840 unsigned int cmd, unsigned int arg)
841 {
842
843 switch (cmd)
844 {
845 case SNDCTL_SYNTH_INFO:
846 gus_info.nr_voices = nr_voices;
847 IOCTL_TO_USER ((char *) arg, 0, &gus_info, sizeof (gus_info));
848 return 0;
849 break;
850
851 case SNDCTL_SEQ_RESETSAMPLES:
852 reset_sample_memory ();
853 return 0;
854 break;
855
856 case SNDCTL_SEQ_PERCMODE:
857 return 0;
858 break;
859
860 case SNDCTL_SYNTH_MEMAVL:
861 return gus_mem_size - free_mem_ptr - 32;
862
863 default:
864 return RET_ERROR (EINVAL);
865 }
866 }
867
868 static int
869 guswave_set_instr (int dev, int voice, int instr_no)
870 {
871 int sample_no;
872
873 if (instr_no < 0 || instr_no > MAX_PATCH)
874 return RET_ERROR (EINVAL);
875
876 if (voice < 0 || voice > 31)
877 return RET_ERROR (EINVAL);
878
879 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
880 {
881 voices[voice].sample_pending = instr_no;
882 return 0;
883 }
884
885 sample_no = patch_table[instr_no];
886 patch_map[voice] = -1;
887
888 if (sample_no < 0)
889 {
890 printk ("GUS: Undefined patch %d for voice %d\n", instr_no, voice);
891 return RET_ERROR (EINVAL);
892 }
893
894 if (sample_ptrs[sample_no] == -1)
895 {
896 printk ("GUS: Sample #%d not loaded for patch %d (voice %d)\n",
897 sample_no, instr_no, voice);
898 return RET_ERROR (EINVAL);
899 }
900
901 sample_map[voice] = sample_no;
902 patch_map[voice] = instr_no;
903 return 0;
904 }
905
906 static int
907 guswave_kill_note (int dev, int voice, int note, int velocity)
908 {
909 unsigned long flags;
910
911 DISABLE_INTR (flags);
912 voice_alloc->map[voice] = 0xffff;
913 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
914 {
915 voices[voice].kill_pending = 1;
916 RESTORE_INTR (flags);
917 }
918 else
919 {
920 RESTORE_INTR (flags);
921 gus_voice_fade (voice);
922 }
923
924 return 0;
925 }
926
927 static void
928 guswave_aftertouch (int dev, int voice, int pressure)
929 {
930 short lo_limit, hi_limit;
931 unsigned long flags;
932
933 return;
934
935 if (voice < 0 || voice > 31)
936 return;
937
938 if (voices[voice].mode & WAVE_ENVELOPES && voices[voice].env_phase != 2)
939 return;
940
941 if (pressure < 32)
942 {
943 DISABLE_INTR (flags);
944 gus_select_voice (voice);
945 gus_rampoff ();
946 compute_and_set_volume (voice, 255, 0);
947 RESTORE_INTR (flags);
948 return;
949 }
950
951 hi_limit = voices[voice].current_volume;
952 lo_limit = hi_limit * 99 / 100;
953 if (lo_limit < 65)
954 lo_limit = 65;
955
956 DISABLE_INTR (flags);
957 gus_select_voice (voice);
958 if (hi_limit > (4095 - 65))
959 {
960 hi_limit = 4095 - 65;
961 gus_voice_volume (hi_limit);
962 }
963 gus_ramp_range (lo_limit, hi_limit);
964 gus_ramp_rate (3, 8);
965 gus_rampon (0x58);
966 RESTORE_INTR (flags);
967 }
968
969 static void
970 guswave_panning (int dev, int voice, int value)
971 {
972 if (voice >= 0 || voice < 32)
973 voices[voice].panning = value;
974 }
975
976 static void
977 guswave_volume_method (int dev, int mode)
978 {
979 if (mode == VOL_METHOD_LINEAR || mode == VOL_METHOD_ADAGIO)
980 volume_method = mode;
981 }
982
983 static void
984 compute_volume (int voice, int volume)
985 {
986 if (volume < 128)
987 voices[voice].midi_volume = volume;
988
989 switch (volume_method)
990 {
991 case VOL_METHOD_ADAGIO:
992 voices[voice].initial_volume =
993 gus_adagio_vol (voices[voice].midi_volume, voices[voice].main_vol,
994 voices[voice].expression_vol,
995 voices[voice].patch_vol);
996 break;
997
998 case VOL_METHOD_LINEAR:
999 voices[voice].initial_volume =
1000 gus_linear_vol (volume, voices[voice].main_vol);
1001 break;
1002
1003 default:
1004 voices[voice].initial_volume = volume_base +
1005 (voices[voice].midi_volume * volume_scale);
1006 }
1007
1008 if (voices[voice].initial_volume > 4030)
1009 voices[voice].initial_volume = 4030;
1010 }
1011
1012 static void
1013 compute_and_set_volume (int voice, int volume, int ramp_time)
1014 {
1015 int current, target, rate;
1016 unsigned long flags;
1017
1018 compute_volume (voice, volume);
1019 voices[voice].current_volume = voices[voice].initial_volume;
1020
1021 DISABLE_INTR (flags);
1022
1023
1024
1025
1026 gus_select_voice (voice);
1027
1028 current = gus_read16 (0x09) >> 4;
1029 target = voices[voice].initial_volume;
1030
1031 if (ramp_time == INSTANT_RAMP)
1032 {
1033 gus_rampoff ();
1034 gus_voice_volume (target);
1035 RESTORE_INTR (flags);
1036 return;
1037 }
1038
1039 if (ramp_time == FAST_RAMP)
1040 rate = 63;
1041 else
1042 rate = 16;
1043 gus_ramp_rate (0, rate);
1044
1045 if ((target - current) / 64 == 0)
1046 {
1047 gus_rampoff ();
1048 gus_voice_volume (target);
1049 RESTORE_INTR (flags);
1050 return;
1051 }
1052
1053 if (target > current)
1054 {
1055 if (target > (4095 - 65))
1056 target = 4095 - 65;
1057 gus_ramp_range (current, target);
1058 gus_rampon (0x00);
1059 }
1060 else
1061 {
1062 if (target < 65)
1063 target = 65;
1064
1065 gus_ramp_range (target, current);
1066 gus_rampon (0x40);
1067 }
1068 RESTORE_INTR (flags);
1069 }
1070
1071 static void
1072 dynamic_volume_change (int voice)
1073 {
1074 unsigned char status;
1075 unsigned long flags;
1076
1077 DISABLE_INTR (flags);
1078 gus_select_voice (voice);
1079 status = gus_read8 (0x00);
1080 RESTORE_INTR (flags);
1081
1082 if (status & 0x03)
1083 return;
1084
1085 if (!(voices[voice].mode & WAVE_ENVELOPES))
1086 {
1087 compute_and_set_volume (voice, voices[voice].midi_volume, 1);
1088 return;
1089 }
1090
1091
1092
1093
1094
1095 DISABLE_INTR (flags);
1096 gus_select_voice (voice);
1097 status = gus_read8 (0x0d);
1098 RESTORE_INTR (flags);
1099
1100 if (status & 0x03)
1101 {
1102 compute_and_set_volume (voice, voices[voice].midi_volume, 1);
1103 return;
1104 }
1105
1106 if (voices[voice].env_phase < 0)
1107 return;
1108
1109 compute_volume (voice, voices[voice].midi_volume);
1110
1111 }
1112
1113 static void
1114 guswave_controller (int dev, int voice, int ctrl_num, int value)
1115 {
1116 unsigned long flags;
1117 unsigned long freq;
1118
1119 if (voice < 0 || voice > 31)
1120 return;
1121
1122 switch (ctrl_num)
1123 {
1124 case CTRL_PITCH_BENDER:
1125 voices[voice].bender = value;
1126
1127 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1128 {
1129 freq = compute_finetune (voices[voice].orig_freq, value,
1130 voices[voice].bender_range);
1131 voices[voice].current_freq = freq;
1132
1133 DISABLE_INTR (flags);
1134 gus_select_voice (voice);
1135 gus_voice_freq (freq);
1136 RESTORE_INTR (flags);
1137 }
1138 break;
1139
1140 case CTRL_PITCH_BENDER_RANGE:
1141 voices[voice].bender_range = value;
1142 break;
1143 case CTL_EXPRESSION:
1144 value /= 128;
1145 case CTRL_EXPRESSION:
1146 if (volume_method == VOL_METHOD_ADAGIO)
1147 {
1148 voices[voice].expression_vol = value;
1149 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1150 dynamic_volume_change (voice);
1151 }
1152 break;
1153
1154 case CTL_PAN:
1155 voices[voice].panning = (value * 2) - 128;
1156 break;
1157
1158 case CTL_MAIN_VOLUME:
1159 value = (value * 100) / 16383;
1160
1161 case CTRL_MAIN_VOLUME:
1162 voices[voice].main_vol = value;
1163 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1164 dynamic_volume_change (voice);
1165 break;
1166
1167 default:
1168 break;
1169 }
1170 }
1171
1172 static int
1173 guswave_start_note2 (int dev, int voice, int note_num, int volume)
1174 {
1175 int sample, best_sample, best_delta, delta_freq;
1176 int is16bits, samplep, patch, pan;
1177 unsigned long note_freq, base_note, freq, flags;
1178 unsigned char mode = 0;
1179
1180 if (voice < 0 || voice > 31)
1181 {
1182 printk ("GUS: Invalid voice\n");
1183 return RET_ERROR (EINVAL);
1184 }
1185
1186 if (note_num == 255)
1187 {
1188 if (voices[voice].mode & WAVE_ENVELOPES)
1189 {
1190 voices[voice].midi_volume = volume;
1191 dynamic_volume_change (voice);
1192 return 0;
1193 }
1194
1195 compute_and_set_volume (voice, volume, 1);
1196 return 0;
1197 }
1198
1199 if ((patch = patch_map[voice]) == -1)
1200 {
1201 return RET_ERROR (EINVAL);
1202 }
1203
1204 if ((samplep = patch_table[patch]) == -1)
1205 {
1206 return RET_ERROR (EINVAL);
1207 }
1208
1209 note_freq = note_to_freq (note_num);
1210
1211
1212
1213
1214
1215 sample = -1;
1216
1217 best_sample = samplep;
1218 best_delta = 1000000;
1219 while (samplep >= 0 && sample == -1)
1220 {
1221 delta_freq = note_freq - samples[samplep].base_note;
1222 if (delta_freq < 0)
1223 delta_freq = -delta_freq;
1224 if (delta_freq < best_delta)
1225 {
1226 best_sample = samplep;
1227 best_delta = delta_freq;
1228 }
1229 if (samples[samplep].low_note <= note_freq &&
1230 note_freq <= samples[samplep].high_note)
1231 sample = samplep;
1232 else
1233 samplep = samples[samplep].key;
1234
1235
1236 }
1237 if (sample == -1)
1238 sample = best_sample;
1239
1240 if (sample == -1)
1241 {
1242 printk ("GUS: Patch %d not defined for note %d\n", patch, note_num);
1243 return 0;
1244 }
1245
1246 is16bits = (samples[sample].mode & WAVE_16_BITS) ? 1 : 0;
1247 voices[voice].mode = samples[sample].mode;
1248 voices[voice].patch_vol = samples[sample].volume;
1249
1250 if (voices[voice].mode & WAVE_ENVELOPES)
1251 {
1252 int i;
1253
1254 for (i = 0; i < 6; i++)
1255 {
1256 voices[voice].env_rate[i] = samples[sample].env_rate[i];
1257 voices[voice].env_offset[i] = samples[sample].env_offset[i];
1258 }
1259 }
1260
1261 sample_map[voice] = sample;
1262
1263 base_note = samples[sample].base_note / 100;
1264 note_freq /= 100;
1265
1266 freq = samples[sample].base_freq * note_freq / base_note;
1267
1268 voices[voice].orig_freq = freq;
1269
1270
1271
1272
1273
1274
1275 freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender,
1276 voices[voice].bender_range);
1277 voices[voice].current_freq = freq;
1278
1279 pan = (samples[sample].panning + voices[voice].panning) / 32;
1280 pan += 7;
1281 if (pan < 0)
1282 pan = 0;
1283 if (pan > 15)
1284 pan = 15;
1285
1286 if (samples[sample].mode & WAVE_16_BITS)
1287 {
1288 mode |= 0x04;
1289 if ((sample_ptrs[sample] >> 18) !=
1290 ((sample_ptrs[sample] + samples[sample].len) >> 18))
1291 printk ("GUS: Sample address error\n");
1292 }
1293
1294
1295
1296
1297
1298 DISABLE_INTR (flags);
1299 gus_select_voice (voice);
1300 gus_voice_off ();
1301 gus_rampoff ();
1302
1303 RESTORE_INTR (flags);
1304
1305 if (voices[voice].mode & WAVE_ENVELOPES)
1306 {
1307 compute_volume (voice, volume);
1308 init_envelope (voice);
1309 }
1310 else
1311 compute_and_set_volume (voice, volume, 0);
1312
1313 DISABLE_INTR (flags);
1314 gus_select_voice (voice);
1315
1316 if (samples[sample].mode & WAVE_LOOP_BACK)
1317 gus_write_addr (0x0a, sample_ptrs[sample] + samples[sample].len -
1318 voices[voice].offset_pending, is16bits);
1319 else
1320 gus_write_addr (0x0a, sample_ptrs[sample] + voices[voice].offset_pending,
1321 is16bits);
1322
1323 if (samples[sample].mode & WAVE_LOOPING)
1324 {
1325 mode |= 0x08;
1326
1327 if (samples[sample].mode & WAVE_BIDIR_LOOP)
1328 mode |= 0x10;
1329
1330 if (samples[sample].mode & WAVE_LOOP_BACK)
1331 {
1332 gus_write_addr (0x0a,
1333 sample_ptrs[sample] + samples[sample].loop_end -
1334 voices[voice].offset_pending, is16bits);
1335 mode |= 0x40;
1336 }
1337
1338 gus_write_addr (0x02, sample_ptrs[sample] + samples[sample].loop_start,
1339 is16bits);
1340 gus_write_addr (0x04, sample_ptrs[sample] + samples[sample].loop_end,
1341 is16bits);
1342 }
1343 else
1344 {
1345 mode |= 0x20;
1346 voices[voice].loop_irq_mode = LMODE_FINISH;
1347 voices[voice].loop_irq_parm = 1;
1348 gus_write_addr (0x02, sample_ptrs[sample],
1349 is16bits);
1350 gus_write_addr (0x04, sample_ptrs[sample] + samples[sample].len - 1,
1351 is16bits);
1352 }
1353 gus_voice_freq (freq);
1354 gus_voice_balance (pan);
1355 gus_voice_on (mode);
1356 RESTORE_INTR (flags);
1357
1358 return 0;
1359 }
1360
1361
1362
1363
1364
1365
1366
1367 static int
1368 guswave_start_note (int dev, int voice, int note_num, int volume)
1369 {
1370 long int flags;
1371 int mode;
1372 int ret_val = 0;
1373
1374 DISABLE_INTR (flags);
1375 if (note_num == 255)
1376 {
1377 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1378 voices[voice].volume_pending = volume;
1379 else
1380 {
1381 RESTORE_INTR (flags);
1382 ret_val = guswave_start_note2 (dev, voice, note_num, volume);
1383 }
1384 }
1385 else
1386 {
1387 gus_select_voice (voice);
1388 mode = gus_read8 (0x00);
1389 if (mode & 0x20)
1390 gus_write8 (0x00, mode & 0xdf);
1391
1392 voices[voice].offset_pending = 0;
1393 voices[voice].kill_pending = 0;
1394 voices[voice].volume_irq_mode = 0;
1395 voices[voice].loop_irq_mode = 0;
1396
1397 if (voices[voice].sample_pending >= 0)
1398 {
1399 RESTORE_INTR (flags);
1400 guswave_set_instr (voices[voice].dev_pending, voice,
1401 voices[voice].sample_pending);
1402 voices[voice].sample_pending = -1;
1403 DISABLE_INTR (flags);
1404 }
1405
1406 if ((mode & 0x01) || (int) ((gus_read16 (0x09) >> 4) < 2065))
1407 {
1408 ret_val = guswave_start_note2 (dev, voice, note_num, volume);
1409 }
1410 else
1411 {
1412 voices[voice].dev_pending = dev;
1413 voices[voice].note_pending = note_num;
1414 voices[voice].volume_pending = volume;
1415 voices[voice].volume_irq_mode = VMODE_START_NOTE;
1416
1417 gus_rampoff ();
1418 gus_ramp_range (2000, 4065);
1419 gus_ramp_rate (0, 63);
1420 gus_rampon (0x20 | 0x40);
1421 RESTORE_INTR (flags);
1422 }
1423 }
1424 return ret_val;
1425 }
1426
1427 static void
1428 guswave_reset (int dev)
1429 {
1430 int i;
1431
1432 for (i = 0; i < 32; i++)
1433 {
1434 gus_voice_init (i);
1435 gus_voice_init2 (i);
1436 }
1437 }
1438
1439 static int
1440 guswave_open (int dev, int mode)
1441 {
1442 int err;
1443
1444 if (gus_busy)
1445 return RET_ERROR (EBUSY);
1446
1447 gus_initialize ();
1448
1449 if ((err = DMAbuf_open_dma (gus_devnum)) < 0)
1450 return err;
1451
1452 RESET_WAIT_QUEUE (dram_sleeper, dram_sleep_flag);
1453 gus_busy = 1;
1454 active_device = GUS_DEV_WAVE;
1455
1456 gus_reset ();
1457
1458 return 0;
1459 }
1460
1461 static void
1462 guswave_close (int dev)
1463 {
1464 gus_busy = 0;
1465 active_device = 0;
1466 gus_reset ();
1467
1468 DMAbuf_close_dma (gus_devnum);
1469 }
1470
1471 static int
1472 guswave_load_patch (int dev, int format, snd_rw_buf * addr,
1473 int offs, int count, int pmgr_flag)
1474 {
1475 struct patch_info patch;
1476 int instr;
1477 long sizeof_patch;
1478
1479 unsigned long blk_size, blk_end, left, src_offs, target;
1480
1481 sizeof_patch = (long) &patch.data[0] - (long) &patch;
1482
1483 if (format != GUS_PATCH)
1484 {
1485 printk ("GUS Error: Invalid patch format (key) 0x%x\n", format);
1486 return RET_ERROR (EINVAL);
1487 }
1488
1489 if (count < sizeof_patch)
1490 {
1491 printk ("GUS Error: Patch header too short\n");
1492 return RET_ERROR (EINVAL);
1493 }
1494
1495 count -= sizeof_patch;
1496
1497 if (free_sample >= MAX_SAMPLE)
1498 {
1499 printk ("GUS: Sample table full\n");
1500 return RET_ERROR (ENOSPC);
1501 }
1502
1503
1504
1505
1506
1507
1508 COPY_FROM_USER (&((char *) &patch)[offs], addr, offs, sizeof_patch - offs);
1509
1510 instr = patch.instr_no;
1511
1512 if (instr < 0 || instr > MAX_PATCH)
1513 {
1514 printk ("GUS: Invalid patch number %d\n", instr);
1515 return RET_ERROR (EINVAL);
1516 }
1517
1518 if (count < patch.len)
1519 {
1520 printk ("GUS Warning: Patch record too short (%d<%d)\n",
1521 count, (int) patch.len);
1522 patch.len = count;
1523 }
1524
1525 if (patch.len <= 0 || patch.len > gus_mem_size)
1526 {
1527 printk ("GUS: Invalid sample length %d\n", (int) patch.len);
1528 return RET_ERROR (EINVAL);
1529 }
1530
1531 if (patch.mode & WAVE_LOOPING)
1532 {
1533 if (patch.loop_start < 0 || patch.loop_start >= patch.len)
1534 {
1535 printk ("GUS: Invalid loop start\n");
1536 return RET_ERROR (EINVAL);
1537 }
1538
1539 if (patch.loop_end < patch.loop_start || patch.loop_end > patch.len)
1540 {
1541 printk ("GUS: Invalid loop end\n");
1542 return RET_ERROR (EINVAL);
1543 }
1544 }
1545
1546 free_mem_ptr = (free_mem_ptr + 31) & ~31;
1547
1548 #define GUS_BANK_SIZE (256*1024)
1549
1550 if (patch.mode & WAVE_16_BITS)
1551 {
1552
1553
1554
1555 if (patch.len >= GUS_BANK_SIZE)
1556 {
1557 printk ("GUS: Sample (16 bit) too long %d\n", (int) patch.len);
1558 return RET_ERROR (ENOSPC);
1559 }
1560
1561 if ((free_mem_ptr / GUS_BANK_SIZE) !=
1562 ((free_mem_ptr + patch.len) / GUS_BANK_SIZE))
1563 {
1564 unsigned long tmp_mem =
1565 ((free_mem_ptr / GUS_BANK_SIZE) + 1) * GUS_BANK_SIZE;
1566
1567 if ((tmp_mem + patch.len) > gus_mem_size)
1568 return RET_ERROR (ENOSPC);
1569
1570 free_mem_ptr = tmp_mem;
1571 }
1572 }
1573
1574 if ((free_mem_ptr + patch.len) > gus_mem_size)
1575 return RET_ERROR (ENOSPC);
1576
1577 sample_ptrs[free_sample] = free_mem_ptr;
1578
1579
1580
1581
1582
1583 if (patch.mode & WAVE_ENVELOPES)
1584 patch.mode &= ~WAVE_TREMOLO;
1585
1586 memcpy ((char *) &samples[free_sample], &patch, sizeof_patch);
1587
1588
1589
1590
1591
1592 samples[free_sample].key = patch_table[instr];
1593 patch_table[instr] = free_sample;
1594
1595
1596
1597
1598
1599 left = patch.len;
1600 src_offs = 0;
1601 target = free_mem_ptr;
1602
1603 while (left)
1604 {
1605 blk_size = audio_devs[gus_devnum]->buffsize;
1606 if (blk_size > left)
1607 blk_size = left;
1608
1609
1610
1611
1612 blk_end = target + blk_size;
1613
1614 if ((target >> 18) != (blk_end >> 18))
1615 {
1616
1617 blk_end &= ~(256 * 1024 - 1);
1618 blk_size = blk_end - target;
1619 }
1620
1621 #if defined(GUS_NO_DMA) || defined(GUS_PATCH_NO_DMA)
1622
1623
1624
1625 {
1626 long i;
1627 unsigned char data;
1628
1629 for (i = 0; i < blk_size; i++)
1630 {
1631 GET_BYTE_FROM_USER (data, addr, sizeof_patch + i);
1632 if (patch.mode & WAVE_UNSIGNED)
1633
1634 if (!(patch.mode & WAVE_16_BITS) || (i & 0x01))
1635 data ^= 0x80;
1636 gus_poke (target + i, data);
1637 }
1638 }
1639 #else
1640 {
1641 unsigned long address, hold_address;
1642 unsigned char dma_command;
1643 unsigned long flags;
1644
1645
1646
1647
1648
1649 COPY_FROM_USER (audio_devs[gus_devnum]->dmap->raw_buf[0],
1650 addr, sizeof_patch + src_offs,
1651 blk_size);
1652
1653 DISABLE_INTR (flags);
1654 gus_write8 (0x41, 0);
1655 DMAbuf_start_dma (gus_devnum,
1656 audio_devs[gus_devnum]->dmap->raw_buf_phys[0],
1657 blk_size, DMA_MODE_WRITE);
1658
1659
1660
1661
1662
1663 address = target;
1664
1665 if (audio_devs[gus_devnum]->dmachan > 3)
1666 {
1667 hold_address = address;
1668 address = address >> 1;
1669 address &= 0x0001ffffL;
1670 address |= (hold_address & 0x000c0000L);
1671 }
1672
1673 gus_write16 (0x42, (address >> 4) & 0xffff);
1674
1675
1676
1677
1678
1679 dma_command = 0x21;
1680 if (patch.mode & WAVE_UNSIGNED)
1681 dma_command |= 0x80;
1682 if (patch.mode & WAVE_16_BITS)
1683 dma_command |= 0x40;
1684 if (audio_devs[gus_devnum]->dmachan > 3)
1685 dma_command |= 0x04;
1686
1687 gus_write8 (0x41, dma_command);
1688
1689
1690
1691
1692 active_device = GUS_DEV_WAVE;
1693
1694 DO_SLEEP (dram_sleeper, dram_sleep_flag, HZ);
1695 if (TIMED_OUT (dram_sleeper, dram_sleep_flag))
1696 printk ("GUS: DMA Transfer timed out\n");
1697 RESTORE_INTR (flags);
1698 }
1699 #endif
1700
1701
1702
1703
1704
1705 left -= blk_size;
1706 src_offs += blk_size;
1707 target += blk_size;
1708
1709 gus_write8 (0x41, 0);
1710 }
1711
1712 free_mem_ptr += patch.len;
1713
1714 if (!pmgr_flag)
1715 pmgr_inform (dev, PM_E_PATCH_LOADED, instr, free_sample, 0, 0);
1716 free_sample++;
1717 return 0;
1718 }
1719
1720 static void
1721 guswave_hw_control (int dev, unsigned char *event)
1722 {
1723 int voice, cmd;
1724 unsigned short p1, p2;
1725 unsigned long plong, flags;
1726
1727 cmd = event[2];
1728 voice = event[3];
1729 p1 = *(unsigned short *) &event[4];
1730 p2 = *(unsigned short *) &event[6];
1731 plong = *(unsigned long *) &event[4];
1732
1733 if ((voices[voice].volume_irq_mode == VMODE_START_NOTE) &&
1734 (cmd != _GUS_VOICESAMPLE) && (cmd != _GUS_VOICE_POS))
1735 do_volume_irq (voice);
1736
1737 switch (cmd)
1738 {
1739
1740 case _GUS_NUMVOICES:
1741 DISABLE_INTR (flags);
1742 gus_select_voice (voice);
1743 gus_select_max_voices (p1);
1744 RESTORE_INTR (flags);
1745 break;
1746
1747 case _GUS_VOICESAMPLE:
1748 guswave_set_instr (dev, voice, p1);
1749 break;
1750
1751 case _GUS_VOICEON:
1752 DISABLE_INTR (flags);
1753 gus_select_voice (voice);
1754 p1 &= ~0x20;
1755 gus_voice_on (p1);
1756 RESTORE_INTR (flags);
1757 break;
1758
1759 case _GUS_VOICEOFF:
1760 DISABLE_INTR (flags);
1761 gus_select_voice (voice);
1762 gus_voice_off ();
1763 RESTORE_INTR (flags);
1764 break;
1765
1766 case _GUS_VOICEFADE:
1767 gus_voice_fade (voice);
1768 break;
1769
1770 case _GUS_VOICEMODE:
1771 DISABLE_INTR (flags);
1772 gus_select_voice (voice);
1773 p1 &= ~0x20;
1774 gus_voice_mode (p1);
1775 RESTORE_INTR (flags);
1776 break;
1777
1778 case _GUS_VOICEBALA:
1779 DISABLE_INTR (flags);
1780 gus_select_voice (voice);
1781 gus_voice_balance (p1);
1782 RESTORE_INTR (flags);
1783 break;
1784
1785 case _GUS_VOICEFREQ:
1786 DISABLE_INTR (flags);
1787 gus_select_voice (voice);
1788 gus_voice_freq (plong);
1789 RESTORE_INTR (flags);
1790 break;
1791
1792 case _GUS_VOICEVOL:
1793 DISABLE_INTR (flags);
1794 gus_select_voice (voice);
1795 gus_voice_volume (p1);
1796 RESTORE_INTR (flags);
1797 break;
1798
1799 case _GUS_VOICEVOL2:
1800 voices[voice].initial_volume =
1801 voices[voice].current_volume = p1;
1802 break;
1803
1804 case _GUS_RAMPRANGE:
1805 if (voices[voice].mode & WAVE_ENVELOPES)
1806 break;
1807 DISABLE_INTR (flags);
1808 gus_select_voice (voice);
1809 gus_ramp_range (p1, p2);
1810 RESTORE_INTR (flags);
1811 break;
1812
1813 case _GUS_RAMPRATE:
1814 if (voices[voice].mode & WAVE_ENVELOPES)
1815 break;
1816 DISABLE_INTR (flags);
1817 gus_select_voice (voice);
1818 gus_ramp_rate (p1, p2);
1819 RESTORE_INTR (flags);
1820 break;
1821
1822 case _GUS_RAMPMODE:
1823 if (voices[voice].mode & WAVE_ENVELOPES)
1824 break;
1825 DISABLE_INTR (flags);
1826 gus_select_voice (voice);
1827 p1 &= ~0x20;
1828 gus_ramp_mode (p1);
1829 RESTORE_INTR (flags);
1830 break;
1831
1832 case _GUS_RAMPON:
1833 if (voices[voice].mode & WAVE_ENVELOPES)
1834 break;
1835 DISABLE_INTR (flags);
1836 gus_select_voice (voice);
1837 p1 &= ~0x20;
1838 gus_rampon (p1);
1839 RESTORE_INTR (flags);
1840 break;
1841
1842 case _GUS_RAMPOFF:
1843 if (voices[voice].mode & WAVE_ENVELOPES)
1844 break;
1845 DISABLE_INTR (flags);
1846 gus_select_voice (voice);
1847 gus_rampoff ();
1848 RESTORE_INTR (flags);
1849 break;
1850
1851 case _GUS_VOLUME_SCALE:
1852 volume_base = p1;
1853 volume_scale = p2;
1854 break;
1855
1856 case _GUS_VOICE_POS:
1857 DISABLE_INTR (flags);
1858 gus_select_voice (voice);
1859 gus_set_voice_pos (voice, plong);
1860 RESTORE_INTR (flags);
1861 break;
1862
1863 default:;
1864 }
1865 }
1866
1867 static int
1868 gus_sampling_set_speed (int speed)
1869 {
1870 if (speed <= 0)
1871 return gus_sampling_speed;
1872
1873 if (speed > 44100)
1874 speed = 44100;
1875
1876 gus_sampling_speed = speed;
1877 return speed;
1878 }
1879
1880 static int
1881 gus_sampling_set_channels (int channels)
1882 {
1883 if (!channels)
1884 return gus_sampling_channels;
1885 if (channels > 2)
1886 channels = 2;
1887 if (channels < 1)
1888 channels = 1;
1889 gus_sampling_channels = channels;
1890 return channels;
1891 }
1892
1893 static int
1894 gus_sampling_set_bits (int bits)
1895 {
1896 if (!bits)
1897 return gus_sampling_bits;
1898
1899 if (bits != 8 && bits != 16)
1900 bits = 8;
1901
1902 gus_sampling_bits = bits;
1903 return bits;
1904 }
1905
1906 static int
1907 gus_sampling_ioctl (int dev, unsigned int cmd, unsigned int arg, int local)
1908 {
1909 switch (cmd)
1910 {
1911 case SOUND_PCM_WRITE_RATE:
1912 if (local)
1913 return gus_sampling_set_speed (arg);
1914 return IOCTL_OUT (arg, gus_sampling_set_speed (IOCTL_IN (arg)));
1915 break;
1916
1917 case SOUND_PCM_READ_RATE:
1918 if (local)
1919 return gus_sampling_speed;
1920 return IOCTL_OUT (arg, gus_sampling_speed);
1921 break;
1922
1923 case SNDCTL_DSP_STEREO:
1924 if (local)
1925 return gus_sampling_set_channels (arg + 1) - 1;
1926 return IOCTL_OUT (arg, gus_sampling_set_channels (IOCTL_IN (arg) + 1) - 1);
1927 break;
1928
1929 case SOUND_PCM_WRITE_CHANNELS:
1930 if (local)
1931 return gus_sampling_set_channels (arg);
1932 return IOCTL_OUT (arg, gus_sampling_set_channels (IOCTL_IN (arg)));
1933 break;
1934
1935 case SOUND_PCM_READ_CHANNELS:
1936 if (local)
1937 return gus_sampling_channels;
1938 return IOCTL_OUT (arg, gus_sampling_channels);
1939 break;
1940
1941 case SNDCTL_DSP_SETFMT:
1942 if (local)
1943 return gus_sampling_set_bits (arg);
1944 return IOCTL_OUT (arg, gus_sampling_set_bits (IOCTL_IN (arg)));
1945 break;
1946
1947 case SOUND_PCM_READ_BITS:
1948 if (local)
1949 return gus_sampling_bits;
1950 return IOCTL_OUT (arg, gus_sampling_bits);
1951
1952 case SOUND_PCM_WRITE_FILTER:
1953 return IOCTL_OUT (arg, RET_ERROR (EINVAL));
1954 break;
1955
1956 case SOUND_PCM_READ_FILTER:
1957 return IOCTL_OUT (arg, RET_ERROR (EINVAL));
1958 break;
1959
1960 default:
1961 return RET_ERROR (EINVAL);
1962 }
1963 return RET_ERROR (EINVAL);
1964 }
1965
1966 static void
1967 gus_sampling_reset (int dev)
1968 {
1969 }
1970
1971 static int
1972 gus_sampling_open (int dev, int mode)
1973 {
1974 #ifdef GUS_NO_DMA
1975 printk ("GUS: DMA mode not enabled. Device not supported\n");
1976 return RET_ERROR (ENXIO);
1977 #endif
1978
1979 if (gus_busy)
1980 return RET_ERROR (EBUSY);
1981
1982 gus_initialize ();
1983
1984 gus_busy = 1;
1985 active_device = 0;
1986
1987 gus_reset ();
1988 reset_sample_memory ();
1989 gus_select_max_voices (14);
1990
1991 pcm_active = 0;
1992 dma_active = 0;
1993 pcm_opened = 1;
1994 if (mode & OPEN_READ)
1995 {
1996 recording_active = 1;
1997 set_input_volumes ();
1998 }
1999
2000 return 0;
2001 }
2002
2003 static void
2004 gus_sampling_close (int dev)
2005 {
2006 gus_reset ();
2007 gus_busy = 0;
2008 pcm_opened = 0;
2009 active_device = 0;
2010
2011 if (recording_active)
2012 set_input_volumes ();
2013
2014 recording_active = 0;
2015 }
2016
2017 static void
2018 gus_sampling_update_volume (void)
2019 {
2020 unsigned long flags;
2021 int voice;
2022
2023 DISABLE_INTR (flags);
2024 if (pcm_active && pcm_opened)
2025 for (voice = 0; voice < gus_sampling_channels; voice++)
2026 {
2027 gus_select_voice (voice);
2028 gus_rampoff ();
2029 gus_voice_volume (1530 + (25 * gus_pcm_volume));
2030 gus_ramp_range (65, 1530 + (25 * gus_pcm_volume));
2031 }
2032 RESTORE_INTR (flags);
2033 }
2034
2035 static void
2036 play_next_pcm_block (void)
2037 {
2038 unsigned long flags;
2039 int speed = gus_sampling_speed;
2040 int this_one, is16bits, chn;
2041 unsigned long dram_loc;
2042 unsigned char mode[2], ramp_mode[2];
2043
2044 if (!pcm_qlen)
2045 return;
2046
2047 this_one = pcm_head;
2048
2049 for (chn = 0; chn < gus_sampling_channels; chn++)
2050 {
2051 mode[chn] = 0x00;
2052 ramp_mode[chn] = 0x03;
2053
2054 if (chn == 0)
2055 {
2056 mode[chn] |= 0x20;
2057 voices[chn].loop_irq_mode = LMODE_PCM;
2058 }
2059
2060 if (gus_sampling_bits != 8)
2061 {
2062 is16bits = 1;
2063 mode[chn] |= 0x04;
2064 }
2065 else
2066 is16bits = 0;
2067
2068 dram_loc = this_one * pcm_bsize;
2069 dram_loc += chn * pcm_banksize;
2070
2071 if (this_one == (pcm_nblk - 1))
2072 {
2073 mode[chn] |= 0x08;
2074 ramp_mode[chn] = 0x03;
2075 }
2076 else
2077 {
2078 if (chn == 0)
2079 ramp_mode[chn] = 0x04;
2080 }
2081
2082 DISABLE_INTR (flags);
2083 gus_select_voice (chn);
2084 gus_voice_freq (speed);
2085
2086 if (gus_sampling_channels == 1)
2087 gus_voice_balance (7);
2088 else if (chn == 0)
2089 gus_voice_balance (0);
2090 else
2091 gus_voice_balance (15);
2092
2093 if (!pcm_active)
2094 {
2095
2096
2097
2098
2099
2100
2101
2102 gus_voice_off ();
2103 gus_rampoff ();
2104 gus_voice_volume (1530 + (25 * gus_pcm_volume));
2105 gus_ramp_range (65, 1530 + (25 * gus_pcm_volume));
2106
2107 gus_write_addr (0x0a, dram_loc, is16bits);
2108 gus_write_addr (0x02, chn * pcm_banksize, is16bits);
2109
2110 if (chn != 0)
2111 gus_write_addr (0x04, pcm_banksize + (pcm_bsize * pcm_nblk),
2112 is16bits);
2113 }
2114
2115 if (chn == 0)
2116 gus_write_addr (0x04, dram_loc + pcm_datasize[this_one],
2117 is16bits);
2118 else
2119 mode[chn] |= 0x08;
2120
2121 if (pcm_datasize[this_one] != pcm_bsize)
2122 {
2123
2124
2125
2126 if (chn == 0)
2127 {
2128 mode[chn] &= ~0x08;
2129 mode[chn] |= 0x20;
2130 voices[0].loop_irq_mode = LMODE_PCM_STOP;
2131 ramp_mode[chn] = 0x03;
2132 }
2133 else
2134 {
2135 gus_write_addr (0x04, dram_loc + pcm_datasize[this_one],
2136 is16bits);
2137 mode[chn] &= ~0x08;
2138 }
2139 }
2140
2141 RESTORE_INTR (flags);
2142 }
2143
2144 for (chn = 0; chn < gus_sampling_channels; chn++)
2145 {
2146 DISABLE_INTR (flags);
2147 gus_select_voice (chn);
2148 gus_write8 (0x0d, ramp_mode[chn]);
2149 gus_voice_on (mode[chn]);
2150 RESTORE_INTR (flags);
2151 }
2152
2153 pcm_active = 1;
2154 }
2155
2156 static void
2157 gus_transfer_output_block (int dev, unsigned long buf,
2158 int total_count, int intrflag, int chn)
2159 {
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169 int this_one, count;
2170 unsigned long flags;
2171 unsigned char dma_command;
2172 unsigned long address, hold_address;
2173
2174 DISABLE_INTR (flags);
2175
2176 count = total_count / gus_sampling_channels;
2177
2178 if (chn == 0)
2179 {
2180 if (pcm_qlen >= pcm_nblk)
2181 printk ("GUS Warning: PCM buffers out of sync\n");
2182
2183 this_one = pcm_current_block = pcm_tail;
2184 pcm_qlen++;
2185 pcm_tail = (pcm_tail + 1) % pcm_nblk;
2186 pcm_datasize[this_one] = count;
2187 }
2188 else
2189 this_one = pcm_current_block;
2190
2191 gus_write8 (0x41, 0);
2192 DMAbuf_start_dma (dev, buf + (chn * count), count, DMA_MODE_WRITE);
2193
2194 address = this_one * pcm_bsize;
2195 address += chn * pcm_banksize;
2196
2197 if (audio_devs[dev]->dmachan > 3)
2198 {
2199 hold_address = address;
2200 address = address >> 1;
2201 address &= 0x0001ffffL;
2202 address |= (hold_address & 0x000c0000L);
2203 }
2204
2205 gus_write16 (0x42, (address >> 4) & 0xffff);
2206
2207 dma_command = 0x21;
2208
2209 if (gus_sampling_bits != 8)
2210 dma_command |= 0x40;
2211 else
2212 dma_command |= 0x80;
2213
2214 if (audio_devs[dev]->dmachan > 3)
2215 dma_command |= 0x04;
2216
2217 gus_write8 (0x41, dma_command);
2218
2219 if (chn == (gus_sampling_channels - 1))
2220 {
2221
2222
2223
2224 dma_active = 1;
2225 active_device = GUS_DEV_PCM_DONE;
2226 if (!pcm_active && (pcm_qlen > 0 || count < pcm_bsize))
2227 {
2228 play_next_pcm_block ();
2229 }
2230 }
2231 else
2232 {
2233
2234
2235
2236
2237 active_device = GUS_DEV_PCM_CONTINUE;
2238 }
2239
2240 RESTORE_INTR (flags);
2241 }
2242
2243 static void
2244 gus_sampling_output_block (int dev, unsigned long buf, int total_count,
2245 int intrflag, int restart_dma)
2246 {
2247 pcm_current_buf = buf;
2248 pcm_current_count = total_count;
2249 pcm_current_intrflag = intrflag;
2250 pcm_current_dev = dev;
2251 gus_transfer_output_block (dev, buf, total_count, intrflag, 0);
2252 }
2253
2254 static void
2255 gus_sampling_start_input (int dev, unsigned long buf, int count,
2256 int intrflag, int restart_dma)
2257 {
2258 unsigned long flags;
2259 unsigned char mode;
2260
2261 DISABLE_INTR (flags);
2262
2263 DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ);
2264
2265 mode = 0xa0;
2266
2267 if (audio_devs[dev]->dmachan > 3)
2268 mode |= 0x04;
2269 if (gus_sampling_channels > 1)
2270 mode |= 0x02;
2271 mode |= 0x01;
2272
2273 gus_write8 (0x49, mode);
2274
2275 RESTORE_INTR (flags);
2276 }
2277
2278 static int
2279 gus_sampling_prepare_for_input (int dev, int bsize, int bcount)
2280 {
2281 unsigned int rate;
2282
2283 rate = (9878400 / (gus_sampling_speed + 2)) / 16;
2284
2285 gus_write8 (0x48, rate & 0xff);
2286
2287 if (gus_sampling_bits != 8)
2288 {
2289 printk ("GUS Error: 16 bit recording not supported\n");
2290 return RET_ERROR (EINVAL);
2291 }
2292
2293 return 0;
2294 }
2295
2296 static int
2297 gus_sampling_prepare_for_output (int dev, int bsize, int bcount)
2298 {
2299 int i;
2300
2301 long mem_ptr, mem_size;
2302
2303 mem_ptr = 0;
2304 mem_size = gus_mem_size / gus_sampling_channels;
2305
2306 if (mem_size > (256 * 1024))
2307 mem_size = 256 * 1024;
2308
2309 pcm_bsize = bsize / gus_sampling_channels;
2310 pcm_head = pcm_tail = pcm_qlen = 0;
2311
2312 pcm_nblk = MAX_PCM_BUFFERS;
2313 if ((pcm_bsize * pcm_nblk) > mem_size)
2314 pcm_nblk = mem_size / pcm_bsize;
2315
2316 for (i = 0; i < pcm_nblk; i++)
2317 pcm_datasize[i] = 0;
2318
2319 pcm_banksize = pcm_nblk * pcm_bsize;
2320
2321 if (gus_sampling_bits != 8 && pcm_banksize == (256 * 1024))
2322 pcm_nblk--;
2323
2324 return 0;
2325 }
2326
2327 static int
2328 gus_local_qlen (int dev)
2329 {
2330 return pcm_qlen;
2331 }
2332
2333 static void
2334 gus_copy_from_user (int dev, char *localbuf, int localoffs,
2335 snd_rw_buf * userbuf, int useroffs, int len)
2336 {
2337 if (gus_sampling_channels == 1)
2338 {
2339 COPY_FROM_USER (&localbuf[localoffs], userbuf, useroffs, len);
2340 }
2341 else if (gus_sampling_bits == 8)
2342 {
2343 int in_left = useroffs;
2344 int in_right = useroffs + 1;
2345 char *out_left, *out_right;
2346 int i;
2347
2348 len /= 2;
2349 localoffs /= 2;
2350 out_left = &localbuf[localoffs];
2351 out_right = out_left + pcm_bsize;
2352
2353 for (i = 0; i < len; i++)
2354 {
2355 GET_BYTE_FROM_USER (*out_left++, userbuf, in_left);
2356 in_left += 2;
2357 GET_BYTE_FROM_USER (*out_right++, userbuf, in_right);
2358 in_right += 2;
2359 }
2360 }
2361 else
2362 {
2363 int in_left = useroffs;
2364 int in_right = useroffs + 1;
2365 short *out_left, *out_right;
2366 int i;
2367
2368 len /= 4;
2369 localoffs /= 4;
2370
2371 out_left = (short *) &localbuf[localoffs];
2372 out_right = out_left + (pcm_bsize / 2);
2373
2374 for (i = 0; i < len; i++)
2375 {
2376 GET_SHORT_FROM_USER (*out_left++, (short *) userbuf, in_left);
2377 in_left += 2;
2378 GET_SHORT_FROM_USER (*out_right++, (short *) userbuf, in_right);
2379 in_right += 2;
2380 }
2381 }
2382 }
2383
2384 static struct audio_operations gus_sampling_operations =
2385 {
2386 "Gravis UltraSound",
2387 NEEDS_RESTART,
2388 AFMT_U8 | AFMT_S16_LE,
2389 NULL,
2390 gus_sampling_open,
2391 gus_sampling_close,
2392 gus_sampling_output_block,
2393 gus_sampling_start_input,
2394 gus_sampling_ioctl,
2395 gus_sampling_prepare_for_input,
2396 gus_sampling_prepare_for_output,
2397 gus_sampling_reset,
2398 gus_sampling_reset,
2399 gus_local_qlen,
2400 gus_copy_from_user
2401 };
2402
2403 static void
2404 guswave_bender (int dev, int voice, int value)
2405 {
2406 int freq;
2407 unsigned long flags;
2408
2409 voices[voice].bender = value - 8192;
2410 freq = compute_finetune (voices[voice].orig_freq, value,
2411 voices[voice].bender_range);
2412 voices[voice].current_freq = freq;
2413
2414 DISABLE_INTR (flags);
2415 gus_select_voice (voice);
2416 gus_voice_freq (freq);
2417 RESTORE_INTR (flags);
2418 }
2419
2420 static int
2421 guswave_patchmgr (int dev, struct patmgr_info *rec)
2422 {
2423 int i, n;
2424
2425 switch (rec->command)
2426 {
2427 case PM_GET_DEVTYPE:
2428 rec->parm1 = PMTYPE_WAVE;
2429 return 0;
2430 break;
2431
2432 case PM_GET_NRPGM:
2433 rec->parm1 = MAX_PATCH;
2434 return 0;
2435 break;
2436
2437 case PM_GET_PGMMAP:
2438 rec->parm1 = MAX_PATCH;
2439
2440 for (i = 0; i < MAX_PATCH; i++)
2441 {
2442 int ptr = patch_table[i];
2443
2444 rec->data.data8[i] = 0;
2445
2446 while (ptr >= 0 && ptr < free_sample)
2447 {
2448 rec->data.data8[i]++;
2449 ptr = samples[ptr].key;
2450 }
2451 }
2452 return 0;
2453 break;
2454
2455 case PM_GET_PGM_PATCHES:
2456 {
2457 int ptr = patch_table[rec->parm1];
2458
2459 n = 0;
2460
2461 while (ptr >= 0 && ptr < free_sample)
2462 {
2463 rec->data.data32[n++] = ptr;
2464 ptr = samples[ptr].key;
2465 }
2466 }
2467 rec->parm1 = n;
2468 return 0;
2469 break;
2470
2471 case PM_GET_PATCH:
2472 {
2473 int ptr = rec->parm1;
2474 struct patch_info *pat;
2475
2476 if (ptr < 0 || ptr >= free_sample)
2477 return RET_ERROR (EINVAL);
2478
2479 memcpy (rec->data.data8, (char *) &samples[ptr],
2480 sizeof (struct patch_info));
2481
2482 pat = (struct patch_info *) rec->data.data8;
2483
2484 pat->key = GUS_PATCH;
2485 rec->parm1 = sample_ptrs[ptr];
2486 rec->parm2 = sizeof (struct patch_info);
2487 }
2488 return 0;
2489 break;
2490
2491 case PM_SET_PATCH:
2492 {
2493 int ptr = rec->parm1;
2494 struct patch_info *pat;
2495
2496 if (ptr < 0 || ptr >= free_sample)
2497 return RET_ERROR (EINVAL);
2498
2499 pat = (struct patch_info *) rec->data.data8;
2500
2501 if (pat->len > samples[ptr].len)
2502 return RET_ERROR (EINVAL);
2503
2504 pat->key = samples[ptr].key;
2505
2506 memcpy ((char *) &samples[ptr], rec->data.data8,
2507 sizeof (struct patch_info));
2508
2509 pat->key = GUS_PATCH;
2510 }
2511 return 0;
2512 break;
2513
2514 case PM_READ_PATCH:
2515 {
2516 int sample = rec->parm1;
2517 int n;
2518 long offs = rec->parm2;
2519 int l = rec->parm3;
2520
2521 if (sample < 0 || sample >= free_sample)
2522 return RET_ERROR (EINVAL);
2523
2524 if (offs < 0 || offs >= samples[sample].len)
2525 return RET_ERROR (EINVAL);
2526
2527 n = samples[sample].len - offs;
2528
2529 if (l > n)
2530 l = n;
2531
2532 if (l > sizeof (rec->data.data8))
2533 l = sizeof (rec->data.data8);
2534
2535 if (l <= 0)
2536 return RET_ERROR (EINVAL);
2537
2538
2539
2540 offs += sample_ptrs[sample];
2541
2542
2543
2544 for (n = 0; n < l; n++)
2545 rec->data.data8[n] = gus_peek (offs++);
2546 rec->parm1 = n;
2547
2548
2549 }
2550 return 0;
2551 break;
2552
2553 case PM_WRITE_PATCH:
2554
2555
2556 {
2557 int sample = rec->parm1;
2558 int n;
2559 long offs = rec->parm2;
2560 int l = rec->parm3;
2561
2562 if (sample < 0 || sample >= free_sample)
2563 return RET_ERROR (EINVAL);
2564
2565 if (offs < 0 || offs >= samples[sample].len)
2566 return RET_ERROR (EINVAL);
2567
2568
2569
2570 n = samples[sample].len - offs;
2571
2572
2573
2574 if (l > n)
2575 l = n;
2576
2577 if (l > sizeof (rec->data.data8))
2578 l = sizeof (rec->data.data8);
2579
2580 if (l <= 0)
2581 return RET_ERROR (EINVAL);
2582
2583
2584
2585 offs += sample_ptrs[sample];
2586
2587
2588
2589 for (n = 0; n < l; n++)
2590 gus_poke (offs++, rec->data.data8[n]);
2591 rec->parm1 = n;
2592
2593
2594 }
2595 return 0;
2596 break;
2597
2598 default:
2599 return RET_ERROR (EINVAL);
2600 }
2601 }
2602
2603 static int
2604 guswave_alloc (int dev, int chn, int note, struct voice_alloc_info *alloc)
2605 {
2606 int i, p;
2607
2608 p = alloc->ptr;
2609
2610
2611
2612
2613 for (i = 0; i < alloc->max_voice; i++)
2614 {
2615 if (alloc->map[p] == 0)
2616 {
2617 alloc->ptr = p;
2618 return p;
2619 }
2620 p = (p + 1) % alloc->max_voice;
2621 }
2622
2623
2624
2625
2626
2627 for (i = 0; i < alloc->max_voice; i++)
2628 {
2629 if (alloc->map[p] == 0xffff)
2630 {
2631 alloc->ptr = p;
2632 return p;
2633 }
2634 p = (p + 1) % alloc->max_voice;
2635 }
2636 printk ("GUS: Out of free voices\n");
2637
2638 alloc->ptr = p;
2639 return p;
2640 }
2641
2642 static struct synth_operations guswave_operations =
2643 {
2644 &gus_info,
2645 0,
2646 SYNTH_TYPE_SAMPLE,
2647 SAMPLE_TYPE_GUS,
2648 guswave_open,
2649 guswave_close,
2650 guswave_ioctl,
2651 guswave_kill_note,
2652 guswave_start_note,
2653 guswave_set_instr,
2654 guswave_reset,
2655 guswave_hw_control,
2656 guswave_load_patch,
2657 guswave_aftertouch,
2658 guswave_controller,
2659 guswave_panning,
2660 guswave_volume_method,
2661 guswave_patchmgr,
2662 guswave_bender,
2663 guswave_alloc
2664 };
2665
2666 static void
2667 set_input_volumes (void)
2668 {
2669 unsigned long flags;
2670 unsigned char mask = 0xff & ~0x06;
2671
2672 DISABLE_INTR (flags);
2673
2674
2675
2676
2677
2678
2679 if (gus_line_vol > 10)
2680 mask &= ~0x01;
2681 if (gus_mic_vol > 10)
2682 mask |= 0x04;
2683
2684 if (recording_active)
2685 {
2686
2687
2688
2689 if (!(gus_recmask & SOUND_MASK_LINE))
2690 mask |= 0x01;
2691 if (!(gus_recmask & SOUND_MASK_MIC))
2692 mask &= ~0x04;
2693 }
2694
2695 mix_image &= ~0x07;
2696 mix_image |= mask & 0x07;
2697 OUTB (mix_image, u_Mixer);
2698
2699 RESTORE_INTR (flags);
2700 }
2701
2702 int
2703 gus_default_mixer_ioctl (int dev, unsigned int cmd, unsigned int arg)
2704 {
2705 #define MIX_DEVS (SOUND_MASK_MIC|SOUND_MASK_LINE| \
2706 SOUND_MASK_SYNTH|SOUND_MASK_PCM)
2707 if (((cmd >> 8) & 0xff) == 'M')
2708 {
2709 if (cmd & IOC_IN)
2710 switch (cmd & 0xff)
2711 {
2712 case SOUND_MIXER_RECSRC:
2713 gus_recmask = IOCTL_IN (arg) & MIX_DEVS;
2714 if (!(gus_recmask & (SOUND_MASK_MIC | SOUND_MASK_LINE)))
2715 gus_recmask = SOUND_MASK_MIC;
2716
2717 return IOCTL_OUT (arg, gus_recmask);
2718 break;
2719
2720 case SOUND_MIXER_MIC:
2721 {
2722 int vol = IOCTL_IN (arg) & 0xff;
2723
2724 if (vol < 0)
2725 vol = 0;
2726 if (vol > 100)
2727 vol = 100;
2728 gus_mic_vol = vol;
2729 set_input_volumes ();
2730 return IOCTL_OUT (arg, vol | (vol << 8));
2731 }
2732 break;
2733
2734 case SOUND_MIXER_LINE:
2735 {
2736 int vol = IOCTL_IN (arg) & 0xff;
2737
2738 if (vol < 0)
2739 vol = 0;
2740 if (vol > 100)
2741 vol = 100;
2742 gus_line_vol = vol;
2743 set_input_volumes ();
2744 return IOCTL_OUT (arg, vol | (vol << 8));
2745 }
2746 break;
2747
2748 case SOUND_MIXER_PCM:
2749 gus_pcm_volume = IOCTL_IN (arg) & 0xff;
2750 if (gus_pcm_volume < 0)
2751 gus_pcm_volume = 0;
2752 if (gus_pcm_volume > 100)
2753 gus_pcm_volume = 100;
2754 gus_sampling_update_volume ();
2755 return IOCTL_OUT (arg, gus_pcm_volume | (gus_pcm_volume << 8));
2756 break;
2757
2758 case SOUND_MIXER_SYNTH:
2759 {
2760 int voice;
2761
2762 gus_wave_volume = IOCTL_IN (arg) & 0xff;
2763
2764 if (gus_wave_volume < 0)
2765 gus_wave_volume = 0;
2766 if (gus_wave_volume > 100)
2767 gus_wave_volume = 100;
2768
2769 if (active_device == GUS_DEV_WAVE)
2770 for (voice = 0; voice < nr_voices; voice++)
2771 dynamic_volume_change (voice);
2772
2773 return IOCTL_OUT (arg, gus_wave_volume | (gus_wave_volume << 8));
2774 }
2775 break;
2776
2777 default:
2778 return RET_ERROR (EINVAL);
2779 }
2780 else
2781 switch (cmd & 0xff)
2782
2783
2784 {
2785
2786 case SOUND_MIXER_RECSRC:
2787 return IOCTL_OUT (arg, gus_recmask);
2788 break;
2789
2790 case SOUND_MIXER_DEVMASK:
2791 return IOCTL_OUT (arg, MIX_DEVS);
2792 break;
2793
2794 case SOUND_MIXER_STEREODEVS:
2795 return IOCTL_OUT (arg, 0);
2796 break;
2797
2798 case SOUND_MIXER_RECMASK:
2799 return IOCTL_OUT (arg, SOUND_MASK_MIC | SOUND_MASK_LINE);
2800 break;
2801
2802 case SOUND_MIXER_CAPS:
2803 return IOCTL_OUT (arg, 0);
2804 break;
2805
2806 case SOUND_MIXER_MIC:
2807 return IOCTL_OUT (arg, gus_mic_vol | (gus_mic_vol << 8));
2808 break;
2809
2810 case SOUND_MIXER_LINE:
2811 return IOCTL_OUT (arg, gus_line_vol | (gus_line_vol << 8));
2812 break;
2813
2814 case SOUND_MIXER_PCM:
2815 return IOCTL_OUT (arg, gus_pcm_volume | (gus_pcm_volume << 8));
2816 break;
2817
2818 case SOUND_MIXER_SYNTH:
2819 return IOCTL_OUT (arg, gus_wave_volume | (gus_wave_volume << 8));
2820 break;
2821
2822 default:
2823 return RET_ERROR (EINVAL);
2824 }
2825 }
2826 else
2827 return RET_ERROR (EINVAL);
2828 }
2829
2830 static struct mixer_operations gus_mixer_operations =
2831 {
2832 gus_default_mixer_ioctl
2833 };
2834
2835 static long
2836 gus_default_mixer_init (long mem_start)
2837 {
2838 if (num_mixers < MAX_MIXER_DEV)
2839
2840
2841
2842 mixer_devs[num_mixers++] = &gus_mixer_operations;
2843
2844 return mem_start;
2845 }
2846
2847 long
2848 gus_wave_init (long mem_start, int irq, int dma)
2849 {
2850 unsigned long flags;
2851 unsigned char val;
2852 char *model_num = "2.4";
2853 int gus_type = 0x24;
2854 int mixer_type = 0;
2855
2856
2857
2858
2859
2860
2861
2862 DISABLE_INTR (flags);
2863 OUTB (0x20, gus_base + 0x0f);
2864 val = INB (gus_base + 0x0f);
2865 RESTORE_INTR (flags);
2866
2867 if (val != 0xff && (val & 0x06))
2868 {
2869
2870
2871
2872
2873
2874 val = INB (u_MixSelect);
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885 if (val == 255 || val < 5)
2886 {
2887 model_num = "3.4";
2888 gus_type = 0x34;
2889 }
2890 else if (val < 10)
2891 {
2892 model_num = "3.7";
2893 gus_type = 0x37;
2894 mixer_type = ICS2101;
2895 }
2896 else
2897 {
2898 model_num = "MAX";
2899 gus_type = 0x40;
2900 mixer_type = CS4231;
2901 #ifndef EXCLUDE_GUSMAX
2902 {
2903 unsigned char max_config = 0x40;
2904
2905 if (dma > 3)
2906 max_config |= 0x30;
2907
2908 max_config |= (gus_base >> 4) & 0x0f;
2909
2910 OUTB (max_config, gus_base + 0x106);
2911 }
2912
2913 if (ad1848_detect (gus_base + 0x10c))
2914 {
2915 gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
2916 gus_wave_volume = 90;
2917 have_gus_max = 1;
2918 ad1848_init ("GUS MAX", gus_base + 0x10c,
2919 -irq,
2920 dma,
2921 dma);
2922 }
2923 else
2924 printk ("[Where's the CS4231?]");
2925 #endif
2926 }
2927 }
2928 else
2929 {
2930
2931
2932
2933
2934
2935
2936 }
2937
2938
2939 printk (" <Gravis UltraSound %s (%dk)>", model_num, (int) gus_mem_size / 1024);
2940
2941 #ifndef SCO
2942 sprintf (gus_info.name, "Gravis UltraSound %s (%dk)", model_num, (int) gus_mem_size / 1024);
2943 #endif
2944
2945 if (irq < 0 || irq > 15)
2946 {
2947 printk ("ERROR! Invalid IRQ#%d. GUS Disabled", irq);
2948 return mem_start;
2949 }
2950
2951 if (dma < 0 || dma > 7)
2952 {
2953 printk ("ERROR! Invalid DMA#%d. GUS Disabled", dma);
2954 return mem_start;
2955 }
2956
2957 gus_irq = irq;
2958 gus_dma = dma;
2959
2960 if (num_synths >= MAX_SYNTH_DEV)
2961 printk ("GUS Error: Too many synthesizers\n");
2962 else
2963 {
2964 voice_alloc = &guswave_operations.alloc;
2965 synth_devs[num_synths++] = &guswave_operations;
2966 }
2967
2968 PERMANENT_MALLOC (struct patch_info *, samples,
2969 (MAX_SAMPLE + 1) * sizeof (*samples), mem_start);
2970
2971 reset_sample_memory ();
2972
2973 gus_initialize ();
2974
2975 if (num_audiodevs < MAX_AUDIO_DEV)
2976 {
2977 audio_devs[gus_devnum = num_audiodevs++] = &gus_sampling_operations;
2978 audio_devs[gus_devnum]->dmachan = dma;
2979 audio_devs[gus_devnum]->buffcount = 1;
2980 audio_devs[gus_devnum]->buffsize = DSP_BUFFSIZE;
2981 }
2982 else
2983 printk ("GUS: Too many PCM devices available\n");
2984
2985
2986
2987
2988
2989 switch (mixer_type)
2990 {
2991 case ICS2101:
2992 gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
2993 gus_wave_volume = 90;
2994 return ics2101_mixer_init (mem_start);
2995
2996 case CS4231:
2997
2998 default:
2999 return gus_default_mixer_init (mem_start);
3000 }
3001
3002 return mem_start;
3003 }
3004
3005 static void
3006 do_loop_irq (int voice)
3007 {
3008 unsigned char tmp;
3009 int mode, parm;
3010 unsigned long flags;
3011
3012 DISABLE_INTR (flags);
3013 gus_select_voice (voice);
3014
3015 tmp = gus_read8 (0x00);
3016 tmp &= ~0x20;
3017
3018
3019 gus_write8 (0x00, tmp);
3020
3021 mode = voices[voice].loop_irq_mode;
3022 voices[voice].loop_irq_mode = 0;
3023 parm = voices[voice].loop_irq_parm;
3024
3025 switch (mode)
3026 {
3027
3028 case LMODE_FINISH:
3029
3030
3031
3032 if ((int) (gus_read16 (0x09) >> 4) < 100)
3033
3034
3035 {
3036 gus_voice_off ();
3037 gus_rampoff ();
3038 gus_voice_init (voice);
3039 break;
3040 }
3041 gus_ramp_range (65, 4065);
3042 gus_ramp_rate (0, 63);
3043
3044
3045 gus_rampon (0x20 | 0x40);
3046
3047
3048 voices[voice].volume_irq_mode = VMODE_HALT;
3049 break;
3050
3051 case LMODE_PCM_STOP:
3052 pcm_active = 0;
3053 case LMODE_PCM:
3054 {
3055 int orig_qlen = pcm_qlen;
3056 int flag;
3057
3058 pcm_qlen--;
3059 pcm_head = (pcm_head + 1) % pcm_nblk;
3060 if (pcm_qlen)
3061 {
3062 play_next_pcm_block ();
3063 }
3064 else
3065 {
3066 gus_voice_off ();
3067 gus_rampoff ();
3068 pcm_active = 0;
3069 }
3070
3071
3072
3073
3074
3075 if (dma_active)
3076 {
3077 if (pcm_qlen == 0)
3078 flag = 1;
3079 else
3080 flag = 0;
3081 dma_active = 0;
3082 }
3083 else
3084 flag = 2;
3085 DMAbuf_outputintr (gus_devnum, flag);
3086 }
3087 break;
3088
3089 default:;
3090 }
3091 RESTORE_INTR (flags);
3092 }
3093
3094 static void
3095 do_volume_irq (int voice)
3096 {
3097 unsigned char tmp;
3098 int mode, parm;
3099 unsigned long flags;
3100
3101 DISABLE_INTR (flags);
3102
3103 gus_select_voice (voice);
3104
3105 tmp = gus_read8 (0x0d);
3106 tmp &= ~0x20;
3107
3108
3109 gus_write8 (0x0d, tmp);
3110
3111 mode = voices[voice].volume_irq_mode;
3112 voices[voice].volume_irq_mode = 0;
3113 parm = voices[voice].volume_irq_parm;
3114
3115 switch (mode)
3116 {
3117 case VMODE_HALT:
3118
3119
3120 RESTORE_INTR (flags);
3121 gus_voice_init (voice);
3122 break;
3123
3124 case VMODE_ENVELOPE:
3125 gus_rampoff ();
3126 RESTORE_INTR (flags);
3127 step_envelope (voice);
3128 break;
3129
3130 case VMODE_START_NOTE:
3131 RESTORE_INTR (flags);
3132 guswave_start_note2 (voices[voice].dev_pending, voice,
3133 voices[voice].note_pending, voices[voice].volume_pending);
3134 if (voices[voice].kill_pending)
3135 guswave_kill_note (voices[voice].dev_pending, voice,
3136 voices[voice].note_pending, 0);
3137
3138 if (voices[voice].sample_pending >= 0)
3139 {
3140 guswave_set_instr (voices[voice].dev_pending, voice,
3141 voices[voice].sample_pending);
3142 voices[voice].sample_pending = -1;
3143 }
3144 break;
3145
3146 default:;
3147 }
3148 }
3149
3150 void
3151 gus_voice_irq (void)
3152 {
3153 unsigned long wave_ignore = 0, volume_ignore = 0;
3154 unsigned long voice_bit;
3155
3156 unsigned char src, voice;
3157
3158 while (1)
3159 {
3160 src = gus_read8 (0x0f);
3161
3162
3163 voice = src & 0x1f;
3164 src &= 0xc0;
3165
3166 if (src == (0x80 | 0x40))
3167 return;
3168
3169
3170
3171 voice_bit = 1 << voice;
3172
3173 if (!(src & 0x80))
3174
3175
3176 if (!(wave_ignore & voice_bit) && (int) voice < nr_voices)
3177
3178
3179
3180 {
3181 wave_ignore |= voice_bit;
3182 do_loop_irq (voice);
3183 }
3184
3185 if (!(src & 0x40))
3186
3187
3188 if (!(volume_ignore & voice_bit) && (int) voice < nr_voices)
3189
3190
3191
3192 {
3193 volume_ignore |= voice_bit;
3194 do_volume_irq (voice);
3195 }
3196 }
3197 }
3198
3199 void
3200 guswave_dma_irq (void)
3201 {
3202 unsigned char status;
3203
3204 status = gus_look8 (0x41);
3205 if (status & 0x40)
3206 switch (active_device)
3207 {
3208 case GUS_DEV_WAVE:
3209 if (SOMEONE_WAITING (dram_sleeper, dram_sleep_flag))
3210 WAKE_UP (dram_sleeper, dram_sleep_flag);
3211 break;
3212
3213 case GUS_DEV_PCM_CONTINUE:
3214 gus_transfer_output_block (pcm_current_dev, pcm_current_buf,
3215 pcm_current_count,
3216 pcm_current_intrflag, 1);
3217 break;
3218
3219 case GUS_DEV_PCM_DONE:
3220 if (pcm_qlen < pcm_nblk)
3221 {
3222 int flag = (1 - dma_active) * 2;
3223
3224 if (pcm_qlen == 0)
3225 flag = 1;
3226 dma_active = 0;
3227 DMAbuf_outputintr (gus_devnum, flag);
3228 }
3229 break;
3230
3231 default:;
3232 }
3233
3234 status = gus_look8 (0x49);
3235
3236
3237 if (status & 0x40)
3238
3239
3240 {
3241 DMAbuf_inputintr (gus_devnum);
3242 }
3243
3244 }
3245
3246 #endif