4 * The sequencer personality manager. 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 : reformatted and fixed a pair of null pointer bugs 17 #include <linux/kmod.h> 20 #include"sound_config.h" 24 static int sequencer_ok
=0; 25 static struct sound_timer_operations
*tmr
; 26 static int tmr_no
= -1;/* Currently selected timer */ 27 static int pending_timer
= -1;/* For timer change operation */ 28 externunsigned long seq_time
; 30 static int obsolete_api_used
=0; 33 * Local counts for number of synth and MIDI devices. These are initialized 34 * by the sequencer_open. 36 static int max_mididev
=0; 37 static int max_synthdev
=0; 40 * The seq_mode gives the operating mode of the sequencer: 41 * 1 = level1 (the default) 42 * 2 = level2 (extended capabilities) 47 static int seq_mode
= SEQ_1
; 49 staticDECLARE_WAIT_QUEUE_HEAD(seq_sleeper
); 50 staticDECLARE_WAIT_QUEUE_HEAD(midi_sleeper
); 52 static int midi_opened
[MAX_MIDI_DEV
] = { 56 static int midi_written
[MAX_MIDI_DEV
] = { 60 static unsigned long prev_input_time
=0; 61 static int prev_event_time
; 68 static unsigned char*queue
= NULL
; 69 static unsigned char*iqueue
= NULL
; 71 staticvolatileint qhead
=0, qtail
=0, qlen
=0; 72 staticvolatileint iqhead
=0, iqtail
=0, iqlen
=0; 73 staticvolatileint seq_playing
=0; 74 staticvolatileint sequencer_busy
=0; 75 static int output_threshold
; 76 static long pre_event_timeout
; 77 static unsigned synth_open_mask
; 79 static intseq_queue(unsigned char*note
,char nonblock
); 80 static voidseq_startplay(void); 81 static intseq_sync(void); 82 static voidseq_reset(void); 84 #if MAX_SYNTH_DEV > 15 85 #error Too many synthesizer devices enabled. 88 intsequencer_read(int dev
,struct file
*file
,char*buf
,int count
) 96 ev_len
= seq_mode
== SEQ_1
?4:8; 103 if(file
->f_flags
& O_NONBLOCK
) { 104 restore_flags(flags
); 108 interruptible_sleep_on_timeout(&midi_sleeper
, 112 restore_flags(flags
); 116 while(iqlen
&& c
>= ev_len
) 118 char*fixit
= (char*) &iqueue
[iqhead
* IEV_SZ
]; 119 copy_to_user(&(buf
)[p
], fixit
, ev_len
); 123 iqhead
= (iqhead
+1) % SEQ_MAX_QUEUE
; 126 restore_flags(flags
); 130 static voidsequencer_midi_output(int dev
) 137 voidseq_copy_to_input(unsigned char*event_rec
,int len
) 142 * Verify that the len is valid for the current mode. 145 if(len
!=4&& len
!=8) 147 if((seq_mode
== SEQ_1
) != (len
==4)) 150 if(iqlen
>= (SEQ_MAX_QUEUE
-1)) 151 return;/* Overflow */ 155 memcpy(&iqueue
[iqtail
* IEV_SZ
], event_rec
, len
); 157 iqtail
= (iqtail
+1) % SEQ_MAX_QUEUE
; 158 wake_up(&midi_sleeper
); 159 restore_flags(flags
); 162 static voidsequencer_midi_input(int dev
,unsigned char data
) 165 unsigned char event_rec
[4]; 167 if(data
==0xfe)/* Ignore active sensing */ 170 tstamp
= jiffies
- seq_time
; 172 if(tstamp
!= prev_input_time
) 174 tstamp
= (tstamp
<<8) | SEQ_WAIT
; 175 seq_copy_to_input((unsigned char*) &tstamp
,4); 176 prev_input_time
= tstamp
; 178 event_rec
[0] = SEQ_MIDIPUTC
; 183 seq_copy_to_input(event_rec
,4); 186 voidseq_input_event(unsigned char*event_rec
,int len
) 188 unsigned long this_time
; 190 if(seq_mode
== SEQ_2
) 191 this_time
= tmr
->get_time(tmr_no
); 193 this_time
= jiffies
- seq_time
; 195 if(this_time
!= prev_input_time
) 197 unsigned char tmp_event
[8]; 199 tmp_event
[0] = EV_TIMING
; 200 tmp_event
[1] = TMR_WAIT_ABS
; 203 *(unsigned int*) &tmp_event
[4] = this_time
; 205 seq_copy_to_input(tmp_event
,8); 206 prev_input_time
= this_time
; 208 seq_copy_to_input(event_rec
, len
); 211 intsequencer_write(int dev
,struct file
*file
,const char*buf
,int count
) 213 unsigned char event_rec
[EV_SZ
], ev_code
; 214 int p
=0, c
, ev_size
; 216 int mode
=translate_mode(file
); 220 DEB(printk("sequencer_write(dev=%d, count=%d)\n", dev
, count
)); 222 if(mode
== OPEN_READ
) 229 copy_from_user((char*) event_rec
, &(buf
)[p
],4); 230 ev_code
= event_rec
[0]; 232 if(ev_code
== SEQ_FULLSIZE
) 236 dev
= *(unsigned short*) &event_rec
[2]; 237 if(dev
<0|| dev
>= max_synthdev
|| synth_devs
[dev
] == NULL
) 240 if(!(synth_open_mask
& (1<< dev
))) 243 fmt
= (*(short*) &event_rec
[0]) &0xffff; 244 err
= synth_devs
[dev
]->load_patch(dev
, fmt
, buf
, p
+4, c
,0); 252 if(seq_mode
== SEQ_2
&& ev_code
== SEQ_EXTENDED
) 254 printk(KERN_WARNING
"Sequencer: Invalid level 2 event %x\n", ev_code
); 265 copy_from_user((char*) &event_rec
[4], &(buf
)[p
+4],4); 270 if(seq_mode
== SEQ_2
) 272 printk(KERN_WARNING
"Sequencer: 4 byte event in level 2 mode\n"); 277 if(event_rec
[0] != SEQ_MIDIPUTC
) 278 obsolete_api_used
=1; 281 if(event_rec
[0] == SEQ_MIDIPUTC
) 283 if(!midi_opened
[event_rec
[2]]) 286 int dev
= event_rec
[2]; 288 if(dev
>= max_mididev
|| midi_devs
[dev
]==NULL
) 290 /*printk("Sequencer Error: Nonexistent MIDI device %d\n", dev);*/ 293 mode
=translate_mode(file
); 295 if((err
= midi_devs
[dev
]->open(dev
, mode
, 296 sequencer_midi_input
, sequencer_midi_output
)) <0) 299 printk(KERN_WARNING
"Sequencer Error: Unable to open Midi #%d\n", dev
); 305 if(!seq_queue(event_rec
, (file
->f_flags
& (O_NONBLOCK
) ?1:0))) 307 int processed
= count
- c
; 312 if(!processed
&& (file
->f_flags
& O_NONBLOCK
)) 327 static intseq_queue(unsigned char*note
,char nonblock
) 331 * Test if there is space in the queue 334 if(qlen
>= SEQ_MAX_QUEUE
) 337 * Give chance to drain the queue 340 if(!nonblock
&& qlen
>= SEQ_MAX_QUEUE
&& !waitqueue_active(&seq_sleeper
)) { 342 * Sleep until there is enough space on the queue 344 interruptible_sleep_on(&seq_sleeper
); 346 if(qlen
>= SEQ_MAX_QUEUE
) 352 memcpy(&queue
[qtail
* EV_SZ
], note
, EV_SZ
); 354 qtail
= (qtail
+1) % SEQ_MAX_QUEUE
; 360 static intextended_event(unsigned char*q
) 364 if(dev
<0|| dev
>= max_synthdev
) 367 if(!(synth_open_mask
& (1<< dev
))) 373 synth_devs
[dev
]->kill_note(dev
, q
[3], q
[4], q
[5]); 377 if(q
[4] >127&& q
[4] !=255) 382 synth_devs
[dev
]->kill_note(dev
, q
[3], q
[4], q
[5]); 385 synth_devs
[dev
]->start_note(dev
, q
[3], q
[4], q
[5]); 389 synth_devs
[dev
]->set_instr(dev
, q
[3], q
[4]); 393 synth_devs
[dev
]->aftertouch(dev
, q
[3], q
[4]); 397 synth_devs
[dev
]->panning(dev
, q
[3], (char) q
[4]); 401 synth_devs
[dev
]->controller(dev
, q
[3], q
[4], (short) (q
[5] | (q
[6] <<8))); 405 if(synth_devs
[dev
]->volume_method
!= NULL
) 406 synth_devs
[dev
]->volume_method(dev
, q
[3]); 415 static intfind_voice(int dev
,int chn
,int note
) 420 key
= (chn
<<8) | (note
+1); 421 for(i
=0; i
< synth_devs
[dev
]->alloc
.max_voice
; i
++) 422 if(synth_devs
[dev
]->alloc
.map
[i
] == key
) 427 static intalloc_voice(int dev
,int chn
,int note
) 432 key
= (chn
<<8) | (note
+1); 434 voice
= synth_devs
[dev
]->alloc_voice(dev
, chn
, note
, 435 &synth_devs
[dev
]->alloc
); 436 synth_devs
[dev
]->alloc
.map
[voice
] = key
; 437 synth_devs
[dev
]->alloc
.alloc_times
[voice
] = 438 synth_devs
[dev
]->alloc
.timestamp
++; 442 static voidseq_chn_voice_event(unsigned char*event_rec
) 444 #define dev event_rec[1] 445 #define cmd event_rec[2] 446 #define chn event_rec[3] 447 #define note event_rec[4] 448 #define parm event_rec[5] 452 if((int) dev
> max_synthdev
|| synth_devs
[dev
] == NULL
) 454 if(!(synth_open_mask
& (1<< dev
))) 459 if(seq_mode
== SEQ_2
) 461 if(synth_devs
[dev
]->alloc_voice
) 462 voice
=find_voice(dev
, chn
, note
); 464 if(cmd
== MIDI_NOTEON
&& parm
==0) 474 if(note
>127&& note
!=255)/* Not a seq2 feature */ 477 if(voice
== -1&& seq_mode
== SEQ_2
&& synth_devs
[dev
]->alloc_voice
) 479 /* Internal synthesizer (FM, GUS, etc) */ 480 voice
=alloc_voice(dev
, chn
, note
); 485 if(seq_mode
== SEQ_2
&& (int) dev
< num_synths
) 488 * The MIDI channel 10 is a percussive channel. Use the note 489 * number to select the proper patch (128 to 255) to play. 494 synth_devs
[dev
]->set_instr(dev
, voice
,128+ note
); 495 synth_devs
[dev
]->chn_info
[chn
].pgm_num
=128+ note
; 497 synth_devs
[dev
]->setup_voice(dev
, voice
, chn
); 499 synth_devs
[dev
]->start_note(dev
, voice
, note
, parm
); 505 synth_devs
[dev
]->kill_note(dev
, voice
, note
, parm
); 508 case MIDI_KEY_PRESSURE
: 511 synth_devs
[dev
]->aftertouch(dev
, voice
, parm
); 524 static voidseq_chn_common_event(unsigned char*event_rec
) 526 unsigned char dev
= event_rec
[1]; 527 unsigned char cmd
= event_rec
[2]; 528 unsigned char chn
= event_rec
[3]; 529 unsigned char p1
= event_rec
[4]; 531 /* unsigned char p2 = event_rec[5]; */ 532 unsigned short w14
= *(short*) &event_rec
[6]; 534 if((int) dev
> max_synthdev
|| synth_devs
[dev
] == NULL
) 536 if(!(synth_open_mask
& (1<< dev
))) 543 case MIDI_PGM_CHANGE
: 544 if(seq_mode
== SEQ_2
) 546 synth_devs
[dev
]->chn_info
[chn
].pgm_num
= p1
; 547 if((int) dev
>= num_synths
) 548 synth_devs
[dev
]->set_instr(dev
, chn
, p1
); 551 synth_devs
[dev
]->set_instr(dev
, chn
, p1
); 555 case MIDI_CTL_CHANGE
: 556 if(seq_mode
== SEQ_2
) 558 if(chn
>15|| p1
>127) 561 synth_devs
[dev
]->chn_info
[chn
].controllers
[p1
] = w14
&0x7f; 563 if(p1
<32)/* Setting MSB should clear LSB to 0 */ 564 synth_devs
[dev
]->chn_info
[chn
].controllers
[p1
+32] =0; 566 if((int) dev
< num_synths
) 571 if(p1
<64)/* Combine MSB and LSB */ 573 val
= ((synth_devs
[dev
]-> 574 chn_info
[chn
].controllers
[p1
& ~32] &0x7f) <<7) 576 chn_info
[chn
].controllers
[p1
|32] &0x7f); 579 /* Handle all playing notes on this channel */ 581 key
= ((int) chn
<<8); 583 for(i
=0; i
< synth_devs
[dev
]->alloc
.max_voice
; i
++) 584 if((synth_devs
[dev
]->alloc
.map
[i
] &0xff00) == key
) 585 synth_devs
[dev
]->controller(dev
, i
, p1
, val
); 588 synth_devs
[dev
]->controller(dev
, chn
, p1
, w14
); 591 synth_devs
[dev
]->controller(dev
, chn
, p1
, w14
); 594 case MIDI_PITCH_BEND
: 595 if(seq_mode
== SEQ_2
) 597 synth_devs
[dev
]->chn_info
[chn
].bender_value
= w14
; 599 if((int) dev
< num_synths
) 601 /* Handle all playing notes on this channel */ 606 for(i
=0; i
< synth_devs
[dev
]->alloc
.max_voice
; i
++) 607 if((synth_devs
[dev
]->alloc
.map
[i
] &0xff00) == key
) 608 synth_devs
[dev
]->bender(dev
, i
, w14
); 611 synth_devs
[dev
]->bender(dev
, chn
, w14
); 614 synth_devs
[dev
]->bender(dev
, chn
, w14
); 621 static intseq_timing_event(unsigned char*event_rec
) 623 unsigned char cmd
= event_rec
[1]; 624 unsigned int parm
= *(int*) &event_rec
[4]; 626 if(seq_mode
== SEQ_2
) 630 if((ret
= tmr
->event(tmr_no
, event_rec
)) == TIMER_ARMED
) 631 if((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
) 632 wake_up(&seq_sleeper
); 638 parm
+= prev_event_time
; 641 * NOTE! No break here. Execution of TMR_WAIT_REL continues in the 642 * next case (TMR_WAIT_ABS) 651 prev_event_time
= time
; 654 request_sound_timer(time
); 656 if((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
) 657 wake_up(&seq_sleeper
); 678 if(seq_mode
== SEQ_2
) 679 seq_copy_to_input(event_rec
,8); 682 parm
= (parm
<<8| SEQ_ECHO
); 683 seq_copy_to_input((unsigned char*) &parm
,4); 690 return TIMER_NOT_ARMED
; 693 static voidseq_local_event(unsigned char*event_rec
) 695 unsigned char cmd
= event_rec
[1]; 696 unsigned int parm
= *((unsigned int*) &event_rec
[4]); 700 case LOCL_STARTAUDIO
: 701 DMAbuf_start_devices(parm
); 708 static voidseq_sysex_message(unsigned char*event_rec
) 710 int dev
= event_rec
[1]; 712 unsigned char*buf
= &event_rec
[2]; 714 if((int) dev
> max_synthdev
) 716 if(!(synth_open_mask
& (1<< dev
))) 722 for(i
=0; i
<6&& buf
[i
] !=0xff; i
++) 725 if(!synth_devs
[dev
]->send_sysex
) 728 synth_devs
[dev
]->send_sysex(dev
, buf
, l
); 731 static intplay_event(unsigned char*q
) 734 * NOTE! This routine returns 735 * 0 = normal event played. 736 * 1 = Timer armed. Suspend playback until timer callback. 737 * 2 = MIDI output buffer full. Restore queue and suspend until timer 744 if(synth_open_mask
& (1<<0)) 746 synth_devs
[0]->kill_note(0, q
[1],255, q
[3]); 750 if(q
[4] <128|| q
[4] ==255) 751 if(synth_open_mask
& (1<<0)) 753 synth_devs
[0]->start_note(0, q
[1], q
[2], q
[3]); 757 delay
= (unsigned int*) q
;/* 758 * Bytes 1 to 3 are containing the * 761 *delay
= (*delay
>>8) &0xffffff; 769 prev_event_time
= time
; 771 request_sound_timer(time
); 773 if((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
) 774 wake_up(&seq_sleeper
); 776 * The timer is now active and will reinvoke this function 777 * after the timer expires. Return to the caller now. 784 if(synth_open_mask
& (1<<0)) 786 synth_devs
[0]->set_instr(0, q
[1], q
[2]); 789 case SEQ_SYNCTIMER
:/* 798 * Put a midi character 800 if(midi_opened
[q
[2]]) 806 if(dev
<0|| dev
>= num_midis
|| midi_devs
[dev
] == NULL
) 809 if(!midi_devs
[dev
]->outputc(dev
, q
[1])) 812 * Output FIFO is full. Wait one timer cycle and try again. 816 request_sound_timer(-1); 820 midi_written
[dev
] =1; 825 seq_copy_to_input(q
,4);/* 826 * Echo back to the process 831 if((int) q
[1] < max_synthdev
) 832 synth_devs
[q
[1]]->hw_control(q
[1], q
); 840 seq_chn_voice_event(q
); 844 seq_chn_common_event(q
); 848 if(seq_timing_event(q
) == TIMER_ARMED
) 859 seq_sysex_message(q
); 867 static voidseq_startplay(void) 870 int this_one
, action
; 877 qhead
= ((this_one
= qhead
) +1) % SEQ_MAX_QUEUE
; 879 restore_flags(flags
); 883 if((action
=play_event(&queue
[this_one
* EV_SZ
]))) 884 {/* Suspend playback. Next timer routine invokes this routine again */ 896 if((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
) 897 wake_up(&seq_sleeper
); 900 static voidreset_controllers(int dev
,unsigned char*controller
,int update_dev
) 903 for(i
=0; i
<128; i
++) 904 controller
[i
] = ctrl_def_values
[i
]; 907 static voidsetup_mode2(void) 911 max_synthdev
= num_synths
; 913 for(dev
=0; dev
< num_midis
; dev
++) 915 if(midi_devs
[dev
] && midi_devs
[dev
]->converter
!= NULL
) 917 synth_devs
[max_synthdev
++] = midi_devs
[dev
]->converter
; 921 for(dev
=0; dev
< max_synthdev
; dev
++) 925 synth_devs
[dev
]->sysex_ptr
=0; 926 synth_devs
[dev
]->emulation
=0; 928 for(chn
=0; chn
<16; chn
++) 930 synth_devs
[dev
]->chn_info
[chn
].pgm_num
=0; 931 reset_controllers(dev
, 932 synth_devs
[dev
]->chn_info
[chn
].controllers
,0); 933 synth_devs
[dev
]->chn_info
[chn
].bender_value
= (1<<7);/* Neutral */ 934 synth_devs
[dev
]->chn_info
[chn
].bender_range
=200; 941 intsequencer_open(int dev
,struct file
*file
) 950 level
= ((dev
&0x0f) == SND_DEV_SEQ2
) ?2:1; 953 mode
=translate_mode(file
); 955 DEB(printk("sequencer_open(dev=%d)\n", dev
)); 959 /* printk("Sound card: sequencer not initialized\n");*/ 962 if(dev
)/* Patch manager device (obsolete) */ 965 if(synth_devs
[dev
] == NULL
) 966 request_module("synth0"); 968 if(mode
== OPEN_READ
) 972 /*printk("Sequencer: No MIDI devices. Input not possible\n");*/ 981 restore_flags(flags
); 985 obsolete_api_used
=0; 986 restore_flags(flags
); 988 max_mididev
= num_midis
; 989 max_synthdev
= num_synths
; 990 pre_event_timeout
= MAX_SCHEDULE_TIMEOUT
; 993 if(pending_timer
!= -1) 995 tmr_no
= pending_timer
; 998 if(tmr_no
== -1)/* Not selected yet */ 1003 for(i
=0; i
< num_sound_timers
; i
++) 1004 if(sound_timer_devs
[i
] && sound_timer_devs
[i
]->priority
> best
) 1007 best
= sound_timer_devs
[i
]->priority
; 1009 if(tmr_no
== -1)/* Should not be */ 1012 tmr
= sound_timer_devs
[tmr_no
]; 1018 /*printk("sequencer: No timer for level 2\n");*/ 1024 if(!max_synthdev
&& !max_mididev
) 1032 for(i
=0; i
< max_mididev
; i
++) 1038 for(i
=0; i
< max_synthdev
; i
++) 1040 if(synth_devs
[i
]==NULL
) 1043 if(synth_devs
[i
]->owner
) 1044 __MOD_INC_USE_COUNT(synth_devs
[i
]->owner
); 1046 if((tmp
= synth_devs
[i
]->open(i
, mode
)) <0) 1048 printk(KERN_WARNING
"Sequencer: Warning! Cannot open synth device #%d (%d)\n", i
, tmp
); 1049 if(synth_devs
[i
]->midi_dev
) 1050 printk(KERN_WARNING
"(Maps to MIDI dev #%d)\n", synth_devs
[i
]->midi_dev
); 1054 synth_open_mask
|= (1<< i
); 1055 if(synth_devs
[i
]->midi_dev
) 1056 midi_opened
[synth_devs
[i
]->midi_dev
] =1; 1065 if(seq_mode
== SEQ_1
&& (mode
== OPEN_READ
|| mode
== OPEN_READWRITE
)) 1068 * Initialize midi input devices 1071 for(i
=0; i
< max_mididev
; i
++) 1072 if(!midi_opened
[i
] && midi_devs
[i
]) 1074 if(midi_devs
[i
]->owner
) 1075 __MOD_INC_USE_COUNT(midi_devs
[i
]->owner
); 1077 if((retval
= midi_devs
[i
]->open(i
, mode
, 1078 sequencer_midi_input
, sequencer_midi_output
)) >=0) 1085 if(seq_mode
== SEQ_2
) { 1087 __MOD_INC_USE_COUNT(tmr
->owner
); 1088 tmr
->open(tmr_no
, seq_mode
); 1091 init_waitqueue_head(&seq_sleeper
); 1092 init_waitqueue_head(&midi_sleeper
); 1093 output_threshold
= SEQ_MAX_QUEUE
/2; 1098 voidseq_drain_midi_queues(void) 1103 * Give the Midi drivers time to drain their output queues 1108 while(!signal_pending(current
) && n
) 1112 for(i
=0; i
< max_mididev
; i
++) 1113 if(midi_opened
[i
] && midi_written
[i
]) 1114 if(midi_devs
[i
]->buffer_status
!= NULL
) 1115 if(midi_devs
[i
]->buffer_status(i
)) 1119 * Let's have a delay 1123 interruptible_sleep_on_timeout(&seq_sleeper
, 1128 voidsequencer_release(int dev
,struct file
*file
) 1131 int mode
=translate_mode(file
); 1135 DEB(printk("sequencer_release(dev=%d)\n", dev
)); 1138 * Wait until the queue is empty (if we don't have nonblock) 1141 if(mode
!= OPEN_READ
&& !(file
->f_flags
& O_NONBLOCK
)) 1143 while(!signal_pending(current
) && qlen
>0) 1146 interruptible_sleep_on_timeout(&seq_sleeper
, 1152 if(mode
!= OPEN_READ
) 1153 seq_drain_midi_queues();/* 1154 * Ensure the output queues are empty 1157 if(mode
!= OPEN_READ
) 1158 seq_drain_midi_queues();/* 1159 * Flush the all notes off messages 1162 for(i
=0; i
< max_synthdev
; i
++) 1164 if(synth_open_mask
& (1<< i
))/* 1169 synth_devs
[i
]->close(i
); 1171 if(synth_devs
[i
]->owner
) 1172 __MOD_DEC_USE_COUNT(synth_devs
[i
]->owner
); 1174 if(synth_devs
[i
]->midi_dev
) 1175 midi_opened
[synth_devs
[i
]->midi_dev
] =0; 1179 for(i
=0; i
< max_mididev
; i
++) 1181 if(midi_opened
[i
]) { 1182 midi_devs
[i
]->close(i
); 1183 if(midi_devs
[i
]->owner
) 1184 __MOD_DEC_USE_COUNT(midi_devs
[i
]->owner
); 1188 if(seq_mode
== SEQ_2
) { 1191 __MOD_DEC_USE_COUNT(tmr
->owner
); 1194 if(obsolete_api_used
) 1195 printk(KERN_WARNING
"/dev/music: Obsolete (4 byte) API was used by %s\n", current
->comm
); 1199 static intseq_sync(void) 1201 unsigned long flags
; 1203 if(qlen
&& !seq_playing
&& !signal_pending(current
)) 1209 interruptible_sleep_on_timeout(&seq_sleeper
, HZ
); 1210 restore_flags(flags
); 1214 static voidmidi_outc(int dev
,unsigned char data
) 1217 * NOTE! Calls sleep(). Don't call this from interrupt. 1221 unsigned long flags
; 1224 * This routine sends one byte to the Midi channel. 1225 * If the output FIFO is full, it waits until there 1226 * is space in the queue 1229 n
=3* HZ
;/* Timeout */ 1233 while(n
&& !midi_devs
[dev
]->outputc(dev
, data
)) { 1234 interruptible_sleep_on_timeout(&seq_sleeper
, HZ
/25); 1237 restore_flags(flags
); 1240 static voidseq_reset(void) 1243 * NOTE! Calls sleep(). Don't call this from interrupt. 1248 unsigned long flags
; 1256 qlen
= qhead
= qtail
=0; 1257 iqlen
= iqhead
= iqtail
=0; 1259 for(i
=0; i
< max_synthdev
; i
++) 1260 if(synth_open_mask
& (1<< i
)) 1262 synth_devs
[i
]->reset(i
); 1264 if(seq_mode
== SEQ_2
) 1266 for(chn
=0; chn
<16; chn
++) 1267 for(i
=0; i
< max_synthdev
; i
++) 1268 if(synth_open_mask
& (1<< i
)) 1271 synth_devs
[i
]->controller(i
, chn
,123,0);/* All notes off */ 1272 synth_devs
[i
]->controller(i
, chn
,121,0);/* Reset all ctl */ 1273 synth_devs
[i
]->bender(i
, chn
,1<<13);/* Bender off */ 1276 else/* seq_mode == SEQ_1 */ 1278 for(i
=0; i
< max_mididev
; i
++) 1279 if(midi_written
[i
])/* 1280 * Midi used. Some notes may still be playing 1284 * Sending just a ACTIVE SENSING message should be enough to stop all 1285 * playing notes. Since there are devices not recognizing the 1286 * active sensing, we have to send some all notes off messages also. 1290 for(chn
=0; chn
<16; chn
++) 1292 midi_outc(i
, (unsigned char) (0xb0+ (chn
&0x0f)));/* control change */ 1293 midi_outc(i
,0x7b);/* All notes off */ 1294 midi_outc(i
,0);/* Dummy parameter */ 1297 midi_devs
[i
]->close(i
); 1309 if(waitqueue_active(&seq_sleeper
)) { 1310 /* printk( "Sequencer Warning: Unexpected sleeping process - Waking up\n"); */ 1311 wake_up(&seq_sleeper
); 1313 restore_flags(flags
); 1316 static voidseq_panic(void) 1319 * This routine is called by the application in case the user 1320 * wants to reset the system to the default state. 1326 * Since some of the devices don't recognize the active sensing and 1327 * all notes off messages, we have to shut all notes manually. 1329 * TO BE IMPLEMENTED LATER 1333 * Also return the controllers to their default states 1337 intsequencer_ioctl(int dev
,struct file
*file
,unsigned int cmd
, caddr_t arg
) 1339 int midi_dev
, orig_dev
, val
, err
; 1340 int mode
=translate_mode(file
); 1341 struct synth_info inf
; 1342 struct seq_event_rec event_rec
; 1343 unsigned long flags
; 1345 orig_dev
= dev
= dev
>>4; 1349 case SNDCTL_TMR_TIMEBASE
: 1350 case SNDCTL_TMR_TEMPO
: 1351 case SNDCTL_TMR_START
: 1352 case SNDCTL_TMR_STOP
: 1353 case SNDCTL_TMR_CONTINUE
: 1354 case SNDCTL_TMR_METRONOME
: 1355 case SNDCTL_TMR_SOURCE
: 1356 if(seq_mode
!= SEQ_2
) 1358 return tmr
->ioctl(tmr_no
, cmd
, arg
); 1360 case SNDCTL_TMR_SELECT
: 1361 if(seq_mode
!= SEQ_2
) 1363 if(get_user(pending_timer
, (int*)arg
)) 1365 if(pending_timer
<0|| pending_timer
>= num_sound_timers
|| sound_timer_devs
[pending_timer
] == NULL
) 1370 val
= pending_timer
; 1373 case SNDCTL_SEQ_PANIC
: 1377 case SNDCTL_SEQ_SYNC
: 1378 if(mode
== OPEN_READ
) 1380 while(qlen
>0&& !signal_pending(current
)) 1382 return qlen
? -EINTR
:0; 1384 case SNDCTL_SEQ_RESET
: 1388 case SNDCTL_SEQ_TESTMIDI
: 1389 if(__get_user(midi_dev
, (int*)arg
)) 1391 if(midi_dev
<0|| midi_dev
>= max_mididev
|| !midi_devs
[midi_dev
]) 1394 if(!midi_opened
[midi_dev
] && 1395 (err
= midi_devs
[midi_dev
]->open(midi_dev
, mode
, sequencer_midi_input
, 1396 sequencer_midi_output
)) <0) 1398 midi_opened
[midi_dev
] =1; 1401 case SNDCTL_SEQ_GETINCOUNT
: 1402 if(mode
== OPEN_WRITE
) 1407 case SNDCTL_SEQ_GETOUTCOUNT
: 1408 if(mode
== OPEN_READ
) 1410 val
= SEQ_MAX_QUEUE
- qlen
; 1413 case SNDCTL_SEQ_GETTIME
: 1414 if(seq_mode
== SEQ_2
) 1415 return tmr
->ioctl(tmr_no
, cmd
, arg
); 1416 val
= jiffies
- seq_time
; 1419 case SNDCTL_SEQ_CTRLRATE
: 1421 * If *arg == 0, just return the current rate 1423 if(seq_mode
== SEQ_2
) 1424 return tmr
->ioctl(tmr_no
, cmd
, arg
); 1426 if(get_user(val
, (int*)arg
)) 1433 case SNDCTL_SEQ_RESETSAMPLES
: 1434 case SNDCTL_SYNTH_REMOVESAMPLE
: 1435 case SNDCTL_SYNTH_CONTROL
: 1436 if(get_user(dev
, (int*)arg
)) 1438 if(dev
<0|| dev
>= num_synths
|| synth_devs
[dev
] == NULL
) 1440 if(!(synth_open_mask
& (1<< dev
)) && !orig_dev
) 1442 return synth_devs
[dev
]->ioctl(dev
, cmd
, arg
); 1444 case SNDCTL_SEQ_NRSYNTHS
: 1448 case SNDCTL_SEQ_NRMIDIS
: 1452 case SNDCTL_SYNTH_MEMAVL
: 1453 if(get_user(dev
, (int*)arg
)) 1455 if(dev
<0|| dev
>= num_synths
|| synth_devs
[dev
] == NULL
) 1457 if(!(synth_open_mask
& (1<< dev
)) && !orig_dev
) 1459 val
= synth_devs
[dev
]->ioctl(dev
, cmd
, arg
); 1462 case SNDCTL_FM_4OP_ENABLE
: 1463 if(get_user(dev
, (int*)arg
)) 1465 if(dev
<0|| dev
>= num_synths
|| synth_devs
[dev
] == NULL
) 1467 if(!(synth_open_mask
& (1<< dev
))) 1469 synth_devs
[dev
]->ioctl(dev
, cmd
, arg
); 1472 case SNDCTL_SYNTH_INFO
: 1473 if(get_user(dev
, (int*)(&(((struct synth_info
*)arg
)->device
)))) 1475 if(dev
<0|| dev
>= max_synthdev
) 1477 if(!(synth_open_mask
& (1<< dev
)) && !orig_dev
) 1479 return synth_devs
[dev
]->ioctl(dev
, cmd
, arg
); 1481 /* Like SYNTH_INFO but returns ID in the name field */ 1482 case SNDCTL_SYNTH_ID
: 1483 if(get_user(dev
, (int*)(&(((struct synth_info
*)arg
)->device
)))) 1485 if(dev
<0|| dev
>= max_synthdev
) 1487 if(!(synth_open_mask
& (1<< dev
)) && !orig_dev
) 1489 memcpy(&inf
, synth_devs
[dev
]->info
,sizeof(inf
)); 1490 strncpy(inf
.name
, synth_devs
[dev
]->id
,sizeof(inf
.name
)); 1492 returncopy_to_user(arg
, &inf
,sizeof(inf
))?-EFAULT
:0; 1494 case SNDCTL_SEQ_OUTOFBAND
: 1495 if(copy_from_user(&event_rec
, arg
,sizeof(event_rec
))) 1499 play_event(event_rec
.arr
); 1500 restore_flags(flags
); 1503 case SNDCTL_MIDI_INFO
: 1504 if(get_user(dev
, (int*)(&(((struct midi_info
*)arg
)->device
)))) 1506 if(dev
<0|| dev
>= max_mididev
|| !midi_devs
[dev
]) 1508 midi_devs
[dev
]->info
.device
= dev
; 1509 returncopy_to_user(arg
, &midi_devs
[dev
]->info
,sizeof(struct midi_info
))?-EFAULT
:0; 1511 case SNDCTL_SEQ_THRESHOLD
: 1512 if(get_user(val
, (int*)arg
)) 1516 if(val
>= SEQ_MAX_QUEUE
) 1517 val
= SEQ_MAX_QUEUE
-1; 1518 output_threshold
= val
; 1521 case SNDCTL_MIDI_PRETIME
: 1522 if(get_user(val
, (int*)arg
)) 1526 val
= (HZ
* val
) /10; 1527 pre_event_timeout
= val
; 1531 if(mode
== OPEN_READ
) 1535 if(!(synth_open_mask
& (1<<0))) 1537 if(!synth_devs
[0]->ioctl
) 1539 return synth_devs
[0]->ioctl(0, cmd
, arg
); 1541 returnput_user(val
, (int*)arg
); 1544 /* No kernel lock - we're using the global irq lock here */ 1545 unsigned intsequencer_poll(int dev
,struct file
*file
, poll_table
* wait
) 1547 unsigned long flags
; 1548 unsigned int mask
=0; 1555 poll_wait(file
, &midi_sleeper
, wait
); 1557 mask
|= POLLIN
| POLLRDNORM
; 1560 poll_wait(file
, &seq_sleeper
, wait
); 1561 if((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
) 1562 mask
|= POLLOUT
| POLLWRNORM
; 1563 restore_flags(flags
); 1568 voidsequencer_timer(unsigned long dummy
) 1573 intnote_to_freq(int note_num
) 1577 * This routine converts a midi note to a frequency (multiplied by 1000) 1580 int note
, octave
, note_freq
; 1581 static int notes
[] = 1583 261632,277189,293671,311132,329632,349232, 1584 369998,391998,415306,440000,466162,493880 1587 #define BASE_OCTAVE 5 1589 octave
= note_num
/12; 1590 note
= note_num
%12; 1592 note_freq
= notes
[note
]; 1594 if(octave
< BASE_OCTAVE
) 1595 note_freq
>>= (BASE_OCTAVE
- octave
); 1596 else if(octave
> BASE_OCTAVE
) 1597 note_freq
<<= (octave
- BASE_OCTAVE
); 1606 unsigned longcompute_finetune(unsigned long base_freq
,int bend
,int range
, 1609 unsigned long amount
; 1610 int negative
, semitones
, cents
, multiplier
=1; 1623 bend
= bend
* range
/8192;/* Convert to cents */ 1624 bend
+= vibrato_cents
; 1629 negative
= bend
<0?1:0; 1646 semitones
= bend
/100; 1651 amount
= (int) (semitone_tuning
[semitones
] * multiplier
* cent_tuning
[cents
]) /10000; 1654 return(base_freq
*10000) / amount
;/* Bend down */ 1656 return(base_freq
* amount
) /10000;/* Bend up */ 1660 voidsequencer_init(void) 1662 /* drag in sequencer_syms.o */ 1664 externchar sequencer_syms_symbol
; 1665 sequencer_syms_symbol
=0; 1671 queue
= (unsigned char*)vmalloc(SEQ_MAX_QUEUE
* EV_SZ
); 1674 printk(KERN_ERR
"sequencer: Can't allocate memory for sequencer output queue\n"); 1677 iqueue
= (unsigned char*)vmalloc(SEQ_MAX_QUEUE
* IEV_SZ
); 1680 printk(KERN_ERR
"sequencer: Can't allocate memory for sequencer input queue\n"); 1687 voidsequencer_unload(void)