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