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