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