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 0xFE
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 if (devc->opened && devc->mode == MODE_SYNTH)
642 {
643 if (mpu_input_scanner (devc, read_data (devc->base)) == MPU_ACK)
644 ok = 1;
645 }
646 else
647 {
648 if (read_data (devc->base) == MPU_ACK)
649 ok = 1;
650 }
651
652 if (!ok)
653 {
654 RESTORE_INTR (flags);
655
656 return RET_ERROR (EIO);
657 }
658
659 if (cmd->nr_args)
660 for (i = 0; i < cmd->nr_args; i++)
661 {
662 for (timeout = 3000; timeout > 0 && !output_ready (devc->base); timeout--);
663
664 if (!mpu401_out (dev, cmd->data[i]))
665 {
666 RESTORE_INTR (flags);
667 printk ("MPU: Command (0x%x), parm send failed.\n", (int) cmd->cmd);
668 return RET_ERROR (EIO);
669 }
670 }
671
672 ret = 0;
673 cmd->data[0] = 0;
674
675 if (cmd->nr_returns)
676 for (i = 0; i < cmd->nr_returns; i++)
677 {
678 ok = 0;
679 for (timeout = 5000; timeout > 0 && !ok; timeout--)
680 if (input_avail (devc->base))
681 {
682 cmd->data[i] = read_data (devc->base);
683 ok = 1;
684 }
685
686 if (!ok)
687 {
688 RESTORE_INTR (flags);
689
690 return RET_ERROR (EIO);
691 }
692 }
693
694 RESTORE_INTR (flags);
695
696 return ret;
697 }
698
699 static int
700 exec_cmd (int dev, int cmd, int data)
701 {
702 int ret;
703
704 static mpu_command_rec rec;
705
706 rec.cmd = cmd & 0xff;
707 rec.nr_args = ((cmd & 0xf0) == 0xE0);
708 rec.nr_returns = ((cmd & 0xf0) == 0xA0);
709 rec.data[0] = data & 0xff;
710
711 if ((ret = mpu401_command (dev, &rec)) < 0)
712 return ret;
713 return (unsigned char) rec.data[0];
714 }
715
716 static int
717 mpu401_prefix_cmd (int dev, unsigned char status)
718 {
719 struct mpu_config *devc = &dev_conf[dev];
720
721 if (devc->uart_mode)
722 return 1;
723
724 if (status < 0xf0)
725 {
726 if (exec_cmd (dev, 0xD0, 0) < 0)
727 return 0;
728
729 return 1;
730 }
731
732 switch (status)
733 {
734 case 0xF0:
735 if (exec_cmd (dev, 0xDF, 0) < 0)
736 return 0;
737
738 return 1;
739 break;
740
741 default:
742 return 0;
743 }
744
745 return 0;
746 }
747
748 static int
749 mpu401_start_read (int dev)
750 {
751 return 0;
752 }
753
754 static int
755 mpu401_end_read (int dev)
756 {
757 return 0;
758 }
759
760 static int
761 mpu401_ioctl (int dev, unsigned cmd, unsigned arg)
762 {
763 struct mpu_config *devc;
764
765 devc = &dev_conf[dev];
766
767 switch (cmd)
768 {
769 case 1:
770 IOCTL_FROM_USER ((char *) &init_sequence, (char *) arg, 0, sizeof (init_sequence));
771 return 0;
772 break;
773
774 case SNDCTL_MIDI_MPUMODE:
775 if (devc->version == 0)
776 {
777 printk ("MPU-401: Intelligent mode not supported by the HW\n");
778 return RET_ERROR (EINVAL);
779 }
780 set_uart_mode (dev, devc, !IOCTL_IN (arg));
781 return 0;
782 break;
783
784 case SNDCTL_MIDI_MPUCMD:
785 {
786 int ret;
787 mpu_command_rec rec;
788
789 IOCTL_FROM_USER ((char *) &rec, (char *) arg, 0, sizeof (rec));
790
791 if ((ret = mpu401_command (dev, &rec)) < 0)
792 return ret;
793
794 IOCTL_TO_USER ((char *) arg, 0, (char *) &rec, sizeof (rec));
795 return 0;
796 }
797 break;
798
799 default:
800 return RET_ERROR (EINVAL);
801 }
802 }
803
804 static void
805 mpu401_kick (int dev)
806 {
807 }
808
809 static int
810 mpu401_buffer_status (int dev)
811 {
812 return 0;
813
814
815 }
816
817 static int
818 mpu_synth_ioctl (int dev,
819 unsigned int cmd, unsigned int arg)
820 {
821 int midi_dev;
822 struct mpu_config *devc;
823
824 midi_dev = synth_devs[dev]->midi_dev;
825
826 if (midi_dev < 0 || midi_dev > num_midis)
827 return RET_ERROR (ENXIO);
828
829 devc = &dev_conf[midi_dev];
830
831 switch (cmd)
832 {
833
834 case SNDCTL_SYNTH_INFO:
835 IOCTL_TO_USER ((char *) arg, 0, &mpu_synth_info[midi_dev],
836 sizeof (struct synth_info));
837
838 return 0;
839 break;
840
841 case SNDCTL_SYNTH_MEMAVL:
842 return 0x7fffffff;
843 break;
844
845 default:
846 return RET_ERROR (EINVAL);
847 }
848 }
849
850 static int
851 mpu_synth_open (int dev, int mode)
852 {
853 int midi_dev, err;
854 struct mpu_config *devc;
855
856 midi_dev = synth_devs[dev]->midi_dev;
857
858 if (midi_dev < 0 || midi_dev > num_midis)
859 {
860 return RET_ERROR (ENXIO);
861 }
862
863 devc = &dev_conf[midi_dev];
864
865
866
867
868
869
870
871
872 if (!devc->initialized)
873 {
874 if (mpu401_status (devc->base) == 0xff)
875 {
876 printk ("MPU-401: Device not initialized properly\n");
877 return RET_ERROR (EIO);
878 }
879 reset_mpu401 (devc);
880 }
881
882 if (devc->opened)
883 {
884 printk ("MPU-401: Midi busy\n");
885 return RET_ERROR (EBUSY);
886 }
887
888 devc->mode = MODE_SYNTH;
889 devc->synthno = dev;
890
891 devc->inputintr = NULL;
892 irq2dev[devc->irq] = midi_dev;
893 if (devc->shared_irq == 0)
894 if ((err = snd_set_irq_handler (devc->irq, mpuintr, midi_devs[midi_dev]->info.name) < 0))
895 {
896 return err;
897 }
898
899 if (midi_devs[midi_dev]->coproc)
900 if ((err = midi_devs[midi_dev]->coproc->
901 open (midi_devs[midi_dev]->coproc->devc, COPR_MIDI)) < 0)
902 {
903 if (devc->shared_irq == 0)
904 snd_release_irq (devc->irq);
905 printk ("MPU-401: Can't access coprocessor device\n");
906
907 return err;
908 }
909
910 devc->opened = mode;
911 reset_mpu401 (devc);
912
913 if (mode & OPEN_READ)
914 {
915 exec_cmd (midi_dev, 0x8B, 0);
916 exec_cmd (midi_dev, 0x34, 0);
917 }
918
919 return 0;
920 }
921
922 static void
923 mpu_synth_close (int dev)
924 {
925 int midi_dev;
926 struct mpu_config *devc;
927
928 midi_dev = synth_devs[dev]->midi_dev;
929
930 devc = &dev_conf[midi_dev];
931 exec_cmd (midi_dev, 0x15, 0);
932 exec_cmd (midi_dev, 0x8a, 0);
933
934 if (devc->shared_irq == 0)
935 snd_release_irq (devc->irq);
936 devc->inputintr = NULL;
937
938 if (midi_devs[midi_dev]->coproc)
939 midi_devs[midi_dev]->coproc->close (midi_devs[midi_dev]->coproc->devc, COPR_MIDI);
940 devc->opened = 0;
941 devc->mode = 0;
942 }
943
944 #define MIDI_SYNTH_NAME "MPU-401 UART Midi"
945 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
946 #include "midi_synth.h"
947
948 static struct synth_operations mpu401_synth_proto =
949 {
950 NULL,
951 0,
952 SYNTH_TYPE_MIDI,
953 0,
954 mpu_synth_open,
955 mpu_synth_close,
956 mpu_synth_ioctl,
957 midi_synth_kill_note,
958 midi_synth_start_note,
959 midi_synth_set_instr,
960 midi_synth_reset,
961 midi_synth_hw_control,
962 midi_synth_load_patch,
963 midi_synth_aftertouch,
964 midi_synth_controller,
965 midi_synth_panning,
966 NULL,
967 midi_synth_patchmgr,
968 midi_synth_bender,
969 NULL,
970 midi_synth_setup_voice
971 };
972
973 static struct synth_operations mpu401_synth_operations[MAX_MIDI_DEV];
974
975 static struct midi_operations mpu401_midi_proto =
976 {
977 {"MPU-401 Midi", 0, MIDI_CAP_MPU401, SNDCARD_MPU401},
978 NULL,
979 {0},
980 mpu401_open,
981 mpu401_close,
982 mpu401_ioctl,
983 mpu401_out,
984 mpu401_start_read,
985 mpu401_end_read,
986 mpu401_kick,
987 NULL,
988 mpu401_buffer_status,
989 mpu401_prefix_cmd
990 };
991
992 static struct midi_operations mpu401_midi_operations[MAX_MIDI_DEV];
993
994 static void
995 mpu401_chk_version (struct mpu_config *devc)
996 {
997 int tmp;
998
999 devc->version = devc->revision = 0;
1000
1001 if ((tmp = exec_cmd (num_midis, 0xAC, 0)) < 0)
1002 return;
1003
1004 if ((tmp & 0xf0) > 0x20)
1005 return;
1006
1007 devc->version = tmp;
1008
1009 if ((tmp = exec_cmd (num_midis, 0xAD, 0)) < 0)
1010 {
1011 devc->version = 0;
1012 return;
1013 }
1014 devc->revision = tmp;
1015 }
1016
1017 long
1018 attach_mpu401 (long mem_start, struct address_info *hw_config)
1019 {
1020 unsigned long flags;
1021 char revision_char;
1022
1023 struct mpu_config *devc;
1024
1025 if (num_midis >= MAX_MIDI_DEV)
1026 {
1027 printk ("MPU-401: Too many midi devices detected\n");
1028 return mem_start;
1029 }
1030
1031 devc = &dev_conf[num_midis];
1032
1033 devc->base = hw_config->io_base;
1034 devc->irq = hw_config->irq;
1035 devc->opened = 0;
1036 devc->uart_mode = 0;
1037 devc->initialized = 0;
1038 devc->version = 0;
1039 devc->revision = 0;
1040 devc->capabilities = 0;
1041 devc->timer_flag = 0;
1042 devc->m_busy = 0;
1043 devc->m_state = ST_INIT;
1044 devc->shared_irq = hw_config->always_detect;
1045
1046 if (!hw_config->always_detect)
1047 {
1048
1049 if (!reset_mpu401 (devc))
1050 return mem_start;
1051
1052 DISABLE_INTR (flags);
1053 mpu401_chk_version (devc);
1054 if (devc->version == 0)
1055 mpu401_chk_version (devc);
1056 RESTORE_INTR (flags);
1057 }
1058
1059 if (devc->version == 0)
1060 {
1061 memcpy ((char *) &mpu401_synth_operations[num_midis],
1062 (char *) &std_midi_synth,
1063 sizeof (struct synth_operations));
1064 }
1065 else
1066 {
1067 devc->capabilities |= MPU_CAP_INTLG;
1068 memcpy ((char *) &mpu401_synth_operations[num_midis],
1069 (char *) &mpu401_synth_proto,
1070 sizeof (struct synth_operations));
1071 }
1072
1073 memcpy ((char *) &mpu401_midi_operations[num_midis],
1074 (char *) &mpu401_midi_proto,
1075 sizeof (struct midi_operations));
1076
1077 mpu401_midi_operations[num_midis].converter =
1078 &mpu401_synth_operations[num_midis];
1079
1080 memcpy ((char *) &mpu_synth_info[num_midis],
1081 (char *) &mpu_synth_info_proto,
1082 sizeof (struct synth_info));
1083
1084 n_mpu_devs++;
1085
1086 if (devc->version == 0x20 && devc->revision >= 0x07)
1087 {
1088 int ports = (devc->revision & 0x08) ? 32 : 16;
1089
1090 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_SMPTE |
1091 MPU_CAP_CLS | MPU_CAP_2PORT;
1092
1093 revision_char = (devc->revision == 0x7f) ? 'M' : ' ';
1094 printk (" <MQX-%d%c MIDI Interface>",
1095 ports,
1096 revision_char);
1097 sprintf (mpu_synth_info[num_midis].name,
1098 "MQX-%d%c MIDI Interface #%d",
1099 ports,
1100 revision_char,
1101 n_mpu_devs);
1102 }
1103 else
1104 {
1105
1106 revision_char = devc->revision ? devc->revision + '@' : ' ';
1107 if (devc->revision > ('Z' - '@'))
1108 revision_char = '+';
1109
1110 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_FSK;
1111
1112 printk (" <MPU-401 MIDI Interface %d.%d%c>",
1113 (devc->version & 0xf0) >> 4,
1114 devc->version & 0x0f,
1115 revision_char);
1116 sprintf (mpu_synth_info[num_midis].name,
1117 "MPU-401 %d.%d%c Midi interface #%d",
1118 (devc->version & 0xf0) >> 4,
1119 devc->version & 0x0f,
1120 revision_char,
1121 n_mpu_devs);
1122 }
1123
1124 strcpy (mpu401_midi_operations[num_midis].info.name,
1125 mpu_synth_info[num_midis].name);
1126
1127 mpu401_synth_operations[num_midis].midi_dev = devc->devno = num_midis;
1128 mpu401_synth_operations[devc->devno].info =
1129 &mpu_synth_info[devc->devno];
1130
1131 if (devc->capabilities & MPU_CAP_INTLG)
1132 mpu_timer_init (num_midis);
1133
1134 irq2dev[devc->irq] = num_midis;
1135 midi_devs[num_midis++] = &mpu401_midi_operations[devc->devno];
1136 return mem_start;
1137 }
1138
1139 static int
1140 reset_mpu401 (struct mpu_config *devc)
1141 {
1142 unsigned long flags;
1143 int ok, timeout, n;
1144 int timeout_limit;
1145
1146
1147
1148
1149
1150
1151 ok = 0;
1152
1153 timeout_limit = devc->initialized ? 30000 : 100000;
1154 devc->initialized = 1;
1155
1156 for (n = 0; n < 2 && !ok; n++)
1157 {
1158 for (timeout = timeout_limit; timeout > 0 && !ok; timeout--)
1159 ok = output_ready (devc->base);
1160
1161 write_command (devc->base, MPU_RESET);
1162
1163
1164
1165
1166
1167
1168
1169
1170 for (timeout = timeout_limit * 2; timeout > 0 && !ok; timeout--)
1171 {
1172 DISABLE_INTR (flags);
1173 if (input_avail (devc->base))
1174 if (read_data (devc->base) == MPU_ACK)
1175 ok = 1;
1176 RESTORE_INTR (flags);
1177 }
1178
1179 }
1180
1181 devc->m_state = ST_INIT;
1182 devc->m_ptr = 0;
1183 devc->m_left = 0;
1184 devc->last_status = 0;
1185 devc->uart_mode = 0;
1186
1187 return ok;
1188 }
1189
1190 static void
1191 set_uart_mode (int dev, struct mpu_config *devc, int arg)
1192 {
1193
1194 if (!arg && devc->version == 0)
1195 {
1196 return;
1197 }
1198
1199 if ((devc->uart_mode == 0) == (arg == 0))
1200 {
1201 return;
1202 }
1203
1204 reset_mpu401 (devc);
1205
1206 if (arg)
1207 {
1208 if (exec_cmd (dev, UART_MODE_ON, 0) < 0)
1209 {
1210 printk ("MPU%d: Can't enter UART mode\n", devc->devno);
1211 devc->uart_mode = 0;
1212 return;
1213 }
1214 }
1215 devc->uart_mode = arg;
1216
1217 }
1218
1219 int
1220 probe_mpu401 (struct address_info *hw_config)
1221 {
1222 int ok = 0;
1223 struct mpu_config tmp_devc;
1224
1225 tmp_devc.base = hw_config->io_base;
1226 tmp_devc.irq = hw_config->irq;
1227 tmp_devc.initialized = 0;
1228 tmp_devc.opened = 0;
1229
1230 #if !defined(EXCLUDE_AEDSP16) && defined(AEDSP16_MPU401)
1231
1232
1233
1234 InitAEDSP16_MPU401 (hw_config);
1235 #endif
1236
1237 if (hw_config->always_detect)
1238 return 1;
1239
1240 if (INB (hw_config->io_base + 1) == 0xff)
1241 return 0;
1242
1243 ok = reset_mpu401 (&tmp_devc);
1244
1245 return ok;
1246 }
1247
1248
1249
1250
1251
1252 #if !defined(EXCLUDE_SEQUENCER)
1253
1254 static volatile int timer_initialized = 0, timer_open = 0, tmr_running = 0;
1255 static volatile int curr_tempo, curr_timebase, hw_timebase;
1256 static int max_timebase = 8;
1257 static volatile unsigned long next_event_time;
1258 static volatile unsigned long curr_ticks, curr_clocks;
1259 static unsigned long prev_event_time;
1260 static int metronome_mode;
1261
1262 static unsigned long
1263 clocks2ticks (unsigned long clocks)
1264 {
1265
1266
1267
1268
1269
1270
1271
1272 return ((clocks * curr_timebase) + (hw_timebase / 2)) / hw_timebase;
1273 }
1274
1275 static void
1276 set_timebase (int midi_dev, int val)
1277 {
1278 int hw_val;
1279
1280 if (val < 48)
1281 val = 48;
1282 if (val > 1000)
1283 val = 1000;
1284
1285 hw_val = val;
1286 hw_val = (hw_val + 23) / 24;
1287 if (hw_val > max_timebase)
1288 hw_val = max_timebase;
1289
1290 if (exec_cmd (midi_dev, 0xC0 | (hw_val & 0x0f), 0) < 0)
1291 {
1292 printk ("MPU: Can't set HW timebase to %d\n", hw_val * 24);
1293 return;
1294 }
1295 hw_timebase = hw_val * 24;
1296 curr_timebase = val;
1297
1298 }
1299
1300 static void
1301 tmr_reset (void)
1302 {
1303 unsigned long flags;
1304
1305 DISABLE_INTR (flags);
1306 next_event_time = 0xffffffff;
1307 prev_event_time = 0;
1308 curr_ticks = curr_clocks = 0;
1309 RESTORE_INTR (flags);
1310 }
1311
1312 static void
1313 set_timer_mode (int midi_dev)
1314 {
1315 if (timer_mode & TMR_MODE_CLS)
1316 exec_cmd (midi_dev, 0x3c, 0);
1317 else if (timer_mode & TMR_MODE_SMPTE)
1318 exec_cmd (midi_dev, 0x3d, 0);
1319
1320 if (timer_mode & TMR_INTERNAL)
1321 {
1322 exec_cmd (midi_dev, 0x80, 0);
1323 }
1324 else
1325 {
1326 if (timer_mode & (TMR_MODE_MIDI | TMR_MODE_CLS))
1327 {
1328 exec_cmd (midi_dev, 0x82, 0);
1329 exec_cmd (midi_dev, 0x91, 0);
1330 }
1331 else if (timer_mode & TMR_MODE_FSK)
1332 exec_cmd (midi_dev, 0x81, 0);
1333 }
1334 }
1335
1336 static void
1337 stop_metronome (int midi_dev)
1338 {
1339 exec_cmd (midi_dev, 0x84, 0);
1340 }
1341
1342 static void
1343 setup_metronome (int midi_dev)
1344 {
1345 int numerator, denominator;
1346 int clks_per_click, num_32nds_per_beat;
1347 int beats_per_measure;
1348
1349 numerator = ((unsigned) metronome_mode >> 24) & 0xff;
1350 denominator = ((unsigned) metronome_mode >> 16) & 0xff;
1351 clks_per_click = ((unsigned) metronome_mode >> 8) & 0xff;
1352 num_32nds_per_beat = (unsigned) metronome_mode & 0xff;
1353 beats_per_measure = (numerator * 4) >> denominator;
1354
1355 if (!metronome_mode)
1356 exec_cmd (midi_dev, 0x84, 0);
1357 else
1358 {
1359 exec_cmd (midi_dev, 0xE4, clks_per_click);
1360 exec_cmd (midi_dev, 0xE6, beats_per_measure);
1361 exec_cmd (midi_dev, 0x83, 0);
1362 }
1363 }
1364
1365 static int
1366 start_timer (int midi_dev)
1367 {
1368 tmr_reset ();
1369 set_timer_mode (midi_dev);
1370
1371 if (tmr_running)
1372 return TIMER_NOT_ARMED;
1373
1374 if (timer_mode & TMR_INTERNAL)
1375 {
1376 exec_cmd (midi_dev, 0x02, 0);
1377 tmr_running = 1;
1378 return TIMER_NOT_ARMED;
1379 }
1380 else
1381 {
1382 exec_cmd (midi_dev, 0x35, 0);
1383 exec_cmd (midi_dev, 0x38, 0);
1384 exec_cmd (midi_dev, 0x39, 0);
1385 exec_cmd (midi_dev, 0x97, 0);
1386 }
1387
1388 return TIMER_ARMED;
1389 }
1390
1391 static int
1392 mpu_timer_open (int dev, int mode)
1393 {
1394 int midi_dev = sound_timer_devs[dev]->devlink;
1395
1396 if (timer_open)
1397 return RET_ERROR (EBUSY);
1398
1399 tmr_reset ();
1400 curr_tempo = 50;
1401 exec_cmd (midi_dev, 0xE0, 50);
1402 curr_timebase = hw_timebase = 120;
1403 set_timebase (midi_dev, 120);
1404 timer_open = 1;
1405 metronome_mode = 0;
1406 set_timer_mode (midi_dev);
1407
1408 exec_cmd (midi_dev, 0xe7, 0x04);
1409 exec_cmd (midi_dev, 0x95, 0);
1410
1411 return 0;
1412 }
1413
1414 static void
1415 mpu_timer_close (int dev)
1416 {
1417 int midi_dev = sound_timer_devs[dev]->devlink;
1418
1419 timer_open = tmr_running = 0;
1420 exec_cmd (midi_dev, 0x15, 0);
1421 exec_cmd (midi_dev, 0x94, 0);
1422 exec_cmd (midi_dev, 0x8c, 0);
1423 stop_metronome (midi_dev);
1424 }
1425
1426 static int
1427 mpu_timer_event (int dev, unsigned char *event)
1428 {
1429 unsigned char command = event[1];
1430 unsigned long parm = *(unsigned int *) &event[4];
1431 int midi_dev = sound_timer_devs[dev]->devlink;
1432
1433 switch (command)
1434 {
1435 case TMR_WAIT_REL:
1436 parm += prev_event_time;
1437 case TMR_WAIT_ABS:
1438 if (parm > 0)
1439 {
1440 long time;
1441
1442 if (parm <= curr_ticks)
1443 return TIMER_NOT_ARMED;
1444
1445 time = parm;
1446 next_event_time = prev_event_time = time;
1447
1448 return TIMER_ARMED;
1449 }
1450 break;
1451
1452 case TMR_START:
1453 if (tmr_running)
1454 break;
1455 return start_timer (midi_dev);
1456 break;
1457
1458 case TMR_STOP:
1459 exec_cmd (midi_dev, 0x01, 0);
1460 stop_metronome (midi_dev);
1461 tmr_running = 0;
1462 break;
1463
1464 case TMR_CONTINUE:
1465 if (tmr_running)
1466 break;
1467 exec_cmd (midi_dev, 0x03, 0);
1468 setup_metronome (midi_dev);
1469 tmr_running = 1;
1470 break;
1471
1472 case TMR_TEMPO:
1473 if (parm)
1474 {
1475 if (parm < 8)
1476 parm = 8;
1477 if (parm > 250)
1478 parm = 250;
1479
1480 if (exec_cmd (midi_dev, 0xE0, parm) < 0)
1481 printk ("MPU: Can't set tempo to %d\n", (int) parm);
1482 curr_tempo = parm;
1483 }
1484 break;
1485
1486 case TMR_ECHO:
1487 seq_copy_to_input (event, 8);
1488 break;
1489
1490 case TMR_TIMESIG:
1491 if (metronome_mode)
1492 {
1493 metronome_mode = parm;
1494 setup_metronome (midi_dev);
1495 }
1496 break;
1497
1498 default:;
1499 }
1500
1501 return TIMER_NOT_ARMED;
1502 }
1503
1504 static unsigned long
1505 mpu_timer_get_time (int dev)
1506 {
1507 if (!timer_open)
1508 return 0;
1509
1510 return curr_ticks;
1511 }
1512
1513 static int
1514 mpu_timer_ioctl (int dev,
1515 unsigned int command, unsigned int arg)
1516 {
1517 int midi_dev = sound_timer_devs[dev]->devlink;
1518
1519 switch (command)
1520 {
1521 case SNDCTL_TMR_SOURCE:
1522 {
1523 int parm = IOCTL_IN (arg) & timer_caps;
1524
1525 if (parm != 0)
1526 {
1527 timer_mode = parm;
1528
1529 if (timer_mode & TMR_MODE_CLS)
1530 exec_cmd (midi_dev, 0x3c, 0);
1531 else if (timer_mode & TMR_MODE_SMPTE)
1532 exec_cmd (midi_dev, 0x3d, 0);
1533 }
1534
1535 return IOCTL_OUT (arg, timer_mode);
1536 }
1537 break;
1538
1539 case SNDCTL_TMR_START:
1540 start_timer (midi_dev);
1541 return 0;
1542 break;
1543
1544 case SNDCTL_TMR_STOP:
1545 tmr_running = 0;
1546 exec_cmd (midi_dev, 0x01, 0);
1547 stop_metronome (midi_dev);
1548 return 0;
1549 break;
1550
1551 case SNDCTL_TMR_CONTINUE:
1552 if (tmr_running)
1553 return 0;
1554 tmr_running = 1;
1555 exec_cmd (midi_dev, 0x03, 0);
1556 return 0;
1557 break;
1558
1559 case SNDCTL_TMR_TIMEBASE:
1560 {
1561 int val = IOCTL_IN (arg);
1562
1563 if (val)
1564 set_timebase (midi_dev, val);
1565
1566 return IOCTL_OUT (arg, curr_timebase);
1567 }
1568 break;
1569
1570 case SNDCTL_TMR_TEMPO:
1571 {
1572 int val = IOCTL_IN (arg);
1573 int ret;
1574
1575 if (val)
1576 {
1577 if (val < 8)
1578 val = 8;
1579 if (val > 250)
1580 val = 250;
1581 if ((ret = exec_cmd (midi_dev, 0xE0, val)) < 0)
1582 {
1583 printk ("MPU: Can't set tempo to %d\n", (int) val);
1584 return ret;
1585 }
1586
1587 curr_tempo = val;
1588 }
1589
1590 return IOCTL_OUT (arg, curr_tempo);
1591 }
1592 break;
1593
1594 case SNDCTL_SEQ_CTRLRATE:
1595 if (IOCTL_IN (arg) != 0)
1596 return RET_ERROR (EINVAL);
1597
1598 return IOCTL_OUT (arg, ((curr_tempo * curr_timebase) + 30) / 60);
1599 break;
1600
1601 case SNDCTL_TMR_METRONOME:
1602 metronome_mode = IOCTL_IN (arg);
1603 setup_metronome (midi_dev);
1604 return 0;
1605 break;
1606
1607 default:
1608 }
1609
1610 return RET_ERROR (EINVAL);
1611 }
1612
1613 static void
1614 mpu_timer_arm (int dev, long time)
1615 {
1616 if (time < 0)
1617 time = curr_ticks + 1;
1618 else if (time <= curr_ticks)
1619 return;
1620
1621 next_event_time = prev_event_time = time;
1622
1623 return;
1624 }
1625
1626 static struct sound_timer_operations mpu_timer =
1627 {
1628 {"MPU-401 Timer", 0},
1629 10,
1630 0,
1631 mpu_timer_open,
1632 mpu_timer_close,
1633 mpu_timer_event,
1634 mpu_timer_get_time,
1635 mpu_timer_ioctl,
1636 mpu_timer_arm
1637 };
1638
1639 static void
1640 mpu_timer_interrupt (void)
1641 {
1642
1643 if (!timer_open)
1644 return;
1645
1646 if (!tmr_running)
1647 return;
1648
1649 curr_clocks++;
1650 curr_ticks = clocks2ticks (curr_clocks);
1651
1652 if (curr_ticks >= next_event_time)
1653 {
1654 next_event_time = 0xffffffff;
1655 sequencer_timer ();
1656 }
1657 }
1658
1659 static void
1660 timer_ext_event (struct mpu_config *devc, int event, int parm)
1661 {
1662 int midi_dev = devc->devno;
1663
1664 if (!devc->timer_flag)
1665 return;
1666
1667 switch (event)
1668 {
1669 case TMR_CLOCK:
1670 printk ("<MIDI clk>");
1671 break;
1672
1673 case TMR_START:
1674 printk ("Ext MIDI start\n");
1675 if (!tmr_running)
1676 if (timer_mode & TMR_EXTERNAL)
1677 {
1678 tmr_running = 1;
1679 setup_metronome (midi_dev);
1680 next_event_time = 0;
1681 STORE (SEQ_START_TIMER ());
1682 }
1683 break;
1684
1685 case TMR_STOP:
1686 printk ("Ext MIDI stop\n");
1687 if (timer_mode & TMR_EXTERNAL)
1688 {
1689 tmr_running = 0;
1690 stop_metronome (midi_dev);
1691 STORE (SEQ_STOP_TIMER ());
1692 }
1693 break;
1694
1695 case TMR_CONTINUE:
1696 printk ("Ext MIDI continue\n");
1697 if (timer_mode & TMR_EXTERNAL)
1698 {
1699 tmr_running = 1;
1700 setup_metronome (midi_dev);
1701 STORE (SEQ_CONTINUE_TIMER ());
1702 }
1703 break;
1704
1705 case TMR_SPP:
1706 printk ("Songpos: %d\n", parm);
1707 if (timer_mode & TMR_EXTERNAL)
1708 {
1709 STORE (SEQ_SONGPOS (parm));
1710 }
1711 break;
1712 }
1713 }
1714
1715 static void
1716 mpu_timer_init (int midi_dev)
1717 {
1718 struct mpu_config *devc;
1719 int n;
1720
1721 devc = &dev_conf[midi_dev];
1722
1723 if (timer_initialized)
1724 return;
1725
1726 timer_initialized = 1;
1727
1728 mpu_timer.devlink = midi_dev;
1729 dev_conf[midi_dev].timer_flag = 1;
1730
1731 #if 1
1732 if (num_sound_timers >= MAX_TIMER_DEV)
1733 n = 0;
1734 else
1735 n = num_sound_timers++;
1736 #else
1737 n = 0;
1738 #endif
1739 sound_timer_devs[n] = &mpu_timer;
1740
1741 if (devc->version < 0x20)
1742 timer_caps = TMR_INTERNAL | TMR_EXTERNAL | TMR_MODE_FSK | TMR_MODE_MIDI;
1743 else
1744 {
1745
1746
1747
1748
1749
1750
1751
1752
1753 if (devc->revision)
1754 timer_caps |= TMR_EXTERNAL | TMR_MODE_MIDI;
1755
1756 if (devc->revision & 0x02)
1757 timer_caps |= TMR_MODE_CLS;
1758
1759 #if 0
1760 if (devc->revision & 0x04)
1761 timer_caps |= TMR_MODE_SMPTE;
1762 #endif
1763
1764 if (devc->revision & 0x40)
1765 max_timebase = 10;
1766 }
1767
1768 timer_mode = (TMR_INTERNAL | TMR_MODE_MIDI) & timer_caps;
1769
1770 }
1771
1772 #endif
1773
1774 #endif
1775
1776 #endif