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/] [sequencer.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 * sound/sequencer.c
3
 *
4
 * The sequencer personality manager.
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 SEQUENCER_C
17
#include "sound_config.h"
18
 
19
#ifdef CONFIG_SEQUENCER
20
 
21
#include "midi_ctrl.h"
22
 
23
static int      sequencer_ok = 0;
24
static struct sound_timer_operations *tmr;
25
static int      tmr_no = -1;    /* Currently selected timer */
26
static int      pending_timer = -1;     /* For timer change operation */
27
 
28
/*
29
 * Local counts for number of synth and MIDI devices. These are initialized
30
 * by the sequencer_open.
31
 */
32
static int      max_mididev = 0;
33
static int      max_synthdev = 0;
34
 
35
/*
36
 * The seq_mode gives the operating mode of the sequencer:
37
 *      1 = level1 (the default)
38
 *      2 = level2 (extended capabilities)
39
 */
40
 
41
#define SEQ_1   1
42
#define SEQ_2   2
43
static int      seq_mode = SEQ_1;
44
 
45
static wait_handle *seq_sleeper = NULL;
46
static volatile struct snd_wait seq_sleep_flag =
47
{0};
48
static wait_handle *midi_sleeper = NULL;
49
static volatile struct snd_wait midi_sleep_flag =
50
{0};
51
 
52
static int      midi_opened[MAX_MIDI_DEV] =
53
{0};
54
static int      midi_written[MAX_MIDI_DEV] =
55
{0};
56
 
57
unsigned long   prev_input_time = 0;
58
int             prev_event_time;
59
unsigned long   seq_time = 0;
60
 
61
#include "tuning.h"
62
 
63
#define EV_SZ   8
64
#define IEV_SZ  8
65
static unsigned char *queue = NULL;
66
static unsigned char *iqueue = NULL;
67
 
68
static volatile int qhead = 0, qtail = 0, qlen = 0;
69
static volatile int iqhead = 0, iqtail = 0, iqlen = 0;
70
static volatile int seq_playing = 0;
71
static volatile int sequencer_busy = 0;
72
static int      output_threshold;
73
static int      pre_event_timeout;
74
static unsigned synth_open_mask;
75
 
76
static int      seq_queue (unsigned char *note, char nonblock);
77
static void     seq_startplay (void);
78
static int      seq_sync (void);
79
static void     seq_reset (void);
80
static int      pmgr_present[MAX_SYNTH_DEV] =
81
{0};
82
 
83
#if MAX_SYNTH_DEV > 15
84
#error Too many synthesizer devices enabled.
85
#endif
86
 
87
int
88
sequencer_read (int dev, struct fileinfo *file, char *buf, int count)
89
{
90
  int             c = count, p = 0;
91
  int             ev_len;
92
  unsigned long   flags;
93
 
94
  dev = dev >> 4;
95
 
96
  ev_len = seq_mode == SEQ_1 ? 4 : 8;
97
 
98
  if (dev)                      /*
99
                                 * Patch manager device
100
                                 */
101
    return pmgr_read (dev - 1, file, buf, count);
102
 
103
  save_flags (flags);
104
  cli ();
105
  if (!iqlen)
106
    {
107
      if ((file->flags & (O_NONBLOCK) ?
108
           1 : 0))
109
        {
110
          restore_flags (flags);
111
          return -(EAGAIN);
112
        }
113
 
114
 
115
      {
116
        unsigned long   tlimit;
117
 
118
        if (pre_event_timeout)
119
          current_set_timeout (tlimit = jiffies + (pre_event_timeout));
120
        else
121
          tlimit = (unsigned long) -1;
122
        midi_sleep_flag.flags = WK_SLEEP;
123
        module_interruptible_sleep_on (&midi_sleeper);
124
        if (!(midi_sleep_flag.flags & WK_WAKEUP))
125
          {
126
            if (jiffies >= tlimit)
127
              midi_sleep_flag.flags |= WK_TIMEOUT;
128
          }
129
        midi_sleep_flag.flags &= ~WK_SLEEP;
130
      };
131
 
132
      if (!iqlen)
133
        {
134
          restore_flags (flags);
135
          return 0;
136
        }
137
    }
138
 
139
  while (iqlen && c >= ev_len)
140
    {
141
 
142
      memcpy_tofs (&(buf)[p], (char *) &iqueue[iqhead * IEV_SZ], ev_len);
143
      p += ev_len;
144
      c -= ev_len;
145
 
146
      iqhead = (iqhead + 1) % SEQ_MAX_QUEUE;
147
      iqlen--;
148
    }
149
  restore_flags (flags);
150
 
151
  return count - c;
152
}
153
 
154
static void
155
sequencer_midi_output (int dev)
156
{
157
  /*
158
   * Currently NOP
159
   */
160
}
161
 
162
void
163
seq_copy_to_input (unsigned char *event_rec, int len)
164
{
165
  unsigned long   flags;
166
 
167
  /*
168
     * Verify that the len is valid for the current mode.
169
   */
170
 
171
  if (len != 4 && len != 8)
172
    return;
173
  if ((seq_mode == SEQ_1) != (len == 4))
174
    return;
175
 
176
  if (iqlen >= (SEQ_MAX_QUEUE - 1))
177
    return;                     /* Overflow */
178
 
179
  save_flags (flags);
180
  cli ();
181
  memcpy (&iqueue[iqtail * IEV_SZ], event_rec, len);
182
  iqlen++;
183
  iqtail = (iqtail + 1) % SEQ_MAX_QUEUE;
184
 
185
  if ((midi_sleep_flag.flags & WK_SLEEP))
186
    {
187
      {
188
        midi_sleep_flag.flags = WK_WAKEUP;
189
        module_wake_up (&midi_sleeper);
190
      };
191
    }
192
  restore_flags (flags);
193
}
194
 
195
static void
196
sequencer_midi_input (int dev, unsigned char data)
197
{
198
  unsigned int    tstamp;
199
  unsigned char   event_rec[4];
200
 
201
  if (data == 0xfe)             /* Ignore active sensing */
202
    return;
203
 
204
  tstamp = jiffies - seq_time;
205
  if (tstamp != prev_input_time)
206
    {
207
      tstamp = (tstamp << 8) | SEQ_WAIT;
208
 
209
      seq_copy_to_input ((unsigned char *) &tstamp, 4);
210
      prev_input_time = tstamp;
211
    }
212
 
213
  event_rec[0] = SEQ_MIDIPUTC;
214
  event_rec[1] = data;
215
  event_rec[2] = dev;
216
  event_rec[3] = 0;
217
 
218
  seq_copy_to_input (event_rec, 4);
219
}
220
 
221
void
222
seq_input_event (unsigned char *event_rec, int len)
223
{
224
  unsigned long   this_time;
225
 
226
  if (seq_mode == SEQ_2)
227
    this_time = tmr->get_time (tmr_no);
228
  else
229
    this_time = jiffies - seq_time;
230
 
231
  if (this_time != prev_input_time)
232
    {
233
      unsigned char   tmp_event[8];
234
 
235
      tmp_event[0] = EV_TIMING;
236
      tmp_event[1] = TMR_WAIT_ABS;
237
      tmp_event[2] = 0;
238
      tmp_event[3] = 0;
239
      *(unsigned int *) &tmp_event[4] = this_time;
240
 
241
      seq_copy_to_input (tmp_event, 8);
242
      prev_input_time = this_time;
243
    }
244
 
245
  seq_copy_to_input (event_rec, len);
246
}
247
 
248
int
249
sequencer_write (int dev, struct fileinfo *file, const char *buf, int count)
250
{
251
  unsigned char   event_rec[EV_SZ], ev_code;
252
  int             p = 0, c, ev_size;
253
  int             err;
254
  int             mode = file->mode & O_ACCMODE;
255
 
256
  dev = dev >> 4;
257
 
258
  DEB (printk ("sequencer_write(dev=%d, count=%d)\n", dev, count));
259
 
260
  if (mode == OPEN_READ)
261
    return -(EIO);
262
 
263
  if (dev)
264
    return pmgr_write (dev - 1, file, buf, count);
265
 
266
  c = count;
267
 
268
  while (c >= 4)
269
    {
270
      memcpy_fromfs ((char *) event_rec, &(buf)[p], 4);
271
      ev_code = event_rec[0];
272
 
273
      if (ev_code == SEQ_FULLSIZE)
274
        {
275
          int             err;
276
 
277
          dev = *(unsigned short *) &event_rec[2];
278
          if (dev < 0 || dev >= max_synthdev)
279
            return -(ENXIO);
280
 
281
          if (!(synth_open_mask & (1 << dev)))
282
            return -(ENXIO);
283
 
284
          err = synth_devs[dev]->load_patch (dev, *(short *) &event_rec[0], buf, p + 4, c, 0);
285
          if (err < 0)
286
            return err;
287
 
288
          return err;
289
        }
290
 
291
      if (ev_code >= 128)
292
        {
293
          if (seq_mode == SEQ_2 && ev_code == SEQ_EXTENDED)
294
            {
295
              printk ("Sequencer: Invalid level 2 event %x\n", ev_code);
296
              return -(EINVAL);
297
            }
298
 
299
          ev_size = 8;
300
 
301
          if (c < ev_size)
302
            {
303
              if (!seq_playing)
304
                seq_startplay ();
305
              return count - c;
306
            }
307
 
308
          memcpy_fromfs ((char *) &event_rec[4], &(buf)[p + 4], 4);
309
 
310
        }
311
      else
312
        {
313
          if (seq_mode == SEQ_2)
314
            {
315
              printk ("Sequencer: 4 byte event in level 2 mode\n");
316
              return -(EINVAL);
317
            }
318
          ev_size = 4;
319
        }
320
 
321
      if (event_rec[0] == SEQ_MIDIPUTC)
322
        {
323
 
324
          if (!midi_opened[event_rec[2]])
325
            {
326
              int             mode;
327
              int             dev = event_rec[2];
328
 
329
              if (dev >= max_mididev)
330
                {
331
                  printk ("Sequencer Error: Nonexistent MIDI device %d\n", dev);
332
                  return -(ENXIO);
333
                }
334
 
335
              mode = file->mode & O_ACCMODE;
336
 
337
              if ((err = midi_devs[dev]->open (dev, mode,
338
                          sequencer_midi_input, sequencer_midi_output)) < 0)
339
                {
340
                  seq_reset ();
341
                  printk ("Sequencer Error: Unable to open Midi #%d\n", dev);
342
                  return err;
343
                }
344
 
345
              midi_opened[dev] = 1;
346
            }
347
 
348
        }
349
 
350
      if (!seq_queue (event_rec, (file->flags & (O_NONBLOCK) ?
351
                                  1 : 0)))
352
        {
353
          int             processed = count - c;
354
 
355
          if (!seq_playing)
356
            seq_startplay ();
357
 
358
          if (!processed && (file->flags & (O_NONBLOCK) ?
359
                             1 : 0))
360
            return -(EAGAIN);
361
          else
362
            return processed;
363
        }
364
 
365
      p += ev_size;
366
      c -= ev_size;
367
    }
368
 
369
  if (!seq_playing)
370
    seq_startplay ();
371
 
372
  return count;                 /* This will "eat" chunks shorter than 4 bytes (if written
373
                                   * alone) Should we really do that ?
374
                                 */
375
}
376
 
377
static int
378
seq_queue (unsigned char *note, char nonblock)
379
{
380
 
381
  /*
382
   * Test if there is space in the queue
383
   */
384
 
385
  if (qlen >= SEQ_MAX_QUEUE)
386
    if (!seq_playing)
387
      seq_startplay ();         /*
388
                                 * Give chance to drain the queue
389
                                 */
390
 
391
  if (!nonblock && qlen >= SEQ_MAX_QUEUE && !(seq_sleep_flag.flags & WK_SLEEP))
392
    {
393
      /*
394
       * Sleep until there is enough space on the queue
395
       */
396
 
397
      seq_sleep_flag.flags = WK_SLEEP;
398
      module_interruptible_sleep_on (&seq_sleeper);
399
      seq_sleep_flag.flags &= ~WK_SLEEP;;
400
    }
401
 
402
  if (qlen >= SEQ_MAX_QUEUE)
403
    {
404
      return 0;                  /*
405
                                 * To be sure
406
                                 */
407
    }
408
  memcpy (&queue[qtail * EV_SZ], note, EV_SZ);
409
 
410
  qtail = (qtail + 1) % SEQ_MAX_QUEUE;
411
  qlen++;
412
 
413
  return 1;
414
}
415
 
416
static int
417
extended_event (unsigned char *q)
418
{
419
  int             dev = q[2];
420
 
421
  if (dev < 0 || dev >= max_synthdev)
422
    return -(ENXIO);
423
 
424
  if (!(synth_open_mask & (1 << dev)))
425
    return -(ENXIO);
426
 
427
  switch (q[1])
428
    {
429
    case SEQ_NOTEOFF:
430
      synth_devs[dev]->kill_note (dev, q[3], q[4], q[5]);
431
      break;
432
 
433
    case SEQ_NOTEON:
434
      if (q[4] > 127 && q[4] != 255)
435
        return 0;
436
 
437
      synth_devs[dev]->start_note (dev, q[3], q[4], q[5]);
438
      break;
439
 
440
    case SEQ_PGMCHANGE:
441
      synth_devs[dev]->set_instr (dev, q[3], q[4]);
442
      break;
443
 
444
    case SEQ_AFTERTOUCH:
445
      synth_devs[dev]->aftertouch (dev, q[3], q[4]);
446
      break;
447
 
448
    case SEQ_BALANCE:
449
      synth_devs[dev]->panning (dev, q[3], (char) q[4]);
450
      break;
451
 
452
    case SEQ_CONTROLLER:
453
      synth_devs[dev]->controller (dev, q[3], q[4], (short) (q[5] | (q[6] << 8)));
454
      break;
455
 
456
    case SEQ_VOLMODE:
457
      if (synth_devs[dev]->volume_method != NULL)
458
        synth_devs[dev]->volume_method (dev, q[3]);
459
      break;
460
 
461
    default:
462
      return -(EINVAL);
463
    }
464
 
465
  return 0;
466
}
467
 
468
static int
469
find_voice (int dev, int chn, int note)
470
{
471
  unsigned short  key;
472
  int             i;
473
 
474
  key = (chn << 8) | (note + 1);
475
 
476
  for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
477
    if (synth_devs[dev]->alloc.map[i] == key)
478
      return i;
479
 
480
  return -1;
481
}
482
 
483
static int
484
alloc_voice (int dev, int chn, int note)
485
{
486
  unsigned short  key;
487
  int             voice;
488
 
489
  key = (chn << 8) | (note + 1);
490
 
491
  voice = synth_devs[dev]->alloc_voice (dev, chn, note,
492
                                        &synth_devs[dev]->alloc);
493
  synth_devs[dev]->alloc.map[voice] = key;
494
  synth_devs[dev]->alloc.alloc_times[voice] =
495
    synth_devs[dev]->alloc.timestamp++;
496
  return voice;
497
}
498
 
499
static void
500
seq_chn_voice_event (unsigned char *event_rec)
501
{
502
  unsigned char   dev = event_rec[1];
503
  unsigned char   cmd = event_rec[2];
504
  unsigned char   chn = event_rec[3];
505
  unsigned char   note = event_rec[4];
506
  unsigned char   parm = event_rec[5];
507
  int             voice = -1;
508
 
509
  if ((int) dev > max_synthdev)
510
    return;
511
  if (!(synth_open_mask & (1 << dev)))
512
    return;
513
  if (!synth_devs[dev])
514
    return;
515
 
516
  if (seq_mode == SEQ_2)
517
    {
518
      if (synth_devs[dev]->alloc_voice)
519
        voice = find_voice (dev, chn, note);
520
 
521
      if (cmd == MIDI_NOTEON && parm == 0)
522
        {
523
          cmd = MIDI_NOTEOFF;
524
          parm = 64;
525
        }
526
    }
527
 
528
  switch (cmd)
529
    {
530
    case MIDI_NOTEON:
531
      if (note > 127 && note != 255)    /* Not a seq2 feature */
532
        return;
533
 
534
      if (voice == -1 && seq_mode == SEQ_2 && synth_devs[dev]->alloc_voice)
535
        {                       /* Internal synthesizer (FM, GUS, etc) */
536
          voice = alloc_voice (dev, chn, note);
537
        }
538
 
539
      if (voice == -1)
540
        voice = chn;
541
 
542
      if (seq_mode == SEQ_2 && (int) dev < num_synths)
543
        {
544
          /*
545
             * The MIDI channel 10 is a percussive channel. Use the note
546
             * number to select the proper patch (128 to 255) to play.
547
           */
548
 
549
          if (chn == 9)
550
            {
551
              synth_devs[dev]->set_instr (dev, voice, 128 + note);
552
              synth_devs[dev]->chn_info[chn].pgm_num = 128 + note;
553
              note = 60;        /* Middle C */
554
 
555
            }
556
        }
557
 
558
      if (seq_mode == SEQ_2)
559
        {
560
          synth_devs[dev]->setup_voice (dev, voice, chn);
561
        }
562
 
563
      synth_devs[dev]->start_note (dev, voice, note, parm);
564
      break;
565
 
566
    case MIDI_NOTEOFF:
567
      if (voice == -1)
568
        voice = chn;
569
      synth_devs[dev]->kill_note (dev, voice, note, parm);
570
      break;
571
 
572
    case MIDI_KEY_PRESSURE:
573
      if (voice == -1)
574
        voice = chn;
575
      synth_devs[dev]->aftertouch (dev, voice, parm);
576
      break;
577
 
578
    default:;
579
    }
580
}
581
 
582
static void
583
seq_chn_common_event (unsigned char *event_rec)
584
{
585
  unsigned char   dev = event_rec[1];
586
  unsigned char   cmd = event_rec[2];
587
  unsigned char   chn = event_rec[3];
588
  unsigned char   p1 = event_rec[4];
589
 
590
  /* unsigned char   p2 = event_rec[5]; */
591
  unsigned short  w14 = *(short *) &event_rec[6];
592
 
593
  if ((int) dev > max_synthdev)
594
    return;
595
  if (!(synth_open_mask & (1 << dev)))
596
    return;
597
  if (!synth_devs[dev])
598
    return;
599
 
600
  switch (cmd)
601
    {
602
    case MIDI_PGM_CHANGE:
603
      if (seq_mode == SEQ_2)
604
        {
605
          synth_devs[dev]->chn_info[chn].pgm_num = p1;
606
          if ((int) dev >= num_synths)
607
            synth_devs[dev]->set_instr (dev, chn, p1);
608
        }
609
      else
610
        synth_devs[dev]->set_instr (dev, chn, p1);
611
 
612
      break;
613
 
614
    case MIDI_CTL_CHANGE:
615
      if (seq_mode == SEQ_2)
616
        {
617
          if (chn > 15 || p1 > 127)
618
            break;
619
 
620
          synth_devs[dev]->chn_info[chn].controllers[p1] = w14 & 0x7f;
621
 
622
          if (p1 < 32)          /* Setting MSB should clear LSB to 0 */
623
            synth_devs[dev]->chn_info[chn].controllers[p1 + 32] = 0;
624
 
625
          if ((int) dev < num_synths)
626
            {
627
              int             val = w14 & 0x7f;
628
              int             i, key;
629
 
630
              if (p1 < 64)      /* Combine MSB and LSB */
631
                {
632
                  val = ((synth_devs[dev]->
633
                          chn_info[chn].controllers[p1 & ~32] & 0x7f) << 7)
634
                    | (synth_devs[dev]->
635
                       chn_info[chn].controllers[p1 | 32] & 0x7f);
636
                  p1 &= ~32;
637
                }
638
 
639
              /* Handle all playing notes on this channel */
640
 
641
              key = ((int) chn << 8);
642
 
643
              for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
644
                if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
645
                  synth_devs[dev]->controller (dev, i, p1, val);
646
            }
647
          else
648
            synth_devs[dev]->controller (dev, chn, p1, w14);
649
        }
650
      else                      /* Mode 1 */
651
        synth_devs[dev]->controller (dev, chn, p1, w14);
652
      break;
653
 
654
    case MIDI_PITCH_BEND:
655
      if (seq_mode == SEQ_2)
656
        {
657
          synth_devs[dev]->chn_info[chn].bender_value = w14;
658
 
659
          if ((int) dev < num_synths)
660
            {                   /* Handle all playing notes on this channel */
661
              int             i, key;
662
 
663
              key = (chn << 8);
664
 
665
              for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
666
                if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
667
                  synth_devs[dev]->bender (dev, i, w14);
668
            }
669
          else
670
            synth_devs[dev]->bender (dev, chn, w14);
671
        }
672
      else                      /* MODE 1 */
673
        synth_devs[dev]->bender (dev, chn, w14);
674
      break;
675
 
676
    default:;
677
    }
678
}
679
 
680
static int
681
seq_timing_event (unsigned char *event_rec)
682
{
683
  unsigned char   cmd = event_rec[1];
684
  unsigned int    parm = *(int *) &event_rec[4];
685
 
686
  if (seq_mode == SEQ_2)
687
    {
688
      int             ret;
689
 
690
      if ((ret = tmr->event (tmr_no, event_rec)) == TIMER_ARMED)
691
        {
692
          if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
693
            {
694
              unsigned long   flags;
695
 
696
              save_flags (flags);
697
              cli ();
698
              if ((seq_sleep_flag.flags & WK_SLEEP))
699
                {
700
                  {
701
                    seq_sleep_flag.flags = WK_WAKEUP;
702
                    module_wake_up (&seq_sleeper);
703
                  };
704
                }
705
              restore_flags (flags);
706
            }
707
        }
708
      return ret;
709
    }
710
 
711
  switch (cmd)
712
    {
713
    case TMR_WAIT_REL:
714
      parm += prev_event_time;
715
 
716
      /*
717
         * NOTE!  No break here. Execution of TMR_WAIT_REL continues in the
718
         * next case (TMR_WAIT_ABS)
719
       */
720
 
721
    case TMR_WAIT_ABS:
722
      if (parm > 0)
723
        {
724
          long            time;
725
 
726
          seq_playing = 1;
727
          time = parm;
728
          prev_event_time = time;
729
 
730
          request_sound_timer (time);
731
 
732
          if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
733
            {
734
              unsigned long   flags;
735
 
736
              save_flags (flags);
737
              cli ();
738
              if ((seq_sleep_flag.flags & WK_SLEEP))
739
                {
740
                  {
741
                    seq_sleep_flag.flags = WK_WAKEUP;
742
                    module_wake_up (&seq_sleeper);
743
                  };
744
                }
745
              restore_flags (flags);
746
            }
747
 
748
          return TIMER_ARMED;
749
        }
750
      break;
751
 
752
    case TMR_START:
753
      seq_time = jiffies;
754
      prev_input_time = 0;
755
      prev_event_time = 0;
756
      break;
757
 
758
    case TMR_STOP:
759
      break;
760
 
761
    case TMR_CONTINUE:
762
      break;
763
 
764
    case TMR_TEMPO:
765
      break;
766
 
767
    case TMR_ECHO:
768
      if (seq_mode == SEQ_2)
769
        seq_copy_to_input (event_rec, 8);
770
      else
771
        {
772
          parm = (parm << 8 | SEQ_ECHO);
773
          seq_copy_to_input ((unsigned char *) &parm, 4);
774
        }
775
      break;
776
 
777
    default:;
778
    }
779
 
780
  return TIMER_NOT_ARMED;
781
}
782
 
783
static void
784
seq_local_event (unsigned char *event_rec)
785
{
786
  unsigned char   cmd = event_rec[1];
787
  unsigned int    parm = *((unsigned int *) &event_rec[4]);
788
 
789
  switch (cmd)
790
    {
791
    case LOCL_STARTAUDIO:
792
#ifdef CONFIG_AUDIO
793
      DMAbuf_start_devices (parm);
794
#endif
795
      break;
796
 
797
    default:;
798
    }
799
}
800
 
801
static void
802
seq_sysex_message (unsigned char *event_rec)
803
{
804
  int             dev = event_rec[1];
805
  int             i, l = 0;
806
  unsigned char  *buf = &event_rec[2];
807
 
808
  if ((int) dev > max_synthdev)
809
    return;
810
  if (!(synth_open_mask & (1 << dev)))
811
    return;
812
  if (!synth_devs[dev])
813
    return;
814
  if (!synth_devs[dev]->send_sysex)
815
    return;
816
 
817
  l = 0;
818
  for (i = 0; i < 6 && buf[i] != 0xff; i++)
819
    l = i + 1;
820
 
821
  if (l > 0)
822
    synth_devs[dev]->send_sysex (dev, buf, l);
823
}
824
 
825
static int
826
play_event (unsigned char *q)
827
{
828
  /*
829
     * NOTE! This routine returns
830
     *   0 = normal event played.
831
     *   1 = Timer armed. Suspend playback until timer callback.
832
     *   2 = MIDI output buffer full. Restore queue and suspend until timer
833
   */
834
  unsigned int   *delay;
835
 
836
  switch (q[0])
837
    {
838
    case SEQ_NOTEOFF:
839
      if (synth_open_mask & (1 << 0))
840
        if (synth_devs[0])
841
          synth_devs[0]->kill_note (0, q[1], 255, q[3]);
842
      break;
843
 
844
    case SEQ_NOTEON:
845
      if (q[4] < 128 || q[4] == 255)
846
        if (synth_open_mask & (1 << 0))
847
          if (synth_devs[0])
848
            synth_devs[0]->start_note (0, q[1], q[2], q[3]);
849
      break;
850
 
851
    case SEQ_WAIT:
852
      delay = (unsigned int *) q;       /*
853
                                         * Bytes 1 to 3 are containing the *
854
                                         * delay in 'ticks'
855
                                         */
856
      *delay = (*delay >> 8) & 0xffffff;
857
 
858
      if (*delay > 0)
859
        {
860
          long            time;
861
 
862
          seq_playing = 1;
863
          time = *delay;
864
          prev_event_time = time;
865
 
866
          request_sound_timer (time);
867
 
868
          if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
869
            {
870
              unsigned long   flags;
871
 
872
              save_flags (flags);
873
              cli ();
874
              if ((seq_sleep_flag.flags & WK_SLEEP))
875
                {
876
                  {
877
                    seq_sleep_flag.flags = WK_WAKEUP;
878
                    module_wake_up (&seq_sleeper);
879
                  };
880
                }
881
              restore_flags (flags);
882
            }
883
          /*
884
             * The timer is now active and will reinvoke this function
885
             * after the timer expires. Return to the caller now.
886
           */
887
          return 1;
888
        }
889
      break;
890
 
891
    case SEQ_PGMCHANGE:
892
      if (synth_open_mask & (1 << 0))
893
        if (synth_devs[0])
894
          synth_devs[0]->set_instr (0, q[1], q[2]);
895
      break;
896
 
897
    case SEQ_SYNCTIMER: /*
898
                                   * Reset timer
899
                                 */
900
      seq_time = jiffies;
901
      prev_input_time = 0;
902
      prev_event_time = 0;
903
      break;
904
 
905
    case SEQ_MIDIPUTC:          /*
906
                                 * Put a midi character
907
                                 */
908
      if (midi_opened[q[2]])
909
        {
910
          int             dev;
911
 
912
          dev = q[2];
913
 
914
          if (dev < 0 || dev >= num_midis)
915
            break;
916
 
917
          if (!midi_devs[dev]->putc (dev, q[1]))
918
            {
919
              /*
920
                 * Output FIFO is full. Wait one timer cycle and try again.
921
               */
922
 
923
              seq_playing = 1;
924
              request_sound_timer (-1);
925
              return 2;
926
            }
927
          else
928
            midi_written[dev] = 1;
929
        }
930
      break;
931
 
932
    case SEQ_ECHO:
933
      seq_copy_to_input (q, 4); /*
934
                                 * Echo back to the process
935
                                 */
936
      break;
937
 
938
    case SEQ_PRIVATE:
939
      if ((int) q[1] < max_synthdev)
940
        synth_devs[q[1]]->hw_control (q[1], q);
941
      break;
942
 
943
    case SEQ_EXTENDED:
944
      extended_event (q);
945
      break;
946
 
947
    case EV_CHN_VOICE:
948
      seq_chn_voice_event (q);
949
      break;
950
 
951
    case EV_CHN_COMMON:
952
      seq_chn_common_event (q);
953
      break;
954
 
955
    case EV_TIMING:
956
      if (seq_timing_event (q) == TIMER_ARMED)
957
        {
958
          return 1;
959
        }
960
      break;
961
 
962
    case EV_SEQ_LOCAL:
963
      seq_local_event (q);
964
      break;
965
 
966
    case EV_SYSEX:
967
      seq_sysex_message (q);
968
      break;
969
 
970
    default:;
971
    }
972
 
973
  return 0;
974
}
975
 
976
static void
977
seq_startplay (void)
978
{
979
  unsigned long   flags;
980
  int             this_one, action;
981
 
982
  while (qlen > 0)
983
    {
984
 
985
      save_flags (flags);
986
      cli ();
987
      qhead = ((this_one = qhead) + 1) % SEQ_MAX_QUEUE;
988
      qlen--;
989
      restore_flags (flags);
990
 
991
      seq_playing = 1;
992
 
993
      if ((action = play_event (&queue[this_one * EV_SZ])))
994
        {                       /* Suspend playback. Next timer routine invokes this routine again */
995
          if (action == 2)
996
            {
997
              qlen++;
998
              qhead = this_one;
999
            }
1000
          return;
1001
        }
1002
 
1003
    }
1004
 
1005
  seq_playing = 0;
1006
 
1007
  if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
1008
    {
1009
      unsigned long   flags;
1010
 
1011
      save_flags (flags);
1012
      cli ();
1013
      if ((seq_sleep_flag.flags & WK_SLEEP))
1014
        {
1015
          {
1016
            seq_sleep_flag.flags = WK_WAKEUP;
1017
            module_wake_up (&seq_sleeper);
1018
          };
1019
        }
1020
      restore_flags (flags);
1021
    }
1022
}
1023
 
1024
static void
1025
reset_controllers (int dev, unsigned char *controller, int update_dev)
1026
{
1027
 
1028
  int             i;
1029
 
1030
  for (i = 0; i < 128; i++)
1031
    controller[i] = ctrl_def_values[i];
1032
}
1033
 
1034
static void
1035
setup_mode2 (void)
1036
{
1037
  int             dev;
1038
 
1039
  max_synthdev = num_synths;
1040
 
1041
  for (dev = 0; dev < num_midis; dev++)
1042
    if (midi_devs[dev]->converter != NULL)
1043
      {
1044
        synth_devs[max_synthdev++] =
1045
          midi_devs[dev]->converter;
1046
      }
1047
 
1048
  for (dev = 0; dev < max_synthdev; dev++)
1049
    {
1050
      int             chn;
1051
 
1052
      for (chn = 0; chn < 16; chn++)
1053
        {
1054
          synth_devs[dev]->chn_info[chn].pgm_num = 0;
1055
          reset_controllers (dev,
1056
                             synth_devs[dev]->chn_info[chn].controllers,
1057
                             0);
1058
          synth_devs[dev]->chn_info[chn].bender_value = (1 << 7);       /* Neutral */
1059
        }
1060
    }
1061
 
1062
  max_mididev = 0;
1063
  seq_mode = SEQ_2;
1064
}
1065
 
1066
int
1067
sequencer_open (int dev, struct fileinfo *file)
1068
{
1069
  int             retval, mode, i;
1070
  int             level, tmp;
1071
  unsigned long   flags;
1072
 
1073
  level = ((dev & 0x0f) == SND_DEV_SEQ2) ? 2 : 1;
1074
 
1075
  dev = dev >> 4;
1076
  mode = file->mode & O_ACCMODE;
1077
 
1078
  DEB (printk ("sequencer_open(dev=%d)\n", dev));
1079
 
1080
  if (!sequencer_ok)
1081
    {
1082
      printk ("Soundcard: Sequencer not initialized\n");
1083
      return -(ENXIO);
1084
    }
1085
 
1086
  if (dev)                      /* Patch manager device */
1087
    {
1088
      printk ("Patch manager interface is currently broken. Sorry\n");
1089
      return -(ENXIO);
1090
    }
1091
 
1092
  save_flags (flags);
1093
  cli ();
1094
  if (sequencer_busy)
1095
    {
1096
      printk ("Sequencer busy\n");
1097
      restore_flags (flags);
1098
      return -(EBUSY);
1099
    }
1100
  sequencer_busy = 1;
1101
  restore_flags (flags);
1102
 
1103
  max_mididev = num_midis;
1104
  max_synthdev = num_synths;
1105
  pre_event_timeout = 0;
1106
  seq_mode = SEQ_1;
1107
 
1108
  if (pending_timer != -1)
1109
    {
1110
      tmr_no = pending_timer;
1111
      pending_timer = -1;
1112
    }
1113
 
1114
  if (tmr_no == -1)             /* Not selected yet */
1115
    {
1116
      int             i, best;
1117
 
1118
      best = -1;
1119
      for (i = 0; i < num_sound_timers; i++)
1120
        if (sound_timer_devs[i]->priority > best)
1121
          {
1122
            tmr_no = i;
1123
            best = sound_timer_devs[i]->priority;
1124
          }
1125
 
1126
      if (tmr_no == -1)         /* Should not be */
1127
        tmr_no = 0;
1128
    }
1129
 
1130
  tmr = sound_timer_devs[tmr_no];
1131
 
1132
  if (level == 2)
1133
    {
1134
      if (tmr == NULL)
1135
        {
1136
          printk ("sequencer: No timer for level 2\n");
1137
          sequencer_busy = 0;
1138
          return -(ENXIO);
1139
        }
1140
      setup_mode2 ();
1141
    }
1142
 
1143
  if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
1144
    if (!max_mididev)
1145
      {
1146
        printk ("Sequencer: No Midi devices. Input not possible\n");
1147
        sequencer_busy = 0;
1148
        return -(ENXIO);
1149
      }
1150
 
1151
  if (!max_synthdev && !max_mididev)
1152
    {
1153
      sequencer_busy = 0;
1154
      return -(ENXIO);
1155
    }
1156
 
1157
  synth_open_mask = 0;
1158
 
1159
  for (i = 0; i < max_mididev; i++)
1160
    {
1161
      midi_opened[i] = 0;
1162
      midi_written[i] = 0;
1163
    }
1164
 
1165
  /*
1166
   * if (mode == OPEN_WRITE || mode == OPEN_READWRITE)
1167
   */
1168
  for (i = 0; i < max_synthdev; i++)     /*
1169
                                         * Open synth devices
1170
                                         */
1171
    if ((tmp = synth_devs[i]->open (i, mode)) < 0)
1172
      {
1173
        printk ("Sequencer: Warning! Cannot open synth device #%d (%d)\n", i, tmp);
1174
        if (synth_devs[i]->midi_dev)
1175
          printk ("(Maps to MIDI dev #%d)\n", synth_devs[i]->midi_dev);
1176
      }
1177
    else
1178
      {
1179
        synth_open_mask |= (1 << i);
1180
        if (synth_devs[i]->midi_dev)    /*
1181
                                         * Is a midi interface
1182
                                         */
1183
          midi_opened[synth_devs[i]->midi_dev] = 1;
1184
      }
1185
 
1186
  seq_time = jiffies;
1187
  prev_input_time = 0;
1188
  prev_event_time = 0;
1189
 
1190
  if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
1191
    {                           /*
1192
                                 * Initialize midi input devices
1193
                                 */
1194
      for (i = 0; i < max_mididev; i++)
1195
        if (!midi_opened[i])
1196
          {
1197
            if ((retval = midi_devs[i]->open (i, mode,
1198
                         sequencer_midi_input, sequencer_midi_output)) >= 0)
1199
              midi_opened[i] = 1;
1200
          }
1201
    }
1202
 
1203
  if (seq_mode == SEQ_2)
1204
    {
1205
      tmr->open (tmr_no, seq_mode);
1206
    }
1207
 
1208
  seq_sleep_flag.flags = WK_NONE;
1209
  midi_sleep_flag.flags = WK_NONE;
1210
  output_threshold = SEQ_MAX_QUEUE / 2;
1211
 
1212
  for (i = 0; i < num_synths; i++)
1213
    if (pmgr_present[i])
1214
      pmgr_inform (i, PM_E_OPENED, 0, 0, 0, 0);
1215
 
1216
  return 0;
1217
}
1218
 
1219
void
1220
seq_drain_midi_queues (void)
1221
{
1222
  int             i, n;
1223
 
1224
  /*
1225
   * Give the Midi drivers time to drain their output queues
1226
   */
1227
 
1228
  n = 1;
1229
 
1230
  while (!current_got_fatal_signal () && n)
1231
    {
1232
      n = 0;
1233
 
1234
      for (i = 0; i < max_mididev; i++)
1235
        if (midi_opened[i] && midi_written[i])
1236
          if (midi_devs[i]->buffer_status != NULL)
1237
            if (midi_devs[i]->buffer_status (i))
1238
              n++;
1239
 
1240
      /*
1241
       * Let's have a delay
1242
       */
1243
      if (n)
1244
        {
1245
 
1246
          {
1247
            unsigned long   tlimit;
1248
 
1249
            if (HZ / 10)
1250
              current_set_timeout (tlimit = jiffies + (HZ / 10));
1251
            else
1252
              tlimit = (unsigned long) -1;
1253
            seq_sleep_flag.flags = WK_SLEEP;
1254
            module_interruptible_sleep_on (&seq_sleeper);
1255
            if (!(seq_sleep_flag.flags & WK_WAKEUP))
1256
              {
1257
                if (jiffies >= tlimit)
1258
                  seq_sleep_flag.flags |= WK_TIMEOUT;
1259
              }
1260
            seq_sleep_flag.flags &= ~WK_SLEEP;
1261
          };
1262
        }
1263
    }
1264
}
1265
 
1266
void
1267
sequencer_release (int dev, struct fileinfo *file)
1268
{
1269
  int             i;
1270
  int             mode = file->mode & O_ACCMODE;
1271
 
1272
  dev = dev >> 4;
1273
 
1274
  DEB (printk ("sequencer_release(dev=%d)\n", dev));
1275
 
1276
  if (dev)                      /*
1277
                                 * Patch manager device
1278
                                 */
1279
    {
1280
      dev--;
1281
      pmgr_release (dev);
1282
      pmgr_present[dev] = 0;
1283
      return;
1284
    }
1285
 
1286
  /*
1287
   * * Wait until the queue is empty (if we don't have nonblock)
1288
   */
1289
 
1290
  if (mode != OPEN_READ && !(file->flags & (O_NONBLOCK) ?
1291
                             1 : 0))
1292
    while (!current_got_fatal_signal () && qlen)
1293
      {
1294
        seq_sync ();
1295
      }
1296
 
1297
  if (mode != OPEN_READ)
1298
    seq_drain_midi_queues ();   /*
1299
                                 * Ensure the output queues are empty
1300
                                 */
1301
  seq_reset ();
1302
  if (mode != OPEN_READ)
1303
    seq_drain_midi_queues ();   /*
1304
                                 * Flush the all notes off messages
1305
                                 */
1306
 
1307
  for (i = 0; i < max_synthdev; i++)
1308
    if (synth_open_mask & (1 << i))     /*
1309
                                         * Actually opened
1310
                                         */
1311
      if (synth_devs[i])
1312
        {
1313
          synth_devs[i]->close (i);
1314
 
1315
          if (synth_devs[i]->midi_dev)
1316
            midi_opened[synth_devs[i]->midi_dev] = 0;
1317
        }
1318
 
1319
  for (i = 0; i < num_synths; i++)
1320
    if (pmgr_present[i])
1321
      pmgr_inform (i, PM_E_CLOSED, 0, 0, 0, 0);
1322
 
1323
  for (i = 0; i < max_mididev; i++)
1324
    if (midi_opened[i])
1325
      midi_devs[i]->close (i);
1326
 
1327
  if (seq_mode == SEQ_2)
1328
    tmr->close (tmr_no);
1329
 
1330
  sequencer_busy = 0;
1331
}
1332
 
1333
static int
1334
seq_sync (void)
1335
{
1336
  unsigned long   flags;
1337
 
1338
  if (qlen && !seq_playing && !current_got_fatal_signal ())
1339
    seq_startplay ();
1340
 
1341
  save_flags (flags);
1342
  cli ();
1343
  if (qlen && !(seq_sleep_flag.flags & WK_SLEEP))
1344
    {
1345
 
1346
      {
1347
        unsigned long   tlimit;
1348
 
1349
        if (HZ)
1350
          current_set_timeout (tlimit = jiffies + (HZ));
1351
        else
1352
          tlimit = (unsigned long) -1;
1353
        seq_sleep_flag.flags = WK_SLEEP;
1354
        module_interruptible_sleep_on (&seq_sleeper);
1355
        if (!(seq_sleep_flag.flags & WK_WAKEUP))
1356
          {
1357
            if (jiffies >= tlimit)
1358
              seq_sleep_flag.flags |= WK_TIMEOUT;
1359
          }
1360
        seq_sleep_flag.flags &= ~WK_SLEEP;
1361
      };
1362
    }
1363
  restore_flags (flags);
1364
 
1365
  return qlen;
1366
}
1367
 
1368
static void
1369
midi_outc (int dev, unsigned char data)
1370
{
1371
  /*
1372
   * NOTE! Calls sleep(). Don't call this from interrupt.
1373
   */
1374
 
1375
  int             n;
1376
  unsigned long   flags;
1377
 
1378
  /*
1379
   * This routine sends one byte to the Midi channel.
1380
   * If the output FIFO is full, it waits until there
1381
   * is space in the queue
1382
   */
1383
 
1384
  n = 3 * HZ;                   /* Timeout */
1385
 
1386
  save_flags (flags);
1387
  cli ();
1388
  while (n && !midi_devs[dev]->putc (dev, data))
1389
    {
1390
 
1391
      {
1392
        unsigned long   tlimit;
1393
 
1394
        if (4)
1395
          current_set_timeout (tlimit = jiffies + (4));
1396
        else
1397
          tlimit = (unsigned long) -1;
1398
        seq_sleep_flag.flags = WK_SLEEP;
1399
        module_interruptible_sleep_on (&seq_sleeper);
1400
        if (!(seq_sleep_flag.flags & WK_WAKEUP))
1401
          {
1402
            if (jiffies >= tlimit)
1403
              seq_sleep_flag.flags |= WK_TIMEOUT;
1404
          }
1405
        seq_sleep_flag.flags &= ~WK_SLEEP;
1406
      };
1407
      n--;
1408
    }
1409
  restore_flags (flags);
1410
}
1411
 
1412
static void
1413
seq_reset (void)
1414
{
1415
  /*
1416
   * NOTE! Calls sleep(). Don't call this from interrupt.
1417
   */
1418
 
1419
  int             i;
1420
  int             chn;
1421
  unsigned long   flags;
1422
 
1423
  sound_stop_timer ();
1424
  seq_time = jiffies;
1425
  prev_input_time = 0;
1426
  prev_event_time = 0;
1427
 
1428
  qlen = qhead = qtail = 0;
1429
  iqlen = iqhead = iqtail = 0;
1430
 
1431
  for (i = 0; i < max_synthdev; i++)
1432
    if (synth_open_mask & (1 << i))
1433
      if (synth_devs[i])
1434
        synth_devs[i]->reset (i);
1435
 
1436
  if (seq_mode == SEQ_2)
1437
    {
1438
 
1439
      for (chn = 0; chn < 16; chn++)
1440
        for (i = 0; i < max_synthdev; i++)
1441
          if (synth_open_mask & (1 << i))
1442
            if (synth_devs[i])
1443
              {
1444
                synth_devs[i]->controller (i, chn, 123, 0);      /* All notes off */
1445
                synth_devs[i]->controller (i, chn, 121, 0);      /* Reset all ctl */
1446
                synth_devs[i]->bender (i, chn, 1 << 13);        /* Bender off */
1447
              }
1448
 
1449
    }
1450
  else
1451
    /* seq_mode == SEQ_1 */
1452
    {
1453
      for (i = 0; i < max_mididev; i++)
1454
        if (midi_written[i])    /*
1455
                                 * Midi used. Some notes may still be playing
1456
                                 */
1457
          {
1458
            /*
1459
               *      Sending just a ACTIVE SENSING message should be enough to stop all
1460
               *      playing notes. Since there are devices not recognizing the
1461
               *      active sensing, we have to send some all notes off messages also.
1462
             */
1463
            midi_outc (i, 0xfe);
1464
 
1465
            for (chn = 0; chn < 16; chn++)
1466
              {
1467
                midi_outc (i,
1468
                           (unsigned char) (0xb0 + (chn & 0x0f)));      /* control change */
1469
                midi_outc (i, 0x7b);    /* All notes off */
1470
                midi_outc (i, 0);        /* Dummy parameter */
1471
              }
1472
 
1473
            midi_devs[i]->close (i);
1474
 
1475
            midi_written[i] = 0;
1476
            midi_opened[i] = 0;
1477
          }
1478
    }
1479
 
1480
  seq_playing = 0;
1481
 
1482
  save_flags (flags);
1483
  cli ();
1484
  if ((seq_sleep_flag.flags & WK_SLEEP))
1485
    {
1486
      /*      printk ("Sequencer Warning: Unexpected sleeping process - Waking up\n"); */
1487
      {
1488
        seq_sleep_flag.flags = WK_WAKEUP;
1489
        module_wake_up (&seq_sleeper);
1490
      };
1491
    }
1492
  restore_flags (flags);
1493
 
1494
}
1495
 
1496
static void
1497
seq_panic (void)
1498
{
1499
  /*
1500
     * This routine is called by the application in case the user
1501
     * wants to reset the system to the default state.
1502
   */
1503
 
1504
  seq_reset ();
1505
 
1506
  /*
1507
     * Since some of the devices don't recognize the active sensing and
1508
     * all notes off messages, we have to shut all notes manually.
1509
     *
1510
     *      TO BE IMPLEMENTED LATER
1511
   */
1512
 
1513
  /*
1514
     * Also return the controllers to their default states
1515
   */
1516
}
1517
 
1518
int
1519
sequencer_ioctl (int dev, struct fileinfo *file,
1520
                 unsigned int cmd, caddr_t arg)
1521
{
1522
  int             midi_dev, orig_dev;
1523
  int             mode = file->mode & O_ACCMODE;
1524
 
1525
  orig_dev = dev = dev >> 4;
1526
 
1527
  switch (cmd)
1528
    {
1529
    case SNDCTL_TMR_TIMEBASE:
1530
    case SNDCTL_TMR_TEMPO:
1531
    case SNDCTL_TMR_START:
1532
    case SNDCTL_TMR_STOP:
1533
    case SNDCTL_TMR_CONTINUE:
1534
    case SNDCTL_TMR_METRONOME:
1535
    case SNDCTL_TMR_SOURCE:
1536
      if (dev)                  /* Patch manager */
1537
        return -(EIO);
1538
 
1539
      if (seq_mode != SEQ_2)
1540
        return -(EINVAL);
1541
      return tmr->ioctl (tmr_no, cmd, arg);
1542
      break;
1543
 
1544
    case SNDCTL_TMR_SELECT:
1545
      if (dev)                  /* Patch manager */
1546
        return -(EIO);
1547
 
1548
      if (seq_mode != SEQ_2)
1549
        return -(EINVAL);
1550
      pending_timer = get_user ((int *) arg);
1551
 
1552
      if (pending_timer < 0 || pending_timer >= num_sound_timers)
1553
        {
1554
          pending_timer = -1;
1555
          return -(EINVAL);
1556
        }
1557
 
1558
      return snd_ioctl_return ((int *) arg, pending_timer);
1559
      break;
1560
 
1561
    case SNDCTL_SEQ_PANIC:
1562
      seq_panic ();
1563
      break;
1564
 
1565
    case SNDCTL_SEQ_SYNC:
1566
      if (dev)                  /*
1567
                                 * Patch manager
1568
                                 */
1569
        return -(EIO);
1570
 
1571
      if (mode == OPEN_READ)
1572
        return 0;
1573
      while (qlen && !current_got_fatal_signal ())
1574
        seq_sync ();
1575
      if (qlen)
1576
        return -(EINTR);
1577
      else
1578
        return 0;
1579
      break;
1580
 
1581
    case SNDCTL_SEQ_RESET:
1582
      if (dev)                  /*
1583
                                 * Patch manager
1584
                                 */
1585
        return -(EIO);
1586
 
1587
      seq_reset ();
1588
      return 0;
1589
      break;
1590
 
1591
    case SNDCTL_SEQ_TESTMIDI:
1592
      if (dev)                  /*
1593
                                 * Patch manager
1594
                                 */
1595
        return -(EIO);
1596
 
1597
      midi_dev = get_user ((int *) arg);
1598
      if (midi_dev < 0 || midi_dev >= max_mididev)
1599
        return -(ENXIO);
1600
 
1601
      if (!midi_opened[midi_dev])
1602
        {
1603
          int             err, mode;
1604
 
1605
          mode = file->mode & O_ACCMODE;
1606
          if ((err = midi_devs[midi_dev]->open (midi_dev, mode,
1607
                                                sequencer_midi_input,
1608
                                                sequencer_midi_output)) < 0)
1609
            return err;
1610
        }
1611
 
1612
      midi_opened[midi_dev] = 1;
1613
 
1614
      return 0;
1615
      break;
1616
 
1617
    case SNDCTL_SEQ_GETINCOUNT:
1618
      if (dev)                  /*
1619
                                 * Patch manager
1620
                                 */
1621
        return -(EIO);
1622
 
1623
      if (mode == OPEN_WRITE)
1624
        return 0;
1625
      return snd_ioctl_return ((int *) arg, iqlen);
1626
      break;
1627
 
1628
    case SNDCTL_SEQ_GETOUTCOUNT:
1629
 
1630
      if (mode == OPEN_READ)
1631
        return 0;
1632
      return snd_ioctl_return ((int *) arg, SEQ_MAX_QUEUE - qlen);
1633
      break;
1634
 
1635
    case SNDCTL_SEQ_CTRLRATE:
1636
      if (dev)                  /* Patch manager */
1637
        return -(EIO);
1638
 
1639
      /*
1640
       * If *arg == 0, just return the current rate
1641
       */
1642
      if (seq_mode == SEQ_2)
1643
        return tmr->ioctl (tmr_no, cmd, arg);
1644
 
1645
      if (get_user ((int *) arg) != 0)
1646
        return -(EINVAL);
1647
 
1648
      return snd_ioctl_return ((int *) arg, HZ);
1649
      break;
1650
 
1651
    case SNDCTL_SEQ_RESETSAMPLES:
1652
      {
1653
        int             err;
1654
 
1655
        dev = get_user ((int *) arg);
1656
        if (dev < 0 || dev >= num_synths)
1657
          {
1658
            return -(ENXIO);
1659
          }
1660
 
1661
        if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1662
          {
1663
            return -(EBUSY);
1664
          }
1665
 
1666
        if (!orig_dev && pmgr_present[dev])
1667
          pmgr_inform (dev, PM_E_PATCH_RESET, 0, 0, 0, 0);
1668
 
1669
        err = synth_devs[dev]->ioctl (dev, cmd, arg);
1670
        return err;
1671
      }
1672
      break;
1673
 
1674
    case SNDCTL_SEQ_NRSYNTHS:
1675
      return snd_ioctl_return ((int *) arg, max_synthdev);
1676
      break;
1677
 
1678
    case SNDCTL_SEQ_NRMIDIS:
1679
      return snd_ioctl_return ((int *) arg, max_mididev);
1680
      break;
1681
 
1682
    case SNDCTL_SYNTH_MEMAVL:
1683
      {
1684
        int             dev = get_user ((int *) arg);
1685
 
1686
        if (dev < 0 || dev >= num_synths)
1687
          return -(ENXIO);
1688
 
1689
        if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1690
          return -(EBUSY);
1691
 
1692
        return snd_ioctl_return ((int *) arg, synth_devs[dev]->ioctl (dev, cmd, arg));
1693
      }
1694
      break;
1695
 
1696
    case SNDCTL_FM_4OP_ENABLE:
1697
      {
1698
        int             dev = get_user ((int *) arg);
1699
 
1700
        if (dev < 0 || dev >= num_synths)
1701
          return -(ENXIO);
1702
 
1703
        if (!(synth_open_mask & (1 << dev)))
1704
          return -(ENXIO);
1705
 
1706
        synth_devs[dev]->ioctl (dev, cmd, arg);
1707
        return 0;
1708
      }
1709
      break;
1710
 
1711
    case SNDCTL_SYNTH_INFO:
1712
      {
1713
        struct synth_info inf;
1714
        int             dev;
1715
 
1716
        memcpy_fromfs ((char *) &inf, &((char *) arg)[0], sizeof (inf));
1717
        dev = inf.device;
1718
 
1719
        if (dev < 0 || dev >= max_synthdev)
1720
          return -(ENXIO);
1721
 
1722
        if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1723
          return -(EBUSY);
1724
 
1725
        return synth_devs[dev]->ioctl (dev, cmd, arg);
1726
      }
1727
      break;
1728
 
1729
    case SNDCTL_SEQ_OUTOFBAND:
1730
      {
1731
        struct seq_event_rec event_rec;
1732
        unsigned long   flags;
1733
 
1734
        memcpy_fromfs ((char *) &event_rec, &((char *) arg)[0], sizeof (event_rec));
1735
 
1736
        save_flags (flags);
1737
        cli ();
1738
        play_event (event_rec.arr);
1739
        restore_flags (flags);
1740
 
1741
        return 0;
1742
      }
1743
      break;
1744
 
1745
    case SNDCTL_MIDI_INFO:
1746
      {
1747
        struct midi_info inf;
1748
        int             dev;
1749
 
1750
        memcpy_fromfs ((char *) &inf, &((char *) arg)[0], sizeof (inf));
1751
        dev = inf.device;
1752
 
1753
        if (dev < 0 || dev >= max_mididev)
1754
          return -(ENXIO);
1755
 
1756
        memcpy_tofs (&((char *) arg)[0], (char *) &(midi_devs[dev]->info), sizeof (inf));
1757
        return 0;
1758
      }
1759
      break;
1760
 
1761
    case SNDCTL_PMGR_IFACE:
1762
      {
1763
        struct patmgr_info *inf;
1764
        int             dev, err;
1765
 
1766
        if ((inf = (struct patmgr_info *) vmalloc (sizeof (*inf))) == NULL)
1767
          {
1768
            printk ("patmgr: Can't allocate memory for a message\n");
1769
            return -(EIO);
1770
          }
1771
 
1772
        memcpy_fromfs ((char *) inf, &((char *) arg)[0], sizeof (*inf));
1773
        dev = inf->device;
1774
 
1775
        if (dev < 0 || dev >= num_synths)
1776
          {
1777
            vfree (inf);
1778
            return -(ENXIO);
1779
          }
1780
 
1781
        if (!synth_devs[dev]->pmgr_interface)
1782
          {
1783
            vfree (inf);
1784
            return -(ENXIO);
1785
          }
1786
 
1787
        if ((err = synth_devs[dev]->pmgr_interface (dev, inf)) == -1)
1788
          {
1789
            vfree (inf);
1790
            return err;
1791
          }
1792
 
1793
        memcpy_tofs (&((char *) arg)[0], (char *) inf, sizeof (*inf));
1794
        vfree (inf);
1795
        return 0;
1796
      }
1797
      break;
1798
 
1799
    case SNDCTL_PMGR_ACCESS:
1800
      {
1801
        struct patmgr_info *inf;
1802
        int             dev, err;
1803
 
1804
        if ((inf = (struct patmgr_info *) vmalloc (sizeof (*inf))) == NULL)
1805
          {
1806
            printk ("patmgr: Can't allocate memory for a message\n");
1807
            return -(EIO);
1808
          }
1809
 
1810
        memcpy_fromfs ((char *) inf, &((char *) arg)[0], sizeof (*inf));
1811
        dev = inf->device;
1812
 
1813
        if (dev < 0 || dev >= num_synths)
1814
          {
1815
            vfree (inf);
1816
            return -(ENXIO);
1817
          }
1818
 
1819
        if (!pmgr_present[dev])
1820
          {
1821
            vfree (inf);
1822
            return -(ESRCH);
1823
          }
1824
 
1825
        if ((err = pmgr_access (dev, inf)) < 0)
1826
          {
1827
            vfree (inf);
1828
            return err;
1829
          }
1830
 
1831
        memcpy_tofs (&((char *) arg)[0], (char *) inf, sizeof (*inf));
1832
        vfree (inf);
1833
        return 0;
1834
      }
1835
      break;
1836
 
1837
    case SNDCTL_SEQ_THRESHOLD:
1838
      {
1839
        int             tmp = get_user ((int *) arg);
1840
 
1841
        if (dev)                /*
1842
                                 * Patch manager
1843
                                 */
1844
          return -(EIO);
1845
 
1846
        if (tmp < 1)
1847
          tmp = 1;
1848
        if (tmp >= SEQ_MAX_QUEUE)
1849
          tmp = SEQ_MAX_QUEUE - 1;
1850
        output_threshold = tmp;
1851
        return 0;
1852
      }
1853
      break;
1854
 
1855
    case SNDCTL_MIDI_PRETIME:
1856
      {
1857
        int             val = get_user ((int *) arg);
1858
 
1859
        if (val < 0)
1860
          val = 0;
1861
 
1862
        val = (HZ * val) / 10;
1863
        pre_event_timeout = val;
1864
        return snd_ioctl_return ((int *) arg, val);
1865
      }
1866
      break;
1867
 
1868
    default:
1869
      if (dev)                  /*
1870
                                 * Patch manager
1871
                                 */
1872
        return -(EIO);
1873
 
1874
      if (mode == OPEN_READ)
1875
        return -(EIO);
1876
 
1877
      if (!synth_devs[0])
1878
        return -(ENXIO);
1879
      if (!(synth_open_mask & (1 << 0)))
1880
        return -(ENXIO);
1881
      return synth_devs[0]->ioctl (0, cmd, arg);
1882
      break;
1883
    }
1884
 
1885
  return -(EINVAL);
1886
}
1887
 
1888
int
1889
sequencer_select (int dev, struct fileinfo *file, int sel_type, select_table_handle * wait)
1890
{
1891
  unsigned long   flags;
1892
 
1893
  dev = dev >> 4;
1894
 
1895
  switch (sel_type)
1896
    {
1897
    case SEL_IN:
1898
      save_flags (flags);
1899
      cli ();
1900
      if (!iqlen)
1901
        {
1902
 
1903
          midi_sleep_flag.flags = WK_SLEEP;
1904
          module_select_wait (&midi_sleeper, wait);
1905
          restore_flags (flags);
1906
          return 0;
1907
        }
1908
      restore_flags (flags);
1909
      return 1;
1910
      break;
1911
 
1912
    case SEL_OUT:
1913
      save_flags (flags);
1914
      cli ();
1915
      if ((SEQ_MAX_QUEUE - qlen) < output_threshold)
1916
        {
1917
 
1918
          seq_sleep_flag.flags = WK_SLEEP;
1919
          module_select_wait (&seq_sleeper, wait);
1920
          restore_flags (flags);
1921
          return 0;
1922
        }
1923
      restore_flags (flags);
1924
      return 1;
1925
      break;
1926
 
1927
    case SEL_EX:
1928
      return 0;
1929
    }
1930
 
1931
  return 0;
1932
}
1933
 
1934
 
1935
void
1936
sequencer_timer (unsigned long dummy)
1937
{
1938
  seq_startplay ();
1939
}
1940
 
1941
int
1942
note_to_freq (int note_num)
1943
{
1944
 
1945
  /*
1946
   * This routine converts a midi note to a frequency (multiplied by 1000)
1947
   */
1948
 
1949
  int             note, octave, note_freq;
1950
  int             notes[] =
1951
  {
1952
    261632, 277189, 293671, 311132, 329632, 349232,
1953
    369998, 391998, 415306, 440000, 466162, 493880
1954
  };
1955
 
1956
#define BASE_OCTAVE     5
1957
 
1958
  octave = note_num / 12;
1959
  note = note_num % 12;
1960
 
1961
  note_freq = notes[note];
1962
 
1963
  if (octave < BASE_OCTAVE)
1964
    note_freq >>= (BASE_OCTAVE - octave);
1965
  else if (octave > BASE_OCTAVE)
1966
    note_freq <<= (octave - BASE_OCTAVE);
1967
 
1968
  /*
1969
   * note_freq >>= 1;
1970
   */
1971
 
1972
  return note_freq;
1973
}
1974
 
1975
unsigned long
1976
compute_finetune (unsigned long base_freq, int bend, int range)
1977
{
1978
  unsigned long   amount;
1979
  int             negative, semitones, cents, multiplier = 1;
1980
 
1981
  if (!bend)
1982
    return base_freq;
1983
  if (!range)
1984
    return base_freq;
1985
 
1986
  if (!base_freq)
1987
    return base_freq;
1988
 
1989
  if (range >= 8192)
1990
    range = 8192;
1991
 
1992
  bend = bend * range / 8192;
1993
  if (!bend)
1994
    return base_freq;
1995
 
1996
  negative = bend < 0 ? 1 : 0;
1997
 
1998
  if (bend < 0)
1999
    bend *= -1;
2000
  if (bend > range)
2001
    bend = range;
2002
 
2003
  /*
2004
     if (bend > 2399)
2005
     bend = 2399;
2006
   */
2007
  while (bend > 2399)
2008
    {
2009
      multiplier *= 4;
2010
      bend -= 2400;
2011
    }
2012
 
2013
  semitones = bend / 100;
2014
  cents = bend % 100;
2015
 
2016
  amount = (int) (semitone_tuning[semitones] * multiplier * cent_tuning[cents])
2017
    / 10000;
2018
 
2019
  if (negative)
2020
    return (base_freq * 10000) / amount;        /* Bend down */
2021
  else
2022
    return (base_freq * amount) / 10000;        /* Bend up */
2023
}
2024
 
2025
 
2026
void
2027
sequencer_init (void)
2028
{
2029
 
2030
 
2031
  queue = (unsigned char *) (sound_mem_blocks[sound_nblocks] = vmalloc (SEQ_MAX_QUEUE * EV_SZ));
2032
  if (sound_nblocks < 1024)
2033
    sound_nblocks++;;
2034
  if (queue == NULL)
2035
    {
2036
      printk ("Sound: Can't allocate memory for sequencer output queue\n");
2037
      return;
2038
    }
2039
 
2040
 
2041
  iqueue = (unsigned char *) (sound_mem_blocks[sound_nblocks] = vmalloc (SEQ_MAX_QUEUE * IEV_SZ));
2042
  if (sound_nblocks < 1024)
2043
    sound_nblocks++;;
2044
  if (iqueue == NULL)
2045
    {
2046
      printk ("Sound: Can't allocate memory for sequencer input queue\n");
2047
      return;
2048
    }
2049
 
2050
  sequencer_ok = 1;
2051
}
2052
 
2053
#endif

powered by: WebSVN 2.1.0

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