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
16 * Bartlomiej Zolnierkiewicz removed some __init to allow using many drivers
17 * Chris Rankin Update the module-usage counter for the coprocessor
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/spinlock.h>
23 #define USE_SEQ_MACROS
24 #define USE_SIMPLE_MACROS
26 #include "sound_config.h"
31 static int timer_mode
= TMR_INTERNAL
, timer_caps
= TMR_INTERNAL
;
49 unsigned char version
, revision
;
50 unsigned int capabilities
;
51 #define MPU_CAP_INTLG 0x10000000
52 #define MPU_CAP_SYNC 0x00000010
53 #define MPU_CAP_FSK 0x00000020
54 #define MPU_CAP_CLS 0x00000040
55 #define MPU_CAP_SMPTE 0x00000080
56 #define MPU_CAP_2PORT 0x00000001
60 #define BUFTEST(dc) if (dc->m_ptr >= MBUF_MAX || dc->m_ptr < 0) \
61 {printk( "MPU: Invalid buffer pointer %d/%d, s=%d\n", dc->m_ptr, dc->m_left, dc->m_state);dc->m_ptr--;}
63 unsigned char m_buf
[MBUF_MAX
];
67 unsigned char last_status
;
68 void (*inputintr
) (int dev
, unsigned char data
);
74 #define DATAPORT(base) (base)
75 #define COMDPORT(base) (base+1)
76 #define STATPORT(base) (base+1)
79 static void mpu401_close(int dev
);
81 static int mpu401_status(struct mpu_config
*devc
)
83 return inb(STATPORT(devc
->base
));
86 #define input_avail(devc) (!(mpu401_status(devc)&INPUT_AVAIL))
87 #define output_ready(devc) (!(mpu401_status(devc)&OUTPUT_READY))
89 static void write_command(struct mpu_config
*devc
, unsigned char cmd
)
91 outb(cmd
, COMDPORT(devc
->base
));
94 static int read_data(struct mpu_config
*devc
)
96 return inb(DATAPORT(devc
->base
));
99 static void write_data(struct mpu_config
*devc
, unsigned char byte
)
101 outb(byte
, DATAPORT(devc
->base
));
104 #define OUTPUT_READY 0x40
105 #define INPUT_AVAIL 0x80
107 #define MPU_RESET 0xFF
108 #define UART_MODE_ON 0x3F
110 static struct mpu_config dev_conf
[MAX_MIDI_DEV
] =
115 static int n_mpu_devs
= 0;
117 static int reset_mpu401(struct mpu_config
*devc
);
118 static void set_uart_mode(int dev
, struct mpu_config
*devc
, int arg
);
120 static int mpu_timer_init(int midi_dev
);
121 static void mpu_timer_interrupt(void);
122 static void timer_ext_event(struct mpu_config
*devc
, int event
, int parm
);
124 static struct synth_info mpu_synth_info_proto
= {
125 "MPU-401 MIDI interface",
134 static struct synth_info mpu_synth_info
[MAX_MIDI_DEV
];
137 * States for the input scanner
140 #define ST_INIT 0 /* Ready for timing byte or msg */
141 #define ST_TIMED 1 /* Leading timing byte rcvd */
142 #define ST_DATABYTE 2 /* Waiting for (nr_left) data bytes */
144 #define ST_SYSMSG 100 /* System message (sysx etc). */
145 #define ST_SYSEX 101 /* System exclusive msg */
146 #define ST_MTC 102 /* Midi Time Code (MTC) qframe msg */
147 #define ST_SONGSEL 103 /* Song select */
148 #define ST_SONGPOS 104 /* Song position pointer */
150 static unsigned char len_tab
[] = /* # of data bytes following a status
166 unsigned char obuf[8]; \
168 seq_input_event(obuf, len); \
173 #define _SEQ_ADVBUF(x) len=x
175 static int mpu_input_scanner(struct mpu_config
*devc
, unsigned char midic
)
178 switch (devc
->m_state
)
192 if (devc
->timer_flag
)
193 mpu_timer_interrupt();
207 printk("<Trk data rq #%d>", midic
& 0x0f);
211 printk("<conductor rq>");
215 devc
->m_state
= ST_SYSMSG
;
221 /* printk( "mpu time: %d ", midic); */
222 devc
->m_state
= ST_TIMED
;
225 printk("<MPU: Unknown event %02x> ", midic
);
231 int msg
= ((int) (midic
& 0xf0) >> 4);
233 devc
->m_state
= ST_DATABYTE
;
235 if (msg
< 8) /* Data byte */
237 /* printk( "midi msg (running status) "); */
238 msg
= ((int) (devc
->last_status
& 0xf0) >> 4);
240 devc
->m_left
= len_tab
[msg
] - 1;
243 devc
->m_buf
[0] = devc
->last_status
;
244 devc
->m_buf
[1] = midic
;
246 if (devc
->m_left
<= 0)
248 devc
->m_state
= ST_INIT
;
249 do_midi_msg(devc
->synthno
, devc
->m_buf
, devc
->m_ptr
);
253 else if (msg
== 0xf) /* MPU MARK */
255 devc
->m_state
= ST_INIT
;
260 /* printk( "NOP "); */
264 /* printk( "meas end "); */
268 /* printk( "data end "); */
272 printk("Unknown MPU mark %02x\n", midic
);
277 devc
->last_status
= midic
;
278 /* printk( "midi msg "); */
280 devc
->m_left
= len_tab
[msg
];
283 devc
->m_buf
[0] = midic
;
285 if (devc
->m_left
<= 0)
287 devc
->m_state
= ST_INIT
;
288 do_midi_msg(devc
->synthno
, devc
->m_buf
, devc
->m_ptr
);
300 devc
->m_state
= ST_SYSEX
;
304 devc
->m_state
= ST_MTC
;
308 devc
->m_state
= ST_SONGPOS
;
313 devc
->m_state
= ST_SONGSEL
;
317 /* printk( "tune_request\n"); */
318 devc
->m_state
= ST_INIT
;
325 devc
->m_state
= ST_INIT
;
326 timer_ext_event(devc
, TMR_CLOCK
, 0);
330 devc
->m_state
= ST_INIT
;
331 timer_ext_event(devc
, TMR_START
, 0);
335 devc
->m_state
= ST_INIT
;
336 timer_ext_event(devc
, TMR_CONTINUE
, 0);
340 devc
->m_state
= ST_INIT
;
341 timer_ext_event(devc
, TMR_STOP
, 0);
346 devc
->m_state
= ST_INIT
;
350 /* printk( "midi hard reset"); */
351 devc
->m_state
= ST_INIT
;
355 printk("unknown MIDI sysmsg %0x\n", midic
);
356 devc
->m_state
= ST_INIT
;
361 devc
->m_state
= ST_INIT
;
362 printk("MTC frame %x02\n", midic
);
369 devc
->m_state
= ST_INIT
;
372 printk("%02x ", midic
);
377 devc
->m_buf
[devc
->m_ptr
++] = midic
;
378 if (devc
->m_ptr
== 2)
380 devc
->m_state
= ST_INIT
;
382 timer_ext_event(devc
, TMR_SPP
,
383 ((devc
->m_buf
[1] & 0x7f) << 7) |
384 (devc
->m_buf
[0] & 0x7f));
390 devc
->m_buf
[devc
->m_ptr
++] = midic
;
391 if ((--devc
->m_left
) <= 0)
393 devc
->m_state
= ST_INIT
;
394 do_midi_msg(devc
->synthno
, devc
->m_buf
, devc
->m_ptr
);
400 printk("Bad state %d ", devc
->m_state
);
401 devc
->m_state
= ST_INIT
;
406 static void mpu401_input_loop(struct mpu_config
*devc
)
412 spin_lock_irqsave(&devc
->lock
,flags
);
415 spin_unlock_irqrestore(&devc
->lock
,flags
);
417 if (busy
) /* Already inside the scanner */
422 while (input_avail(devc
) && n
-- > 0)
424 unsigned char c
= read_data(devc
);
426 if (devc
->mode
== MODE_SYNTH
)
428 mpu_input_scanner(devc
, c
);
430 else if (devc
->opened
& OPEN_READ
&& devc
->inputintr
!= NULL
)
431 devc
->inputintr(devc
->devno
, c
);
436 int intchk_mpu401(void *dev_id
)
438 struct mpu_config
*devc
;
439 int dev
= (int) dev_id
;
441 devc
= &dev_conf
[dev
];
442 return input_avail(devc
);
445 void mpuintr(int irq
, void *dev_id
, struct pt_regs
*dummy
)
447 struct mpu_config
*devc
;
448 int dev
= (int) dev_id
;
450 devc
= &dev_conf
[dev
];
452 if (input_avail(devc
))
454 if (devc
->base
!= 0 && (devc
->opened
& OPEN_READ
|| devc
->mode
== MODE_SYNTH
))
455 mpu401_input_loop(devc
);
458 /* Dummy read (just to acknowledge the interrupt) */
464 static int mpu401_open(int dev
, int mode
,
465 void (*input
) (int dev
, unsigned char data
),
466 void (*output
) (int dev
)
470 struct mpu_config
*devc
;
471 struct coproc_operations
*coprocessor
;
473 if (dev
< 0 || dev
>= num_midis
|| midi_devs
[dev
] == NULL
)
476 devc
= &dev_conf
[dev
];
481 * Verify that the device is really running.
482 * Some devices (such as Ensoniq SoundScape don't
483 * work before the on board processor (OBP) is initialized
484 * by downloading its microcode.
487 if (!devc
->initialized
)
489 if (mpu401_status(devc
) == 0xff) /* Bus float */
491 printk(KERN_ERR
"mpu401: Device not initialized properly\n");
497 if ( (coprocessor
= midi_devs
[dev
]->coproc
) != NULL
)
499 if (coprocessor
->owner
)
500 __MOD_INC_USE_COUNT(coprocessor
->owner
);
502 if ((err
= coprocessor
->open(coprocessor
->devc
, COPR_MIDI
)) < 0)
504 printk(KERN_WARNING
"MPU-401: Can't access coprocessor device\n");
510 set_uart_mode(dev
, devc
, 1);
511 devc
->mode
= MODE_MIDI
;
514 mpu401_input_loop(devc
);
516 devc
->inputintr
= input
;
522 static void mpu401_close(int dev
)
524 struct mpu_config
*devc
;
525 struct coproc_operations
*coprocessor
;
527 devc
= &dev_conf
[dev
];
529 reset_mpu401(devc
); /*
530 * This disables the UART mode
533 devc
->inputintr
= NULL
;
535 coprocessor
= midi_devs
[dev
]->coproc
;
537 coprocessor
->close(coprocessor
->devc
, COPR_MIDI
);
539 if (coprocessor
->owner
)
540 __MOD_DEC_USE_COUNT(coprocessor
->owner
);
545 static int mpu401_out(int dev
, unsigned char midi_byte
)
550 struct mpu_config
*devc
;
552 devc
= &dev_conf
[dev
];
555 * Sometimes it takes about 30000 loops before the output becomes ready
556 * (After reset). Normally it takes just about 10 loops.
559 for (timeout
= 30000; timeout
> 0 && !output_ready(devc
); timeout
--);
561 spin_lock_irqsave(&devc
->lock
,flags
);
562 if (!output_ready(devc
))
564 printk(KERN_WARNING
"mpu401: Send data timeout\n");
565 spin_unlock_irqrestore(&devc
->lock
,flags
);
568 write_data(devc
, midi_byte
);
569 spin_unlock_irqrestore(&devc
->lock
,flags
);
573 static int mpu401_command(int dev
, mpu_command_rec
* cmd
)
578 struct mpu_config
*devc
;
580 devc
= &dev_conf
[dev
];
582 if (devc
->uart_mode
) /*
583 * Not possible in UART mode
586 printk(KERN_WARNING
"mpu401: commands not possible in the UART mode\n");
590 * Test for input since pending input seems to block the output.
592 if (input_avail(devc
))
593 mpu401_input_loop(devc
);
596 * Sometimes it takes about 50000 loops before the output becomes ready
597 * (After reset). Normally it takes just about 10 loops.
604 printk(KERN_WARNING
"mpu401: Command (0x%x) timeout\n", (int) cmd
->cmd
);
607 spin_lock_irqsave(&devc
->lock
,flags
);
609 if (!output_ready(devc
))
611 spin_unlock_irqrestore(&devc
->lock
,flags
);
614 write_command(devc
, cmd
->cmd
);
617 for (timeout
= 50000; timeout
> 0 && !ok
; timeout
--)
619 if (input_avail(devc
))
621 if (devc
->opened
&& devc
->mode
== MODE_SYNTH
)
623 if (mpu_input_scanner(devc
, read_data(devc
)) == MPU_ACK
)
628 /* Device is not currently open. Use simpler method */
629 if (read_data(devc
) == MPU_ACK
)
636 spin_unlock_irqrestore(&devc
->lock
,flags
);
641 for (i
= 0; i
< cmd
->nr_args
; i
++)
643 for (timeout
= 3000; timeout
> 0 && !output_ready(devc
); timeout
--);
645 if (!mpu401_out(dev
, cmd
->data
[i
]))
647 spin_unlock_irqrestore(&devc
->lock
,flags
);
648 printk(KERN_WARNING
"mpu401: Command (0x%x), parm send failed.\n", (int) cmd
->cmd
);
658 for (i
= 0; i
< cmd
->nr_returns
; i
++)
661 for (timeout
= 5000; timeout
> 0 && !ok
; timeout
--)
662 if (input_avail(devc
))
664 cmd
->data
[i
] = read_data(devc
);
669 spin_unlock_irqrestore(&devc
->lock
,flags
);
674 spin_unlock_irqrestore(&devc
->lock
,flags
);
678 static int mpu_cmd(int dev
, int cmd
, int data
)
682 static mpu_command_rec rec
;
684 rec
.cmd
= cmd
& 0xff;
685 rec
.nr_args
= ((cmd
& 0xf0) == 0xE0);
686 rec
.nr_returns
= ((cmd
& 0xf0) == 0xA0);
687 rec
.data
[0] = data
& 0xff;
689 if ((ret
= mpu401_command(dev
, &rec
)) < 0)
691 return (unsigned char) rec
.data
[0];
694 static int mpu401_prefix_cmd(int dev
, unsigned char status
)
696 struct mpu_config
*devc
= &dev_conf
[dev
];
703 if (mpu_cmd(dev
, 0xD0, 0) < 0)
710 if (mpu_cmd(dev
, 0xDF, 0) < 0)
719 static int mpu401_start_read(int dev
)
724 static int mpu401_end_read(int dev
)
729 static int mpu401_ioctl(int dev
, unsigned cmd
, caddr_t arg
)
731 struct mpu_config
*devc
;
735 devc
= &dev_conf
[dev
];
738 case SNDCTL_MIDI_MPUMODE
:
739 if (!(devc
->capabilities
& MPU_CAP_INTLG
)) { /* No intelligent mode */
740 printk(KERN_WARNING
"mpu401: Intelligent mode not supported by the HW\n");
743 if (get_user(val
, (int *)arg
))
745 set_uart_mode(dev
, devc
, !val
);
748 case SNDCTL_MIDI_MPUCMD
:
749 if (copy_from_user(&rec
, arg
, sizeof(rec
)))
751 if ((ret
= mpu401_command(dev
, &rec
)) < 0)
753 if (copy_to_user(arg
, &rec
, sizeof(rec
)))
762 static void mpu401_kick(int dev
)
766 static int mpu401_buffer_status(int dev
)
773 static int mpu_synth_ioctl(int dev
,
774 unsigned int cmd
, caddr_t arg
)
777 struct mpu_config
*devc
;
779 midi_dev
= synth_devs
[dev
]->midi_dev
;
781 if (midi_dev
< 0 || midi_dev
> num_midis
|| midi_devs
[midi_dev
] == NULL
)
784 devc
= &dev_conf
[midi_dev
];
789 case SNDCTL_SYNTH_INFO
:
790 memcpy((&((char *) arg
)[0]), (char *) &mpu_synth_info
[midi_dev
], sizeof(struct synth_info
));
793 case SNDCTL_SYNTH_MEMAVL
:
801 static int mpu_synth_open(int dev
, int mode
)
804 struct mpu_config
*devc
;
805 struct coproc_operations
*coprocessor
;
807 midi_dev
= synth_devs
[dev
]->midi_dev
;
809 if (midi_dev
< 0 || midi_dev
> num_midis
|| midi_devs
[midi_dev
] == NULL
)
812 devc
= &dev_conf
[midi_dev
];
815 * Verify that the device is really running.
816 * Some devices (such as Ensoniq SoundScape don't
817 * work before the on board processor (OBP) is initialized
818 * by downloading its microcode.
821 if (!devc
->initialized
)
823 if (mpu401_status(devc
) == 0xff) /* Bus float */
825 printk(KERN_ERR
"mpu401: Device not initialized properly\n");
832 devc
->mode
= MODE_SYNTH
;
835 devc
->inputintr
= NULL
;
837 coprocessor
= midi_devs
[midi_dev
]->coproc
;
839 if (coprocessor
->owner
)
840 __MOD_INC_USE_COUNT(coprocessor
->owner
);
842 if ((err
= coprocessor
->open(coprocessor
->devc
, COPR_MIDI
)) < 0)
844 printk(KERN_WARNING
"mpu401: Can't access coprocessor device\n");
851 if (mode
& OPEN_READ
)
853 mpu_cmd(midi_dev
, 0x8B, 0); /* Enable data in stop mode */
854 mpu_cmd(midi_dev
, 0x34, 0); /* Return timing bytes in stop mode */
855 mpu_cmd(midi_dev
, 0x87, 0); /* Enable pitch & controller */
860 static void mpu_synth_close(int dev
)
863 struct mpu_config
*devc
;
864 struct coproc_operations
*coprocessor
;
866 midi_dev
= synth_devs
[dev
]->midi_dev
;
868 devc
= &dev_conf
[midi_dev
];
869 mpu_cmd(midi_dev
, 0x15, 0); /* Stop recording, playback and MIDI */
870 mpu_cmd(midi_dev
, 0x8a, 0); /* Disable data in stopped mode */
872 devc
->inputintr
= NULL
;
874 coprocessor
= midi_devs
[midi_dev
]->coproc
;
876 coprocessor
->close(coprocessor
->devc
, COPR_MIDI
);
878 if (coprocessor
->owner
)
879 __MOD_DEC_USE_COUNT(coprocessor
->owner
);
885 #define MIDI_SYNTH_NAME "MPU-401 UART Midi"
886 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
887 #include "midi_synth.h"
889 static struct synth_operations mpu401_synth_proto
=
895 synth_type
: SYNTH_TYPE_MIDI
,
897 open
: mpu_synth_open
,
898 close
: mpu_synth_close
,
899 ioctl
: mpu_synth_ioctl
,
900 kill_note
: midi_synth_kill_note
,
901 start_note
: midi_synth_start_note
,
902 set_instr
: midi_synth_set_instr
,
903 reset
: midi_synth_reset
,
904 hw_control
: midi_synth_hw_control
,
905 load_patch
: midi_synth_load_patch
,
906 aftertouch
: midi_synth_aftertouch
,
907 controller
: midi_synth_controller
,
908 panning
: midi_synth_panning
,
909 bender
: midi_synth_bender
,
910 setup_voice
: midi_synth_setup_voice
,
911 send_sysex
: midi_synth_send_sysex
914 static struct synth_operations
*mpu401_synth_operations
[MAX_MIDI_DEV
];
916 static struct midi_operations mpu401_midi_proto
=
919 info
: {"MPU-401 Midi", 0, MIDI_CAP_MPU401
, SNDCARD_MPU401
},
925 start_read
: mpu401_start_read
,
926 end_read
: mpu401_end_read
,
928 buffer_status
: mpu401_buffer_status
,
929 prefix_cmd
: mpu401_prefix_cmd
932 static struct midi_operations mpu401_midi_operations
[MAX_MIDI_DEV
];
934 static void mpu401_chk_version(int n
, struct mpu_config
*devc
)
939 devc
->version
= devc
->revision
= 0;
941 spin_lock_irqsave(&devc
->lock
,flags
);
942 if ((tmp
= mpu_cmd(n
, 0xAC, 0)) < 0)
944 spin_unlock_irqrestore(&devc
->lock
,flags
);
947 if ((tmp
& 0xf0) > 0x20) /* Why it's larger than 2.x ??? */
949 spin_unlock_irqrestore(&devc
->lock
,flags
);
954 if ((tmp
= mpu_cmd(n
, 0xAD, 0)) < 0)
957 spin_unlock_irqrestore(&devc
->lock
,flags
);
960 devc
->revision
= tmp
;
961 spin_unlock_irqrestore(&devc
->lock
,flags
);
964 void attach_mpu401(struct address_info
*hw_config
, struct module
*owner
)
970 struct mpu_config
*devc
;
972 hw_config
->slots
[1] = -1;
973 m
= sound_alloc_mididev();
976 printk(KERN_WARNING
"MPU-401: Too many midi devices detected\n");
980 devc
->base
= hw_config
->io_base
;
981 devc
->osp
= hw_config
->osp
;
982 devc
->irq
= hw_config
->irq
;
985 devc
->initialized
= 0;
988 devc
->capabilities
= 0;
989 devc
->timer_flag
= 0;
991 devc
->m_state
= ST_INIT
;
992 devc
->shared_irq
= hw_config
->always_detect
;
993 devc
->irq
= hw_config
->irq
;
994 spin_lock_init(&devc
->lock
);
999 devc
->shared_irq
= 1;
1002 if (!hw_config
->always_detect
)
1004 /* Verify the hardware again */
1005 if (!reset_mpu401(devc
))
1007 printk(KERN_WARNING
"mpu401: Device didn't respond\n");
1008 sound_unload_mididev(m
);
1011 if (!devc
->shared_irq
)
1013 if (request_irq(devc
->irq
, mpuintr
, 0, "mpu401", (void *)m
) < 0)
1015 printk(KERN_WARNING
"mpu401: Failed to allocate IRQ%d\n", devc
->irq
);
1016 sound_unload_mididev(m
);
1020 spin_lock_irqsave(&devc
->lock
,flags
);
1021 mpu401_chk_version(m
, devc
);
1022 if (devc
->version
== 0)
1023 mpu401_chk_version(m
, devc
);
1024 spin_unlock_irqrestore(&devc
->lock
,flags
);
1026 request_region(hw_config
->io_base
, 2, "mpu401");
1028 if (devc
->version
!= 0)
1029 if (mpu_cmd(m
, 0xC5, 0) >= 0) /* Set timebase OK */
1030 if (mpu_cmd(m
, 0xE0, 120) >= 0) /* Set tempo OK */
1031 devc
->capabilities
|= MPU_CAP_INTLG
; /* Supports intelligent mode */
1034 mpu401_synth_operations
[m
] = (struct synth_operations
*)kmalloc(sizeof(struct synth_operations
), GFP_KERNEL
);
1036 if (mpu401_synth_operations
[m
] == NULL
)
1038 sound_unload_mididev(m
);
1039 printk(KERN_ERR
"mpu401: Can't allocate memory\n");
1042 if (!(devc
->capabilities
& MPU_CAP_INTLG
)) /* No intelligent mode */
1044 memcpy((char *) mpu401_synth_operations
[m
],
1045 (char *) &std_midi_synth
,
1046 sizeof(struct synth_operations
));
1050 memcpy((char *) mpu401_synth_operations
[m
],
1051 (char *) &mpu401_synth_proto
,
1052 sizeof(struct synth_operations
));
1055 mpu401_synth_operations
[m
]->owner
= owner
;
1057 memcpy((char *) &mpu401_midi_operations
[m
],
1058 (char *) &mpu401_midi_proto
,
1059 sizeof(struct midi_operations
));
1061 mpu401_midi_operations
[m
].converter
= mpu401_synth_operations
[m
];
1063 memcpy((char *) &mpu_synth_info
[m
],
1064 (char *) &mpu_synth_info_proto
,
1065 sizeof(struct synth_info
));
1069 if (devc
->version
== 0x20 && devc
->revision
>= 0x07) /* MusicQuest interface */
1071 int ports
= (devc
->revision
& 0x08) ? 32 : 16;
1073 devc
->capabilities
|= MPU_CAP_SYNC
| MPU_CAP_SMPTE
|
1074 MPU_CAP_CLS
| MPU_CAP_2PORT
;
1076 revision_char
= (devc
->revision
== 0x7f) ? 'M' : ' ';
1077 sprintf(mpu_synth_info
[m
].name
, "MQX-%d%c MIDI Interface #%d",
1084 revision_char
= devc
->revision
? devc
->revision
+ '@' : ' ';
1085 if ((int) devc
->revision
> ('Z' - '@'))
1086 revision_char
= '+';
1088 devc
->capabilities
|= MPU_CAP_SYNC
| MPU_CAP_FSK
;
1090 if (hw_config
->name
)
1091 sprintf(mpu_synth_info
[m
].name
, "%s (MPU401)", hw_config
->name
);
1093 sprintf(mpu_synth_info
[m
].name
,
1094 "MPU-401 %d.%d%c Midi interface #%d",
1095 (int) (devc
->version
& 0xf0) >> 4,
1096 devc
->version
& 0x0f,
1101 strcpy(mpu401_midi_operations
[m
].info
.name
,
1102 mpu_synth_info
[m
].name
);
1104 conf_printf(mpu_synth_info
[m
].name
, hw_config
);
1106 mpu401_synth_operations
[m
]->midi_dev
= devc
->devno
= m
;
1107 mpu401_synth_operations
[devc
->devno
]->info
= &mpu_synth_info
[devc
->devno
];
1109 if (devc
->capabilities
& MPU_CAP_INTLG
) /* Intelligent mode */
1110 hw_config
->slots
[2] = mpu_timer_init(m
);
1112 midi_devs
[m
] = &mpu401_midi_operations
[devc
->devno
];
1115 midi_devs
[m
]->owner
= owner
;
1117 hw_config
->slots
[1] = m
;
1121 static int reset_mpu401(struct mpu_config
*devc
)
1123 unsigned long flags
;
1128 * Send the RESET command. Try again if no success at the first time.
1129 * (If the device is in the UART mode, it will not ack the reset cmd).
1134 timeout_limit
= devc
->initialized
? 30000 : 100000;
1135 devc
->initialized
= 1;
1137 for (n
= 0; n
< 2 && !ok
; n
++)
1139 for (timeout
= timeout_limit
; timeout
> 0 && !ok
; timeout
--)
1140 ok
= output_ready(devc
);
1142 write_command(devc
, MPU_RESET
); /*
1143 * Send MPU-401 RESET Command
1147 * Wait at least 25 msec. This method is not accurate so let's make the
1148 * loop bit longer. Cannot sleep since this is called during boot.
1151 for (timeout
= timeout_limit
* 2; timeout
> 0 && !ok
; timeout
--)
1153 spin_lock_irqsave(&devc
->lock
,flags
);
1154 if (input_avail(devc
))
1155 if (read_data(devc
) == MPU_ACK
)
1157 spin_unlock_irqrestore(&devc
->lock
,flags
);
1162 devc
->m_state
= ST_INIT
;
1165 devc
->last_status
= 0;
1166 devc
->uart_mode
= 0;
1171 static void set_uart_mode(int dev
, struct mpu_config
*devc
, int arg
)
1173 if (!arg
&& (devc
->capabilities
& MPU_CAP_INTLG
))
1175 if ((devc
->uart_mode
== 0) == (arg
== 0))
1176 return; /* Already set */
1177 reset_mpu401(devc
); /* This exits the uart mode */
1181 if (mpu_cmd(dev
, UART_MODE_ON
, 0) < 0)
1183 printk(KERN_ERR
"mpu401: Can't enter UART mode\n");
1184 devc
->uart_mode
= 0;
1188 devc
->uart_mode
= arg
;
1192 int probe_mpu401(struct address_info
*hw_config
)
1195 struct mpu_config tmp_devc
;
1197 if (check_region(hw_config
->io_base
, 2))
1199 printk(KERN_ERR
"mpu401: I/O port %x already in use\n\n", hw_config
->io_base
);
1202 tmp_devc
.base
= hw_config
->io_base
;
1203 tmp_devc
.irq
= hw_config
->irq
;
1204 tmp_devc
.initialized
= 0;
1205 tmp_devc
.opened
= 0;
1206 tmp_devc
.osp
= hw_config
->osp
;
1208 if (hw_config
->always_detect
)
1211 if (inb(hw_config
->io_base
+ 1) == 0xff)
1213 DDB(printk("MPU401: Port %x looks dead.\n", hw_config
->io_base
));
1214 return 0; /* Just bus float? */
1216 ok
= reset_mpu401(&tmp_devc
);
1220 DDB(printk("MPU401: Reset failed on port %x\n", hw_config
->io_base
));
1225 void unload_mpu401(struct address_info
*hw_config
)
1228 int n
=hw_config
->slots
[1];
1230 release_region(hw_config
->io_base
, 2);
1231 if (hw_config
->always_detect
== 0 && hw_config
->irq
> 0)
1232 free_irq(hw_config
->irq
, (void *)n
);
1233 p
=mpu401_synth_operations
[n
];
1234 sound_unload_mididev(n
);
1235 sound_unload_timerdev(hw_config
->slots
[2]);
1240 /*****************************************************
1242 ****************************************************/
1244 static volatile int timer_initialized
= 0, timer_open
= 0, tmr_running
= 0;
1245 static volatile int curr_tempo
, curr_timebase
, hw_timebase
;
1246 static int max_timebase
= 8; /* 8*24=192 ppqn */
1247 static volatile unsigned long next_event_time
;
1248 static volatile unsigned long curr_ticks
, curr_clocks
;
1249 static unsigned long prev_event_time
;
1250 static int metronome_mode
;
1252 static unsigned long clocks2ticks(unsigned long clocks
)
1255 * The MPU-401 supports just a limited set of possible timebase values.
1256 * Since the applications require more choices, the driver has to
1257 * program the HW to do its best and to convert between the HW and
1260 return ((clocks
* curr_timebase
) + (hw_timebase
/ 2)) / hw_timebase
;
1263 static void set_timebase(int midi_dev
, int val
)
1273 hw_val
= (hw_val
+ 12) / 24;
1274 if (hw_val
> max_timebase
)
1275 hw_val
= max_timebase
;
1277 if (mpu_cmd(midi_dev
, 0xC0 | (hw_val
& 0x0f), 0) < 0)
1279 printk(KERN_WARNING
"mpu401: Can't set HW timebase to %d\n", hw_val
* 24);
1282 hw_timebase
= hw_val
* 24;
1283 curr_timebase
= val
;
1287 static void tmr_reset(struct mpu_config
*devc
)
1289 unsigned long flags
;
1291 spin_lock_irqsave(&devc
->lock
,flags
);
1292 next_event_time
= (unsigned long) -1;
1293 prev_event_time
= 0;
1294 curr_ticks
= curr_clocks
= 0;
1295 spin_unlock_irqrestore(&devc
->lock
,flags
);
1298 static void set_timer_mode(int midi_dev
)
1300 if (timer_mode
& TMR_MODE_CLS
)
1301 mpu_cmd(midi_dev
, 0x3c, 0); /* Use CLS sync */
1302 else if (timer_mode
& TMR_MODE_SMPTE
)
1303 mpu_cmd(midi_dev
, 0x3d, 0); /* Use SMPTE sync */
1305 if (timer_mode
& TMR_INTERNAL
)
1307 mpu_cmd(midi_dev
, 0x80, 0); /* Use MIDI sync */
1311 if (timer_mode
& (TMR_MODE_MIDI
| TMR_MODE_CLS
))
1313 mpu_cmd(midi_dev
, 0x82, 0); /* Use MIDI sync */
1314 mpu_cmd(midi_dev
, 0x91, 0); /* Enable ext MIDI ctrl */
1316 else if (timer_mode
& TMR_MODE_FSK
)
1317 mpu_cmd(midi_dev
, 0x81, 0); /* Use FSK sync */
1321 static void stop_metronome(int midi_dev
)
1323 mpu_cmd(midi_dev
, 0x84, 0); /* Disable metronome */
1326 static void setup_metronome(int midi_dev
)
1328 int numerator
, denominator
;
1329 int clks_per_click
, num_32nds_per_beat
;
1330 int beats_per_measure
;
1332 numerator
= ((unsigned) metronome_mode
>> 24) & 0xff;
1333 denominator
= ((unsigned) metronome_mode
>> 16) & 0xff;
1334 clks_per_click
= ((unsigned) metronome_mode
>> 8) & 0xff;
1335 num_32nds_per_beat
= (unsigned) metronome_mode
& 0xff;
1336 beats_per_measure
= (numerator
* 4) >> denominator
;
1338 if (!metronome_mode
)
1339 mpu_cmd(midi_dev
, 0x84, 0); /* Disable metronome */
1342 mpu_cmd(midi_dev
, 0xE4, clks_per_click
);
1343 mpu_cmd(midi_dev
, 0xE6, beats_per_measure
);
1344 mpu_cmd(midi_dev
, 0x83, 0); /* Enable metronome without accents */
1348 static int mpu_start_timer(int midi_dev
)
1350 struct mpu_config
*devc
= &dev_conf
[midi_dev
];
1353 set_timer_mode(midi_dev
);
1356 return TIMER_NOT_ARMED
; /* Already running */
1358 if (timer_mode
& TMR_INTERNAL
)
1360 mpu_cmd(midi_dev
, 0x02, 0); /* Send MIDI start */
1362 return TIMER_NOT_ARMED
;
1366 mpu_cmd(midi_dev
, 0x35, 0); /* Enable mode messages to PC */
1367 mpu_cmd(midi_dev
, 0x38, 0); /* Enable sys common messages to PC */
1368 mpu_cmd(midi_dev
, 0x39, 0); /* Enable real time messages to PC */
1369 mpu_cmd(midi_dev
, 0x97, 0); /* Enable system exclusive messages to PC */
1374 static int mpu_timer_open(int dev
, int mode
)
1376 int midi_dev
= sound_timer_devs
[dev
]->devlink
;
1377 struct mpu_config
*devc
= &dev_conf
[midi_dev
];
1384 mpu_cmd(midi_dev
, 0xE0, 50);
1385 curr_timebase
= hw_timebase
= 120;
1386 set_timebase(midi_dev
, 120);
1389 set_timer_mode(midi_dev
);
1391 mpu_cmd(midi_dev
, 0xe7, 0x04); /* Send all clocks to host */
1392 mpu_cmd(midi_dev
, 0x95, 0); /* Enable clock to host */
1397 static void mpu_timer_close(int dev
)
1399 int midi_dev
= sound_timer_devs
[dev
]->devlink
;
1401 timer_open
= tmr_running
= 0;
1402 mpu_cmd(midi_dev
, 0x15, 0); /* Stop all */
1403 mpu_cmd(midi_dev
, 0x94, 0); /* Disable clock to host */
1404 mpu_cmd(midi_dev
, 0x8c, 0); /* Disable measure end messages to host */
1405 stop_metronome(midi_dev
);
1408 static int mpu_timer_event(int dev
, unsigned char *event
)
1410 unsigned char command
= event
[1];
1411 unsigned long parm
= *(unsigned int *) &event
[4];
1412 int midi_dev
= sound_timer_devs
[dev
]->devlink
;
1417 parm
+= prev_event_time
;
1423 if (parm
<= curr_ticks
) /* It's the time */
1424 return TIMER_NOT_ARMED
;
1426 next_event_time
= prev_event_time
= time
;
1435 return mpu_start_timer(midi_dev
);
1438 mpu_cmd(midi_dev
, 0x01, 0); /* Send MIDI stop */
1439 stop_metronome(midi_dev
);
1446 mpu_cmd(midi_dev
, 0x03, 0); /* Send MIDI continue */
1447 setup_metronome(midi_dev
);
1458 if (mpu_cmd(midi_dev
, 0xE0, parm
) < 0)
1459 printk(KERN_WARNING
"mpu401: Can't set tempo to %d\n", (int) parm
);
1465 seq_copy_to_input(event
, 8);
1469 if (metronome_mode
) /* Metronome enabled */
1471 metronome_mode
= parm
;
1472 setup_metronome(midi_dev
);
1478 return TIMER_NOT_ARMED
;
1481 static unsigned long mpu_timer_get_time(int dev
)
1489 static int mpu_timer_ioctl(int dev
, unsigned int command
, caddr_t arg
)
1491 int midi_dev
= sound_timer_devs
[dev
]->devlink
;
1495 case SNDCTL_TMR_SOURCE
:
1499 parm
= *(int *) arg
;
1506 if (timer_mode
& TMR_MODE_CLS
)
1507 mpu_cmd(midi_dev
, 0x3c, 0); /* Use CLS sync */
1508 else if (timer_mode
& TMR_MODE_SMPTE
)
1509 mpu_cmd(midi_dev
, 0x3d, 0); /* Use SMPTE sync */
1511 return (*(int *) arg
= timer_mode
);
1515 case SNDCTL_TMR_START
:
1516 mpu_start_timer(midi_dev
);
1519 case SNDCTL_TMR_STOP
:
1521 mpu_cmd(midi_dev
, 0x01, 0); /* Send MIDI stop */
1522 stop_metronome(midi_dev
);
1525 case SNDCTL_TMR_CONTINUE
:
1529 mpu_cmd(midi_dev
, 0x03, 0); /* Send MIDI continue */
1532 case SNDCTL_TMR_TIMEBASE
:
1538 set_timebase(midi_dev
, val
);
1539 return (*(int *) arg
= curr_timebase
);
1543 case SNDCTL_TMR_TEMPO
:
1556 if ((ret
= mpu_cmd(midi_dev
, 0xE0, val
)) < 0)
1558 printk(KERN_WARNING
"mpu401: Can't set tempo to %d\n", (int) val
);
1563 return (*(int *) arg
= curr_tempo
);
1567 case SNDCTL_SEQ_CTRLRATE
:
1572 if (val
!= 0) /* Can't change */
1574 return (*(int *) arg
= ((curr_tempo
* curr_timebase
) + 30) / 60);
1578 case SNDCTL_SEQ_GETTIME
:
1579 return (*(int *) arg
= curr_ticks
);
1581 case SNDCTL_TMR_METRONOME
:
1582 metronome_mode
= *(int *) arg
;
1583 setup_metronome(midi_dev
);
1591 static void mpu_timer_arm(int dev
, long time
)
1594 time
= curr_ticks
+ 1;
1595 else if (time
<= curr_ticks
) /* It's the time */
1597 next_event_time
= prev_event_time
= time
;
1601 static struct sound_timer_operations mpu_timer
=
1604 info
: {"MPU-401 Timer", 0},
1605 priority
: 10, /* Priority */
1606 devlink
: 0, /* Local device link */
1607 open
: mpu_timer_open
,
1608 close
: mpu_timer_close
,
1609 event
: mpu_timer_event
,
1610 get_time
: mpu_timer_get_time
,
1611 ioctl
: mpu_timer_ioctl
,
1612 arm_timer
: mpu_timer_arm
1615 static void mpu_timer_interrupt(void)
1624 curr_ticks
= clocks2ticks(curr_clocks
);
1626 if (curr_ticks
>= next_event_time
)
1628 next_event_time
= (unsigned long) -1;
1633 static void timer_ext_event(struct mpu_config
*devc
, int event
, int parm
)
1635 int midi_dev
= devc
->devno
;
1637 if (!devc
->timer_flag
)
1643 printk("<MIDI clk>");
1647 printk("Ext MIDI start\n");
1650 if (timer_mode
& TMR_EXTERNAL
)
1653 setup_metronome(midi_dev
);
1654 next_event_time
= 0;
1655 STORE(SEQ_START_TIMER());
1661 printk("Ext MIDI stop\n");
1662 if (timer_mode
& TMR_EXTERNAL
)
1665 stop_metronome(midi_dev
);
1666 STORE(SEQ_STOP_TIMER());
1671 printk("Ext MIDI continue\n");
1672 if (timer_mode
& TMR_EXTERNAL
)
1675 setup_metronome(midi_dev
);
1676 STORE(SEQ_CONTINUE_TIMER());
1681 printk("Songpos: %d\n", parm
);
1682 if (timer_mode
& TMR_EXTERNAL
)
1684 STORE(SEQ_SONGPOS(parm
));
1690 static int mpu_timer_init(int midi_dev
)
1692 struct mpu_config
*devc
;
1695 devc
= &dev_conf
[midi_dev
];
1697 if (timer_initialized
)
1698 return -1; /* There is already a similar timer */
1700 timer_initialized
= 1;
1702 mpu_timer
.devlink
= midi_dev
;
1703 dev_conf
[midi_dev
].timer_flag
= 1;
1705 n
= sound_alloc_timerdev();
1708 sound_timer_devs
[n
] = &mpu_timer
;
1710 if (devc
->version
< 0x20) /* Original MPU-401 */
1711 timer_caps
= TMR_INTERNAL
| TMR_EXTERNAL
| TMR_MODE_FSK
| TMR_MODE_MIDI
;
1715 * The version number 2.0 is used (at least) by the
1716 * MusicQuest cards and the Roland Super-MPU.
1718 * MusicQuest has given a special meaning to the bits of the
1719 * revision number. The Super-MPU returns 0.
1723 timer_caps
|= TMR_EXTERNAL
| TMR_MODE_MIDI
;
1725 if (devc
->revision
& 0x02)
1726 timer_caps
|= TMR_MODE_CLS
;
1729 if (devc
->revision
& 0x40)
1730 max_timebase
= 10; /* Has the 216 and 240 ppqn modes */
1733 timer_mode
= (TMR_INTERNAL
| TMR_MODE_MIDI
) & timer_caps
;
1738 EXPORT_SYMBOL(probe_mpu401
);
1739 EXPORT_SYMBOL(attach_mpu401
);
1740 EXPORT_SYMBOL(unload_mpu401
);
1741 EXPORT_SYMBOL(intchk_mpu401
);
1742 EXPORT_SYMBOL(mpuintr
);
1744 static struct address_info cfg
;
1746 static int __initdata io
= -1;
1747 static int __initdata irq
= -1;
1749 MODULE_PARM(irq
, "i");
1750 MODULE_PARM(io
, "i");
1752 int __init
init_mpu401(void)
1754 /* Can be loaded either for module use or to provide functions
1756 if (io
!= -1 && irq
!= -1) {
1759 if (probe_mpu401(&cfg
) == 0)
1761 attach_mpu401(&cfg
, THIS_MODULE
);
1767 void __exit
cleanup_mpu401(void)
1769 if (io
!= -1 && irq
!= -1) {
1770 /* Check for use by, for example, sscape driver */
1771 unload_mpu401(&cfg
);
1775 module_init(init_mpu401
);
1776 module_exit(cleanup_mpu401
);
1779 static int __init
setup_mpu401(char *str
)
1784 str
= get_options(str
, ARRAY_SIZE(ints
), ints
);
1792 __setup("mpu401=", setup_mpu401
);
1794 MODULE_LICENSE("GPL");