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