Import 2.3.18pre1
[davej-history.git] / drivers / sound / mpu401.c
blob17089b0adad407669cf7ceb5c754690b439a97c5
1 /*
2 * sound/mpu401.c
4 * The low level driver for Roland MPU-401 compatible Midi cards.
5 */
6 /*
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
11 * for more info.
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)
28 #include "coproc.h"
31 #ifdef CONFIG_SEQUENCER
32 static int timer_mode = TMR_INTERNAL, timer_caps = TMR_INTERNAL;
34 #endif
36 struct mpu_config
38 int base; /*
39 * I/O base
41 int irq;
42 int opened; /*
43 * Open mode
45 int devno;
46 int synthno;
47 int uart_mode;
48 int initialized;
49 int mode;
50 #define MODE_MIDI 1
51 #define MODE_SYNTH 2
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
60 int timer_flag;
62 #define MBUF_MAX 10
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--;}
65 int m_busy;
66 unsigned char m_buf[MBUF_MAX];
67 int m_ptr;
68 int m_state;
69 int m_left;
70 unsigned char last_status;
71 void (*inputintr) (int dev, unsigned char data);
72 int shared_irq;
73 int *osp;
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
105 #define MPU_ACK 0xFE
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",
126 SYNTH_TYPE_MIDI,
127 MIDI_TYPE_MPU401,
128 0, 128,
129 0, 128,
130 SYNTH_CAP_INPUT
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
152 2, /* 8x */
153 2, /* 9x */
154 2, /* Ax */
155 2, /* Bx */
156 1, /* Cx */
157 1, /* Dx */
158 2, /* Ex */
159 0 /* Fx */
162 #ifndef CONFIG_SEQUENCER
163 #define STORE(cmd)
164 #else
165 #define STORE(cmd) \
167 int len; \
168 unsigned char obuf[8]; \
169 cmd; \
170 seq_input_event(obuf, len); \
172 #endif
174 #define _seqbuf obuf
175 #define _seqbufptr 0
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)
183 case ST_INIT:
184 switch (midic)
186 case 0xf8:
187 /* Timer overflow */
188 break;
190 case 0xfc:
191 printk("<all end>");
192 break;
194 case 0xfd:
195 if (devc->timer_flag)
196 mpu_timer_interrupt();
197 break;
199 case 0xfe:
200 return MPU_ACK;
202 case 0xf0:
203 case 0xf1:
204 case 0xf2:
205 case 0xf3:
206 case 0xf4:
207 case 0xf5:
208 case 0xf6:
209 case 0xf7:
210 printk("<Trk data rq #%d>", midic & 0x0f);
211 break;
213 case 0xf9:
214 printk("<conductor rq>");
215 break;
217 case 0xff:
218 devc->m_state = ST_SYSMSG;
219 break;
221 default:
222 if (midic <= 0xef)
224 /* printk( "mpu time: %d ", midic); */
225 devc->m_state = ST_TIMED;
227 else
228 printk("<MPU: Unknown event %02x> ", midic);
230 break;
232 case ST_TIMED:
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);
242 msg -= 8;
243 devc->m_left = len_tab[msg] - 1;
245 devc->m_ptr = 2;
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);
253 devc->m_ptr = 0;
256 else if (msg == 0xf) /* MPU MARK */
258 devc->m_state = ST_INIT;
260 switch (midic)
262 case 0xf8:
263 /* printk( "NOP "); */
264 break;
266 case 0xf9:
267 /* printk( "meas end "); */
268 break;
270 case 0xfc:
271 /* printk( "data end "); */
272 break;
274 default:
275 printk("Unknown MPU mark %02x\n", midic);
278 else
280 devc->last_status = midic;
281 /* printk( "midi msg "); */
282 msg -= 8;
283 devc->m_left = len_tab[msg];
285 devc->m_ptr = 1;
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);
292 devc->m_ptr = 0;
296 break;
298 case ST_SYSMSG:
299 switch (midic)
301 case 0xf0:
302 printk("<SYX>");
303 devc->m_state = ST_SYSEX;
304 break;
306 case 0xf1:
307 devc->m_state = ST_MTC;
308 break;
310 case 0xf2:
311 devc->m_state = ST_SONGPOS;
312 devc->m_ptr = 0;
313 break;
315 case 0xf3:
316 devc->m_state = ST_SONGSEL;
317 break;
319 case 0xf6:
320 /* printk( "tune_request\n"); */
321 devc->m_state = ST_INIT;
324 * Real time messages
326 case 0xf8:
327 /* midi clock */
328 devc->m_state = ST_INIT;
329 timer_ext_event(devc, TMR_CLOCK, 0);
330 break;
332 case 0xfA:
333 devc->m_state = ST_INIT;
334 timer_ext_event(devc, TMR_START, 0);
335 break;
337 case 0xFB:
338 devc->m_state = ST_INIT;
339 timer_ext_event(devc, TMR_CONTINUE, 0);
340 break;
342 case 0xFC:
343 devc->m_state = ST_INIT;
344 timer_ext_event(devc, TMR_STOP, 0);
345 break;
347 case 0xFE:
348 /* active sensing */
349 devc->m_state = ST_INIT;
350 break;
352 case 0xff:
353 /* printk( "midi hard reset"); */
354 devc->m_state = ST_INIT;
355 break;
357 default:
358 printk("unknown MIDI sysmsg %0x\n", midic);
359 devc->m_state = ST_INIT;
361 break;
363 case ST_MTC:
364 devc->m_state = ST_INIT;
365 printk("MTC frame %x02\n", midic);
366 break;
368 case ST_SYSEX:
369 if (midic == 0xf7)
371 printk("<EOX>");
372 devc->m_state = ST_INIT;
374 else
375 printk("%02x ", midic);
376 break;
378 case ST_SONGPOS:
379 BUFTEST(devc);
380 devc->m_buf[devc->m_ptr++] = midic;
381 if (devc->m_ptr == 2)
383 devc->m_state = ST_INIT;
384 devc->m_ptr = 0;
385 timer_ext_event(devc, TMR_SPP,
386 ((devc->m_buf[1] & 0x7f) << 7) |
387 (devc->m_buf[0] & 0x7f));
389 break;
391 case ST_DATABYTE:
392 BUFTEST(devc);
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);
398 devc->m_ptr = 0;
400 break;
402 default:
403 printk("Bad state %d ", devc->m_state);
404 devc->m_state = ST_INIT;
406 return 1;
409 static void mpu401_input_loop(struct mpu_config *devc)
411 unsigned long flags;
412 int busy;
413 int n;
415 save_flags(flags);
416 cli();
417 busy = devc->m_busy;
418 devc->m_busy = 1;
419 restore_flags(flags);
421 if (busy) /* Already inside the scanner */
422 return;
424 n = 50;
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);
437 devc->m_busy = 0;
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;
454 sti();
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);
461 else
463 /* Dummy read (just to acknowledge the interrupt) */
464 read_data(devc);
469 static int mpu401_open(int dev, int mode,
470 void (*input) (int dev, unsigned char data),
471 void (*output) (int dev)
474 int err;
475 struct mpu_config *devc;
477 if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
478 return -ENXIO;
480 devc = &dev_conf[dev];
482 if (devc->opened)
483 return -EBUSY;
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");
496 return -EIO;
498 reset_mpu401(devc);
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");
507 return err;
511 set_uart_mode(dev, devc, 1);
512 devc->mode = MODE_MIDI;
513 devc->synthno = 0;
515 mpu401_input_loop(devc);
517 devc->inputintr = input;
518 devc->opened = mode;
520 return 0;
523 static void mpu401_close(int dev)
525 struct mpu_config *devc;
527 devc = &dev_conf[dev];
528 if (devc->uart_mode)
529 reset_mpu401(devc); /*
530 * This disables the UART mode
532 devc->mode = 0;
533 devc->inputintr = NULL;
535 if (midi_devs[dev]->coproc)
536 midi_devs[dev]->coproc->close(midi_devs[dev]->coproc->devc, COPR_MIDI);
537 devc->opened = 0;
540 static int mpu401_out(int dev, unsigned char midi_byte)
542 int timeout;
543 unsigned long flags;
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--);
556 save_flags(flags);
557 cli();
558 if (!output_ready(devc))
560 printk(KERN_WARNING "mpu401: Send data timeout\n");
561 restore_flags(flags);
562 return 0;
564 write_data(devc, midi_byte);
565 restore_flags(flags);
566 return 1;
569 static int mpu401_command(int dev, mpu_command_rec * cmd)
571 int i, timeout, ok;
572 int ret = 0;
573 unsigned long flags;
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");
583 return -EINVAL;
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.
596 timeout = 50000;
597 retry:
598 if (timeout-- <= 0)
600 printk(KERN_WARNING "mpu401: Command (0x%x) timeout\n", (int) cmd->cmd);
601 return -EIO;
603 save_flags(flags);
604 cli();
606 if (!output_ready(devc))
608 restore_flags(flags);
609 goto retry;
611 write_command(devc, cmd->cmd);
613 ok = 0;
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)
621 ok = 1;
623 else
625 /* Device is not currently open. Use simpler method */
626 if (read_data(devc) == MPU_ACK)
627 ok = 1;
631 if (!ok)
633 restore_flags(flags);
634 return -EIO;
636 if (cmd->nr_args)
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);
646 return -EIO;
650 ret = 0;
651 cmd->data[0] = 0;
653 if (cmd->nr_returns)
655 for (i = 0; i < cmd->nr_returns; i++)
657 ok = 0;
658 for (timeout = 5000; timeout > 0 && !ok; timeout--)
659 if (input_avail(devc))
661 cmd->data[i] = read_data(devc);
662 ok = 1;
664 if (!ok)
666 restore_flags(flags);
667 return -EIO;
671 restore_flags(flags);
672 return ret;
675 static int mpu_cmd(int dev, int cmd, int data)
677 int ret;
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)
687 return ret;
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];
695 if (devc->uart_mode)
696 return 1;
698 if (status < 0xf0)
700 if (mpu_cmd(dev, 0xD0, 0) < 0)
701 return 0;
702 return 1;
704 switch (status)
706 case 0xF0:
707 if (mpu_cmd(dev, 0xDF, 0) < 0)
708 return 0;
709 return 1;
711 default:
712 return 0;
716 static int mpu401_start_read(int dev)
718 return 0;
721 static int mpu401_end_read(int dev)
723 return 0;
726 static int mpu401_ioctl(int dev, unsigned cmd, caddr_t arg)
728 struct mpu_config *devc;
729 mpu_command_rec rec;
730 int val, ret;
732 devc = &dev_conf[dev];
733 switch (cmd)
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");
738 return -EINVAL;
740 if (get_user(val, (int *)arg))
741 return -EFAULT;
742 set_uart_mode(dev, devc, !val);
743 return 0;
745 case SNDCTL_MIDI_MPUCMD:
746 if (copy_from_user(&rec, arg, sizeof(rec)))
747 return -EFAULT;
748 if ((ret = mpu401_command(dev, &rec)) < 0)
749 return ret;
750 if (copy_to_user(arg, &rec, sizeof(rec)))
751 return -EFAULT;
752 return 0;
754 default:
755 return -EINVAL;
759 static void mpu401_kick(int dev)
763 static int mpu401_buffer_status(int dev)
765 return 0; /*
766 * No data in buffers
770 static int mpu_synth_ioctl(int dev,
771 unsigned int cmd, caddr_t arg)
773 int midi_dev;
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)
779 return -ENXIO;
781 devc = &dev_conf[midi_dev];
783 switch (cmd)
786 case SNDCTL_SYNTH_INFO:
787 memcpy((&((char *) arg)[0]), (char *) &mpu_synth_info[midi_dev], sizeof(struct synth_info));
788 return 0;
790 case SNDCTL_SYNTH_MEMAVL:
791 return 0x7fffffff;
793 default:
794 return -EINVAL;
798 static int mpu_synth_open(int dev, int mode)
800 int midi_dev, err;
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)
806 return -ENXIO;
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");
822 return -EIO;
824 reset_mpu401(devc);
826 if (devc->opened)
827 return -EBUSY;
828 devc->mode = MODE_SYNTH;
829 devc->synthno = dev;
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");
838 return err;
840 devc->opened = mode;
841 reset_mpu401(devc);
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 */
849 return 0;
852 static void mpu_synth_close(int dev)
854 int midi_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);
867 devc->opened = 0;
868 devc->mode = 0;
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 =
877 "MPU401",
878 NULL,
880 SYNTH_TYPE_MIDI,
882 mpu_synth_open,
883 mpu_synth_close,
884 mpu_synth_ioctl,
885 midi_synth_kill_note,
886 midi_synth_start_note,
887 midi_synth_set_instr,
888 midi_synth_reset,
889 midi_synth_hw_control,
890 midi_synth_load_patch,
891 midi_synth_aftertouch,
892 midi_synth_controller,
893 midi_synth_panning,
894 NULL,
895 midi_synth_bender,
896 NULL, /* alloc */
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},
906 NULL,
907 {0},
908 mpu401_open,
909 mpu401_close,
910 mpu401_ioctl,
911 mpu401_out,
912 mpu401_start_read,
913 mpu401_end_read,
914 mpu401_kick,
915 NULL,
916 mpu401_buffer_status,
917 mpu401_prefix_cmd
920 static struct midi_operations mpu401_midi_operations[MAX_MIDI_DEV];
922 static void mpu401_chk_version(int n, struct mpu_config *devc)
924 int tmp;
925 unsigned long flags;
927 devc->version = devc->revision = 0;
929 save_flags(flags);
930 cli();
931 if ((tmp = mpu_cmd(n, 0xAC, 0)) < 0)
933 restore_flags(flags);
934 return;
936 if ((tmp & 0xf0) > 0x20) /* Why it's larger than 2.x ??? */
938 restore_flags(flags);
939 return;
941 devc->version = tmp;
943 if ((tmp = mpu_cmd(n, 0xAD, 0)) < 0)
945 devc->version = 0;
946 restore_flags(flags);
947 return;
949 devc->revision = tmp;
950 restore_flags(flags);
953 void attach_mpu401(struct address_info *hw_config)
955 unsigned long flags;
956 char revision_char;
958 int m;
959 struct mpu_config *devc;
961 hw_config->slots[1] = -1;
962 m = sound_alloc_mididev();
963 if (m == -1)
965 printk(KERN_WARNING "MPU-401: Too many midi devices detected\n");
966 return;
968 devc = &dev_conf[m];
969 devc->base = hw_config->io_base;
970 devc->osp = hw_config->osp;
971 devc->irq = hw_config->irq;
972 devc->opened = 0;
973 devc->uart_mode = 0;
974 devc->initialized = 0;
975 devc->version = 0;
976 devc->revision = 0;
977 devc->capabilities = 0;
978 devc->timer_flag = 0;
979 devc->m_busy = 0;
980 devc->m_state = ST_INIT;
981 devc->shared_irq = hw_config->always_detect;
982 devc->irq = hw_config->irq;
984 if (devc->irq < 0)
986 devc->irq *= -1;
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);
997 return;
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);
1005 return;
1008 save_flags(flags);
1009 cli();
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");
1029 return;
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));
1037 else
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));
1054 n_mpu_devs++;
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",
1065 ports,
1066 revision_char,
1067 n_mpu_devs);
1069 else
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);
1079 else
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,
1084 revision_char,
1085 n_mpu_devs);
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;
1101 sequencer_init();
1104 static int reset_mpu401(struct mpu_config *devc)
1106 unsigned long flags;
1107 int ok, timeout, n;
1108 int timeout_limit;
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).
1115 ok = 0;
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--)
1136 save_flags(flags);
1137 cli();
1138 if (input_avail(devc))
1139 if (read_data(devc) == MPU_ACK)
1140 ok = 1;
1141 restore_flags(flags);
1146 devc->m_state = ST_INIT;
1147 devc->m_ptr = 0;
1148 devc->m_left = 0;
1149 devc->last_status = 0;
1150 devc->uart_mode = 0;
1152 return ok;
1155 static void set_uart_mode(int dev, struct mpu_config *devc, int arg)
1157 if (!arg && (devc->capabilities & MPU_CAP_INTLG))
1158 return;
1159 if ((devc->uart_mode == 0) == (arg == 0))
1160 return; /* Already set */
1161 reset_mpu401(devc); /* This exits the uart mode */
1163 if (arg)
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;
1169 return;
1172 devc->uart_mode = arg;
1176 int probe_mpu401(struct address_info *hw_config)
1178 int ok = 0;
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);
1184 return 0;
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)
1193 return 1;
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);
1202 if (!ok)
1204 DDB(printk("MPU401: Reset failed on port %x\n", hw_config->io_base));
1206 return ok;
1209 void unload_mpu401(struct address_info *hw_config)
1211 void *p;
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]);
1220 if(p)
1221 kfree(p);
1224 /*****************************************************
1225 * Timer stuff
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
1244 * actual timebases.
1246 return ((clocks * curr_timebase) + (hw_timebase / 2)) / hw_timebase;
1249 static void set_timebase(int midi_dev, int val)
1251 int hw_val;
1253 if (val < 48)
1254 val = 48;
1255 if (val > 1000)
1256 val = 1000;
1258 hw_val = 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);
1266 return;
1268 hw_timebase = hw_val * 24;
1269 curr_timebase = val;
1273 static void tmr_reset(void)
1275 unsigned long flags;
1277 save_flags(flags);
1278 cli();
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 */
1296 else
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 */
1327 else
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)
1337 tmr_reset();
1338 set_timer_mode(midi_dev);
1340 if (tmr_running)
1341 return TIMER_NOT_ARMED; /* Already running */
1343 if (timer_mode & TMR_INTERNAL)
1345 mpu_cmd(midi_dev, 0x02, 0); /* Send MIDI start */
1346 tmr_running = 1;
1347 return TIMER_NOT_ARMED;
1349 else
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 */
1356 return TIMER_ARMED;
1359 static int mpu_timer_open(int dev, int mode)
1361 int midi_dev = sound_timer_devs[dev]->devlink;
1363 if (timer_open)
1364 return -EBUSY;
1366 tmr_reset();
1367 curr_tempo = 50;
1368 mpu_cmd(midi_dev, 0xE0, 50);
1369 curr_timebase = hw_timebase = 120;
1370 set_timebase(midi_dev, 120);
1371 timer_open = 1;
1372 metronome_mode = 0;
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 */
1378 return 0;
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;
1398 switch (command)
1400 case TMR_WAIT_REL:
1401 parm += prev_event_time;
1402 case TMR_WAIT_ABS:
1403 if (parm > 0)
1405 long time;
1407 if (parm <= curr_ticks) /* It's the time */
1408 return TIMER_NOT_ARMED;
1409 time = parm;
1410 next_event_time = prev_event_time = time;
1412 return TIMER_ARMED;
1414 break;
1416 case TMR_START:
1417 if (tmr_running)
1418 break;
1419 return mpu_start_timer(midi_dev);
1421 case TMR_STOP:
1422 mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
1423 stop_metronome(midi_dev);
1424 tmr_running = 0;
1425 break;
1427 case TMR_CONTINUE:
1428 if (tmr_running)
1429 break;
1430 mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
1431 setup_metronome(midi_dev);
1432 tmr_running = 1;
1433 break;
1435 case TMR_TEMPO:
1436 if (parm)
1438 if (parm < 8)
1439 parm = 8;
1440 if (parm > 250)
1441 parm = 250;
1442 if (mpu_cmd(midi_dev, 0xE0, parm) < 0)
1443 printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) parm);
1444 curr_tempo = parm;
1446 break;
1448 case TMR_ECHO:
1449 seq_copy_to_input(event, 8);
1450 break;
1452 case TMR_TIMESIG:
1453 if (metronome_mode) /* Metronome enabled */
1455 metronome_mode = parm;
1456 setup_metronome(midi_dev);
1458 break;
1460 default:
1462 return TIMER_NOT_ARMED;
1465 static unsigned long mpu_timer_get_time(int dev)
1467 if (!timer_open)
1468 return 0;
1470 return curr_ticks;
1473 static int mpu_timer_ioctl(int dev, unsigned int command, caddr_t arg)
1475 int midi_dev = sound_timer_devs[dev]->devlink;
1477 switch (command)
1479 case SNDCTL_TMR_SOURCE:
1481 int parm;
1483 parm = *(int *) arg;
1484 parm &= timer_caps;
1486 if (parm != 0)
1488 timer_mode = parm;
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);
1497 break;
1499 case SNDCTL_TMR_START:
1500 mpu_start_timer(midi_dev);
1501 return 0;
1503 case SNDCTL_TMR_STOP:
1504 tmr_running = 0;
1505 mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
1506 stop_metronome(midi_dev);
1507 return 0;
1509 case SNDCTL_TMR_CONTINUE:
1510 if (tmr_running)
1511 return 0;
1512 tmr_running = 1;
1513 mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
1514 return 0;
1516 case SNDCTL_TMR_TIMEBASE:
1518 int val;
1520 val = *(int *) arg;
1521 if (val)
1522 set_timebase(midi_dev, val);
1523 return (*(int *) arg = curr_timebase);
1525 break;
1527 case SNDCTL_TMR_TEMPO:
1529 int val;
1530 int ret;
1532 val = *(int *) arg;
1534 if (val)
1536 if (val < 8)
1537 val = 8;
1538 if (val > 250)
1539 val = 250;
1540 if ((ret = mpu_cmd(midi_dev, 0xE0, val)) < 0)
1542 printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) val);
1543 return ret;
1545 curr_tempo = val;
1547 return (*(int *) arg = curr_tempo);
1549 break;
1551 case SNDCTL_SEQ_CTRLRATE:
1553 int val;
1555 val = *(int *) arg;
1556 if (val != 0) /* Can't change */
1557 return -EINVAL;
1558 return (*(int *) arg = ((curr_tempo * curr_timebase) + 30) / 60);
1560 break;
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);
1568 return 0;
1570 default:
1572 return -EINVAL;
1575 static void mpu_timer_arm(int dev, long time)
1577 if (time < 0)
1578 time = curr_ticks + 1;
1579 else if (time <= curr_ticks) /* It's the time */
1580 return;
1581 next_event_time = prev_event_time = time;
1582 return;
1585 static struct sound_timer_operations mpu_timer =
1587 {"MPU-401 Timer", 0},
1588 10, /* Priority */
1589 0, /* Local device link */
1590 mpu_timer_open,
1591 mpu_timer_close,
1592 mpu_timer_event,
1593 mpu_timer_get_time,
1594 mpu_timer_ioctl,
1595 mpu_timer_arm
1598 static void mpu_timer_interrupt(void)
1600 if (!timer_open)
1601 return;
1603 if (!tmr_running)
1604 return;
1606 curr_clocks++;
1607 curr_ticks = clocks2ticks(curr_clocks);
1609 if (curr_ticks >= next_event_time)
1611 next_event_time = (unsigned long) -1;
1612 sequencer_timer(0);
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)
1621 return;
1623 switch (event)
1625 case TMR_CLOCK:
1626 printk("<MIDI clk>");
1627 break;
1629 case TMR_START:
1630 printk("Ext MIDI start\n");
1631 if (!tmr_running)
1633 if (timer_mode & TMR_EXTERNAL)
1635 tmr_running = 1;
1636 setup_metronome(midi_dev);
1637 next_event_time = 0;
1638 STORE(SEQ_START_TIMER());
1641 break;
1643 case TMR_STOP:
1644 printk("Ext MIDI stop\n");
1645 if (timer_mode & TMR_EXTERNAL)
1647 tmr_running = 0;
1648 stop_metronome(midi_dev);
1649 STORE(SEQ_STOP_TIMER());
1651 break;
1653 case TMR_CONTINUE:
1654 printk("Ext MIDI continue\n");
1655 if (timer_mode & TMR_EXTERNAL)
1657 tmr_running = 1;
1658 setup_metronome(midi_dev);
1659 STORE(SEQ_CONTINUE_TIMER());
1661 break;
1663 case TMR_SPP:
1664 printk("Songpos: %d\n", parm);
1665 if (timer_mode & TMR_EXTERNAL)
1667 STORE(SEQ_SONGPOS(parm));
1669 break;
1673 static int mpu_timer_init(int midi_dev)
1675 struct mpu_config *devc;
1676 int n;
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();
1689 if (n == -1)
1690 n = 0;
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;
1695 else
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.
1705 if (devc->revision)
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;
1717 return n;
1721 #endif
1724 EXPORT_SYMBOL(probe_mpu401);
1725 EXPORT_SYMBOL(attach_mpu401);
1726 EXPORT_SYMBOL(unload_mpu401);
1727 EXPORT_SYMBOL(intchk_mpu401);
1728 EXPORT_SYMBOL(mpuintr);
1730 #ifdef MODULE
1732 MODULE_PARM(irq, "i");
1733 MODULE_PARM(io, "i");
1735 int io = -1;
1736 int irq = -1;
1737 struct address_info hw;
1739 int init_module(void)
1741 /* Can be loaded either for module use or to provide functions
1742 to others */
1743 if (io != -1 && irq != -1)
1745 hw.irq = irq;
1746 hw.io_base = io;
1747 if (probe_mpu401(&hw) == 0)
1748 return -ENODEV;
1749 attach_mpu401(&hw);
1751 SOUND_LOCK;
1752 return 0;
1755 void cleanup_module(void)
1757 if (io != -1 && irq != -1)
1759 unload_mpu401(&hw);
1761 /* FREE SYMTAB */
1762 SOUND_LOCK_END;
1765 #endif
1766 #endif