4 * The low level driver for Roland MPU-401 compatible Midi cards.
7 * Copyright (C) by Hannu Savolainen 1993-1997
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
14 * Thomas Sailer ioctl code reworked (vmalloc/vfree removed)
15 * Alan Cox modularisation, use normal request_irq, use dev_id
18 #include <linux/config.h>
19 #include <linux/module.h>
21 #define USE_SEQ_MACROS
22 #define USE_SIMPLE_MACROS
24 #include "sound_config.h"
25 #include "soundmodule.h"
27 #if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI)
31 #ifdef CONFIG_SEQUENCER
32 static int timer_mode
= TMR_INTERNAL
, timer_caps
= TMR_INTERNAL
;
52 unsigned char version
, revision
;
53 unsigned int capabilities
;
54 #define MPU_CAP_INTLG 0x10000000
55 #define MPU_CAP_SYNC 0x00000010
56 #define MPU_CAP_FSK 0x00000020
57 #define MPU_CAP_CLS 0x00000040
58 #define MPU_CAP_SMPTE 0x00000080
59 #define MPU_CAP_2PORT 0x00000001
63 #define BUFTEST(dc) if (dc->m_ptr >= MBUF_MAX || dc->m_ptr < 0) \
64 {printk( "MPU: Invalid buffer pointer %d/%d, s=%d\n", dc->m_ptr, dc->m_left, dc->m_state);dc->m_ptr--;}
66 unsigned char m_buf
[MBUF_MAX
];
70 unsigned char last_status
;
71 void (*inputintr
) (int dev
, unsigned char data
);
76 #define DATAPORT(base) (base)
77 #define COMDPORT(base) (base+1)
78 #define STATPORT(base) (base+1)
80 static int mpu401_status(struct mpu_config
*devc
)
82 return inb(STATPORT(devc
->base
));
85 #define input_avail(devc) (!(mpu401_status(devc)&INPUT_AVAIL))
86 #define output_ready(devc) (!(mpu401_status(devc)&OUTPUT_READY))
88 static void write_command(struct mpu_config
*devc
, unsigned char cmd
)
90 outb(cmd
, COMDPORT(devc
->base
));
93 static int read_data(struct mpu_config
*devc
)
95 return inb(DATAPORT(devc
->base
));
98 static void write_data(struct mpu_config
*devc
, unsigned char byte
)
100 outb(byte
, DATAPORT(devc
->base
));
103 #define OUTPUT_READY 0x40
104 #define INPUT_AVAIL 0x80
106 #define MPU_RESET 0xFF
107 #define UART_MODE_ON 0x3F
109 static struct mpu_config dev_conf
[MAX_MIDI_DEV
] =
114 static int n_mpu_devs
= 0;
116 static int reset_mpu401(struct mpu_config
*devc
);
117 static void set_uart_mode(int dev
, struct mpu_config
*devc
, int arg
);
119 static int mpu_timer_init(int midi_dev
);
120 static void mpu_timer_interrupt(void);
121 static void timer_ext_event(struct mpu_config
*devc
, int event
, int parm
);
123 static struct synth_info mpu_synth_info_proto
= {
124 "MPU-401 MIDI interface",
133 static struct synth_info mpu_synth_info
[MAX_MIDI_DEV
];
136 * States for the input scanner
139 #define ST_INIT 0 /* Ready for timing byte or msg */
140 #define ST_TIMED 1 /* Leading timing byte rcvd */
141 #define ST_DATABYTE 2 /* Waiting for (nr_left) data bytes */
143 #define ST_SYSMSG 100 /* System message (sysx etc). */
144 #define ST_SYSEX 101 /* System exclusive msg */
145 #define ST_MTC 102 /* Midi Time Code (MTC) qframe msg */
146 #define ST_SONGSEL 103 /* Song select */
147 #define ST_SONGPOS 104 /* Song position pointer */
149 static unsigned char len_tab
[] = /* # of data bytes following a status
162 #ifndef CONFIG_SEQUENCER
168 unsigned char obuf[8]; \
170 seq_input_event(obuf, len); \
176 #define _SEQ_ADVBUF(x) len=x
178 static int mpu_input_scanner(struct mpu_config
*devc
, unsigned char midic
)
181 switch (devc
->m_state
)
195 if (devc
->timer_flag
)
196 mpu_timer_interrupt();
210 printk("<Trk data rq #%d>", midic
& 0x0f);
214 printk("<conductor rq>");
218 devc
->m_state
= ST_SYSMSG
;
224 /* printk( "mpu time: %d ", midic); */
225 devc
->m_state
= ST_TIMED
;
228 printk("<MPU: Unknown event %02x> ", midic
);
234 int msg
= ((int) (midic
& 0xf0) >> 4);
236 devc
->m_state
= ST_DATABYTE
;
238 if (msg
< 8) /* Data byte */
240 /* printk( "midi msg (running status) "); */
241 msg
= ((int) (devc
->last_status
& 0xf0) >> 4);
243 devc
->m_left
= len_tab
[msg
] - 1;
246 devc
->m_buf
[0] = devc
->last_status
;
247 devc
->m_buf
[1] = midic
;
249 if (devc
->m_left
<= 0)
251 devc
->m_state
= ST_INIT
;
252 do_midi_msg(devc
->synthno
, devc
->m_buf
, devc
->m_ptr
);
256 else if (msg
== 0xf) /* MPU MARK */
258 devc
->m_state
= ST_INIT
;
263 /* printk( "NOP "); */
267 /* printk( "meas end "); */
271 /* printk( "data end "); */
275 printk("Unknown MPU mark %02x\n", midic
);
280 devc
->last_status
= midic
;
281 /* printk( "midi msg "); */
283 devc
->m_left
= len_tab
[msg
];
286 devc
->m_buf
[0] = midic
;
288 if (devc
->m_left
<= 0)
290 devc
->m_state
= ST_INIT
;
291 do_midi_msg(devc
->synthno
, devc
->m_buf
, devc
->m_ptr
);
303 devc
->m_state
= ST_SYSEX
;
307 devc
->m_state
= ST_MTC
;
311 devc
->m_state
= ST_SONGPOS
;
316 devc
->m_state
= ST_SONGSEL
;
320 /* printk( "tune_request\n"); */
321 devc
->m_state
= ST_INIT
;
328 devc
->m_state
= ST_INIT
;
329 timer_ext_event(devc
, TMR_CLOCK
, 0);
333 devc
->m_state
= ST_INIT
;
334 timer_ext_event(devc
, TMR_START
, 0);
338 devc
->m_state
= ST_INIT
;
339 timer_ext_event(devc
, TMR_CONTINUE
, 0);
343 devc
->m_state
= ST_INIT
;
344 timer_ext_event(devc
, TMR_STOP
, 0);
349 devc
->m_state
= ST_INIT
;
353 /* printk( "midi hard reset"); */
354 devc
->m_state
= ST_INIT
;
358 printk("unknown MIDI sysmsg %0x\n", midic
);
359 devc
->m_state
= ST_INIT
;
364 devc
->m_state
= ST_INIT
;
365 printk("MTC frame %x02\n", midic
);
372 devc
->m_state
= ST_INIT
;
375 printk("%02x ", midic
);
380 devc
->m_buf
[devc
->m_ptr
++] = midic
;
381 if (devc
->m_ptr
== 2)
383 devc
->m_state
= ST_INIT
;
385 timer_ext_event(devc
, TMR_SPP
,
386 ((devc
->m_buf
[1] & 0x7f) << 7) |
387 (devc
->m_buf
[0] & 0x7f));
393 devc
->m_buf
[devc
->m_ptr
++] = midic
;
394 if ((--devc
->m_left
) <= 0)
396 devc
->m_state
= ST_INIT
;
397 do_midi_msg(devc
->synthno
, devc
->m_buf
, devc
->m_ptr
);
403 printk("Bad state %d ", devc
->m_state
);
404 devc
->m_state
= ST_INIT
;
409 static void mpu401_input_loop(struct mpu_config
*devc
)
419 restore_flags(flags
);
421 if (busy
) /* Already inside the scanner */
426 while (input_avail(devc
) && n
-- > 0)
428 unsigned char c
= read_data(devc
);
430 if (devc
->mode
== MODE_SYNTH
)
432 mpu_input_scanner(devc
, c
);
434 else if (devc
->opened
& OPEN_READ
&& devc
->inputintr
!= NULL
)
435 devc
->inputintr(devc
->devno
, c
);
440 int intchk_mpu401(void *dev_id
)
442 struct mpu_config
*devc
;
443 int dev
= (int) dev_id
;
445 devc
= &dev_conf
[dev
];
446 return input_avail(devc
);
449 void mpuintr(int irq
, void *dev_id
, struct pt_regs
*dummy
)
451 struct mpu_config
*devc
;
452 int dev
= (int) dev_id
;
455 devc
= &dev_conf
[dev
];
457 if (input_avail(devc
))
459 if (devc
->base
!= 0 && (devc
->opened
& OPEN_READ
|| devc
->mode
== MODE_SYNTH
))
460 mpu401_input_loop(devc
);
463 /* Dummy read (just to acknowledge the interrupt) */
469 static int mpu401_open(int dev
, int mode
,
470 void (*input
) (int dev
, unsigned char data
),
471 void (*output
) (int dev
)
475 struct mpu_config
*devc
;
477 if (dev
< 0 || dev
>= num_midis
|| midi_devs
[dev
] == NULL
)
480 devc
= &dev_conf
[dev
];
485 * Verify that the device is really running.
486 * Some devices (such as Ensoniq SoundScape don't
487 * work before the on board processor (OBP) is initialized
488 * by downloading its microcode.
491 if (!devc
->initialized
)
493 if (mpu401_status(devc
) == 0xff) /* Bus float */
495 printk(KERN_ERR
"mpu401: Device not initialized properly\n");
501 if (midi_devs
[dev
]->coproc
)
503 if ((err
= midi_devs
[dev
]->coproc
->
504 open(midi_devs
[dev
]->coproc
->devc
, COPR_MIDI
)) < 0)
506 printk("MPU-401: Can't access coprocessor device\n");
511 set_uart_mode(dev
, devc
, 1);
512 devc
->mode
= MODE_MIDI
;
515 mpu401_input_loop(devc
);
517 devc
->inputintr
= input
;
523 static void mpu401_close(int dev
)
525 struct mpu_config
*devc
;
527 devc
= &dev_conf
[dev
];
529 reset_mpu401(devc
); /*
530 * This disables the UART mode
533 devc
->inputintr
= NULL
;
535 if (midi_devs
[dev
]->coproc
)
536 midi_devs
[dev
]->coproc
->close(midi_devs
[dev
]->coproc
->devc
, COPR_MIDI
);
540 static int mpu401_out(int dev
, unsigned char midi_byte
)
545 struct mpu_config
*devc
;
547 devc
= &dev_conf
[dev
];
550 * Sometimes it takes about 30000 loops before the output becomes ready
551 * (After reset). Normally it takes just about 10 loops.
554 for (timeout
= 30000; timeout
> 0 && !output_ready(devc
); timeout
--);
558 if (!output_ready(devc
))
560 printk(KERN_WARNING
"mpu401: Send data timeout\n");
561 restore_flags(flags
);
564 write_data(devc
, midi_byte
);
565 restore_flags(flags
);
569 static int mpu401_command(int dev
, mpu_command_rec
* cmd
)
574 struct mpu_config
*devc
;
576 devc
= &dev_conf
[dev
];
578 if (devc
->uart_mode
) /*
579 * Not possible in UART mode
582 printk(KERN_WARNING
"mpu401: commands not possible in the UART mode\n");
586 * Test for input since pending input seems to block the output.
588 if (input_avail(devc
))
589 mpu401_input_loop(devc
);
592 * Sometimes it takes about 50000 loops before the output becomes ready
593 * (After reset). Normally it takes just about 10 loops.
600 printk(KERN_WARNING
"mpu401: Command (0x%x) timeout\n", (int) cmd
->cmd
);
606 if (!output_ready(devc
))
608 restore_flags(flags
);
611 write_command(devc
, cmd
->cmd
);
614 for (timeout
= 50000; timeout
> 0 && !ok
; timeout
--)
616 if (input_avail(devc
))
618 if (devc
->opened
&& devc
->mode
== MODE_SYNTH
)
620 if (mpu_input_scanner(devc
, read_data(devc
)) == MPU_ACK
)
625 /* Device is not currently open. Use simpler method */
626 if (read_data(devc
) == MPU_ACK
)
633 restore_flags(flags
);
638 for (i
= 0; i
< cmd
->nr_args
; i
++)
640 for (timeout
= 3000; timeout
> 0 && !output_ready(devc
); timeout
--);
642 if (!mpu401_out(dev
, cmd
->data
[i
]))
644 restore_flags(flags
);
645 printk(KERN_WARNING
"mpu401: Command (0x%x), parm send failed.\n", (int) cmd
->cmd
);
655 for (i
= 0; i
< cmd
->nr_returns
; i
++)
658 for (timeout
= 5000; timeout
> 0 && !ok
; timeout
--)
659 if (input_avail(devc
))
661 cmd
->data
[i
] = read_data(devc
);
666 restore_flags(flags
);
671 restore_flags(flags
);
675 static int mpu_cmd(int dev
, int cmd
, int data
)
679 static mpu_command_rec rec
;
681 rec
.cmd
= cmd
& 0xff;
682 rec
.nr_args
= ((cmd
& 0xf0) == 0xE0);
683 rec
.nr_returns
= ((cmd
& 0xf0) == 0xA0);
684 rec
.data
[0] = data
& 0xff;
686 if ((ret
= mpu401_command(dev
, &rec
)) < 0)
688 return (unsigned char) rec
.data
[0];
691 static int mpu401_prefix_cmd(int dev
, unsigned char status
)
693 struct mpu_config
*devc
= &dev_conf
[dev
];
700 if (mpu_cmd(dev
, 0xD0, 0) < 0)
707 if (mpu_cmd(dev
, 0xDF, 0) < 0)
716 static int mpu401_start_read(int dev
)
721 static int mpu401_end_read(int dev
)
726 static int mpu401_ioctl(int dev
, unsigned cmd
, caddr_t arg
)
728 struct mpu_config
*devc
;
732 devc
= &dev_conf
[dev
];
735 case SNDCTL_MIDI_MPUMODE
:
736 if (!(devc
->capabilities
& MPU_CAP_INTLG
)) { /* No intelligent mode */
737 printk(KERN_WARNING
"mpu401: Intelligent mode not supported by the HW\n");
740 if (get_user(val
, (int *)arg
))
742 set_uart_mode(dev
, devc
, !val
);
745 case SNDCTL_MIDI_MPUCMD
:
746 if (copy_from_user(&rec
, arg
, sizeof(rec
)))
748 if ((ret
= mpu401_command(dev
, &rec
)) < 0)
750 if (copy_to_user(arg
, &rec
, sizeof(rec
)))
759 static void mpu401_kick(int dev
)
763 static int mpu401_buffer_status(int dev
)
770 static int mpu_synth_ioctl(int dev
,
771 unsigned int cmd
, caddr_t arg
)
774 struct mpu_config
*devc
;
776 midi_dev
= synth_devs
[dev
]->midi_dev
;
778 if (midi_dev
< 0 || midi_dev
> num_midis
|| midi_devs
[midi_dev
] == NULL
)
781 devc
= &dev_conf
[midi_dev
];
786 case SNDCTL_SYNTH_INFO
:
787 memcpy((&((char *) arg
)[0]), (char *) &mpu_synth_info
[midi_dev
], sizeof(struct synth_info
));
790 case SNDCTL_SYNTH_MEMAVL
:
798 static int mpu_synth_open(int dev
, int mode
)
801 struct mpu_config
*devc
;
803 midi_dev
= synth_devs
[dev
]->midi_dev
;
805 if (midi_dev
< 0 || midi_dev
> num_midis
|| midi_devs
[midi_dev
] == NULL
)
808 devc
= &dev_conf
[midi_dev
];
811 * Verify that the device is really running.
812 * Some devices (such as Ensoniq SoundScape don't
813 * work before the on board processor (OBP) is initialized
814 * by downloading its microcode.
817 if (!devc
->initialized
)
819 if (mpu401_status(devc
) == 0xff) /* Bus float */
821 printk(KERN_ERR
"mpu401: Device not initialized properly\n");
828 devc
->mode
= MODE_SYNTH
;
831 devc
->inputintr
= NULL
;
833 if (midi_devs
[midi_dev
]->coproc
)
834 if ((err
= midi_devs
[midi_dev
]->coproc
->
835 open(midi_devs
[midi_dev
]->coproc
->devc
, COPR_MIDI
)) < 0)
837 printk(KERN_WARNING
"mpu401: Can't access coprocessor device\n");
843 if (mode
& OPEN_READ
)
845 mpu_cmd(midi_dev
, 0x8B, 0); /* Enable data in stop mode */
846 mpu_cmd(midi_dev
, 0x34, 0); /* Return timing bytes in stop mode */
847 mpu_cmd(midi_dev
, 0x87, 0); /* Enable pitch & controller */
852 static void mpu_synth_close(int dev
)
855 struct mpu_config
*devc
;
857 midi_dev
= synth_devs
[dev
]->midi_dev
;
859 devc
= &dev_conf
[midi_dev
];
860 mpu_cmd(midi_dev
, 0x15, 0); /* Stop recording, playback and MIDI */
861 mpu_cmd(midi_dev
, 0x8a, 0); /* Disable data in stopped mode */
863 devc
->inputintr
= NULL
;
865 if (midi_devs
[midi_dev
]->coproc
)
866 midi_devs
[midi_dev
]->coproc
->close(midi_devs
[midi_dev
]->coproc
->devc
, COPR_MIDI
);
871 #define MIDI_SYNTH_NAME "MPU-401 UART Midi"
872 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
873 #include "midi_synth.h"
875 static struct synth_operations mpu401_synth_proto
=
885 midi_synth_kill_note
,
886 midi_synth_start_note
,
887 midi_synth_set_instr
,
889 midi_synth_hw_control
,
890 midi_synth_load_patch
,
891 midi_synth_aftertouch
,
892 midi_synth_controller
,
897 midi_synth_setup_voice
,
898 midi_synth_send_sysex
901 static struct synth_operations
*mpu401_synth_operations
[MAX_MIDI_DEV
];
903 static struct midi_operations mpu401_midi_proto
=
905 {"MPU-401 Midi", 0, MIDI_CAP_MPU401
, SNDCARD_MPU401
},
916 mpu401_buffer_status
,
920 static struct midi_operations mpu401_midi_operations
[MAX_MIDI_DEV
];
922 static void mpu401_chk_version(int n
, struct mpu_config
*devc
)
927 devc
->version
= devc
->revision
= 0;
931 if ((tmp
= mpu_cmd(n
, 0xAC, 0)) < 0)
933 restore_flags(flags
);
936 if ((tmp
& 0xf0) > 0x20) /* Why it's larger than 2.x ??? */
938 restore_flags(flags
);
943 if ((tmp
= mpu_cmd(n
, 0xAD, 0)) < 0)
946 restore_flags(flags
);
949 devc
->revision
= tmp
;
950 restore_flags(flags
);
953 void attach_mpu401(struct address_info
*hw_config
)
959 struct mpu_config
*devc
;
961 hw_config
->slots
[1] = -1;
962 m
= sound_alloc_mididev();
965 printk(KERN_WARNING
"MPU-401: Too many midi devices detected\n");
969 devc
->base
= hw_config
->io_base
;
970 devc
->osp
= hw_config
->osp
;
971 devc
->irq
= hw_config
->irq
;
974 devc
->initialized
= 0;
977 devc
->capabilities
= 0;
978 devc
->timer_flag
= 0;
980 devc
->m_state
= ST_INIT
;
981 devc
->shared_irq
= hw_config
->always_detect
;
982 devc
->irq
= hw_config
->irq
;
987 devc
->shared_irq
= 1;
990 if (!hw_config
->always_detect
)
992 /* Verify the hardware again */
993 if (!reset_mpu401(devc
))
995 printk(KERN_WARNING
"mpu401: Device didn't respond\n");
996 sound_unload_mididev(m
);
999 if (!devc
->shared_irq
)
1001 if (request_irq(devc
->irq
, mpuintr
, 0, "mpu401", (void *)m
) < 0)
1003 printk(KERN_WARNING
"mpu401: Failed to allocate IRQ%d\n", devc
->irq
);
1004 sound_unload_mididev(m
);
1010 mpu401_chk_version(m
, devc
);
1011 if (devc
->version
== 0)
1012 mpu401_chk_version(m
, devc
);
1013 restore_flags(flags
);
1015 request_region(hw_config
->io_base
, 2, "mpu401");
1017 if (devc
->version
!= 0)
1018 if (mpu_cmd(m
, 0xC5, 0) >= 0) /* Set timebase OK */
1019 if (mpu_cmd(m
, 0xE0, 120) >= 0) /* Set tempo OK */
1020 devc
->capabilities
|= MPU_CAP_INTLG
; /* Supports intelligent mode */
1023 mpu401_synth_operations
[m
] = (struct synth_operations
*)kmalloc(sizeof(struct synth_operations
), GFP_KERNEL
);
1025 if (mpu401_synth_operations
[m
] == NULL
)
1027 sound_unload_mididev(m
);
1028 printk(KERN_ERR
"mpu401: Can't allocate memory\n");
1031 if (!(devc
->capabilities
& MPU_CAP_INTLG
)) /* No intelligent mode */
1033 memcpy((char *) mpu401_synth_operations
[m
],
1034 (char *) &std_midi_synth
,
1035 sizeof(struct synth_operations
));
1039 memcpy((char *) mpu401_synth_operations
[m
],
1040 (char *) &mpu401_synth_proto
,
1041 sizeof(struct synth_operations
));
1044 memcpy((char *) &mpu401_midi_operations
[m
],
1045 (char *) &mpu401_midi_proto
,
1046 sizeof(struct midi_operations
));
1048 mpu401_midi_operations
[m
].converter
= mpu401_synth_operations
[m
];
1050 memcpy((char *) &mpu_synth_info
[m
],
1051 (char *) &mpu_synth_info_proto
,
1052 sizeof(struct synth_info
));
1056 if (devc
->version
== 0x20 && devc
->revision
>= 0x07) /* MusicQuest interface */
1058 int ports
= (devc
->revision
& 0x08) ? 32 : 16;
1060 devc
->capabilities
|= MPU_CAP_SYNC
| MPU_CAP_SMPTE
|
1061 MPU_CAP_CLS
| MPU_CAP_2PORT
;
1063 revision_char
= (devc
->revision
== 0x7f) ? 'M' : ' ';
1064 sprintf(mpu_synth_info
[m
].name
, "MQX-%d%c MIDI Interface #%d",
1071 revision_char
= devc
->revision
? devc
->revision
+ '@' : ' ';
1072 if ((int) devc
->revision
> ('Z' - '@'))
1073 revision_char
= '+';
1075 devc
->capabilities
|= MPU_CAP_SYNC
| MPU_CAP_FSK
;
1077 if (hw_config
->name
)
1078 sprintf(mpu_synth_info
[m
].name
, "%s (MPU401)", hw_config
->name
);
1080 sprintf(mpu_synth_info
[m
].name
,
1081 "MPU-401 %d.%d%c Midi interface #%d",
1082 (int) (devc
->version
& 0xf0) >> 4,
1083 devc
->version
& 0x0f,
1088 strcpy(mpu401_midi_operations
[m
].info
.name
,
1089 mpu_synth_info
[m
].name
);
1091 conf_printf(mpu_synth_info
[m
].name
, hw_config
);
1093 mpu401_synth_operations
[m
]->midi_dev
= devc
->devno
= m
;
1094 mpu401_synth_operations
[devc
->devno
]->info
= &mpu_synth_info
[devc
->devno
];
1096 if (devc
->capabilities
& MPU_CAP_INTLG
) /* Intelligent mode */
1097 hw_config
->slots
[2] = mpu_timer_init(m
);
1099 midi_devs
[m
] = &mpu401_midi_operations
[devc
->devno
];
1100 hw_config
->slots
[1] = m
;
1104 static int reset_mpu401(struct mpu_config
*devc
)
1106 unsigned long flags
;
1111 * Send the RESET command. Try again if no success at the first time.
1112 * (If the device is in the UART mode, it will not ack the reset cmd).
1117 timeout_limit
= devc
->initialized
? 30000 : 100000;
1118 devc
->initialized
= 1;
1120 for (n
= 0; n
< 2 && !ok
; n
++)
1122 for (timeout
= timeout_limit
; timeout
> 0 && !ok
; timeout
--)
1123 ok
= output_ready(devc
);
1125 write_command(devc
, MPU_RESET
); /*
1126 * Send MPU-401 RESET Command
1130 * Wait at least 25 msec. This method is not accurate so let's make the
1131 * loop bit longer. Cannot sleep since this is called during boot.
1134 for (timeout
= timeout_limit
* 2; timeout
> 0 && !ok
; timeout
--)
1138 if (input_avail(devc
))
1139 if (read_data(devc
) == MPU_ACK
)
1141 restore_flags(flags
);
1146 devc
->m_state
= ST_INIT
;
1149 devc
->last_status
= 0;
1150 devc
->uart_mode
= 0;
1155 static void set_uart_mode(int dev
, struct mpu_config
*devc
, int arg
)
1157 if (!arg
&& (devc
->capabilities
& MPU_CAP_INTLG
))
1159 if ((devc
->uart_mode
== 0) == (arg
== 0))
1160 return; /* Already set */
1161 reset_mpu401(devc
); /* This exits the uart mode */
1165 if (mpu_cmd(dev
, UART_MODE_ON
, 0) < 0)
1167 printk(KERN_ERR
"mpu401: Can't enter UART mode\n");
1168 devc
->uart_mode
= 0;
1172 devc
->uart_mode
= arg
;
1176 int probe_mpu401(struct address_info
*hw_config
)
1179 struct mpu_config tmp_devc
;
1181 if (check_region(hw_config
->io_base
, 2))
1183 printk(KERN_ERR
"mpu401: I/O port %x already in use\n\n", hw_config
->io_base
);
1186 tmp_devc
.base
= hw_config
->io_base
;
1187 tmp_devc
.irq
= hw_config
->irq
;
1188 tmp_devc
.initialized
= 0;
1189 tmp_devc
.opened
= 0;
1190 tmp_devc
.osp
= hw_config
->osp
;
1192 if (hw_config
->always_detect
)
1195 if (inb(hw_config
->io_base
+ 1) == 0xff)
1197 DDB(printk("MPU401: Port %x looks dead.\n", hw_config
->io_base
));
1198 return 0; /* Just bus float? */
1200 ok
= reset_mpu401(&tmp_devc
);
1204 DDB(printk("MPU401: Reset failed on port %x\n", hw_config
->io_base
));
1209 void unload_mpu401(struct address_info
*hw_config
)
1212 int n
=hw_config
->slots
[1];
1214 release_region(hw_config
->io_base
, 2);
1215 if (hw_config
->always_detect
== 0 && hw_config
->irq
> 0)
1216 free_irq(hw_config
->irq
, (void *)n
);
1217 p
=mpu401_synth_operations
[n
];
1218 sound_unload_mididev(n
);
1219 sound_unload_timerdev(hw_config
->slots
[2]);
1224 /*****************************************************
1226 ****************************************************/
1228 #if defined(CONFIG_SEQUENCER)
1230 static volatile int timer_initialized
= 0, timer_open
= 0, tmr_running
= 0;
1231 static volatile int curr_tempo
, curr_timebase
, hw_timebase
;
1232 static int max_timebase
= 8; /* 8*24=192 ppqn */
1233 static volatile unsigned long next_event_time
;
1234 static volatile unsigned long curr_ticks
, curr_clocks
;
1235 static unsigned long prev_event_time
;
1236 static int metronome_mode
;
1238 static unsigned long clocks2ticks(unsigned long clocks
)
1241 * The MPU-401 supports just a limited set of possible timebase values.
1242 * Since the applications require more choices, the driver has to
1243 * program the HW to do its best and to convert between the HW and
1246 return ((clocks
* curr_timebase
) + (hw_timebase
/ 2)) / hw_timebase
;
1249 static void set_timebase(int midi_dev
, int val
)
1259 hw_val
= (hw_val
+ 12) / 24;
1260 if (hw_val
> max_timebase
)
1261 hw_val
= max_timebase
;
1263 if (mpu_cmd(midi_dev
, 0xC0 | (hw_val
& 0x0f), 0) < 0)
1265 printk(KERN_WARNING
"mpu401: Can't set HW timebase to %d\n", hw_val
* 24);
1268 hw_timebase
= hw_val
* 24;
1269 curr_timebase
= val
;
1273 static void tmr_reset(void)
1275 unsigned long flags
;
1279 next_event_time
= (unsigned long) -1;
1280 prev_event_time
= 0;
1281 curr_ticks
= curr_clocks
= 0;
1282 restore_flags(flags
);
1285 static void set_timer_mode(int midi_dev
)
1287 if (timer_mode
& TMR_MODE_CLS
)
1288 mpu_cmd(midi_dev
, 0x3c, 0); /* Use CLS sync */
1289 else if (timer_mode
& TMR_MODE_SMPTE
)
1290 mpu_cmd(midi_dev
, 0x3d, 0); /* Use SMPTE sync */
1292 if (timer_mode
& TMR_INTERNAL
)
1294 mpu_cmd(midi_dev
, 0x80, 0); /* Use MIDI sync */
1298 if (timer_mode
& (TMR_MODE_MIDI
| TMR_MODE_CLS
))
1300 mpu_cmd(midi_dev
, 0x82, 0); /* Use MIDI sync */
1301 mpu_cmd(midi_dev
, 0x91, 0); /* Enable ext MIDI ctrl */
1303 else if (timer_mode
& TMR_MODE_FSK
)
1304 mpu_cmd(midi_dev
, 0x81, 0); /* Use FSK sync */
1308 static void stop_metronome(int midi_dev
)
1310 mpu_cmd(midi_dev
, 0x84, 0); /* Disable metronome */
1313 static void setup_metronome(int midi_dev
)
1315 int numerator
, denominator
;
1316 int clks_per_click
, num_32nds_per_beat
;
1317 int beats_per_measure
;
1319 numerator
= ((unsigned) metronome_mode
>> 24) & 0xff;
1320 denominator
= ((unsigned) metronome_mode
>> 16) & 0xff;
1321 clks_per_click
= ((unsigned) metronome_mode
>> 8) & 0xff;
1322 num_32nds_per_beat
= (unsigned) metronome_mode
& 0xff;
1323 beats_per_measure
= (numerator
* 4) >> denominator
;
1325 if (!metronome_mode
)
1326 mpu_cmd(midi_dev
, 0x84, 0); /* Disable metronome */
1329 mpu_cmd(midi_dev
, 0xE4, clks_per_click
);
1330 mpu_cmd(midi_dev
, 0xE6, beats_per_measure
);
1331 mpu_cmd(midi_dev
, 0x83, 0); /* Enable metronome without accents */
1335 static int mpu_start_timer(int midi_dev
)
1338 set_timer_mode(midi_dev
);
1341 return TIMER_NOT_ARMED
; /* Already running */
1343 if (timer_mode
& TMR_INTERNAL
)
1345 mpu_cmd(midi_dev
, 0x02, 0); /* Send MIDI start */
1347 return TIMER_NOT_ARMED
;
1351 mpu_cmd(midi_dev
, 0x35, 0); /* Enable mode messages to PC */
1352 mpu_cmd(midi_dev
, 0x38, 0); /* Enable sys common messages to PC */
1353 mpu_cmd(midi_dev
, 0x39, 0); /* Enable real time messages to PC */
1354 mpu_cmd(midi_dev
, 0x97, 0); /* Enable system exclusive messages to PC */
1359 static int mpu_timer_open(int dev
, int mode
)
1361 int midi_dev
= sound_timer_devs
[dev
]->devlink
;
1368 mpu_cmd(midi_dev
, 0xE0, 50);
1369 curr_timebase
= hw_timebase
= 120;
1370 set_timebase(midi_dev
, 120);
1373 set_timer_mode(midi_dev
);
1375 mpu_cmd(midi_dev
, 0xe7, 0x04); /* Send all clocks to host */
1376 mpu_cmd(midi_dev
, 0x95, 0); /* Enable clock to host */
1381 static void mpu_timer_close(int dev
)
1383 int midi_dev
= sound_timer_devs
[dev
]->devlink
;
1385 timer_open
= tmr_running
= 0;
1386 mpu_cmd(midi_dev
, 0x15, 0); /* Stop all */
1387 mpu_cmd(midi_dev
, 0x94, 0); /* Disable clock to host */
1388 mpu_cmd(midi_dev
, 0x8c, 0); /* Disable measure end messages to host */
1389 stop_metronome(midi_dev
);
1392 static int mpu_timer_event(int dev
, unsigned char *event
)
1394 unsigned char command
= event
[1];
1395 unsigned long parm
= *(unsigned int *) &event
[4];
1396 int midi_dev
= sound_timer_devs
[dev
]->devlink
;
1401 parm
+= prev_event_time
;
1407 if (parm
<= curr_ticks
) /* It's the time */
1408 return TIMER_NOT_ARMED
;
1410 next_event_time
= prev_event_time
= time
;
1419 return mpu_start_timer(midi_dev
);
1422 mpu_cmd(midi_dev
, 0x01, 0); /* Send MIDI stop */
1423 stop_metronome(midi_dev
);
1430 mpu_cmd(midi_dev
, 0x03, 0); /* Send MIDI continue */
1431 setup_metronome(midi_dev
);
1442 if (mpu_cmd(midi_dev
, 0xE0, parm
) < 0)
1443 printk(KERN_WARNING
"mpu401: Can't set tempo to %d\n", (int) parm
);
1449 seq_copy_to_input(event
, 8);
1453 if (metronome_mode
) /* Metronome enabled */
1455 metronome_mode
= parm
;
1456 setup_metronome(midi_dev
);
1462 return TIMER_NOT_ARMED
;
1465 static unsigned long mpu_timer_get_time(int dev
)
1473 static int mpu_timer_ioctl(int dev
, unsigned int command
, caddr_t arg
)
1475 int midi_dev
= sound_timer_devs
[dev
]->devlink
;
1479 case SNDCTL_TMR_SOURCE
:
1483 parm
= *(int *) arg
;
1490 if (timer_mode
& TMR_MODE_CLS
)
1491 mpu_cmd(midi_dev
, 0x3c, 0); /* Use CLS sync */
1492 else if (timer_mode
& TMR_MODE_SMPTE
)
1493 mpu_cmd(midi_dev
, 0x3d, 0); /* Use SMPTE sync */
1495 return (*(int *) arg
= timer_mode
);
1499 case SNDCTL_TMR_START
:
1500 mpu_start_timer(midi_dev
);
1503 case SNDCTL_TMR_STOP
:
1505 mpu_cmd(midi_dev
, 0x01, 0); /* Send MIDI stop */
1506 stop_metronome(midi_dev
);
1509 case SNDCTL_TMR_CONTINUE
:
1513 mpu_cmd(midi_dev
, 0x03, 0); /* Send MIDI continue */
1516 case SNDCTL_TMR_TIMEBASE
:
1522 set_timebase(midi_dev
, val
);
1523 return (*(int *) arg
= curr_timebase
);
1527 case SNDCTL_TMR_TEMPO
:
1540 if ((ret
= mpu_cmd(midi_dev
, 0xE0, val
)) < 0)
1542 printk(KERN_WARNING
"mpu401: Can't set tempo to %d\n", (int) val
);
1547 return (*(int *) arg
= curr_tempo
);
1551 case SNDCTL_SEQ_CTRLRATE
:
1556 if (val
!= 0) /* Can't change */
1558 return (*(int *) arg
= ((curr_tempo
* curr_timebase
) + 30) / 60);
1562 case SNDCTL_SEQ_GETTIME
:
1563 return (*(int *) arg
= curr_ticks
);
1565 case SNDCTL_TMR_METRONOME
:
1566 metronome_mode
= *(int *) arg
;
1567 setup_metronome(midi_dev
);
1575 static void mpu_timer_arm(int dev
, long time
)
1578 time
= curr_ticks
+ 1;
1579 else if (time
<= curr_ticks
) /* It's the time */
1581 next_event_time
= prev_event_time
= time
;
1585 static struct sound_timer_operations mpu_timer
=
1587 {"MPU-401 Timer", 0},
1589 0, /* Local device link */
1598 static void mpu_timer_interrupt(void)
1607 curr_ticks
= clocks2ticks(curr_clocks
);
1609 if (curr_ticks
>= next_event_time
)
1611 next_event_time
= (unsigned long) -1;
1616 static void timer_ext_event(struct mpu_config
*devc
, int event
, int parm
)
1618 int midi_dev
= devc
->devno
;
1620 if (!devc
->timer_flag
)
1626 printk("<MIDI clk>");
1630 printk("Ext MIDI start\n");
1633 if (timer_mode
& TMR_EXTERNAL
)
1636 setup_metronome(midi_dev
);
1637 next_event_time
= 0;
1638 STORE(SEQ_START_TIMER());
1644 printk("Ext MIDI stop\n");
1645 if (timer_mode
& TMR_EXTERNAL
)
1648 stop_metronome(midi_dev
);
1649 STORE(SEQ_STOP_TIMER());
1654 printk("Ext MIDI continue\n");
1655 if (timer_mode
& TMR_EXTERNAL
)
1658 setup_metronome(midi_dev
);
1659 STORE(SEQ_CONTINUE_TIMER());
1664 printk("Songpos: %d\n", parm
);
1665 if (timer_mode
& TMR_EXTERNAL
)
1667 STORE(SEQ_SONGPOS(parm
));
1673 static int mpu_timer_init(int midi_dev
)
1675 struct mpu_config
*devc
;
1678 devc
= &dev_conf
[midi_dev
];
1680 if (timer_initialized
)
1681 return -1; /* There is already a similar timer */
1683 timer_initialized
= 1;
1685 mpu_timer
.devlink
= midi_dev
;
1686 dev_conf
[midi_dev
].timer_flag
= 1;
1688 n
= sound_alloc_timerdev();
1691 sound_timer_devs
[n
] = &mpu_timer
;
1693 if (devc
->version
< 0x20) /* Original MPU-401 */
1694 timer_caps
= TMR_INTERNAL
| TMR_EXTERNAL
| TMR_MODE_FSK
| TMR_MODE_MIDI
;
1698 * The version number 2.0 is used (at least) by the
1699 * MusicQuest cards and the Roland Super-MPU.
1701 * MusicQuest has given a special meaning to the bits of the
1702 * revision number. The Super-MPU returns 0.
1706 timer_caps
|= TMR_EXTERNAL
| TMR_MODE_MIDI
;
1708 if (devc
->revision
& 0x02)
1709 timer_caps
|= TMR_MODE_CLS
;
1712 if (devc
->revision
& 0x40)
1713 max_timebase
= 10; /* Has the 216 and 240 ppqn modes */
1716 timer_mode
= (TMR_INTERNAL
| TMR_MODE_MIDI
) & timer_caps
;
1724 EXPORT_SYMBOL(probe_mpu401
);
1725 EXPORT_SYMBOL(attach_mpu401
);
1726 EXPORT_SYMBOL(unload_mpu401
);
1727 EXPORT_SYMBOL(intchk_mpu401
);
1728 EXPORT_SYMBOL(mpuintr
);
1732 MODULE_PARM(irq
, "i");
1733 MODULE_PARM(io
, "i");
1737 struct address_info hw
;
1739 int init_module(void)
1741 /* Can be loaded either for module use or to provide functions
1743 if (io
!= -1 && irq
!= -1)
1747 if (probe_mpu401(&hw
) == 0)
1755 void cleanup_module(void)
1757 if (io
!= -1 && irq
!= -1)