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