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/] [mpu401.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/mpu401.c
3
 *
4
 * The low level driver for Roland MPU-401 compatible Midi cards.
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 USE_SEQ_MACROS
17
#define USE_SIMPLE_MACROS
18
 
19
#include "sound_config.h"
20
 
21
#if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI)
22
#include "coproc.h"
23
 
24
static int      init_sequence[20];      /* NOTE! pos 0 = len, start pos 1. */
25
 
26
#ifdef CONFIG_SEQUENCER
27
static int      timer_mode = TMR_INTERNAL, timer_caps = TMR_INTERNAL;
28
 
29
#endif
30
 
31
struct mpu_config
32
  {
33
    int             base;       /*
34
                                 * I/O base
35
                                 */
36
    int             irq;
37
    int             opened;     /*
38
                                 * Open mode
39
                                 */
40
    int             devno;
41
    int             synthno;
42
    int             uart_mode;
43
    int             initialized;
44
    int             mode;
45
#define MODE_MIDI       1
46
#define MODE_SYNTH      2
47
    unsigned char   version, revision;
48
    unsigned int    capabilities;
49
#define MPU_CAP_INTLG   0x10000000
50
#define MPU_CAP_SYNC    0x00000010
51
#define MPU_CAP_FSK     0x00000020
52
#define MPU_CAP_CLS     0x00000040
53
#define MPU_CAP_SMPTE   0x00000080
54
#define MPU_CAP_2PORT   0x00000001
55
    int             timer_flag;
56
 
57
#define MBUF_MAX        10
58
#define BUFTEST(dc) if (dc->m_ptr >= MBUF_MAX || dc->m_ptr < 0) \
59
        {printk("MPU: Invalid buffer pointer %d/%d, s=%d\n", dc->m_ptr, dc->m_left, dc->m_state);dc->m_ptr--;}
60
    int             m_busy;
61
    unsigned char   m_buf[MBUF_MAX];
62
    int             m_ptr;
63
    int             m_state;
64
    int             m_left;
65
    unsigned char   last_status;
66
    void            (*inputintr) (int dev, unsigned char data);
67
    int             shared_irq;
68
    int            *osp;
69
  };
70
 
71
#define DATAPORT(base)   (base)
72
#define COMDPORT(base)   (base+1)
73
#define STATPORT(base)   (base+1)
74
 
75
static int
76
mpu401_status (struct mpu_config *devc)
77
{
78
  return inb (STATPORT (devc->base));
79
}
80
#define input_avail(devc)               (!(mpu401_status(devc)&INPUT_AVAIL))
81
#define output_ready(devc)              (!(mpu401_status(devc)&OUTPUT_READY))
82
static void
83
write_command (struct mpu_config *devc, unsigned char cmd)
84
{
85
  outb (cmd, COMDPORT (devc->base));
86
}
87
static int
88
read_data (struct mpu_config *devc)
89
{
90
  return inb (DATAPORT (devc->base));
91
}
92
 
93
static void
94
write_data (struct mpu_config *devc, unsigned char byte)
95
{
96
  outb (byte, DATAPORT (devc->base));
97
}
98
 
99
#define OUTPUT_READY    0x40
100
#define INPUT_AVAIL     0x80
101
#define MPU_ACK         0xFE
102
#define MPU_RESET       0xFF
103
#define UART_MODE_ON    0x3F
104
 
105
static struct mpu_config dev_conf[MAX_MIDI_DEV] =
106
{
107
  {0}};
108
 
109
static int      n_mpu_devs = 0;
110
static volatile int irq2dev[17] =
111
{-1, -1, -1, -1, -1, -1, -1, -1,
112
 -1, -1, -1, -1, -1, -1, -1, -1, -1};
113
 
114
static int      reset_mpu401 (struct mpu_config *devc);
115
static void     set_uart_mode (int dev, struct mpu_config *devc, int arg);
116
 
117
static void     mpu_timer_init (int midi_dev);
118
static void     mpu_timer_interrupt (void);
119
static void     timer_ext_event (struct mpu_config *devc, int event, int parm);
120
 
121
static struct synth_info mpu_synth_info_proto =
122
{"MPU-401 MIDI interface", 0, SYNTH_TYPE_MIDI, MIDI_TYPE_MPU401, 0, 128, 0, 128, SYNTH_CAP_INPUT};
123
 
124
static struct synth_info mpu_synth_info[MAX_MIDI_DEV];
125
 
126
/*
127
 * States for the input scanner
128
 */
129
 
130
#define ST_INIT                 0        /* Ready for timing byte or msg */
131
#define ST_TIMED                1       /* Leading timing byte rcvd */
132
#define ST_DATABYTE             2       /* Waiting for (nr_left) data bytes */
133
 
134
#define ST_SYSMSG               100     /* System message (sysx etc). */
135
#define ST_SYSEX                101     /* System exclusive msg */
136
#define ST_MTC                  102     /* Midi Time Code (MTC) qframe msg */
137
#define ST_SONGSEL              103     /* Song select */
138
#define ST_SONGPOS              104     /* Song position pointer */
139
 
140
static unsigned char len_tab[] =        /* # of data bytes following a status
141
                                         */
142
{
143
  2,                            /* 8x */
144
  2,                            /* 9x */
145
  2,                            /* Ax */
146
  2,                            /* Bx */
147
  1,                            /* Cx */
148
  1,                            /* Dx */
149
  2,                            /* Ex */
150
 
151
};
152
 
153
#ifndef CONFIG_SEQUENCER
154
#define STORE(cmd)
155
#else
156
#define STORE(cmd) \
157
{ \
158
  int len; \
159
  unsigned char obuf[8]; \
160
  cmd; \
161
  seq_input_event(obuf, len); \
162
}
163
#endif
164
 
165
#define _seqbuf obuf
166
#define _seqbufptr 0
167
#define _SEQ_ADVBUF(x) len=x
168
 
169
static int
170
mpu_input_scanner (struct mpu_config *devc, unsigned char midic)
171
{
172
 
173
  switch (devc->m_state)
174
    {
175
    case ST_INIT:
176
      switch (midic)
177
        {
178
        case 0xf8:
179
          /* Timer overflow */
180
          break;
181
 
182
        case 0xfc:
183
          printk ("<all end>");
184
          break;
185
 
186
        case 0xfd:
187
          if (devc->timer_flag)
188
            mpu_timer_interrupt ();
189
          break;
190
 
191
        case 0xfe:
192
          return MPU_ACK;
193
          break;
194
 
195
        case 0xf0:
196
        case 0xf1:
197
        case 0xf2:
198
        case 0xf3:
199
        case 0xf4:
200
        case 0xf5:
201
        case 0xf6:
202
        case 0xf7:
203
          printk ("<Trk data rq #%d>", midic & 0x0f);
204
          break;
205
 
206
        case 0xf9:
207
          printk ("<conductor rq>");
208
          break;
209
 
210
        case 0xff:
211
          devc->m_state = ST_SYSMSG;
212
          break;
213
 
214
        default:
215
          if (midic <= 0xef)
216
            {
217
              /* printk("mpu time: %d ", midic); */
218
              devc->m_state = ST_TIMED;
219
            }
220
          else
221
            printk ("<MPU: Unknown event %02x> ", midic);
222
        }
223
      break;
224
 
225
    case ST_TIMED:
226
      {
227
        int             msg = ((int) (midic & 0xf0) >> 4);
228
 
229
        devc->m_state = ST_DATABYTE;
230
 
231
        if (msg < 8)            /* Data byte */
232
          {
233
            /* printk("midi msg (running status) "); */
234
            msg = ((int) (devc->last_status & 0xf0) >> 4);
235
            msg -= 8;
236
            devc->m_left = len_tab[msg] - 1;
237
 
238
            devc->m_ptr = 2;
239
            devc->m_buf[0] = devc->last_status;
240
            devc->m_buf[1] = midic;
241
 
242
            if (devc->m_left <= 0)
243
              {
244
                devc->m_state = ST_INIT;
245
                do_midi_msg (devc->synthno, devc->m_buf, devc->m_ptr);
246
                devc->m_ptr = 0;
247
              }
248
          }
249
        else if (msg == 0xf)    /* MPU MARK */
250
          {
251
            devc->m_state = ST_INIT;
252
 
253
            switch (midic)
254
              {
255
              case 0xf8:
256
                /* printk("NOP "); */
257
                break;
258
 
259
              case 0xf9:
260
                /* printk("meas end "); */
261
                break;
262
 
263
              case 0xfc:
264
                /* printk("data end "); */
265
                break;
266
 
267
              default:
268
                printk ("Unknown MPU mark %02x\n", midic);
269
              }
270
          }
271
        else
272
          {
273
            devc->last_status = midic;
274
            /* printk ("midi msg "); */
275
            msg -= 8;
276
            devc->m_left = len_tab[msg];
277
 
278
            devc->m_ptr = 1;
279
            devc->m_buf[0] = midic;
280
 
281
            if (devc->m_left <= 0)
282
              {
283
                devc->m_state = ST_INIT;
284
                do_midi_msg (devc->synthno, devc->m_buf, devc->m_ptr);
285
                devc->m_ptr = 0;
286
              }
287
          }
288
      }
289
      break;
290
 
291
    case ST_SYSMSG:
292
      switch (midic)
293
        {
294
        case 0xf0:
295
          printk ("<SYX>");
296
          devc->m_state = ST_SYSEX;
297
          break;
298
 
299
        case 0xf1:
300
          devc->m_state = ST_MTC;
301
          break;
302
 
303
        case 0xf2:
304
          devc->m_state = ST_SONGPOS;
305
          devc->m_ptr = 0;
306
          break;
307
 
308
        case 0xf3:
309
          devc->m_state = ST_SONGSEL;
310
          break;
311
 
312
        case 0xf6:
313
          /* printk("tune_request\n"); */
314
          devc->m_state = ST_INIT;
315
 
316
          /*
317
             *    Real time messages
318
           */
319
        case 0xf8:
320
          /* midi clock */
321
          devc->m_state = ST_INIT;
322
          timer_ext_event (devc, TMR_CLOCK, 0);
323
          break;
324
 
325
        case 0xfA:
326
          devc->m_state = ST_INIT;
327
          timer_ext_event (devc, TMR_START, 0);
328
          break;
329
 
330
        case 0xFB:
331
          devc->m_state = ST_INIT;
332
          timer_ext_event (devc, TMR_CONTINUE, 0);
333
          break;
334
 
335
        case 0xFC:
336
          devc->m_state = ST_INIT;
337
          timer_ext_event (devc, TMR_STOP, 0);
338
          break;
339
 
340
        case 0xFE:
341
          /* active sensing */
342
          devc->m_state = ST_INIT;
343
          break;
344
 
345
        case 0xff:
346
          /* printk("midi hard reset"); */
347
          devc->m_state = ST_INIT;
348
          break;
349
 
350
        default:
351
          printk ("unknown MIDI sysmsg %0x\n", midic);
352
          devc->m_state = ST_INIT;
353
        }
354
      break;
355
 
356
    case ST_MTC:
357
      devc->m_state = ST_INIT;
358
      printk ("MTC frame %x02\n", midic);
359
      break;
360
 
361
    case ST_SYSEX:
362
      if (midic == 0xf7)
363
        {
364
          printk ("<EOX>");
365
          devc->m_state = ST_INIT;
366
        }
367
      else
368
        printk ("%02x ", midic);
369
      break;
370
 
371
    case ST_SONGPOS:
372
      BUFTEST (devc);
373
      devc->m_buf[devc->m_ptr++] = midic;
374
      if (devc->m_ptr == 2)
375
        {
376
          devc->m_state = ST_INIT;
377
          devc->m_ptr = 0;
378
          timer_ext_event (devc, TMR_SPP,
379
                           ((devc->m_buf[1] & 0x7f) << 7) |
380
                           (devc->m_buf[0] & 0x7f));
381
        }
382
      break;
383
 
384
    case ST_DATABYTE:
385
      BUFTEST (devc);
386
      devc->m_buf[devc->m_ptr++] = midic;
387
      if ((--devc->m_left) <= 0)
388
        {
389
          devc->m_state = ST_INIT;
390
          do_midi_msg (devc->synthno, devc->m_buf, devc->m_ptr);
391
          devc->m_ptr = 0;
392
        }
393
      break;
394
 
395
    default:
396
      printk ("Bad state %d ", devc->m_state);
397
      devc->m_state = ST_INIT;
398
    }
399
 
400
  return 1;
401
}
402
 
403
static void
404
mpu401_input_loop (struct mpu_config *devc)
405
{
406
  unsigned long   flags;
407
  int             busy;
408
  int             n;
409
 
410
  save_flags (flags);
411
  cli ();
412
  busy = devc->m_busy;
413
  devc->m_busy = 1;
414
  restore_flags (flags);
415
 
416
  if (busy)                     /* Already inside the scanner */
417
    return;
418
 
419
  n = 50;
420
 
421
  while (input_avail (devc) && n-- > 0)
422
    {
423
      unsigned char   c = read_data (devc);
424
 
425
      if (devc->mode == MODE_SYNTH)
426
        {
427
          mpu_input_scanner (devc, c);
428
        }
429
      else if (devc->opened & OPEN_READ && devc->inputintr != NULL)
430
        devc->inputintr (devc->devno, c);
431
    }
432
 
433
  devc->m_busy = 0;
434
}
435
 
436
void
437
mpuintr (int irq, void *dev_id, struct pt_regs *dummy)
438
{
439
  struct mpu_config *devc;
440
  int             dev;
441
 
442
  sti ();
443
 
444
/*
445
 * FreeBSD (and some others) pass unit number to the interrupt handler.
446
 * In this case we have to scan the table for first handler.
447
 */
448
 
449
  if (irq < 1 || irq > 15)
450
    {
451
      dev = -1;
452
    }
453
  else
454
    dev = irq2dev[irq];
455
 
456
  if (dev == -1)
457
    {
458
      int             origirq = irq;
459
 
460
      for (irq = 0; irq <= 16; irq++)
461
        if (irq2dev[irq] != -1)
462
          break;
463
      if (irq > 15)
464
        {
465
          printk ("MPU-401: Bogus interrupt #%d?\n", origirq);
466
          return;
467
        }
468
      dev = irq2dev[irq];
469
      devc = &dev_conf[dev];
470
    }
471
  else
472
    devc = &dev_conf[dev];
473
 
474
  if (input_avail (devc))
475
    if (devc->base != 0 && (devc->opened & OPEN_READ || devc->mode == MODE_SYNTH))
476
      mpu401_input_loop (devc);
477
    else
478
      {
479
        /* Dummy read (just to acknowledge the interrupt) */
480
        read_data (devc);
481
      }
482
 
483
}
484
 
485
static int
486
mpu401_open (int dev, int mode,
487
             void            (*input) (int dev, unsigned char data),
488
             void            (*output) (int dev)
489
)
490
{
491
  int             err;
492
  struct mpu_config *devc;
493
 
494
  if (dev < 0 || dev >= num_midis)
495
    return -(ENXIO);
496
 
497
  devc = &dev_conf[dev];
498
 
499
  if (devc->opened)
500
    {
501
      printk ("MPU-401: Midi busy\n");
502
      return -(EBUSY);
503
    }
504
 
505
  /*
506
     *  Verify that the device is really running.
507
     *  Some devices (such as Ensoniq SoundScape don't
508
     *  work before the on board processor (OBP) is initialized
509
     *  by downloading its microcode.
510
   */
511
 
512
  if (!devc->initialized)
513
    {
514
      if (mpu401_status (devc) == 0xff)         /* Bus float */
515
        {
516
          printk ("MPU-401: Device not initialized properly\n");
517
          return -(EIO);
518
        }
519
      reset_mpu401 (devc);
520
    }
521
 
522
  irq2dev[devc->irq] = dev;
523
 
524
  if (midi_devs[dev]->coproc)
525
    if ((err = midi_devs[dev]->coproc->
526
         open (midi_devs[dev]->coproc->devc, COPR_MIDI)) < 0)
527
      {
528
        printk ("MPU-401: Can't access coprocessor device\n");
529
 
530
        return err;
531
      }
532
 
533
  set_uart_mode (dev, devc, 1);
534
  devc->mode = MODE_MIDI;
535
  devc->synthno = 0;
536
 
537
  mpu401_input_loop (devc);
538
 
539
  devc->inputintr = input;
540
  devc->opened = mode;
541
 
542
  return 0;
543
}
544
 
545
static void
546
mpu401_close (int dev)
547
{
548
  struct mpu_config *devc;
549
 
550
  devc = &dev_conf[dev];
551
 
552
  if (devc->uart_mode)
553
    reset_mpu401 (devc);        /*
554
                                 * This disables the UART mode
555
                                 */
556
  devc->mode = 0;
557
 
558
  devc->inputintr = NULL;
559
 
560
  if (midi_devs[dev]->coproc)
561
    midi_devs[dev]->coproc->close (midi_devs[dev]->coproc->devc, COPR_MIDI);
562
  devc->opened = 0;
563
}
564
 
565
static int
566
mpu401_out (int dev, unsigned char midi_byte)
567
{
568
  int             timeout;
569
  unsigned long   flags;
570
 
571
  struct mpu_config *devc;
572
 
573
  devc = &dev_conf[dev];
574
 
575
  /*
576
   * Sometimes it takes about 30000 loops before the output becomes ready
577
   * (After reset). Normally it takes just about 10 loops.
578
   */
579
 
580
  for (timeout = 30000; timeout > 0 && !output_ready (devc); timeout--);
581
 
582
  save_flags (flags);
583
  cli ();
584
  if (!output_ready (devc))
585
    {
586
      printk ("MPU-401: Send data timeout\n");
587
      restore_flags (flags);
588
      return 0;
589
    }
590
 
591
  write_data (devc, midi_byte);
592
  restore_flags (flags);
593
  return 1;
594
}
595
 
596
static int
597
mpu401_command (int dev, mpu_command_rec * cmd)
598
{
599
  int             i, timeout, ok;
600
  int             ret = 0;
601
  unsigned long   flags;
602
  struct mpu_config *devc;
603
 
604
  devc = &dev_conf[dev];
605
 
606
  if (devc->uart_mode)          /*
607
                                 * Not possible in UART mode
608
                                 */
609
    {
610
      printk ("MPU-401 commands not possible in the UART mode\n");
611
      return -(EINVAL);
612
    }
613
 
614
  /*
615
   * Test for input since pending input seems to block the output.
616
   */
617
  if (input_avail (devc))
618
    mpu401_input_loop (devc);
619
 
620
  /*
621
   * Sometimes it takes about 50000 loops before the output becomes ready
622
   * (After reset). Normally it takes just about 10 loops.
623
   */
624
 
625
  timeout = 50000;
626
retry:
627
  if (timeout-- <= 0)
628
    {
629
      printk ("MPU-401: Command (0x%x) timeout\n", (int) cmd->cmd);
630
      return -(EIO);
631
    }
632
 
633
  save_flags (flags);
634
  cli ();
635
 
636
  if (!output_ready (devc))
637
    {
638
      restore_flags (flags);
639
      goto retry;
640
    }
641
 
642
  write_command (devc, cmd->cmd);
643
 
644
  ok = 0;
645
  for (timeout = 50000; timeout > 0 && !ok; timeout--)
646
    if (input_avail (devc))
647
      {
648
        if (devc->opened && devc->mode == MODE_SYNTH)
649
          {
650
            if (mpu_input_scanner (devc, read_data (devc)) == MPU_ACK)
651
              ok = 1;
652
          }
653
        else
654
          {                     /* Device is not currently open. Use simpler method */
655
            if (read_data (devc) == MPU_ACK)
656
              ok = 1;
657
          }
658
      }
659
 
660
  if (!ok)
661
    {
662
      restore_flags (flags);
663
      /*       printk ("MPU: No ACK to command (0x%x)\n", (int) cmd->cmd); */
664
      return -(EIO);
665
    }
666
 
667
  if (cmd->nr_args)
668
    for (i = 0; i < cmd->nr_args; i++)
669
      {
670
        for (timeout = 3000; timeout > 0 && !output_ready (devc); timeout--);
671
 
672
        if (!mpu401_out (dev, cmd->data[i]))
673
          {
674
            restore_flags (flags);
675
            printk ("MPU: Command (0x%x), parm send failed.\n", (int) cmd->cmd);
676
            return -(EIO);
677
          }
678
      }
679
 
680
  ret = 0;
681
  cmd->data[0] = 0;
682
 
683
  if (cmd->nr_returns)
684
    for (i = 0; i < cmd->nr_returns; i++)
685
      {
686
        ok = 0;
687
        for (timeout = 5000; timeout > 0 && !ok; timeout--)
688
          if (input_avail (devc))
689
            {
690
              cmd->data[i] = read_data (devc);
691
              ok = 1;
692
            }
693
 
694
        if (!ok)
695
          {
696
            restore_flags (flags);
697
            /* printk ("MPU: No response(%d) to command (0x%x)\n", i, (int) cmd->cmd);  */
698
            return -(EIO);
699
          }
700
      }
701
 
702
  restore_flags (flags);
703
 
704
  return ret;
705
}
706
 
707
static int
708
mpu_cmd (int dev, int cmd, int data)
709
{
710
  int             ret;
711
 
712
  static mpu_command_rec rec;
713
 
714
  rec.cmd = cmd & 0xff;
715
  rec.nr_args = ((cmd & 0xf0) == 0xE0);
716
  rec.nr_returns = ((cmd & 0xf0) == 0xA0);
717
  rec.data[0] = data & 0xff;
718
 
719
  if ((ret = mpu401_command (dev, &rec)) < 0)
720
    {
721
      return ret;
722
    }
723
  return (unsigned char) rec.data[0];
724
}
725
 
726
static int
727
mpu401_prefix_cmd (int dev, unsigned char status)
728
{
729
  struct mpu_config *devc = &dev_conf[dev];
730
 
731
  if (devc->uart_mode)
732
    return 1;
733
 
734
  if (status < 0xf0)
735
    {
736
      if (mpu_cmd (dev, 0xD0, 0) < 0)
737
        {
738
          return 0;
739
        }
740
 
741
      return 1;
742
    }
743
 
744
  switch (status)
745
    {
746
    case 0xF0:
747
      if (mpu_cmd (dev, 0xDF, 0) < 0)
748
        {
749
          return 0;
750
        }
751
 
752
      return 1;
753
      break;
754
 
755
    default:
756
      return 0;
757
    }
758
 
759
}
760
 
761
static int
762
mpu401_start_read (int dev)
763
{
764
  return 0;
765
}
766
 
767
static int
768
mpu401_end_read (int dev)
769
{
770
  return 0;
771
}
772
 
773
static int
774
mpu401_ioctl (int dev, unsigned cmd, caddr_t arg)
775
{
776
  struct mpu_config *devc;
777
 
778
  devc = &dev_conf[dev];
779
 
780
  switch (cmd)
781
    {
782
    case 1:
783
      memcpy_fromfs ((char *) init_sequence, &((char *) arg)[0], sizeof (init_sequence));
784
      return 0;
785
      break;
786
 
787
    case SNDCTL_MIDI_MPUMODE:
788
      if (!(devc->capabilities & MPU_CAP_INTLG))        /* No intelligent mode */
789
        {
790
          printk ("MPU-401: Intelligent mode not supported by the HW\n");
791
          return -(EINVAL);
792
        }
793
      set_uart_mode (dev, devc, !get_user ((int *) arg));
794
      return 0;
795
      break;
796
 
797
    case SNDCTL_MIDI_MPUCMD:
798
      {
799
        int             ret;
800
        mpu_command_rec rec;
801
 
802
        memcpy_fromfs ((char *) &rec, &((char *) arg)[0], sizeof (rec));
803
 
804
        if ((ret = mpu401_command (dev, &rec)) < 0)
805
          return ret;
806
 
807
        memcpy_tofs (&((char *) arg)[0], (char *) &rec, sizeof (rec));
808
        return 0;
809
      }
810
      break;
811
 
812
    default:
813
      return -(EINVAL);
814
    }
815
}
816
 
817
static void
818
mpu401_kick (int dev)
819
{
820
}
821
 
822
static int
823
mpu401_buffer_status (int dev)
824
{
825
  return 0;                      /*
826
                                 * No data in buffers
827
                                 */
828
}
829
 
830
static int
831
mpu_synth_ioctl (int dev,
832
                 unsigned int cmd, caddr_t arg)
833
{
834
  int             midi_dev;
835
  struct mpu_config *devc;
836
 
837
  midi_dev = synth_devs[dev]->midi_dev;
838
 
839
  if (midi_dev < 0 || midi_dev > num_midis)
840
    return -(ENXIO);
841
 
842
  devc = &dev_conf[midi_dev];
843
 
844
  switch (cmd)
845
    {
846
 
847
    case SNDCTL_SYNTH_INFO:
848
      memcpy_tofs (&((char *) arg)[0], &mpu_synth_info[midi_dev], sizeof (struct synth_info));
849
 
850
      return 0;
851
      break;
852
 
853
    case SNDCTL_SYNTH_MEMAVL:
854
      return 0x7fffffff;
855
      break;
856
 
857
    default:
858
      return -(EINVAL);
859
    }
860
}
861
 
862
static int
863
mpu_synth_open (int dev, int mode)
864
{
865
  int             midi_dev, err;
866
  struct mpu_config *devc;
867
 
868
  midi_dev = synth_devs[dev]->midi_dev;
869
 
870
  if (midi_dev < 0 || midi_dev > num_midis)
871
    {
872
      return -(ENXIO);
873
    }
874
 
875
  devc = &dev_conf[midi_dev];
876
 
877
  /*
878
     *  Verify that the device is really running.
879
     *  Some devices (such as Ensoniq SoundScape don't
880
     *  work before the on board processor (OBP) is initialized
881
     *  by downloading its microcode.
882
   */
883
 
884
  if (!devc->initialized)
885
    {
886
      if (mpu401_status (devc) == 0xff)         /* Bus float */
887
        {
888
          printk ("MPU-401: Device not initialized properly\n");
889
          return -(EIO);
890
        }
891
      reset_mpu401 (devc);
892
    }
893
 
894
  if (devc->opened)
895
    {
896
      printk ("MPU-401: Midi busy\n");
897
      return -(EBUSY);
898
    }
899
 
900
  devc->mode = MODE_SYNTH;
901
  devc->synthno = dev;
902
 
903
  devc->inputintr = NULL;
904
  irq2dev[devc->irq] = midi_dev;
905
 
906
  if (midi_devs[midi_dev]->coproc)
907
    if ((err = midi_devs[midi_dev]->coproc->
908
         open (midi_devs[midi_dev]->coproc->devc, COPR_MIDI)) < 0)
909
      {
910
        printk ("MPU-401: Can't access coprocessor device\n");
911
 
912
        return err;
913
      }
914
 
915
  devc->opened = mode;
916
  reset_mpu401 (devc);
917
 
918
  if (mode & OPEN_READ)
919
    {
920
      mpu_cmd (midi_dev, 0x8B, 0);       /* Enable data in stop mode */
921
      mpu_cmd (midi_dev, 0x34, 0);       /* Return timing bytes in stop mode */
922
      mpu_cmd (midi_dev, 0x87, 0);       /* Enable pitch & controller */
923
    }
924
 
925
  return 0;
926
}
927
 
928
static void
929
mpu_synth_close (int dev)
930
{
931
  int             midi_dev;
932
  struct mpu_config *devc;
933
 
934
  midi_dev = synth_devs[dev]->midi_dev;
935
 
936
  devc = &dev_conf[midi_dev];
937
  mpu_cmd (midi_dev, 0x15, 0);   /* Stop recording, playback and MIDI */
938
  mpu_cmd (midi_dev, 0x8a, 0);   /* Disable data in stopped mode */
939
 
940
  devc->inputintr = NULL;
941
 
942
  if (midi_devs[midi_dev]->coproc)
943
    midi_devs[midi_dev]->coproc->close (midi_devs[midi_dev]->coproc->devc, COPR_MIDI);
944
  devc->opened = 0;
945
  devc->mode = 0;
946
}
947
 
948
#define MIDI_SYNTH_NAME "MPU-401 UART Midi"
949
#define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
950
#include "midi_synth.h"
951
 
952
static struct synth_operations mpu401_synth_proto =
953
{
954
  NULL,
955
  0,
956
  SYNTH_TYPE_MIDI,
957
  0,
958
  mpu_synth_open,
959
  mpu_synth_close,
960
  mpu_synth_ioctl,
961
  midi_synth_kill_note,
962
  midi_synth_start_note,
963
  midi_synth_set_instr,
964
  midi_synth_reset,
965
  midi_synth_hw_control,
966
  midi_synth_load_patch,
967
  midi_synth_aftertouch,
968
  midi_synth_controller,
969
  midi_synth_panning,
970
  NULL,
971
  midi_synth_patchmgr,
972
  midi_synth_bender,
973
  NULL,                         /* alloc */
974
  midi_synth_setup_voice,
975
  midi_synth_send_sysex
976
};
977
 
978
static struct synth_operations *mpu401_synth_operations[MAX_MIDI_DEV];
979
 
980
static struct midi_operations mpu401_midi_proto =
981
{
982
  {"MPU-401 Midi", 0, MIDI_CAP_MPU401, SNDCARD_MPU401},
983
  NULL,
984
  {0},
985
  mpu401_open,
986
  mpu401_close,
987
  mpu401_ioctl,
988
  mpu401_out,
989
  mpu401_start_read,
990
  mpu401_end_read,
991
  mpu401_kick,
992
  NULL,
993
  mpu401_buffer_status,
994
  mpu401_prefix_cmd
995
};
996
 
997
static struct midi_operations mpu401_midi_operations[MAX_MIDI_DEV];
998
 
999
static void
1000
mpu401_chk_version (struct mpu_config *devc)
1001
{
1002
  int             tmp;
1003
  unsigned long   flags;
1004
 
1005
  devc->version = devc->revision = 0;
1006
 
1007
  save_flags (flags);
1008
  cli ();
1009
  if ((tmp = mpu_cmd (num_midis, 0xAC, 0)) < 0)
1010
    {
1011
      restore_flags (flags);
1012
      return;
1013
    }
1014
 
1015
  if ((tmp & 0xf0) > 0x20)      /* Why it's larger than 2.x ??? */
1016
    {
1017
      restore_flags (flags);
1018
      return;
1019
    }
1020
 
1021
  devc->version = tmp;
1022
 
1023
  if ((tmp = mpu_cmd (num_midis, 0xAD, 0)) < 0)
1024
    {
1025
      devc->version = 0;
1026
      restore_flags (flags);
1027
      return;
1028
    }
1029
  devc->revision = tmp;
1030
 
1031
  restore_flags (flags);
1032
}
1033
 
1034
void
1035
attach_mpu401 (struct address_info *hw_config)
1036
{
1037
  unsigned long   flags;
1038
  char            revision_char;
1039
 
1040
  struct mpu_config *devc;
1041
 
1042
  if (num_midis >= MAX_MIDI_DEV)
1043
    {
1044
      printk ("MPU-401: Too many midi devices detected\n");
1045
      return;
1046
    }
1047
 
1048
  devc = &dev_conf[num_midis];
1049
 
1050
  devc->base = hw_config->io_base;
1051
  devc->osp = hw_config->osp;
1052
  devc->irq = hw_config->irq;
1053
  devc->opened = 0;
1054
  devc->uart_mode = 0;
1055
  devc->initialized = 0;
1056
  devc->version = 0;
1057
  devc->revision = 0;
1058
  devc->capabilities = 0;
1059
  devc->timer_flag = 0;
1060
  devc->m_busy = 0;
1061
  devc->m_state = ST_INIT;
1062
  devc->shared_irq = hw_config->always_detect;
1063
  devc->irq = hw_config->irq;
1064
 
1065
  if (devc->irq < 0)
1066
    {
1067
      devc->irq *= -1;
1068
      devc->shared_irq = 1;
1069
    }
1070
  irq2dev[devc->irq] = num_midis;
1071
 
1072
  if (!hw_config->always_detect)
1073
    {
1074
      /* Verify the hardware again */
1075
      if (!reset_mpu401 (devc))
1076
        {
1077
          printk ("MPU401: Device didn't respond\n");
1078
          return;
1079
        }
1080
 
1081
      if (!devc->shared_irq)
1082
        if (snd_set_irq_handler (devc->irq, mpuintr, "mpu401", devc->osp) < 0)
1083
          {
1084
            printk ("MPU401: Failed to allocate IRQ%d\n", devc->irq);
1085
            return;
1086
          }
1087
 
1088
      save_flags (flags);
1089
      cli ();
1090
      mpu401_chk_version (devc);
1091
      if (devc->version == 0)
1092
        mpu401_chk_version (devc);
1093
      restore_flags (flags);
1094
    }
1095
 
1096
  request_region (hw_config->io_base, 2, "mpu401");
1097
 
1098
  if (devc->version != 0)
1099
    if (mpu_cmd (num_midis, 0xC5, 0) >= 0)        /* Set timebase OK */
1100
      if (mpu_cmd (num_midis, 0xE0, 120) >= 0)   /* Set tempo OK */
1101
        devc->capabilities |= MPU_CAP_INTLG;    /* Supports intelligent mode */
1102
 
1103
 
1104
  mpu401_synth_operations[num_midis] = (struct synth_operations *) (sound_mem_blocks[sound_nblocks] = vmalloc (sizeof (struct synth_operations)));
1105
 
1106
  if (sound_nblocks < 1024)
1107
    sound_nblocks++;;
1108
 
1109
  if (mpu401_synth_operations[num_midis] == NULL)
1110
    {
1111
      printk ("mpu401: Can't allocate memory\n");
1112
      return;
1113
    }
1114
 
1115
  if (!(devc->capabilities & MPU_CAP_INTLG))    /* No intelligent mode */
1116
    {
1117
      memcpy ((char *) mpu401_synth_operations[num_midis],
1118
              (char *) &std_midi_synth,
1119
              sizeof (struct synth_operations));
1120
    }
1121
  else
1122
    {
1123
      memcpy ((char *) mpu401_synth_operations[num_midis],
1124
              (char *) &mpu401_synth_proto,
1125
              sizeof (struct synth_operations));
1126
    }
1127
 
1128
  memcpy ((char *) &mpu401_midi_operations[num_midis],
1129
          (char *) &mpu401_midi_proto,
1130
          sizeof (struct midi_operations));
1131
 
1132
  mpu401_midi_operations[num_midis].converter =
1133
    mpu401_synth_operations[num_midis];
1134
 
1135
  memcpy ((char *) &mpu_synth_info[num_midis],
1136
          (char *) &mpu_synth_info_proto,
1137
          sizeof (struct synth_info));
1138
 
1139
  n_mpu_devs++;
1140
 
1141
  if (devc->version == 0x20 && devc->revision >= 0x07)  /* MusicQuest interface */
1142
    {
1143
      int             ports = (devc->revision & 0x08) ? 32 : 16;
1144
 
1145
      devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_SMPTE |
1146
        MPU_CAP_CLS | MPU_CAP_2PORT;
1147
 
1148
      revision_char = (devc->revision == 0x7f) ? 'M' : ' ';
1149
      sprintf (mpu_synth_info[num_midis].name,
1150
               "MQX-%d%c MIDI Interface #%d",
1151
               ports,
1152
               revision_char,
1153
               n_mpu_devs);
1154
    }
1155
  else
1156
    {
1157
 
1158
      revision_char = devc->revision ? devc->revision + '@' : ' ';
1159
      if ((int) devc->revision > ('Z' - '@'))
1160
        revision_char = '+';
1161
 
1162
      devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_FSK;
1163
 
1164
      if (hw_config->name)
1165
        sprintf (mpu_synth_info[num_midis].name, "%s (MPU401)", hw_config->name);
1166
      else
1167
        sprintf (mpu_synth_info[num_midis].name,
1168
                 "MPU-401 %d.%d%c Midi interface #%d",
1169
                 (int) (devc->version & 0xf0) >> 4,
1170
                 devc->version & 0x0f,
1171
                 revision_char,
1172
                 n_mpu_devs);
1173
    }
1174
 
1175
  strcpy (mpu401_midi_operations[num_midis].info.name,
1176
          mpu_synth_info[num_midis].name);
1177
 
1178
  conf_printf (mpu_synth_info[num_midis].name, hw_config);
1179
 
1180
  mpu401_synth_operations[num_midis]->midi_dev = devc->devno = num_midis;
1181
  mpu401_synth_operations[devc->devno]->info =
1182
    &mpu_synth_info[devc->devno];
1183
 
1184
  if (devc->capabilities & MPU_CAP_INTLG)       /* Intelligent mode */
1185
    mpu_timer_init (num_midis);
1186
 
1187
  irq2dev[devc->irq] = num_midis;
1188
  midi_devs[num_midis++] = &mpu401_midi_operations[devc->devno];
1189
}
1190
 
1191
static int
1192
reset_mpu401 (struct mpu_config *devc)
1193
{
1194
  unsigned long   flags;
1195
  int             ok, timeout, n;
1196
  int             timeout_limit;
1197
 
1198
  /*
1199
   * Send the RESET command. Try again if no success at the first time.
1200
   * (If the device is in the UART mode, it will not ack the reset cmd).
1201
   */
1202
 
1203
  ok = 0;
1204
 
1205
  timeout_limit = devc->initialized ? 30000 : 100000;
1206
  devc->initialized = 1;
1207
 
1208
  for (n = 0; n < 2 && !ok; n++)
1209
    {
1210
      for (timeout = timeout_limit; timeout > 0 && !ok; timeout--)
1211
        ok = output_ready (devc);
1212
 
1213
      write_command (devc, MPU_RESET);  /*
1214
                                           * Send MPU-401 RESET Command
1215
                                         */
1216
 
1217
      /*
1218
       * Wait at least 25 msec. This method is not accurate so let's make the
1219
       * loop bit longer. Cannot sleep since this is called during boot.
1220
       */
1221
 
1222
      for (timeout = timeout_limit * 2; timeout > 0 && !ok; timeout--)
1223
        {
1224
          save_flags (flags);
1225
          cli ();
1226
          if (input_avail (devc))
1227
            if (read_data (devc) == MPU_ACK)
1228
              ok = 1;
1229
          restore_flags (flags);
1230
        }
1231
 
1232
    }
1233
 
1234
  devc->m_state = ST_INIT;
1235
  devc->m_ptr = 0;
1236
  devc->m_left = 0;
1237
  devc->last_status = 0;
1238
  devc->uart_mode = 0;
1239
 
1240
  return ok;
1241
}
1242
 
1243
static void
1244
set_uart_mode (int dev, struct mpu_config *devc, int arg)
1245
{
1246
  if (!arg && (devc->capabilities & MPU_CAP_INTLG))
1247
    {
1248
      return;
1249
    }
1250
 
1251
  if ((devc->uart_mode == 0) == (arg == 0))
1252
    {
1253
      return;                   /* Already set */
1254
    }
1255
 
1256
  reset_mpu401 (devc);          /* This exits the uart mode */
1257
 
1258
  if (arg)
1259
    {
1260
      if (mpu_cmd (dev, UART_MODE_ON, 0) < 0)
1261
        {
1262
          printk ("MPU%d: Can't enter UART mode\n", devc->devno);
1263
          devc->uart_mode = 0;
1264
          return;
1265
        }
1266
    }
1267
  devc->uart_mode = arg;
1268
 
1269
}
1270
 
1271
int
1272
probe_mpu401 (struct address_info *hw_config)
1273
{
1274
  int             ok = 0;
1275
  struct mpu_config tmp_devc;
1276
 
1277
  if (check_region (hw_config->io_base, 2))
1278
    {
1279
      printk ("\n\nmpu401.c: I/O port %x already in use\n\n",
1280
              hw_config->io_base);
1281
      return 0;
1282
    }
1283
 
1284
  tmp_devc.base = hw_config->io_base;
1285
  tmp_devc.irq = hw_config->irq;
1286
  tmp_devc.initialized = 0;
1287
  tmp_devc.opened = 0;
1288
  tmp_devc.osp = hw_config->osp;
1289
 
1290
  if (hw_config->always_detect)
1291
    return 1;
1292
 
1293
  if (inb (hw_config->io_base + 1) == 0xff)
1294
    {
1295
      DDB (printk ("MPU401: Port %x looks dead.\n", hw_config->io_base));
1296
      return 0;                  /* Just bus float? */
1297
    }
1298
 
1299
  ok = reset_mpu401 (&tmp_devc);
1300
 
1301
  if (!ok)
1302
    {
1303
      DDB (printk ("MPU401: Reset failed on port %x\n", hw_config->io_base));
1304
    }
1305
 
1306
  return ok;
1307
}
1308
 
1309
void
1310
unload_mpu401 (struct address_info *hw_config)
1311
{
1312
  release_region (hw_config->io_base, 2);
1313
  if (hw_config->always_detect == 0 && hw_config->irq > 0)
1314
    snd_release_irq (hw_config->irq);
1315
}
1316
 
1317
/*****************************************************
1318
 *      Timer stuff
1319
 ****************************************************/
1320
 
1321
#if defined(CONFIG_SEQUENCER)
1322
 
1323
static volatile int timer_initialized = 0, timer_open = 0, tmr_running = 0;
1324
static volatile int curr_tempo, curr_timebase, hw_timebase;
1325
static int      max_timebase = 8;       /* 8*24=192 ppqn */
1326
static volatile unsigned long next_event_time;
1327
static volatile unsigned long curr_ticks, curr_clocks;
1328
static unsigned long prev_event_time;
1329
static int      metronome_mode;
1330
 
1331
static unsigned long
1332
clocks2ticks (unsigned long clocks)
1333
{
1334
  /*
1335
     * The MPU-401 supports just a limited set of possible timebase values.
1336
     * Since the applications require more choices, the driver has to
1337
     * program the HW to do its best and to convert between the HW and
1338
     * actual timebases.
1339
   */
1340
 
1341
  return ((clocks * curr_timebase) + (hw_timebase / 2)) / hw_timebase;
1342
}
1343
 
1344
static void
1345
set_timebase (int midi_dev, int val)
1346
{
1347
  int             hw_val;
1348
 
1349
  if (val < 48)
1350
    val = 48;
1351
  if (val > 1000)
1352
    val = 1000;
1353
 
1354
  hw_val = val;
1355
  hw_val = (hw_val + 12) / 24;
1356
  if (hw_val > max_timebase)
1357
    hw_val = max_timebase;
1358
 
1359
  if (mpu_cmd (midi_dev, 0xC0 | (hw_val & 0x0f), 0) < 0)
1360
    {
1361
      printk ("MPU: Can't set HW timebase to %d\n", hw_val * 24);
1362
      return;
1363
    }
1364
  hw_timebase = hw_val * 24;
1365
  curr_timebase = val;
1366
 
1367
}
1368
 
1369
static void
1370
tmr_reset (void)
1371
{
1372
  unsigned long   flags;
1373
 
1374
  save_flags (flags);
1375
  cli ();
1376
  next_event_time = (unsigned long) -1;
1377
  prev_event_time = 0;
1378
  curr_ticks = curr_clocks = 0;
1379
  restore_flags (flags);
1380
}
1381
 
1382
static void
1383
set_timer_mode (int midi_dev)
1384
{
1385
  if (timer_mode & TMR_MODE_CLS)
1386
    mpu_cmd (midi_dev, 0x3c, 0); /* Use CLS sync */
1387
  else if (timer_mode & TMR_MODE_SMPTE)
1388
    mpu_cmd (midi_dev, 0x3d, 0); /* Use SMPTE sync */
1389
 
1390
  if (timer_mode & TMR_INTERNAL)
1391
    {
1392
      mpu_cmd (midi_dev, 0x80, 0);       /* Use MIDI sync */
1393
    }
1394
  else
1395
    {
1396
      if (timer_mode & (TMR_MODE_MIDI | TMR_MODE_CLS))
1397
        {
1398
          mpu_cmd (midi_dev, 0x82, 0);   /* Use MIDI sync */
1399
          mpu_cmd (midi_dev, 0x91, 0);   /* Enable ext MIDI ctrl */
1400
        }
1401
      else if (timer_mode & TMR_MODE_FSK)
1402
        mpu_cmd (midi_dev, 0x81, 0);     /* Use FSK sync */
1403
    }
1404
}
1405
 
1406
static void
1407
stop_metronome (int midi_dev)
1408
{
1409
  mpu_cmd (midi_dev, 0x84, 0);   /* Disable metronome */
1410
}
1411
 
1412
static void
1413
setup_metronome (int midi_dev)
1414
{
1415
  int             numerator, denominator;
1416
  int             clks_per_click, num_32nds_per_beat;
1417
  int             beats_per_measure;
1418
 
1419
  numerator = ((unsigned) metronome_mode >> 24) & 0xff;
1420
  denominator = ((unsigned) metronome_mode >> 16) & 0xff;
1421
  clks_per_click = ((unsigned) metronome_mode >> 8) & 0xff;
1422
  num_32nds_per_beat = (unsigned) metronome_mode & 0xff;
1423
  beats_per_measure = (numerator * 4) >> denominator;
1424
 
1425
  if (!metronome_mode)
1426
    mpu_cmd (midi_dev, 0x84, 0); /* Disable metronome */
1427
  else
1428
    {
1429
      mpu_cmd (midi_dev, 0xE4, clks_per_click);
1430
      mpu_cmd (midi_dev, 0xE6, beats_per_measure);
1431
      mpu_cmd (midi_dev, 0x83, 0);       /* Enable metronome without accents */
1432
    }
1433
}
1434
 
1435
static int
1436
mpu_start_timer (int midi_dev)
1437
{
1438
  tmr_reset ();
1439
  set_timer_mode (midi_dev);
1440
 
1441
  if (tmr_running)
1442
    return TIMER_NOT_ARMED;     /* Already running */
1443
 
1444
  if (timer_mode & TMR_INTERNAL)
1445
    {
1446
      mpu_cmd (midi_dev, 0x02, 0);       /* Send MIDI start */
1447
      tmr_running = 1;
1448
      return TIMER_NOT_ARMED;
1449
    }
1450
  else
1451
    {
1452
      mpu_cmd (midi_dev, 0x35, 0);       /* Enable mode messages to PC */
1453
      mpu_cmd (midi_dev, 0x38, 0);       /* Enable sys common messages to PC */
1454
      mpu_cmd (midi_dev, 0x39, 0);       /* Enable real time messages to PC */
1455
      mpu_cmd (midi_dev, 0x97, 0);       /* Enable system exclusive messages to PC */
1456
    }
1457
 
1458
  return TIMER_ARMED;
1459
}
1460
 
1461
static int
1462
mpu_timer_open (int dev, int mode)
1463
{
1464
  int             midi_dev = sound_timer_devs[dev]->devlink;
1465
 
1466
  if (timer_open)
1467
    return -(EBUSY);
1468
 
1469
  tmr_reset ();
1470
  curr_tempo = 50;
1471
  mpu_cmd (midi_dev, 0xE0, 50);
1472
  curr_timebase = hw_timebase = 120;
1473
  set_timebase (midi_dev, 120);
1474
  timer_open = 1;
1475
  metronome_mode = 0;
1476
  set_timer_mode (midi_dev);
1477
 
1478
  mpu_cmd (midi_dev, 0xe7, 0x04);       /* Send all clocks to host */
1479
  mpu_cmd (midi_dev, 0x95, 0);   /* Enable clock to host */
1480
 
1481
  return 0;
1482
}
1483
 
1484
static void
1485
mpu_timer_close (int dev)
1486
{
1487
  int             midi_dev = sound_timer_devs[dev]->devlink;
1488
 
1489
  timer_open = tmr_running = 0;
1490
  mpu_cmd (midi_dev, 0x15, 0);   /* Stop all */
1491
  mpu_cmd (midi_dev, 0x94, 0);   /* Disable clock to host */
1492
  mpu_cmd (midi_dev, 0x8c, 0);   /* Disable measure end messages to host */
1493
  stop_metronome (midi_dev);
1494
}
1495
 
1496
static int
1497
mpu_timer_event (int dev, unsigned char *event)
1498
{
1499
  unsigned char   command = event[1];
1500
  unsigned long   parm = *(unsigned int *) &event[4];
1501
  int             midi_dev = sound_timer_devs[dev]->devlink;
1502
 
1503
  switch (command)
1504
    {
1505
    case TMR_WAIT_REL:
1506
      parm += prev_event_time;
1507
    case TMR_WAIT_ABS:
1508
      if (parm > 0)
1509
        {
1510
          long            time;
1511
 
1512
          if (parm <= curr_ticks)       /* It's the time */
1513
            return TIMER_NOT_ARMED;
1514
 
1515
          time = parm;
1516
          next_event_time = prev_event_time = time;
1517
 
1518
          return TIMER_ARMED;
1519
        }
1520
      break;
1521
 
1522
    case TMR_START:
1523
      if (tmr_running)
1524
        break;
1525
      return mpu_start_timer (midi_dev);
1526
      break;
1527
 
1528
    case TMR_STOP:
1529
      mpu_cmd (midi_dev, 0x01, 0);       /* Send MIDI stop */
1530
      stop_metronome (midi_dev);
1531
      tmr_running = 0;
1532
      break;
1533
 
1534
    case TMR_CONTINUE:
1535
      if (tmr_running)
1536
        break;
1537
      mpu_cmd (midi_dev, 0x03, 0);       /* Send MIDI continue */
1538
      setup_metronome (midi_dev);
1539
      tmr_running = 1;
1540
      break;
1541
 
1542
    case TMR_TEMPO:
1543
      if (parm)
1544
        {
1545
          if (parm < 8)
1546
            parm = 8;
1547
          if (parm > 250)
1548
            parm = 250;
1549
 
1550
          if (mpu_cmd (midi_dev, 0xE0, parm) < 0)
1551
            printk ("MPU: Can't set tempo to %d\n", (int) parm);
1552
          curr_tempo = parm;
1553
        }
1554
      break;
1555
 
1556
    case TMR_ECHO:
1557
      seq_copy_to_input (event, 8);
1558
      break;
1559
 
1560
    case TMR_TIMESIG:
1561
      if (metronome_mode)       /* Metronome enabled */
1562
        {
1563
          metronome_mode = parm;
1564
          setup_metronome (midi_dev);
1565
        }
1566
      break;
1567
 
1568
    default:;
1569
    }
1570
 
1571
  return TIMER_NOT_ARMED;
1572
}
1573
 
1574
static unsigned long
1575
mpu_timer_get_time (int dev)
1576
{
1577
  if (!timer_open)
1578
    return 0;
1579
 
1580
  return curr_ticks;
1581
}
1582
 
1583
static int
1584
mpu_timer_ioctl (int dev,
1585
                 unsigned int command, caddr_t arg)
1586
{
1587
  int             midi_dev = sound_timer_devs[dev]->devlink;
1588
 
1589
  switch (command)
1590
    {
1591
    case SNDCTL_TMR_SOURCE:
1592
      {
1593
        int             parm = (int) get_user ((int *) arg) & timer_caps;
1594
 
1595
        if (parm != 0)
1596
          {
1597
            timer_mode = parm;
1598
 
1599
            if (timer_mode & TMR_MODE_CLS)
1600
              mpu_cmd (midi_dev, 0x3c, 0);       /* Use CLS sync */
1601
            else if (timer_mode & TMR_MODE_SMPTE)
1602
              mpu_cmd (midi_dev, 0x3d, 0);       /* Use SMPTE sync */
1603
          }
1604
 
1605
        return snd_ioctl_return ((int *) arg, timer_mode);
1606
      }
1607
      break;
1608
 
1609
    case SNDCTL_TMR_START:
1610
      mpu_start_timer (midi_dev);
1611
      return 0;
1612
      break;
1613
 
1614
    case SNDCTL_TMR_STOP:
1615
      tmr_running = 0;
1616
      mpu_cmd (midi_dev, 0x01, 0);       /* Send MIDI stop */
1617
      stop_metronome (midi_dev);
1618
      return 0;
1619
      break;
1620
 
1621
    case SNDCTL_TMR_CONTINUE:
1622
      if (tmr_running)
1623
        return 0;
1624
      tmr_running = 1;
1625
      mpu_cmd (midi_dev, 0x03, 0);       /* Send MIDI continue */
1626
      return 0;
1627
      break;
1628
 
1629
    case SNDCTL_TMR_TIMEBASE:
1630
      {
1631
        int             val = (int) get_user ((int *) arg);
1632
 
1633
        if (val)
1634
          set_timebase (midi_dev, val);
1635
 
1636
        return snd_ioctl_return ((int *) arg, curr_timebase);
1637
      }
1638
      break;
1639
 
1640
    case SNDCTL_TMR_TEMPO:
1641
      {
1642
        int             val = (int) get_user ((int *) arg);
1643
        int             ret;
1644
 
1645
        if (val)
1646
          {
1647
            if (val < 8)
1648
              val = 8;
1649
            if (val > 250)
1650
              val = 250;
1651
            if ((ret = mpu_cmd (midi_dev, 0xE0, val)) < 0)
1652
              {
1653
                printk ("MPU: Can't set tempo to %d\n", (int) val);
1654
                return ret;
1655
              }
1656
 
1657
            curr_tempo = val;
1658
          }
1659
 
1660
        return snd_ioctl_return ((int *) arg, curr_tempo);
1661
      }
1662
      break;
1663
 
1664
    case SNDCTL_SEQ_CTRLRATE:
1665
      if (get_user ((int *) arg) != 0)   /* Can't change */
1666
        return -(EINVAL);
1667
 
1668
      return snd_ioctl_return ((int *) arg, ((curr_tempo * curr_timebase) + 30) / 60);
1669
      break;
1670
 
1671
    case SNDCTL_TMR_METRONOME:
1672
      metronome_mode = (int) get_user ((int *) arg);
1673
      setup_metronome (midi_dev);
1674
      return 0;
1675
      break;
1676
 
1677
    default:;
1678
    }
1679
 
1680
  return -(EINVAL);
1681
}
1682
 
1683
static void
1684
mpu_timer_arm (int dev, long time)
1685
{
1686
  if (time < 0)
1687
    time = curr_ticks + 1;
1688
  else if (time <= curr_ticks)  /* It's the time */
1689
    return;
1690
 
1691
  next_event_time = prev_event_time = time;
1692
 
1693
  return;
1694
}
1695
 
1696
static struct sound_timer_operations mpu_timer =
1697
{
1698
  {"MPU-401 Timer", 0},
1699
  10,                           /* Priority */
1700
  0,                             /* Local device link */
1701
  mpu_timer_open,
1702
  mpu_timer_close,
1703
  mpu_timer_event,
1704
  mpu_timer_get_time,
1705
  mpu_timer_ioctl,
1706
  mpu_timer_arm
1707
};
1708
 
1709
static void
1710
mpu_timer_interrupt (void)
1711
{
1712
 
1713
  if (!timer_open)
1714
    return;
1715
 
1716
  if (!tmr_running)
1717
    return;
1718
 
1719
  curr_clocks++;
1720
  curr_ticks = clocks2ticks (curr_clocks);
1721
 
1722
  if (curr_ticks >= next_event_time)
1723
    {
1724
      next_event_time = (unsigned long) -1;
1725
      sequencer_timer (0);
1726
    }
1727
}
1728
 
1729
static void
1730
timer_ext_event (struct mpu_config *devc, int event, int parm)
1731
{
1732
  int             midi_dev = devc->devno;
1733
 
1734
  if (!devc->timer_flag)
1735
    return;
1736
 
1737
  switch (event)
1738
    {
1739
    case TMR_CLOCK:
1740
      printk ("<MIDI clk>");
1741
      break;
1742
 
1743
    case TMR_START:
1744
      printk ("Ext MIDI start\n");
1745
      if (!tmr_running)
1746
        if (timer_mode & TMR_EXTERNAL)
1747
          {
1748
            tmr_running = 1;
1749
            setup_metronome (midi_dev);
1750
            next_event_time = 0;
1751
            STORE (SEQ_START_TIMER ());
1752
          }
1753
      break;
1754
 
1755
    case TMR_STOP:
1756
      printk ("Ext MIDI stop\n");
1757
      if (timer_mode & TMR_EXTERNAL)
1758
        {
1759
          tmr_running = 0;
1760
          stop_metronome (midi_dev);
1761
          STORE (SEQ_STOP_TIMER ());
1762
        }
1763
      break;
1764
 
1765
    case TMR_CONTINUE:
1766
      printk ("Ext MIDI continue\n");
1767
      if (timer_mode & TMR_EXTERNAL)
1768
        {
1769
          tmr_running = 1;
1770
          setup_metronome (midi_dev);
1771
          STORE (SEQ_CONTINUE_TIMER ());
1772
        }
1773
      break;
1774
 
1775
    case TMR_SPP:
1776
      printk ("Songpos: %d\n", parm);
1777
      if (timer_mode & TMR_EXTERNAL)
1778
        {
1779
          STORE (SEQ_SONGPOS (parm));
1780
        }
1781
      break;
1782
    }
1783
}
1784
 
1785
static void
1786
mpu_timer_init (int midi_dev)
1787
{
1788
  struct mpu_config *devc;
1789
  int             n;
1790
 
1791
  devc = &dev_conf[midi_dev];
1792
 
1793
  if (timer_initialized)
1794
    return;                     /* There is already a similar timer */
1795
 
1796
  timer_initialized = 1;
1797
 
1798
  mpu_timer.devlink = midi_dev;
1799
  dev_conf[midi_dev].timer_flag = 1;
1800
 
1801
  if (num_sound_timers >= MAX_TIMER_DEV)
1802
    n = 0;                       /* Overwrite the system timer */
1803
  else
1804
    n = num_sound_timers++;
1805
  sound_timer_devs[n] = &mpu_timer;
1806
 
1807
  if (devc->version < 0x20)     /* Original MPU-401 */
1808
    timer_caps = TMR_INTERNAL | TMR_EXTERNAL | TMR_MODE_FSK | TMR_MODE_MIDI;
1809
  else
1810
    {
1811
      /*
1812
         * The version number 2.0 is used (at least) by the
1813
         * MusicQuest cards and the Roland Super-MPU.
1814
         *
1815
         * MusicQuest has given a special meaning to the bits of the
1816
         * revision number. The Super-MPU returns 0.
1817
       */
1818
 
1819
      if (devc->revision)
1820
        timer_caps |= TMR_EXTERNAL | TMR_MODE_MIDI;
1821
 
1822
      if (devc->revision & 0x02)
1823
        timer_caps |= TMR_MODE_CLS;
1824
 
1825
 
1826
      if (devc->revision & 0x40)
1827
        max_timebase = 10;      /* Has the 216 and 240 ppqn modes */
1828
    }
1829
 
1830
  timer_mode = (TMR_INTERNAL | TMR_MODE_MIDI) & timer_caps;
1831
 
1832
}
1833
 
1834
#endif
1835
 
1836
 
1837
 
1838
#endif

powered by: WebSVN 2.1.0

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