OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uClinux-2.0.x/] [drivers/] [sound/] [gus_wave.c] - Blame information for rev 199

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.