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/config.h>
18 #include <linux/kmod.h>
21 #include "sound_config.h"
24 int (*softsynthp
) (int cmd
, int parm1
, int parm2
, unsigned long parm3
) = NULL
;
26 #include "midi_ctrl.h"
28 static int sequencer_ok
= 0;
29 static struct sound_timer_operations
*tmr
;
30 static int tmr_no
= -1; /* Currently selected timer */
31 static int pending_timer
= -1; /* For timer change operation */
32 extern unsigned long seq_time
;
34 static int obsolete_api_used
= 0;
37 * Local counts for number of synth and MIDI devices. These are initialized
38 * by the sequencer_open.
40 static int max_mididev
= 0;
41 static int max_synthdev
= 0;
44 * The seq_mode gives the operating mode of the sequencer:
45 * 1 = level1 (the default)
46 * 2 = level2 (extended capabilities)
51 static int seq_mode
= SEQ_1
;
53 static DECLARE_WAIT_QUEUE_HEAD(seq_sleeper
);
54 static DECLARE_WAIT_QUEUE_HEAD(midi_sleeper
);
56 static int midi_opened
[MAX_MIDI_DEV
] = {
60 static int midi_written
[MAX_MIDI_DEV
] = {
64 static unsigned long prev_input_time
= 0;
65 static int prev_event_time
;
72 static unsigned char *queue
= NULL
;
73 static unsigned char *iqueue
= NULL
;
75 static volatile int qhead
= 0, qtail
= 0, qlen
= 0;
76 static volatile int iqhead
= 0, iqtail
= 0, iqlen
= 0;
77 static volatile int seq_playing
= 0;
78 static volatile int sequencer_busy
= 0;
79 static int output_threshold
;
80 static long pre_event_timeout
;
81 static unsigned synth_open_mask
;
83 static int seq_queue(unsigned char *note
, char nonblock
);
84 static void seq_startplay(void);
85 static int seq_sync(void);
86 static void seq_reset(void);
88 #if MAX_SYNTH_DEV > 15
89 #error Too many synthesizer devices enabled.
92 int sequencer_read(int dev
, struct file
*file
, char *buf
, int count
)
100 ev_len
= seq_mode
== SEQ_1
? 4 : 8;
107 if (file
->f_flags
& O_NONBLOCK
) {
108 restore_flags(flags
);
112 interruptible_sleep_on_timeout(&midi_sleeper
,
116 restore_flags(flags
);
120 while (iqlen
&& c
>= ev_len
)
122 char *fixit
= (char *) &iqueue
[iqhead
* IEV_SZ
];
123 copy_to_user(&(buf
)[p
], fixit
, ev_len
);
127 iqhead
= (iqhead
+ 1) % SEQ_MAX_QUEUE
;
130 restore_flags(flags
);
134 static void sequencer_midi_output(int dev
)
141 void seq_copy_to_input(unsigned char *event_rec
, int len
)
146 * Verify that the len is valid for the current mode.
149 if (len
!= 4 && len
!= 8)
151 if ((seq_mode
== SEQ_1
) != (len
== 4))
154 if (iqlen
>= (SEQ_MAX_QUEUE
- 1))
155 return; /* Overflow */
159 memcpy(&iqueue
[iqtail
* IEV_SZ
], event_rec
, len
);
161 iqtail
= (iqtail
+ 1) % SEQ_MAX_QUEUE
;
162 wake_up(&midi_sleeper
);
163 restore_flags(flags
);
166 static void sequencer_midi_input(int dev
, unsigned char data
)
169 unsigned char event_rec
[4];
171 if (data
== 0xfe) /* Ignore active sensing */
174 if (softsynthp
!= NULL
)
175 tstamp
= softsynthp(SSYN_GETTIME
, 0, 0, 0);
177 tstamp
= jiffies
- seq_time
;
179 if (tstamp
!= prev_input_time
)
181 tstamp
= (tstamp
<< 8) | SEQ_WAIT
;
182 seq_copy_to_input((unsigned char *) &tstamp
, 4);
183 prev_input_time
= tstamp
;
185 event_rec
[0] = SEQ_MIDIPUTC
;
190 seq_copy_to_input(event_rec
, 4);
193 void seq_input_event(unsigned char *event_rec
, int len
)
195 unsigned long this_time
;
197 if (seq_mode
== SEQ_2
)
198 this_time
= tmr
->get_time(tmr_no
);
199 else if (softsynthp
!= NULL
)
200 this_time
= softsynthp(SSYN_GETTIME
, 0, 0, 0);
202 this_time
= jiffies
- seq_time
;
204 if (this_time
!= prev_input_time
)
206 unsigned char tmp_event
[8];
208 tmp_event
[0] = EV_TIMING
;
209 tmp_event
[1] = TMR_WAIT_ABS
;
212 *(unsigned int *) &tmp_event
[4] = this_time
;
214 seq_copy_to_input(tmp_event
, 8);
215 prev_input_time
= this_time
;
217 seq_copy_to_input(event_rec
, len
);
220 int sequencer_write(int dev
, struct file
*file
, const char *buf
, int count
)
222 unsigned char event_rec
[EV_SZ
], ev_code
;
223 int p
= 0, c
, ev_size
;
225 int mode
= translate_mode(file
);
229 DEB(printk("sequencer_write(dev=%d, count=%d)\n", dev
, count
));
231 if (mode
== OPEN_READ
)
238 copy_from_user((char *) event_rec
, &(buf
)[p
], 4);
239 ev_code
= event_rec
[0];
241 if (ev_code
== SEQ_FULLSIZE
)
245 dev
= *(unsigned short *) &event_rec
[2];
246 if (dev
< 0 || dev
>= max_synthdev
|| synth_devs
[dev
] == NULL
)
249 if (!(synth_open_mask
& (1 << dev
)))
252 fmt
= (*(short *) &event_rec
[0]) & 0xffff;
253 err
= synth_devs
[dev
]->load_patch(dev
, fmt
, buf
, p
+ 4, c
, 0);
261 if (seq_mode
== SEQ_2
&& ev_code
== SEQ_EXTENDED
)
263 printk(KERN_WARNING
"Sequencer: Invalid level 2 event %x\n", ev_code
);
274 copy_from_user((char *) &event_rec
[4], &(buf
)[p
+ 4], 4);
279 if (seq_mode
== SEQ_2
)
281 printk(KERN_WARNING
"Sequencer: 4 byte event in level 2 mode\n");
286 if (event_rec
[0] != SEQ_MIDIPUTC
)
287 obsolete_api_used
= 1;
290 if (event_rec
[0] == SEQ_MIDIPUTC
)
292 if (!midi_opened
[event_rec
[2]])
295 int dev
= event_rec
[2];
297 if (dev
>= max_mididev
|| midi_devs
[dev
]==NULL
)
299 /*printk("Sequencer Error: Nonexistent MIDI device %d\n", dev);*/
302 mode
= translate_mode(file
);
304 if ((err
= midi_devs
[dev
]->open(dev
, mode
,
305 sequencer_midi_input
, sequencer_midi_output
)) < 0)
308 printk(KERN_WARNING
"Sequencer Error: Unable to open Midi #%d\n", dev
);
311 midi_opened
[dev
] = 1;
314 if (!seq_queue(event_rec
, (file
->f_flags
& (O_NONBLOCK
) ? 1 : 0)))
316 int processed
= count
- c
;
321 if (!processed
&& (file
->f_flags
& O_NONBLOCK
))
336 static int seq_queue(unsigned char *note
, char nonblock
)
340 * Test if there is space in the queue
343 if (qlen
>= SEQ_MAX_QUEUE
)
346 * Give chance to drain the queue
349 if (!nonblock
&& qlen
>= SEQ_MAX_QUEUE
&& !waitqueue_active(&seq_sleeper
)) {
351 * Sleep until there is enough space on the queue
353 interruptible_sleep_on(&seq_sleeper
);
355 if (qlen
>= SEQ_MAX_QUEUE
)
361 memcpy(&queue
[qtail
* EV_SZ
], note
, EV_SZ
);
363 qtail
= (qtail
+ 1) % SEQ_MAX_QUEUE
;
369 static int extended_event(unsigned char *q
)
373 if (dev
< 0 || dev
>= max_synthdev
)
376 if (!(synth_open_mask
& (1 << dev
)))
382 synth_devs
[dev
]->kill_note(dev
, q
[3], q
[4], q
[5]);
386 if (q
[4] > 127 && q
[4] != 255)
391 synth_devs
[dev
]->kill_note(dev
, q
[3], q
[4], q
[5]);
394 synth_devs
[dev
]->start_note(dev
, q
[3], q
[4], q
[5]);
398 synth_devs
[dev
]->set_instr(dev
, q
[3], q
[4]);
402 synth_devs
[dev
]->aftertouch(dev
, q
[3], q
[4]);
406 synth_devs
[dev
]->panning(dev
, q
[3], (char) q
[4]);
410 synth_devs
[dev
]->controller(dev
, q
[3], q
[4], (short) (q
[5] | (q
[6] << 8)));
414 if (synth_devs
[dev
]->volume_method
!= NULL
)
415 synth_devs
[dev
]->volume_method(dev
, q
[3]);
424 static int find_voice(int dev
, int chn
, int note
)
429 key
= (chn
<< 8) | (note
+ 1);
430 for (i
= 0; i
< synth_devs
[dev
]->alloc
.max_voice
; i
++)
431 if (synth_devs
[dev
]->alloc
.map
[i
] == key
)
436 static int alloc_voice(int dev
, int chn
, int note
)
441 key
= (chn
<< 8) | (note
+ 1);
443 voice
= synth_devs
[dev
]->alloc_voice(dev
, chn
, note
,
444 &synth_devs
[dev
]->alloc
);
445 synth_devs
[dev
]->alloc
.map
[voice
] = key
;
446 synth_devs
[dev
]->alloc
.alloc_times
[voice
] =
447 synth_devs
[dev
]->alloc
.timestamp
++;
451 static void seq_chn_voice_event(unsigned char *event_rec
)
453 #define dev event_rec[1]
454 #define cmd event_rec[2]
455 #define chn event_rec[3]
456 #define note event_rec[4]
457 #define parm event_rec[5]
461 if ((int) dev
> max_synthdev
|| synth_devs
[dev
] == NULL
)
463 if (!(synth_open_mask
& (1 << dev
)))
465 if (!synth_devs
[dev
])
468 if (seq_mode
== SEQ_2
)
470 if (synth_devs
[dev
]->alloc_voice
)
471 voice
= find_voice(dev
, chn
, note
);
473 if (cmd
== MIDI_NOTEON
&& parm
== 0)
483 if (note
> 127 && note
!= 255) /* Not a seq2 feature */
486 if (voice
== -1 && seq_mode
== SEQ_2
&& synth_devs
[dev
]->alloc_voice
)
488 /* Internal synthesizer (FM, GUS, etc) */
489 voice
= alloc_voice(dev
, chn
, note
);
494 if (seq_mode
== SEQ_2
&& (int) dev
< num_synths
)
497 * The MIDI channel 10 is a percussive channel. Use the note
498 * number to select the proper patch (128 to 255) to play.
503 synth_devs
[dev
]->set_instr(dev
, voice
, 128 + note
);
504 synth_devs
[dev
]->chn_info
[chn
].pgm_num
= 128 + note
;
506 synth_devs
[dev
]->setup_voice(dev
, voice
, chn
);
508 synth_devs
[dev
]->start_note(dev
, voice
, note
, parm
);
514 synth_devs
[dev
]->kill_note(dev
, voice
, note
, parm
);
517 case MIDI_KEY_PRESSURE
:
520 synth_devs
[dev
]->aftertouch(dev
, voice
, parm
);
533 static void seq_chn_common_event(unsigned char *event_rec
)
535 unsigned char dev
= event_rec
[1];
536 unsigned char cmd
= event_rec
[2];
537 unsigned char chn
= event_rec
[3];
538 unsigned char p1
= event_rec
[4];
540 /* unsigned char p2 = event_rec[5]; */
541 unsigned short w14
= *(short *) &event_rec
[6];
543 if ((int) dev
> max_synthdev
|| synth_devs
[dev
] == NULL
)
545 if (!(synth_open_mask
& (1 << dev
)))
547 if (!synth_devs
[dev
])
552 case MIDI_PGM_CHANGE
:
553 if (seq_mode
== SEQ_2
)
555 synth_devs
[dev
]->chn_info
[chn
].pgm_num
= p1
;
556 if ((int) dev
>= num_synths
)
557 synth_devs
[dev
]->set_instr(dev
, chn
, p1
);
560 synth_devs
[dev
]->set_instr(dev
, chn
, p1
);
564 case MIDI_CTL_CHANGE
:
565 if (seq_mode
== SEQ_2
)
567 if (chn
> 15 || p1
> 127)
570 synth_devs
[dev
]->chn_info
[chn
].controllers
[p1
] = w14
& 0x7f;
572 if (p1
< 32) /* Setting MSB should clear LSB to 0 */
573 synth_devs
[dev
]->chn_info
[chn
].controllers
[p1
+ 32] = 0;
575 if ((int) dev
< num_synths
)
577 int val
= w14
& 0x7f;
580 if (p1
< 64) /* Combine MSB and LSB */
582 val
= ((synth_devs
[dev
]->
583 chn_info
[chn
].controllers
[p1
& ~32] & 0x7f) << 7)
585 chn_info
[chn
].controllers
[p1
| 32] & 0x7f);
588 /* Handle all playing notes on this channel */
590 key
= ((int) chn
<< 8);
592 for (i
= 0; i
< synth_devs
[dev
]->alloc
.max_voice
; i
++)
593 if ((synth_devs
[dev
]->alloc
.map
[i
] & 0xff00) == key
)
594 synth_devs
[dev
]->controller(dev
, i
, p1
, val
);
597 synth_devs
[dev
]->controller(dev
, chn
, p1
, w14
);
600 synth_devs
[dev
]->controller(dev
, chn
, p1
, w14
);
603 case MIDI_PITCH_BEND
:
604 if (seq_mode
== SEQ_2
)
606 synth_devs
[dev
]->chn_info
[chn
].bender_value
= w14
;
608 if ((int) dev
< num_synths
)
610 /* Handle all playing notes on this channel */
615 for (i
= 0; i
< synth_devs
[dev
]->alloc
.max_voice
; i
++)
616 if ((synth_devs
[dev
]->alloc
.map
[i
] & 0xff00) == key
)
617 synth_devs
[dev
]->bender(dev
, i
, w14
);
620 synth_devs
[dev
]->bender(dev
, chn
, w14
);
623 synth_devs
[dev
]->bender(dev
, chn
, w14
);
630 static int seq_timing_event(unsigned char *event_rec
)
632 unsigned char cmd
= event_rec
[1];
633 unsigned int parm
= *(int *) &event_rec
[4];
635 if (seq_mode
== SEQ_2
)
639 if ((ret
= tmr
->event(tmr_no
, event_rec
)) == TIMER_ARMED
)
640 if ((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
)
641 wake_up(&seq_sleeper
);
647 parm
+= prev_event_time
;
650 * NOTE! No break here. Execution of TMR_WAIT_REL continues in the
651 * next case (TMR_WAIT_ABS)
660 prev_event_time
= time
;
663 if (softsynthp
!= NULL
)
664 softsynthp(SSYN_REQUEST
, time
, 0, 0);
666 request_sound_timer(time
);
668 if ((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
)
669 wake_up(&seq_sleeper
);
675 if (softsynthp
!= NULL
)
677 softsynthp(SSYN_START
, 0, 0, 0);
696 if (seq_mode
== SEQ_2
)
697 seq_copy_to_input(event_rec
, 8);
700 parm
= (parm
<< 8 | SEQ_ECHO
);
701 seq_copy_to_input((unsigned char *) &parm
, 4);
708 return TIMER_NOT_ARMED
;
711 static void seq_local_event(unsigned char *event_rec
)
713 unsigned char cmd
= event_rec
[1];
714 unsigned int parm
= *((unsigned int *) &event_rec
[4]);
718 case LOCL_STARTAUDIO
:
719 DMAbuf_start_devices(parm
);
726 static void seq_sysex_message(unsigned char *event_rec
)
728 int dev
= event_rec
[1];
730 unsigned char *buf
= &event_rec
[2];
732 if ((int) dev
> max_synthdev
)
734 if (!(synth_open_mask
& (1 << dev
)))
736 if (!synth_devs
[dev
])
740 for (i
= 0; i
< 6 && buf
[i
] != 0xff; i
++)
743 if (!synth_devs
[dev
]->send_sysex
)
746 synth_devs
[dev
]->send_sysex(dev
, buf
, l
);
749 static int play_event(unsigned char *q
)
752 * NOTE! This routine returns
753 * 0 = normal event played.
754 * 1 = Timer armed. Suspend playback until timer callback.
755 * 2 = MIDI output buffer full. Restore queue and suspend until timer
762 if (synth_open_mask
& (1 << 0))
764 synth_devs
[0]->kill_note(0, q
[1], 255, q
[3]);
768 if (q
[4] < 128 || q
[4] == 255)
769 if (synth_open_mask
& (1 << 0))
771 synth_devs
[0]->start_note(0, q
[1], q
[2], q
[3]);
775 delay
= (unsigned int *) q
; /*
776 * Bytes 1 to 3 are containing the *
779 *delay
= (*delay
>> 8) & 0xffffff;
787 prev_event_time
= time
;
789 if (softsynthp
!= NULL
)
790 softsynthp(SSYN_REQUEST
, time
, 0, 0);
792 request_sound_timer(time
);
794 if ((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
)
795 wake_up(&seq_sleeper
);
797 * The timer is now active and will reinvoke this function
798 * after the timer expires. Return to the caller now.
805 if (synth_open_mask
& (1 << 0))
807 synth_devs
[0]->set_instr(0, q
[1], q
[2]);
810 case SEQ_SYNCTIMER
: /*
813 if (softsynthp
!= NULL
)
819 if (softsynthp
!= NULL
)
820 softsynthp(SSYN_START
, 0, 0, 0);
823 case SEQ_MIDIPUTC
: /*
824 * Put a midi character
826 if (midi_opened
[q
[2]])
832 if (dev
< 0 || dev
>= num_midis
|| midi_devs
[dev
] == NULL
)
835 if (!midi_devs
[dev
]->outputc(dev
, q
[1]))
838 * Output FIFO is full. Wait one timer cycle and try again.
842 if (softsynthp
!= NULL
)
843 softsynthp(SSYN_REQUEST
, -1, 0, 0);
845 request_sound_timer(-1);
849 midi_written
[dev
] = 1;
854 seq_copy_to_input(q
, 4); /*
855 * Echo back to the process
860 if ((int) q
[1] < max_synthdev
)
861 synth_devs
[q
[1]]->hw_control(q
[1], q
);
869 seq_chn_voice_event(q
);
873 seq_chn_common_event(q
);
877 if (seq_timing_event(q
) == TIMER_ARMED
)
888 seq_sysex_message(q
);
896 static void seq_startplay(void)
899 int this_one
, action
;
906 qhead
= ((this_one
= qhead
) + 1) % SEQ_MAX_QUEUE
;
908 restore_flags(flags
);
912 if ((action
= play_event(&queue
[this_one
* EV_SZ
])))
913 { /* Suspend playback. Next timer routine invokes this routine again */
925 if ((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
)
926 wake_up(&seq_sleeper
);
929 static void reset_controllers(int dev
, unsigned char *controller
, int update_dev
)
932 for (i
= 0; i
< 128; i
++)
933 controller
[i
] = ctrl_def_values
[i
];
936 static void setup_mode2(void)
940 max_synthdev
= num_synths
;
942 for (dev
= 0; dev
< num_midis
; dev
++)
944 if (midi_devs
[dev
] && midi_devs
[dev
]->converter
!= NULL
)
946 synth_devs
[max_synthdev
++] = midi_devs
[dev
]->converter
;
950 for (dev
= 0; dev
< max_synthdev
; dev
++)
954 synth_devs
[dev
]->sysex_ptr
= 0;
955 synth_devs
[dev
]->emulation
= 0;
957 for (chn
= 0; chn
< 16; chn
++)
959 synth_devs
[dev
]->chn_info
[chn
].pgm_num
= 0;
960 reset_controllers(dev
,
961 synth_devs
[dev
]->chn_info
[chn
].controllers
,0);
962 synth_devs
[dev
]->chn_info
[chn
].bender_value
= (1 << 7); /* Neutral */
963 synth_devs
[dev
]->chn_info
[chn
].bender_range
= 200;
970 int sequencer_open(int dev
, struct file
*file
)
979 level
= ((dev
& 0x0f) == SND_DEV_SEQ2
) ? 2 : 1;
982 mode
= translate_mode(file
);
984 DEB(printk("sequencer_open(dev=%d)\n", dev
));
988 /* printk("Sound card: sequencer not initialized\n");*/
991 if (dev
) /* Patch manager device (obsolete) */
994 if(synth_devs
[dev
] == NULL
)
995 request_module("synth0");
997 if (mode
== OPEN_READ
)
1001 /*printk("Sequencer: No MIDI devices. Input not possible\n");*/
1010 restore_flags(flags
);
1014 obsolete_api_used
= 0;
1015 restore_flags(flags
);
1017 max_mididev
= num_midis
;
1018 max_synthdev
= num_synths
;
1019 pre_event_timeout
= MAX_SCHEDULE_TIMEOUT
;
1022 if (pending_timer
!= -1)
1024 tmr_no
= pending_timer
;
1027 if (tmr_no
== -1) /* Not selected yet */
1032 for (i
= 0; i
< num_sound_timers
; i
++)
1033 if (sound_timer_devs
[i
] && sound_timer_devs
[i
]->priority
> best
)
1036 best
= sound_timer_devs
[i
]->priority
;
1038 if (tmr_no
== -1) /* Should not be */
1041 tmr
= sound_timer_devs
[tmr_no
];
1047 /*printk("sequencer: No timer for level 2\n");*/
1053 if (!max_synthdev
&& !max_mididev
)
1059 synth_open_mask
= 0;
1061 for (i
= 0; i
< max_mididev
; i
++)
1064 midi_written
[i
] = 0;
1067 for (i
= 0; i
< max_synthdev
; i
++)
1069 if (synth_devs
[i
]==NULL
)
1072 if ((tmp
= synth_devs
[i
]->open(i
, mode
)) < 0)
1074 printk(KERN_WARNING
"Sequencer: Warning! Cannot open synth device #%d (%d)\n", i
, tmp
);
1075 if (synth_devs
[i
]->midi_dev
)
1076 printk(KERN_WARNING
"(Maps to MIDI dev #%d)\n", synth_devs
[i
]->midi_dev
);
1080 synth_open_mask
|= (1 << i
);
1081 if (synth_devs
[i
]->midi_dev
)
1082 midi_opened
[synth_devs
[i
]->midi_dev
] = 1;
1086 if (softsynthp
!= NULL
)
1091 prev_input_time
= 0;
1092 prev_event_time
= 0;
1093 if (softsynthp
!= NULL
)
1094 softsynthp(SSYN_START
, 0, 0, 0);
1096 if (seq_mode
== SEQ_1
&& (mode
== OPEN_READ
|| mode
== OPEN_READWRITE
))
1099 * Initialize midi input devices
1102 for (i
= 0; i
< max_mididev
; i
++)
1103 if (!midi_opened
[i
] && midi_devs
[i
])
1105 if ((retval
= midi_devs
[i
]->open(i
, mode
,
1106 sequencer_midi_input
, sequencer_midi_output
)) >= 0)
1112 if (seq_mode
== SEQ_2
)
1113 tmr
->open(tmr_no
, seq_mode
);
1115 init_waitqueue_head(&seq_sleeper
);
1116 init_waitqueue_head(&midi_sleeper
);
1117 output_threshold
= SEQ_MAX_QUEUE
/ 2;
1122 void seq_drain_midi_queues(void)
1127 * Give the Midi drivers time to drain their output queues
1132 while (!signal_pending(current
) && n
)
1136 for (i
= 0; i
< max_mididev
; i
++)
1137 if (midi_opened
[i
] && midi_written
[i
])
1138 if (midi_devs
[i
]->buffer_status
!= NULL
)
1139 if (midi_devs
[i
]->buffer_status(i
))
1143 * Let's have a delay
1147 interruptible_sleep_on_timeout(&seq_sleeper
,
1152 void sequencer_release(int dev
, struct file
*file
)
1155 int mode
= translate_mode(file
);
1159 DEB(printk("sequencer_release(dev=%d)\n", dev
));
1162 * Wait until the queue is empty (if we don't have nonblock)
1165 if (mode
!= OPEN_READ
&& !(file
->f_flags
& O_NONBLOCK
))
1167 while (!signal_pending(current
) && qlen
> 0)
1170 interruptible_sleep_on_timeout(&seq_sleeper
,
1176 if (mode
!= OPEN_READ
)
1177 seq_drain_midi_queues(); /*
1178 * Ensure the output queues are empty
1181 if (mode
!= OPEN_READ
)
1182 seq_drain_midi_queues(); /*
1183 * Flush the all notes off messages
1186 for (i
= 0; i
< max_synthdev
; i
++)
1188 if (synth_open_mask
& (1 << i
)) /*
1193 synth_devs
[i
]->close(i
);
1195 if (synth_devs
[i
]->midi_dev
)
1196 midi_opened
[synth_devs
[i
]->midi_dev
] = 0;
1200 for (i
= 0; i
< max_mididev
; i
++)
1203 midi_devs
[i
]->close(i
);
1206 if (seq_mode
== SEQ_2
)
1209 if (obsolete_api_used
)
1210 printk(KERN_WARNING
"/dev/music: Obsolete (4 byte) API was used by %s\n", current
->comm
);
1214 static int seq_sync(void)
1216 unsigned long flags
;
1218 if (qlen
&& !seq_playing
&& !signal_pending(current
))
1224 interruptible_sleep_on_timeout(&seq_sleeper
, HZ
);
1225 restore_flags(flags
);
1229 static void midi_outc(int dev
, unsigned char data
)
1232 * NOTE! Calls sleep(). Don't call this from interrupt.
1236 unsigned long flags
;
1239 * This routine sends one byte to the Midi channel.
1240 * If the output FIFO is full, it waits until there
1241 * is space in the queue
1244 n
= 3 * HZ
; /* Timeout */
1248 while (n
&& !midi_devs
[dev
]->outputc(dev
, data
)) {
1249 interruptible_sleep_on_timeout(&seq_sleeper
, HZ
/25);
1252 restore_flags(flags
);
1255 static void seq_reset(void)
1258 * NOTE! Calls sleep(). Don't call this from interrupt.
1263 unsigned long flags
;
1265 if (softsynthp
!= NULL
)
1266 softsynthp(SSYN_STOP
, 0, 0, 0);
1271 prev_input_time
= 0;
1272 prev_event_time
= 0;
1274 qlen
= qhead
= qtail
= 0;
1275 iqlen
= iqhead
= iqtail
= 0;
1277 for (i
= 0; i
< max_synthdev
; i
++)
1278 if (synth_open_mask
& (1 << i
))
1280 synth_devs
[i
]->reset(i
);
1282 if (seq_mode
== SEQ_2
)
1284 for (chn
= 0; chn
< 16; chn
++)
1285 for (i
= 0; i
< max_synthdev
; i
++)
1286 if (synth_open_mask
& (1 << i
))
1289 synth_devs
[i
]->controller(i
, chn
, 123, 0); /* All notes off */
1290 synth_devs
[i
]->controller(i
, chn
, 121, 0); /* Reset all ctl */
1291 synth_devs
[i
]->bender(i
, chn
, 1 << 13); /* Bender off */
1294 else /* seq_mode == SEQ_1 */
1296 for (i
= 0; i
< max_mididev
; i
++)
1297 if (midi_written
[i
]) /*
1298 * Midi used. Some notes may still be playing
1302 * Sending just a ACTIVE SENSING message should be enough to stop all
1303 * playing notes. Since there are devices not recognizing the
1304 * active sensing, we have to send some all notes off messages also.
1308 for (chn
= 0; chn
< 16; chn
++)
1310 midi_outc(i
, (unsigned char) (0xb0 + (chn
& 0x0f))); /* control change */
1311 midi_outc(i
, 0x7b); /* All notes off */
1312 midi_outc(i
, 0); /* Dummy parameter */
1315 midi_devs
[i
]->close(i
);
1317 midi_written
[i
] = 0;
1327 if (waitqueue_active(&seq_sleeper
)) {
1328 /* printk( "Sequencer Warning: Unexpected sleeping process - Waking up\n"); */
1329 wake_up(&seq_sleeper
);
1331 restore_flags(flags
);
1334 static void seq_panic(void)
1337 * This routine is called by the application in case the user
1338 * wants to reset the system to the default state.
1344 * Since some of the devices don't recognize the active sensing and
1345 * all notes off messages, we have to shut all notes manually.
1347 * TO BE IMPLEMENTED LATER
1351 * Also return the controllers to their default states
1355 int sequencer_ioctl(int dev
, struct file
*file
, unsigned int cmd
, caddr_t arg
)
1357 int midi_dev
, orig_dev
, val
, err
;
1358 int mode
= translate_mode(file
);
1359 struct synth_info inf
;
1360 struct seq_event_rec event_rec
;
1361 unsigned long flags
;
1363 orig_dev
= dev
= dev
>> 4;
1367 case SNDCTL_TMR_TIMEBASE
:
1368 case SNDCTL_TMR_TEMPO
:
1369 case SNDCTL_TMR_START
:
1370 case SNDCTL_TMR_STOP
:
1371 case SNDCTL_TMR_CONTINUE
:
1372 case SNDCTL_TMR_METRONOME
:
1373 case SNDCTL_TMR_SOURCE
:
1374 if (seq_mode
!= SEQ_2
)
1376 return tmr
->ioctl(tmr_no
, cmd
, arg
);
1378 case SNDCTL_TMR_SELECT
:
1379 if (seq_mode
!= SEQ_2
)
1381 if (get_user(pending_timer
, (int *)arg
))
1383 if (pending_timer
< 0 || pending_timer
>= num_sound_timers
|| sound_timer_devs
[pending_timer
] == NULL
)
1388 val
= pending_timer
;
1391 case SNDCTL_SEQ_PANIC
:
1395 case SNDCTL_SEQ_SYNC
:
1396 if (mode
== OPEN_READ
)
1398 while (qlen
> 0 && !signal_pending(current
))
1400 return qlen
? -EINTR
: 0;
1402 case SNDCTL_SEQ_RESET
:
1406 case SNDCTL_SEQ_TESTMIDI
:
1407 if (__get_user(midi_dev
, (int *)arg
))
1409 if (midi_dev
< 0 || midi_dev
>= max_mididev
|| !midi_devs
[midi_dev
])
1412 if (!midi_opened
[midi_dev
] &&
1413 (err
= midi_devs
[midi_dev
]->open(midi_dev
, mode
, sequencer_midi_input
,
1414 sequencer_midi_output
)) < 0)
1416 midi_opened
[midi_dev
] = 1;
1419 case SNDCTL_SEQ_GETINCOUNT
:
1420 if (mode
== OPEN_WRITE
)
1425 case SNDCTL_SEQ_GETOUTCOUNT
:
1426 if (mode
== OPEN_READ
)
1428 val
= SEQ_MAX_QUEUE
- qlen
;
1431 case SNDCTL_SEQ_GETTIME
:
1432 if (seq_mode
== SEQ_2
)
1433 return tmr
->ioctl(tmr_no
, cmd
, arg
);
1434 if (softsynthp
!= NULL
)
1435 val
= softsynthp(SSYN_GETTIME
, 0, 0, 0);
1437 val
= jiffies
- seq_time
;
1440 case SNDCTL_SEQ_CTRLRATE
:
1442 * If *arg == 0, just return the current rate
1444 if (seq_mode
== SEQ_2
)
1445 return tmr
->ioctl(tmr_no
, cmd
, arg
);
1447 if (get_user(val
, (int *)arg
))
1454 case SNDCTL_SEQ_RESETSAMPLES
:
1455 case SNDCTL_SYNTH_REMOVESAMPLE
:
1456 case SNDCTL_SYNTH_CONTROL
:
1457 if (get_user(dev
, (int *)arg
))
1459 if (dev
< 0 || dev
>= num_synths
|| synth_devs
[dev
] == NULL
)
1461 if (!(synth_open_mask
& (1 << dev
)) && !orig_dev
)
1463 return synth_devs
[dev
]->ioctl(dev
, cmd
, arg
);
1465 case SNDCTL_SEQ_NRSYNTHS
:
1469 case SNDCTL_SEQ_NRMIDIS
:
1473 case SNDCTL_SYNTH_MEMAVL
:
1474 if (get_user(dev
, (int *)arg
))
1476 if (dev
< 0 || dev
>= num_synths
|| synth_devs
[dev
] == NULL
)
1478 if (!(synth_open_mask
& (1 << dev
)) && !orig_dev
)
1480 val
= synth_devs
[dev
]->ioctl(dev
, cmd
, arg
);
1483 case SNDCTL_FM_4OP_ENABLE
:
1484 if (get_user(dev
, (int *)arg
))
1486 if (dev
< 0 || dev
>= num_synths
|| synth_devs
[dev
] == NULL
)
1488 if (!(synth_open_mask
& (1 << dev
)))
1490 synth_devs
[dev
]->ioctl(dev
, cmd
, arg
);
1493 case SNDCTL_SYNTH_INFO
:
1494 if (get_user(dev
, (int *)(&(((struct synth_info
*)arg
)->device
))))
1496 if (dev
< 0 || dev
>= max_synthdev
)
1498 if (!(synth_open_mask
& (1 << dev
)) && !orig_dev
)
1500 return synth_devs
[dev
]->ioctl(dev
, cmd
, arg
);
1502 /* Like SYNTH_INFO but returns ID in the name field */
1503 case SNDCTL_SYNTH_ID
:
1504 if (get_user(dev
, (int *)(&(((struct synth_info
*)arg
)->device
))))
1506 if (dev
< 0 || dev
>= max_synthdev
)
1508 if (!(synth_open_mask
& (1 << dev
)) && !orig_dev
)
1510 memcpy(&inf
, synth_devs
[dev
]->info
, sizeof(inf
));
1511 strncpy(inf
.name
, synth_devs
[dev
]->id
, sizeof(inf
.name
));
1513 return copy_to_user(arg
, &inf
, sizeof(inf
))?-EFAULT
:0;
1515 case SNDCTL_SEQ_OUTOFBAND
:
1516 if (copy_from_user(&event_rec
, arg
, sizeof(event_rec
)))
1520 play_event(event_rec
.arr
);
1521 restore_flags(flags
);
1524 case SNDCTL_MIDI_INFO
:
1525 if (get_user(dev
, (int *)(&(((struct midi_info
*)arg
)->device
))))
1527 if (dev
< 0 || dev
>= max_mididev
|| !midi_devs
[dev
])
1529 midi_devs
[dev
]->info
.device
= dev
;
1530 return copy_to_user(arg
, &midi_devs
[dev
]->info
, sizeof(struct midi_info
))?-EFAULT
:0;
1532 case SNDCTL_SEQ_THRESHOLD
:
1533 if (get_user(val
, (int *)arg
))
1537 if (val
>= SEQ_MAX_QUEUE
)
1538 val
= SEQ_MAX_QUEUE
- 1;
1539 output_threshold
= val
;
1542 case SNDCTL_MIDI_PRETIME
:
1543 if (get_user(val
, (int *)arg
))
1547 val
= (HZ
* val
) / 10;
1548 pre_event_timeout
= val
;
1552 if (mode
== OPEN_READ
)
1556 if (!(synth_open_mask
& (1 << 0)))
1558 if (!synth_devs
[0]->ioctl
)
1560 return synth_devs
[0]->ioctl(0, cmd
, arg
);
1562 return put_user(val
, (int *)arg
);
1565 unsigned int sequencer_poll(int dev
, struct file
*file
, poll_table
* wait
)
1567 unsigned long flags
;
1568 unsigned int mask
= 0;
1575 poll_wait(file
, &midi_sleeper
, wait
);
1577 mask
|= POLLIN
| POLLRDNORM
;
1580 poll_wait(file
, &seq_sleeper
, wait
);
1581 if ((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
)
1582 mask
|= POLLOUT
| POLLWRNORM
;
1583 restore_flags(flags
);
1588 void sequencer_timer(unsigned long dummy
)
1593 int note_to_freq(int note_num
)
1597 * This routine converts a midi note to a frequency (multiplied by 1000)
1600 int note
, octave
, note_freq
;
1601 static int notes
[] =
1603 261632, 277189, 293671, 311132, 329632, 349232,
1604 369998, 391998, 415306, 440000, 466162, 493880
1607 #define BASE_OCTAVE 5
1609 octave
= note_num
/ 12;
1610 note
= note_num
% 12;
1612 note_freq
= notes
[note
];
1614 if (octave
< BASE_OCTAVE
)
1615 note_freq
>>= (BASE_OCTAVE
- octave
);
1616 else if (octave
> BASE_OCTAVE
)
1617 note_freq
<<= (octave
- BASE_OCTAVE
);
1626 unsigned long compute_finetune(unsigned long base_freq
, int bend
, int range
,
1629 unsigned long amount
;
1630 int negative
, semitones
, cents
, multiplier
= 1;
1643 bend
= bend
* range
/ 8192; /* Convert to cents */
1644 bend
+= vibrato_cents
;
1649 negative
= bend
< 0 ? 1 : 0;
1666 semitones
= bend
/ 100;
1671 amount
= (int) (semitone_tuning
[semitones
] * multiplier
* cent_tuning
[cents
]) / 10000;
1674 return (base_freq
* 10000) / amount
; /* Bend down */
1676 return (base_freq
* amount
) / 10000; /* Bend up */
1680 void sequencer_init(void)
1682 /* drag in sequencer_syms.o */
1684 extern char sequencer_syms_symbol
;
1685 sequencer_syms_symbol
= 0;
1691 queue
= (unsigned char *)vmalloc(SEQ_MAX_QUEUE
* EV_SZ
);
1694 printk(KERN_ERR
"sequencer: Can't allocate memory for sequencer output queue\n");
1697 iqueue
= (unsigned char *)vmalloc(SEQ_MAX_QUEUE
* IEV_SZ
);
1700 printk(KERN_ERR
"sequencer: Can't allocate memory for sequencer input queue\n");
1707 void sequencer_unload(void)