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 19 #include <linux/module.h> 20 #include <linux/init.h> 22 #define USE_SEQ_MACROS 23 #define USE_SIMPLE_MACROS 25 #include"sound_config.h" 30 static int timer_mode
= TMR_INTERNAL
, timer_caps
= TMR_INTERNAL
; 48 unsigned char version
, revision
; 49 unsigned int capabilities
; 50 #define MPU_CAP_INTLG 0x10000000 51 #define MPU_CAP_SYNC 0x00000010 52 #define MPU_CAP_FSK 0x00000020 53 #define MPU_CAP_CLS 0x00000040 54 #define MPU_CAP_SMPTE 0x00000080 55 #define MPU_CAP_2PORT 0x00000001 59 #define BUFTEST(dc) if (dc->m_ptr >= MBUF_MAX || dc->m_ptr < 0) \ 60 {printk("MPU: Invalid buffer pointer %d/%d, s=%d\n", dc->m_ptr, dc->m_left, dc->m_state);dc->m_ptr--;} 62 unsigned char m_buf
[MBUF_MAX
]; 66 unsigned char last_status
; 67 void(*inputintr
) (int dev
,unsigned char data
); 72 #define DATAPORT(base) (base) 73 #define COMDPORT(base) (base+1) 74 #define STATPORT(base) (base+1) 76 static intmpu401_status(struct mpu_config
*devc
) 78 returninb(STATPORT(devc
->base
)); 81 #define input_avail(devc) (!(mpu401_status(devc)&INPUT_AVAIL)) 82 #define output_ready(devc) (!(mpu401_status(devc)&OUTPUT_READY)) 84 static voidwrite_command(struct mpu_config
*devc
,unsigned char cmd
) 86 outb(cmd
,COMDPORT(devc
->base
)); 89 static intread_data(struct mpu_config
*devc
) 91 returninb(DATAPORT(devc
->base
)); 94 static voidwrite_data(struct mpu_config
*devc
,unsigned char byte
) 96 outb(byte
,DATAPORT(devc
->base
)); 99 #define OUTPUT_READY 0x40 100 #define INPUT_AVAIL 0x80 102 #define MPU_RESET 0xFF 103 #define UART_MODE_ON 0x3F 105 static struct mpu_config dev_conf
[MAX_MIDI_DEV
] = 110 static int n_mpu_devs
=0; 112 static intreset_mpu401(struct mpu_config
*devc
); 113 static voidset_uart_mode(int dev
,struct mpu_config
*devc
,int arg
); 115 static intmpu_timer_init(int midi_dev
); 116 static voidmpu_timer_interrupt(void); 117 static voidtimer_ext_event(struct mpu_config
*devc
,int event
,int parm
); 119 static struct synth_info mpu_synth_info_proto
= { 120 "MPU-401 MIDI interface", 129 static struct synth_info mpu_synth_info
[MAX_MIDI_DEV
]; 132 * States for the input scanner 135 #define ST_INIT 0/* Ready for timing byte or msg */ 136 #define ST_TIMED 1/* Leading timing byte rcvd */ 137 #define ST_DATABYTE 2/* Waiting for (nr_left) data bytes */ 139 #define ST_SYSMSG 100/* System message (sysx etc). */ 140 #define ST_SYSEX 101/* System exclusive msg */ 141 #define ST_MTC 102/* Midi Time Code (MTC) qframe msg */ 142 #define ST_SONGSEL 103/* Song select */ 143 #define ST_SONGPOS 104/* Song position pointer */ 145 static unsigned char len_tab
[] =/* # of data bytes following a status 161 unsigned char obuf[8]; \ 163 seq_input_event(obuf, len); \ 168 #define _SEQ_ADVBUF(x) len=x 170 static intmpu_input_scanner(struct mpu_config
*devc
,unsigned char midic
) 173 switch(devc
->m_state
) 188 mpu_timer_interrupt(); 202 printk("<Trk data rq #%d>", midic
&0x0f); 206 printk("<conductor rq>"); 210 devc
->m_state
= ST_SYSMSG
; 216 /* printk( "mpu time: %d ", midic); */ 217 devc
->m_state
= ST_TIMED
; 220 printk("<MPU: Unknown event %02x> ", midic
); 226 int msg
= ((int) (midic
&0xf0) >>4); 228 devc
->m_state
= ST_DATABYTE
; 230 if(msg
<8)/* Data byte */ 232 /* printk( "midi msg (running status) "); */ 233 msg
= ((int) (devc
->last_status
&0xf0) >>4); 235 devc
->m_left
= len_tab
[msg
] -1; 238 devc
->m_buf
[0] = devc
->last_status
; 239 devc
->m_buf
[1] = midic
; 243 devc
->m_state
= ST_INIT
; 244 do_midi_msg(devc
->synthno
, devc
->m_buf
, devc
->m_ptr
); 248 else if(msg
==0xf)/* MPU MARK */ 250 devc
->m_state
= ST_INIT
; 255 /* printk( "NOP "); */ 259 /* printk( "meas end "); */ 263 /* printk( "data end "); */ 267 printk("Unknown MPU mark %02x\n", midic
); 272 devc
->last_status
= midic
; 273 /* printk( "midi msg "); */ 275 devc
->m_left
= len_tab
[msg
]; 278 devc
->m_buf
[0] = midic
; 282 devc
->m_state
= ST_INIT
; 283 do_midi_msg(devc
->synthno
, devc
->m_buf
, devc
->m_ptr
); 295 devc
->m_state
= ST_SYSEX
; 299 devc
->m_state
= ST_MTC
; 303 devc
->m_state
= ST_SONGPOS
; 308 devc
->m_state
= ST_SONGSEL
; 312 /* printk( "tune_request\n"); */ 313 devc
->m_state
= ST_INIT
; 320 devc
->m_state
= ST_INIT
; 321 timer_ext_event(devc
, TMR_CLOCK
,0); 325 devc
->m_state
= ST_INIT
; 326 timer_ext_event(devc
, TMR_START
,0); 330 devc
->m_state
= ST_INIT
; 331 timer_ext_event(devc
, TMR_CONTINUE
,0); 335 devc
->m_state
= ST_INIT
; 336 timer_ext_event(devc
, TMR_STOP
,0); 341 devc
->m_state
= ST_INIT
; 345 /* printk( "midi hard reset"); */ 346 devc
->m_state
= ST_INIT
; 350 printk("unknown MIDI sysmsg %0x\n", midic
); 351 devc
->m_state
= ST_INIT
; 356 devc
->m_state
= ST_INIT
; 357 printk("MTC frame %x02\n", midic
); 364 devc
->m_state
= ST_INIT
; 367 printk("%02x ", midic
); 372 devc
->m_buf
[devc
->m_ptr
++] = midic
; 375 devc
->m_state
= ST_INIT
; 377 timer_ext_event(devc
, TMR_SPP
, 378 ((devc
->m_buf
[1] &0x7f) <<7) | 379 (devc
->m_buf
[0] &0x7f)); 385 devc
->m_buf
[devc
->m_ptr
++] = midic
; 386 if((--devc
->m_left
) <=0) 388 devc
->m_state
= ST_INIT
; 389 do_midi_msg(devc
->synthno
, devc
->m_buf
, devc
->m_ptr
); 395 printk("Bad state %d ", devc
->m_state
); 396 devc
->m_state
= ST_INIT
; 401 static voidmpu401_input_loop(struct mpu_config
*devc
) 411 restore_flags(flags
); 413 if(busy
)/* Already inside the scanner */ 418 while(input_avail(devc
) && n
-- >0) 420 unsigned char c
=read_data(devc
); 422 if(devc
->mode
== MODE_SYNTH
) 424 mpu_input_scanner(devc
, c
); 426 else if(devc
->opened
& OPEN_READ
&& devc
->inputintr
!= NULL
) 427 devc
->inputintr(devc
->devno
, c
); 432 intintchk_mpu401(void*dev_id
) 434 struct mpu_config
*devc
; 435 int dev
= (int) dev_id
; 437 devc
= &dev_conf
[dev
]; 438 returninput_avail(devc
); 441 voidmpuintr(int irq
,void*dev_id
,struct pt_regs
*dummy
) 443 struct mpu_config
*devc
; 444 int dev
= (int) dev_id
; 447 devc
= &dev_conf
[dev
]; 449 if(input_avail(devc
)) 451 if(devc
->base
!=0&& (devc
->opened
& OPEN_READ
|| devc
->mode
== MODE_SYNTH
)) 452 mpu401_input_loop(devc
); 455 /* Dummy read (just to acknowledge the interrupt) */ 461 static intmpu401_open(int dev
,int mode
, 462 void(*input
) (int dev
,unsigned char data
), 463 void(*output
) (int dev
) 467 struct mpu_config
*devc
; 469 if(dev
<0|| dev
>= num_midis
|| midi_devs
[dev
] == NULL
) 472 devc
= &dev_conf
[dev
]; 477 * Verify that the device is really running. 478 * Some devices (such as Ensoniq SoundScape don't 479 * work before the on board processor (OBP) is initialized 480 * by downloading its microcode. 483 if(!devc
->initialized
) 485 if(mpu401_status(devc
) ==0xff)/* Bus float */ 487 printk(KERN_ERR
"mpu401: Device not initialized properly\n"); 493 if(midi_devs
[dev
]->coproc
) 495 if((err
= midi_devs
[dev
]->coproc
-> 496 open(midi_devs
[dev
]->coproc
->devc
, COPR_MIDI
)) <0) 498 printk("MPU-401: Can't access coprocessor device\n"); 503 set_uart_mode(dev
, devc
,1); 504 devc
->mode
= MODE_MIDI
; 507 mpu401_input_loop(devc
); 509 devc
->inputintr
= input
; 515 static voidmpu401_close(int dev
) 517 struct mpu_config
*devc
; 519 devc
= &dev_conf
[dev
]; 521 reset_mpu401(devc
);/* 522 * This disables the UART mode 525 devc
->inputintr
= NULL
; 527 if(midi_devs
[dev
]->coproc
) 528 midi_devs
[dev
]->coproc
->close(midi_devs
[dev
]->coproc
->devc
, COPR_MIDI
); 532 static intmpu401_out(int dev
,unsigned char midi_byte
) 537 struct mpu_config
*devc
; 539 devc
= &dev_conf
[dev
]; 542 * Sometimes it takes about 30000 loops before the output becomes ready 543 * (After reset). Normally it takes just about 10 loops. 546 for(timeout
=30000; timeout
>0&& !output_ready(devc
); timeout
--); 550 if(!output_ready(devc
)) 552 printk(KERN_WARNING
"mpu401: Send data timeout\n"); 553 restore_flags(flags
); 556 write_data(devc
, midi_byte
); 557 restore_flags(flags
); 561 static intmpu401_command(int dev
, mpu_command_rec
* cmd
) 566 struct mpu_config
*devc
; 568 devc
= &dev_conf
[dev
]; 570 if(devc
->uart_mode
)/* 571 * Not possible in UART mode 574 printk(KERN_WARNING
"mpu401: commands not possible in the UART mode\n"); 578 * Test for input since pending input seems to block the output. 580 if(input_avail(devc
)) 581 mpu401_input_loop(devc
); 584 * Sometimes it takes about 50000 loops before the output becomes ready 585 * (After reset). Normally it takes just about 10 loops. 592 printk(KERN_WARNING
"mpu401: Command (0x%x) timeout\n", (int) cmd
->cmd
); 598 if(!output_ready(devc
)) 600 restore_flags(flags
); 603 write_command(devc
, cmd
->cmd
); 606 for(timeout
=50000; timeout
>0&& !ok
; timeout
--) 608 if(input_avail(devc
)) 610 if(devc
->opened
&& devc
->mode
== MODE_SYNTH
) 612 if(mpu_input_scanner(devc
,read_data(devc
)) == MPU_ACK
) 617 /* Device is not currently open. Use simpler method */ 618 if(read_data(devc
) == MPU_ACK
) 625 restore_flags(flags
); 630 for(i
=0; i
< cmd
->nr_args
; i
++) 632 for(timeout
=3000; timeout
>0&& !output_ready(devc
); timeout
--); 634 if(!mpu401_out(dev
, cmd
->data
[i
])) 636 restore_flags(flags
); 637 printk(KERN_WARNING
"mpu401: Command (0x%x), parm send failed.\n", (int) cmd
->cmd
); 647 for(i
=0; i
< cmd
->nr_returns
; i
++) 650 for(timeout
=5000; timeout
>0&& !ok
; timeout
--) 651 if(input_avail(devc
)) 653 cmd
->data
[i
] =read_data(devc
); 658 restore_flags(flags
); 663 restore_flags(flags
); 667 static intmpu_cmd(int dev
,int cmd
,int data
) 671 static mpu_command_rec rec
; 674 rec
.nr_args
= ((cmd
&0xf0) ==0xE0); 675 rec
.nr_returns
= ((cmd
&0xf0) ==0xA0); 676 rec
.data
[0] = data
&0xff; 678 if((ret
=mpu401_command(dev
, &rec
)) <0) 680 return(unsigned char) rec
.data
[0]; 683 static intmpu401_prefix_cmd(int dev
,unsigned char status
) 685 struct mpu_config
*devc
= &dev_conf
[dev
]; 692 if(mpu_cmd(dev
,0xD0,0) <0) 699 if(mpu_cmd(dev
,0xDF,0) <0) 708 static intmpu401_start_read(int dev
) 713 static intmpu401_end_read(int dev
) 718 static intmpu401_ioctl(int dev
,unsigned cmd
, caddr_t arg
) 720 struct mpu_config
*devc
; 724 devc
= &dev_conf
[dev
]; 727 case SNDCTL_MIDI_MPUMODE
: 728 if(!(devc
->capabilities
& MPU_CAP_INTLG
)) {/* No intelligent mode */ 729 printk(KERN_WARNING
"mpu401: Intelligent mode not supported by the HW\n"); 732 if(get_user(val
, (int*)arg
)) 734 set_uart_mode(dev
, devc
, !val
); 737 case SNDCTL_MIDI_MPUCMD
: 738 if(copy_from_user(&rec
, arg
,sizeof(rec
))) 740 if((ret
=mpu401_command(dev
, &rec
)) <0) 742 if(copy_to_user(arg
, &rec
,sizeof(rec
))) 751 static voidmpu401_kick(int dev
) 755 static intmpu401_buffer_status(int dev
) 762 static intmpu_synth_ioctl(int dev
, 763 unsigned int cmd
, caddr_t arg
) 766 struct mpu_config
*devc
; 768 midi_dev
= synth_devs
[dev
]->midi_dev
; 770 if(midi_dev
<0|| midi_dev
> num_midis
|| midi_devs
[midi_dev
] == NULL
) 773 devc
= &dev_conf
[midi_dev
]; 778 case SNDCTL_SYNTH_INFO
: 779 memcpy((&((char*) arg
)[0]), (char*) &mpu_synth_info
[midi_dev
],sizeof(struct synth_info
)); 782 case SNDCTL_SYNTH_MEMAVL
: 790 static intmpu_synth_open(int dev
,int mode
) 793 struct mpu_config
*devc
; 795 midi_dev
= synth_devs
[dev
]->midi_dev
; 797 if(midi_dev
<0|| midi_dev
> num_midis
|| midi_devs
[midi_dev
] == NULL
) 800 devc
= &dev_conf
[midi_dev
]; 803 * Verify that the device is really running. 804 * Some devices (such as Ensoniq SoundScape don't 805 * work before the on board processor (OBP) is initialized 806 * by downloading its microcode. 809 if(!devc
->initialized
) 811 if(mpu401_status(devc
) ==0xff)/* Bus float */ 813 printk(KERN_ERR
"mpu401: Device not initialized properly\n"); 820 devc
->mode
= MODE_SYNTH
; 823 devc
->inputintr
= NULL
; 825 if(midi_devs
[midi_dev
]->coproc
) 826 if((err
= midi_devs
[midi_dev
]->coproc
-> 827 open(midi_devs
[midi_dev
]->coproc
->devc
, COPR_MIDI
)) <0) 829 printk(KERN_WARNING
"mpu401: Can't access coprocessor device\n"); 837 mpu_cmd(midi_dev
,0x8B,0);/* Enable data in stop mode */ 838 mpu_cmd(midi_dev
,0x34,0);/* Return timing bytes in stop mode */ 839 mpu_cmd(midi_dev
,0x87,0);/* Enable pitch & controller */ 844 static voidmpu_synth_close(int dev
) 847 struct mpu_config
*devc
; 849 midi_dev
= synth_devs
[dev
]->midi_dev
; 851 devc
= &dev_conf
[midi_dev
]; 852 mpu_cmd(midi_dev
,0x15,0);/* Stop recording, playback and MIDI */ 853 mpu_cmd(midi_dev
,0x8a,0);/* Disable data in stopped mode */ 855 devc
->inputintr
= NULL
; 857 if(midi_devs
[midi_dev
]->coproc
) 858 midi_devs
[midi_dev
]->coproc
->close(midi_devs
[midi_dev
]->coproc
->devc
, COPR_MIDI
); 863 #define MIDI_SYNTH_NAME"MPU-401 UART Midi" 864 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT 865 #include"midi_synth.h" 867 static struct synth_operations mpu401_synth_proto
= 873 synth_type
: SYNTH_TYPE_MIDI
, 875 open
: mpu_synth_open
, 876 close
: mpu_synth_close
, 877 ioctl
: mpu_synth_ioctl
, 878 kill_note
: midi_synth_kill_note
, 879 start_note
: midi_synth_start_note
, 880 set_instr
: midi_synth_set_instr
, 881 reset
: midi_synth_reset
, 882 hw_control
: midi_synth_hw_control
, 883 load_patch
: midi_synth_load_patch
, 884 aftertouch
: midi_synth_aftertouch
, 885 controller
: midi_synth_controller
, 886 panning
: midi_synth_panning
, 887 bender
: midi_synth_bender
, 888 setup_voice
: midi_synth_setup_voice
, 889 send_sysex
: midi_synth_send_sysex
892 static struct synth_operations
*mpu401_synth_operations
[MAX_MIDI_DEV
]; 894 static struct midi_operations mpu401_midi_proto
= 897 info
: {"MPU-401 Midi",0, MIDI_CAP_MPU401
, SNDCARD_MPU401
}, 903 start_read
: mpu401_start_read
, 904 end_read
: mpu401_end_read
, 906 buffer_status
: mpu401_buffer_status
, 907 prefix_cmd
: mpu401_prefix_cmd
910 static struct midi_operations mpu401_midi_operations
[MAX_MIDI_DEV
]; 912 static voidmpu401_chk_version(int n
,struct mpu_config
*devc
) 917 devc
->version
= devc
->revision
=0; 921 if((tmp
=mpu_cmd(n
,0xAC,0)) <0) 923 restore_flags(flags
); 926 if((tmp
&0xf0) >0x20)/* Why it's larger than 2.x ??? */ 928 restore_flags(flags
); 933 if((tmp
=mpu_cmd(n
,0xAD,0)) <0) 936 restore_flags(flags
); 939 devc
->revision
= tmp
; 940 restore_flags(flags
); 943 voidattach_mpu401(struct address_info
*hw_config
,struct module
*owner
) 949 struct mpu_config
*devc
; 951 hw_config
->slots
[1] = -1; 952 m
=sound_alloc_mididev(); 955 printk(KERN_WARNING
"MPU-401: Too many midi devices detected\n"); 959 devc
->base
= hw_config
->io_base
; 960 devc
->osp
= hw_config
->osp
; 961 devc
->irq
= hw_config
->irq
; 964 devc
->initialized
=0; 967 devc
->capabilities
=0; 970 devc
->m_state
= ST_INIT
; 971 devc
->shared_irq
= hw_config
->always_detect
; 972 devc
->irq
= hw_config
->irq
; 980 if(!hw_config
->always_detect
) 982 /* Verify the hardware again */ 983 if(!reset_mpu401(devc
)) 985 printk(KERN_WARNING
"mpu401: Device didn't respond\n"); 986 sound_unload_mididev(m
); 989 if(!devc
->shared_irq
) 991 if(request_irq(devc
->irq
, mpuintr
,0,"mpu401", (void*)m
) <0) 993 printk(KERN_WARNING
"mpu401: Failed to allocate IRQ%d\n", devc
->irq
); 994 sound_unload_mididev(m
); 1000 mpu401_chk_version(m
, devc
); 1001 if(devc
->version
==0) 1002 mpu401_chk_version(m
, devc
); 1003 restore_flags(flags
); 1005 request_region(hw_config
->io_base
,2,"mpu401"); 1007 if(devc
->version
!=0) 1008 if(mpu_cmd(m
,0xC5,0) >=0)/* Set timebase OK */ 1009 if(mpu_cmd(m
,0xE0,120) >=0)/* Set tempo OK */ 1010 devc
->capabilities
|= MPU_CAP_INTLG
;/* Supports intelligent mode */ 1013 mpu401_synth_operations
[m
] = (struct synth_operations
*)kmalloc(sizeof(struct synth_operations
), GFP_KERNEL
); 1015 if(mpu401_synth_operations
[m
] == NULL
) 1017 sound_unload_mididev(m
); 1018 printk(KERN_ERR
"mpu401: Can't allocate memory\n"); 1021 if(!(devc
->capabilities
& MPU_CAP_INTLG
))/* No intelligent mode */ 1023 memcpy((char*) mpu401_synth_operations
[m
], 1024 (char*) &std_midi_synth
, 1025 sizeof(struct synth_operations
)); 1029 memcpy((char*) mpu401_synth_operations
[m
], 1030 (char*) &mpu401_synth_proto
, 1031 sizeof(struct synth_operations
)); 1034 memcpy((char*) &mpu401_midi_operations
[m
], 1035 (char*) &mpu401_midi_proto
, 1036 sizeof(struct midi_operations
)); 1038 mpu401_midi_operations
[m
].converter
= mpu401_synth_operations
[m
]; 1040 memcpy((char*) &mpu_synth_info
[m
], 1041 (char*) &mpu_synth_info_proto
, 1042 sizeof(struct synth_info
)); 1046 if(devc
->version
==0x20&& devc
->revision
>=0x07)/* MusicQuest interface */ 1048 int ports
= (devc
->revision
&0x08) ?32:16; 1050 devc
->capabilities
|= MPU_CAP_SYNC
| MPU_CAP_SMPTE
| 1051 MPU_CAP_CLS
| MPU_CAP_2PORT
; 1053 revision_char
= (devc
->revision
==0x7f) ?'M':' '; 1054 sprintf(mpu_synth_info
[m
].name
,"MQX-%d%c MIDI Interface #%d", 1061 revision_char
= devc
->revision
? devc
->revision
+'@':' '; 1062 if((int) devc
->revision
> ('Z'-'@')) 1065 devc
->capabilities
|= MPU_CAP_SYNC
| MPU_CAP_FSK
; 1068 sprintf(mpu_synth_info
[m
].name
,"%s (MPU401)", hw_config
->name
); 1070 sprintf(mpu_synth_info
[m
].name
, 1071 "MPU-401 %d.%d%c Midi interface #%d", 1072 (int) (devc
->version
&0xf0) >>4, 1073 devc
->version
&0x0f, 1078 strcpy(mpu401_midi_operations
[m
].info
.name
, 1079 mpu_synth_info
[m
].name
); 1081 conf_printf(mpu_synth_info
[m
].name
, hw_config
); 1083 mpu401_synth_operations
[m
]->midi_dev
= devc
->devno
= m
; 1084 mpu401_synth_operations
[devc
->devno
]->info
= &mpu_synth_info
[devc
->devno
]; 1086 if(devc
->capabilities
& MPU_CAP_INTLG
)/* Intelligent mode */ 1087 hw_config
->slots
[2] =mpu_timer_init(m
); 1089 midi_devs
[m
] = &mpu401_midi_operations
[devc
->devno
]; 1092 midi_devs
[m
]->owner
= owner
; 1094 hw_config
->slots
[1] = m
; 1098 static intreset_mpu401(struct mpu_config
*devc
) 1100 unsigned long flags
; 1105 * Send the RESET command. Try again if no success at the first time. 1106 * (If the device is in the UART mode, it will not ack the reset cmd). 1111 timeout_limit
= devc
->initialized
?30000:100000; 1112 devc
->initialized
=1; 1114 for(n
=0; n
<2&& !ok
; n
++) 1116 for(timeout
= timeout_limit
; timeout
>0&& !ok
; timeout
--) 1117 ok
=output_ready(devc
); 1119 write_command(devc
, MPU_RESET
);/* 1120 * Send MPU-401 RESET Command 1124 * Wait at least 25 msec. This method is not accurate so let's make the 1125 * loop bit longer. Cannot sleep since this is called during boot. 1128 for(timeout
= timeout_limit
*2; timeout
>0&& !ok
; timeout
--) 1132 if(input_avail(devc
)) 1133 if(read_data(devc
) == MPU_ACK
) 1135 restore_flags(flags
); 1140 devc
->m_state
= ST_INIT
; 1143 devc
->last_status
=0; 1149 static voidset_uart_mode(int dev
,struct mpu_config
*devc
,int arg
) 1151 if(!arg
&& (devc
->capabilities
& MPU_CAP_INTLG
)) 1153 if((devc
->uart_mode
==0) == (arg
==0)) 1154 return;/* Already set */ 1155 reset_mpu401(devc
);/* This exits the uart mode */ 1159 if(mpu_cmd(dev
, UART_MODE_ON
,0) <0) 1161 printk(KERN_ERR
"mpu401: Can't enter UART mode\n"); 1166 devc
->uart_mode
= arg
; 1170 intprobe_mpu401(struct address_info
*hw_config
) 1173 struct mpu_config tmp_devc
; 1175 if(check_region(hw_config
->io_base
,2)) 1177 printk(KERN_ERR
"mpu401: I/O port %x already in use\n\n", hw_config
->io_base
); 1180 tmp_devc
.base
= hw_config
->io_base
; 1181 tmp_devc
.irq
= hw_config
->irq
; 1182 tmp_devc
.initialized
=0; 1184 tmp_devc
.osp
= hw_config
->osp
; 1186 if(hw_config
->always_detect
) 1189 if(inb(hw_config
->io_base
+1) ==0xff) 1191 DDB(printk("MPU401: Port %x looks dead.\n", hw_config
->io_base
)); 1192 return0;/* Just bus float? */ 1194 ok
=reset_mpu401(&tmp_devc
); 1198 DDB(printk("MPU401: Reset failed on port %x\n", hw_config
->io_base
)); 1203 void __exit
unload_mpu401(struct address_info
*hw_config
) 1206 int n
=hw_config
->slots
[1]; 1208 release_region(hw_config
->io_base
,2); 1209 if(hw_config
->always_detect
==0&& hw_config
->irq
>0) 1210 free_irq(hw_config
->irq
, (void*)n
); 1211 p
=mpu401_synth_operations
[n
]; 1212 sound_unload_mididev(n
); 1213 sound_unload_timerdev(hw_config
->slots
[2]); 1218 /***************************************************** 1220 ****************************************************/ 1222 staticvolatileint timer_initialized
=0, timer_open
=0, tmr_running
=0; 1223 staticvolatileint curr_tempo
, curr_timebase
, hw_timebase
; 1224 static int max_timebase
=8;/* 8*24=192 ppqn */ 1225 staticvolatileunsigned long next_event_time
; 1226 staticvolatileunsigned long curr_ticks
, curr_clocks
; 1227 static unsigned long prev_event_time
; 1228 static int metronome_mode
; 1230 static unsigned longclocks2ticks(unsigned long clocks
) 1233 * The MPU-401 supports just a limited set of possible timebase values. 1234 * Since the applications require more choices, the driver has to 1235 * program the HW to do its best and to convert between the HW and 1238 return((clocks
* curr_timebase
) + (hw_timebase
/2)) / hw_timebase
; 1241 static voidset_timebase(int midi_dev
,int val
) 1251 hw_val
= (hw_val
+12) /24; 1252 if(hw_val
> max_timebase
) 1253 hw_val
= max_timebase
; 1255 if(mpu_cmd(midi_dev
,0xC0| (hw_val
&0x0f),0) <0) 1257 printk(KERN_WARNING
"mpu401: Can't set HW timebase to %d\n", hw_val
*24); 1260 hw_timebase
= hw_val
*24; 1261 curr_timebase
= val
; 1265 static voidtmr_reset(void) 1267 unsigned long flags
; 1271 next_event_time
= (unsigned long) -1; 1273 curr_ticks
= curr_clocks
=0; 1274 restore_flags(flags
); 1277 static voidset_timer_mode(int midi_dev
) 1279 if(timer_mode
& TMR_MODE_CLS
) 1280 mpu_cmd(midi_dev
,0x3c,0);/* Use CLS sync */ 1281 else if(timer_mode
& TMR_MODE_SMPTE
) 1282 mpu_cmd(midi_dev
,0x3d,0);/* Use SMPTE sync */ 1284 if(timer_mode
& TMR_INTERNAL
) 1286 mpu_cmd(midi_dev
,0x80,0);/* Use MIDI sync */ 1290 if(timer_mode
& (TMR_MODE_MIDI
| TMR_MODE_CLS
)) 1292 mpu_cmd(midi_dev
,0x82,0);/* Use MIDI sync */ 1293 mpu_cmd(midi_dev
,0x91,0);/* Enable ext MIDI ctrl */ 1295 else if(timer_mode
& TMR_MODE_FSK
) 1296 mpu_cmd(midi_dev
,0x81,0);/* Use FSK sync */ 1300 static voidstop_metronome(int midi_dev
) 1302 mpu_cmd(midi_dev
,0x84,0);/* Disable metronome */ 1305 static voidsetup_metronome(int midi_dev
) 1307 int numerator
, denominator
; 1308 int clks_per_click
, num_32nds_per_beat
; 1309 int beats_per_measure
; 1311 numerator
= ((unsigned) metronome_mode
>>24) &0xff; 1312 denominator
= ((unsigned) metronome_mode
>>16) &0xff; 1313 clks_per_click
= ((unsigned) metronome_mode
>>8) &0xff; 1314 num_32nds_per_beat
= (unsigned) metronome_mode
&0xff; 1315 beats_per_measure
= (numerator
*4) >> denominator
; 1318 mpu_cmd(midi_dev
,0x84,0);/* Disable metronome */ 1321 mpu_cmd(midi_dev
,0xE4, clks_per_click
); 1322 mpu_cmd(midi_dev
,0xE6, beats_per_measure
); 1323 mpu_cmd(midi_dev
,0x83,0);/* Enable metronome without accents */ 1327 static intmpu_start_timer(int midi_dev
) 1330 set_timer_mode(midi_dev
); 1333 return TIMER_NOT_ARMED
;/* Already running */ 1335 if(timer_mode
& TMR_INTERNAL
) 1337 mpu_cmd(midi_dev
,0x02,0);/* Send MIDI start */ 1339 return TIMER_NOT_ARMED
; 1343 mpu_cmd(midi_dev
,0x35,0);/* Enable mode messages to PC */ 1344 mpu_cmd(midi_dev
,0x38,0);/* Enable sys common messages to PC */ 1345 mpu_cmd(midi_dev
,0x39,0);/* Enable real time messages to PC */ 1346 mpu_cmd(midi_dev
,0x97,0);/* Enable system exclusive messages to PC */ 1351 static intmpu_timer_open(int dev
,int mode
) 1353 int midi_dev
= sound_timer_devs
[dev
]->devlink
; 1360 mpu_cmd(midi_dev
,0xE0,50); 1361 curr_timebase
= hw_timebase
=120; 1362 set_timebase(midi_dev
,120); 1365 set_timer_mode(midi_dev
); 1367 mpu_cmd(midi_dev
,0xe7,0x04);/* Send all clocks to host */ 1368 mpu_cmd(midi_dev
,0x95,0);/* Enable clock to host */ 1373 static voidmpu_timer_close(int dev
) 1375 int midi_dev
= sound_timer_devs
[dev
]->devlink
; 1377 timer_open
= tmr_running
=0; 1378 mpu_cmd(midi_dev
,0x15,0);/* Stop all */ 1379 mpu_cmd(midi_dev
,0x94,0);/* Disable clock to host */ 1380 mpu_cmd(midi_dev
,0x8c,0);/* Disable measure end messages to host */ 1381 stop_metronome(midi_dev
); 1384 static intmpu_timer_event(int dev
,unsigned char*event
) 1386 unsigned char command
= event
[1]; 1387 unsigned long parm
= *(unsigned int*) &event
[4]; 1388 int midi_dev
= sound_timer_devs
[dev
]->devlink
; 1393 parm
+= prev_event_time
; 1399 if(parm
<= curr_ticks
)/* It's the time */ 1400 return TIMER_NOT_ARMED
; 1402 next_event_time
= prev_event_time
= time
; 1411 returnmpu_start_timer(midi_dev
); 1414 mpu_cmd(midi_dev
,0x01,0);/* Send MIDI stop */ 1415 stop_metronome(midi_dev
); 1422 mpu_cmd(midi_dev
,0x03,0);/* Send MIDI continue */ 1423 setup_metronome(midi_dev
); 1434 if(mpu_cmd(midi_dev
,0xE0, parm
) <0) 1435 printk(KERN_WARNING
"mpu401: Can't set tempo to %d\n", (int) parm
); 1441 seq_copy_to_input(event
,8); 1445 if(metronome_mode
)/* Metronome enabled */ 1447 metronome_mode
= parm
; 1448 setup_metronome(midi_dev
); 1454 return TIMER_NOT_ARMED
; 1457 static unsigned longmpu_timer_get_time(int dev
) 1465 static intmpu_timer_ioctl(int dev
,unsigned int command
, caddr_t arg
) 1467 int midi_dev
= sound_timer_devs
[dev
]->devlink
; 1471 case SNDCTL_TMR_SOURCE
: 1482 if(timer_mode
& TMR_MODE_CLS
) 1483 mpu_cmd(midi_dev
,0x3c,0);/* Use CLS sync */ 1484 else if(timer_mode
& TMR_MODE_SMPTE
) 1485 mpu_cmd(midi_dev
,0x3d,0);/* Use SMPTE sync */ 1487 return(*(int*) arg
= timer_mode
); 1491 case SNDCTL_TMR_START
: 1492 mpu_start_timer(midi_dev
); 1495 case SNDCTL_TMR_STOP
: 1497 mpu_cmd(midi_dev
,0x01,0);/* Send MIDI stop */ 1498 stop_metronome(midi_dev
); 1501 case SNDCTL_TMR_CONTINUE
: 1505 mpu_cmd(midi_dev
,0x03,0);/* Send MIDI continue */ 1508 case SNDCTL_TMR_TIMEBASE
: 1514 set_timebase(midi_dev
, val
); 1515 return(*(int*) arg
= curr_timebase
); 1519 case SNDCTL_TMR_TEMPO
: 1532 if((ret
=mpu_cmd(midi_dev
,0xE0, val
)) <0) 1534 printk(KERN_WARNING
"mpu401: Can't set tempo to %d\n", (int) val
); 1539 return(*(int*) arg
= curr_tempo
); 1543 case SNDCTL_SEQ_CTRLRATE
: 1548 if(val
!=0)/* Can't change */ 1550 return(*(int*) arg
= ((curr_tempo
* curr_timebase
) +30) /60); 1554 case SNDCTL_SEQ_GETTIME
: 1555 return(*(int*) arg
= curr_ticks
); 1557 case SNDCTL_TMR_METRONOME
: 1558 metronome_mode
= *(int*) arg
; 1559 setup_metronome(midi_dev
); 1567 static voidmpu_timer_arm(int dev
,long time
) 1570 time
= curr_ticks
+1; 1571 else if(time
<= curr_ticks
)/* It's the time */ 1573 next_event_time
= prev_event_time
= time
; 1577 static struct sound_timer_operations mpu_timer
= 1580 info
: {"MPU-401 Timer",0}, 1581 priority
:10,/* Priority */ 1582 devlink
:0,/* Local device link */ 1583 open
: mpu_timer_open
, 1584 close
: mpu_timer_close
, 1585 event
: mpu_timer_event
, 1586 get_time
: mpu_timer_get_time
, 1587 ioctl
: mpu_timer_ioctl
, 1588 arm_timer
: mpu_timer_arm
1591 static voidmpu_timer_interrupt(void) 1600 curr_ticks
=clocks2ticks(curr_clocks
); 1602 if(curr_ticks
>= next_event_time
) 1604 next_event_time
= (unsigned long) -1; 1609 static voidtimer_ext_event(struct mpu_config
*devc
,int event
,int parm
) 1611 int midi_dev
= devc
->devno
; 1613 if(!devc
->timer_flag
) 1619 printk("<MIDI clk>"); 1623 printk("Ext MIDI start\n"); 1626 if(timer_mode
& TMR_EXTERNAL
) 1629 setup_metronome(midi_dev
); 1631 STORE(SEQ_START_TIMER()); 1637 printk("Ext MIDI stop\n"); 1638 if(timer_mode
& TMR_EXTERNAL
) 1641 stop_metronome(midi_dev
); 1642 STORE(SEQ_STOP_TIMER()); 1647 printk("Ext MIDI continue\n"); 1648 if(timer_mode
& TMR_EXTERNAL
) 1651 setup_metronome(midi_dev
); 1652 STORE(SEQ_CONTINUE_TIMER()); 1657 printk("Songpos: %d\n", parm
); 1658 if(timer_mode
& TMR_EXTERNAL
) 1660 STORE(SEQ_SONGPOS(parm
)); 1666 static intmpu_timer_init(int midi_dev
) 1668 struct mpu_config
*devc
; 1671 devc
= &dev_conf
[midi_dev
]; 1673 if(timer_initialized
) 1674 return-1;/* There is already a similar timer */ 1676 timer_initialized
=1; 1678 mpu_timer
.devlink
= midi_dev
; 1679 dev_conf
[midi_dev
].timer_flag
=1; 1681 n
=sound_alloc_timerdev(); 1684 sound_timer_devs
[n
] = &mpu_timer
; 1686 if(devc
->version
<0x20)/* Original MPU-401 */ 1687 timer_caps
= TMR_INTERNAL
| TMR_EXTERNAL
| TMR_MODE_FSK
| TMR_MODE_MIDI
; 1691 * The version number 2.0 is used (at least) by the 1692 * MusicQuest cards and the Roland Super-MPU. 1694 * MusicQuest has given a special meaning to the bits of the 1695 * revision number. The Super-MPU returns 0. 1699 timer_caps
|= TMR_EXTERNAL
| TMR_MODE_MIDI
; 1701 if(devc
->revision
&0x02) 1702 timer_caps
|= TMR_MODE_CLS
; 1705 if(devc
->revision
&0x40) 1706 max_timebase
=10;/* Has the 216 and 240 ppqn modes */ 1709 timer_mode
= (TMR_INTERNAL
| TMR_MODE_MIDI
) & timer_caps
; 1714 EXPORT_SYMBOL(probe_mpu401
); 1715 EXPORT_SYMBOL(attach_mpu401
); 1716 EXPORT_SYMBOL(unload_mpu401
); 1717 EXPORT_SYMBOL(intchk_mpu401
); 1718 EXPORT_SYMBOL(mpuintr
); 1720 static struct address_info cfg
; 1722 static int __initdata io
= -1; 1723 static int __initdata irq
= -1; 1725 MODULE_PARM(irq
,"i"); 1726 MODULE_PARM(io
,"i"); 1728 int __init
init_mpu401(void) 1730 /* Can be loaded either for module use or to provide functions 1732 if(io
!= -1&& irq
!= -1) { 1735 if(probe_mpu401(&cfg
) ==0) 1737 attach_mpu401(&cfg
, THIS_MODULE
); 1743 void __exit
cleanup_mpu401(void) 1745 if(io
!= -1&& irq
!= -1) { 1746 /* Check for use by, for example, sscape driver */ 1747 unload_mpu401(&cfg
); 1751 module_init(init_mpu401
); 1752 module_exit(cleanup_mpu401
); 1755 static int __init
setup_mpu401(char*str
) 1760 str
=get_options(str
,ARRAY_SIZE(ints
), ints
); 1768 __setup("mpu401=", setup_mpu401
);