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>
19 #include <linux/kmod.h>
23 #include "sound_config.h"
25 #ifdef CONFIG_SEQUENCER
27 int (*softsynthp
) (int cmd
, int parm1
, int parm2
, unsigned long parm3
) = NULL
;
29 #include "midi_ctrl.h"
31 static int sequencer_ok
= 0;
32 static struct sound_timer_operations
*tmr
;
33 static int tmr_no
= -1; /* Currently selected timer */
34 static int pending_timer
= -1; /* For timer change operation */
35 extern unsigned long seq_time
;
37 static int obsolete_api_used
= 0;
40 * Local counts for number of synth and MIDI devices. These are initialized
41 * by the sequencer_open.
43 static int max_mididev
= 0;
44 static int max_synthdev
= 0;
47 * The seq_mode gives the operating mode of the sequencer:
48 * 1 = level1 (the default)
49 * 2 = level2 (extended capabilities)
54 static int seq_mode
= SEQ_1
;
56 static DECLARE_WAIT_QUEUE_HEAD(seq_sleeper
);
57 static DECLARE_WAIT_QUEUE_HEAD(midi_sleeper
);
59 static int midi_opened
[MAX_MIDI_DEV
] = {
63 static int midi_written
[MAX_MIDI_DEV
] = {
67 static unsigned long prev_input_time
= 0;
68 static int prev_event_time
;
75 static unsigned char *queue
= NULL
;
76 static unsigned char *iqueue
= NULL
;
78 static volatile int qhead
= 0, qtail
= 0, qlen
= 0;
79 static volatile int iqhead
= 0, iqtail
= 0, iqlen
= 0;
80 static volatile int seq_playing
= 0;
81 static volatile int sequencer_busy
= 0;
82 static int output_threshold
;
83 static long pre_event_timeout
;
84 static unsigned synth_open_mask
;
86 static int seq_queue(unsigned char *note
, char nonblock
);
87 static void seq_startplay(void);
88 static int seq_sync(void);
89 static void seq_reset(void);
91 #if MAX_SYNTH_DEV > 15
92 #error Too many synthesizer devices enabled.
95 int sequencer_read(int dev
, struct file
*file
, char *buf
, int count
)
103 ev_len
= seq_mode
== SEQ_1
? 4 : 8;
110 if (file
->f_flags
& O_NONBLOCK
) {
111 restore_flags(flags
);
115 interruptible_sleep_on_timeout(&midi_sleeper
,
119 restore_flags(flags
);
123 while (iqlen
&& c
>= ev_len
)
125 char *fixit
= (char *) &iqueue
[iqhead
* IEV_SZ
];
126 copy_to_user(&(buf
)[p
], fixit
, ev_len
);
130 iqhead
= (iqhead
+ 1) % SEQ_MAX_QUEUE
;
133 restore_flags(flags
);
137 static void sequencer_midi_output(int dev
)
144 void seq_copy_to_input(unsigned char *event_rec
, int len
)
149 * Verify that the len is valid for the current mode.
152 if (len
!= 4 && len
!= 8)
154 if ((seq_mode
== SEQ_1
) != (len
== 4))
157 if (iqlen
>= (SEQ_MAX_QUEUE
- 1))
158 return; /* Overflow */
162 memcpy(&iqueue
[iqtail
* IEV_SZ
], event_rec
, len
);
164 iqtail
= (iqtail
+ 1) % SEQ_MAX_QUEUE
;
165 wake_up(&midi_sleeper
);
166 restore_flags(flags
);
169 static void sequencer_midi_input(int dev
, unsigned char data
)
172 unsigned char event_rec
[4];
174 if (data
== 0xfe) /* Ignore active sensing */
177 if (softsynthp
!= NULL
)
178 tstamp
= softsynthp(SSYN_GETTIME
, 0, 0, 0);
180 tstamp
= jiffies
- seq_time
;
182 if (tstamp
!= prev_input_time
)
184 tstamp
= (tstamp
<< 8) | SEQ_WAIT
;
185 seq_copy_to_input((unsigned char *) &tstamp
, 4);
186 prev_input_time
= tstamp
;
188 event_rec
[0] = SEQ_MIDIPUTC
;
193 seq_copy_to_input(event_rec
, 4);
196 void seq_input_event(unsigned char *event_rec
, int len
)
198 unsigned long this_time
;
200 if (seq_mode
== SEQ_2
)
201 this_time
= tmr
->get_time(tmr_no
);
202 else if (softsynthp
!= NULL
)
203 this_time
= softsynthp(SSYN_GETTIME
, 0, 0, 0);
205 this_time
= jiffies
- seq_time
;
207 if (this_time
!= prev_input_time
)
209 unsigned char tmp_event
[8];
211 tmp_event
[0] = EV_TIMING
;
212 tmp_event
[1] = TMR_WAIT_ABS
;
215 *(unsigned int *) &tmp_event
[4] = this_time
;
217 seq_copy_to_input(tmp_event
, 8);
218 prev_input_time
= this_time
;
220 seq_copy_to_input(event_rec
, len
);
223 int sequencer_write(int dev
, struct file
*file
, const char *buf
, int count
)
225 unsigned char event_rec
[EV_SZ
], ev_code
;
226 int p
= 0, c
, ev_size
;
228 int mode
= translate_mode(file
);
232 DEB(printk("sequencer_write(dev=%d, count=%d)\n", dev
, count
));
234 if (mode
== OPEN_READ
)
241 copy_from_user((char *) event_rec
, &(buf
)[p
], 4);
242 ev_code
= event_rec
[0];
244 if (ev_code
== SEQ_FULLSIZE
)
248 dev
= *(unsigned short *) &event_rec
[2];
249 if (dev
< 0 || dev
>= max_synthdev
|| synth_devs
[dev
] == NULL
)
252 if (!(synth_open_mask
& (1 << dev
)))
255 fmt
= (*(short *) &event_rec
[0]) & 0xffff;
256 err
= synth_devs
[dev
]->load_patch(dev
, fmt
, buf
, p
+ 4, c
, 0);
264 if (seq_mode
== SEQ_2
&& ev_code
== SEQ_EXTENDED
)
266 printk(KERN_WARNING
"Sequencer: Invalid level 2 event %x\n", ev_code
);
277 copy_from_user((char *) &event_rec
[4], &(buf
)[p
+ 4], 4);
282 if (seq_mode
== SEQ_2
)
284 printk(KERN_WARNING
"Sequencer: 4 byte event in level 2 mode\n");
289 if (event_rec
[0] != SEQ_MIDIPUTC
)
290 obsolete_api_used
= 1;
293 if (event_rec
[0] == SEQ_MIDIPUTC
)
295 if (!midi_opened
[event_rec
[2]])
298 int dev
= event_rec
[2];
300 if (dev
>= max_mididev
|| midi_devs
[dev
]==NULL
)
302 /*printk("Sequencer Error: Nonexistent MIDI device %d\n", dev);*/
305 mode
= translate_mode(file
);
307 if ((err
= midi_devs
[dev
]->open(dev
, mode
,
308 sequencer_midi_input
, sequencer_midi_output
)) < 0)
311 printk(KERN_WARNING
"Sequencer Error: Unable to open Midi #%d\n", dev
);
314 midi_opened
[dev
] = 1;
317 if (!seq_queue(event_rec
, (file
->f_flags
& (O_NONBLOCK
) ? 1 : 0)))
319 int processed
= count
- c
;
324 if (!processed
&& (file
->f_flags
& O_NONBLOCK
))
339 static int seq_queue(unsigned char *note
, char nonblock
)
343 * Test if there is space in the queue
346 if (qlen
>= SEQ_MAX_QUEUE
)
349 * Give chance to drain the queue
352 if (!nonblock
&& qlen
>= SEQ_MAX_QUEUE
&& !waitqueue_active(&seq_sleeper
)) {
354 * Sleep until there is enough space on the queue
356 interruptible_sleep_on(&seq_sleeper
);
358 if (qlen
>= SEQ_MAX_QUEUE
)
364 memcpy(&queue
[qtail
* EV_SZ
], note
, EV_SZ
);
366 qtail
= (qtail
+ 1) % SEQ_MAX_QUEUE
;
372 static int extended_event(unsigned char *q
)
376 if (dev
< 0 || dev
>= max_synthdev
)
379 if (!(synth_open_mask
& (1 << dev
)))
385 synth_devs
[dev
]->kill_note(dev
, q
[3], q
[4], q
[5]);
389 if (q
[4] > 127 && q
[4] != 255)
394 synth_devs
[dev
]->kill_note(dev
, q
[3], q
[4], q
[5]);
397 synth_devs
[dev
]->start_note(dev
, q
[3], q
[4], q
[5]);
401 synth_devs
[dev
]->set_instr(dev
, q
[3], q
[4]);
405 synth_devs
[dev
]->aftertouch(dev
, q
[3], q
[4]);
409 synth_devs
[dev
]->panning(dev
, q
[3], (char) q
[4]);
413 synth_devs
[dev
]->controller(dev
, q
[3], q
[4], (short) (q
[5] | (q
[6] << 8)));
417 if (synth_devs
[dev
]->volume_method
!= NULL
)
418 synth_devs
[dev
]->volume_method(dev
, q
[3]);
427 static int find_voice(int dev
, int chn
, int note
)
432 key
= (chn
<< 8) | (note
+ 1);
433 for (i
= 0; i
< synth_devs
[dev
]->alloc
.max_voice
; i
++)
434 if (synth_devs
[dev
]->alloc
.map
[i
] == key
)
439 static int alloc_voice(int dev
, int chn
, int note
)
444 key
= (chn
<< 8) | (note
+ 1);
446 voice
= synth_devs
[dev
]->alloc_voice(dev
, chn
, note
,
447 &synth_devs
[dev
]->alloc
);
448 synth_devs
[dev
]->alloc
.map
[voice
] = key
;
449 synth_devs
[dev
]->alloc
.alloc_times
[voice
] =
450 synth_devs
[dev
]->alloc
.timestamp
++;
454 static void seq_chn_voice_event(unsigned char *event_rec
)
456 #define dev event_rec[1]
457 #define cmd event_rec[2]
458 #define chn event_rec[3]
459 #define note event_rec[4]
460 #define parm event_rec[5]
464 if ((int) dev
> max_synthdev
|| synth_devs
[dev
] == NULL
)
466 if (!(synth_open_mask
& (1 << dev
)))
468 if (!synth_devs
[dev
])
471 if (seq_mode
== SEQ_2
)
473 if (synth_devs
[dev
]->alloc_voice
)
474 voice
= find_voice(dev
, chn
, note
);
476 if (cmd
== MIDI_NOTEON
&& parm
== 0)
486 if (note
> 127 && note
!= 255) /* Not a seq2 feature */
489 if (voice
== -1 && seq_mode
== SEQ_2
&& synth_devs
[dev
]->alloc_voice
)
491 /* Internal synthesizer (FM, GUS, etc) */
492 voice
= alloc_voice(dev
, chn
, note
);
497 if (seq_mode
== SEQ_2
&& (int) dev
< num_synths
)
500 * The MIDI channel 10 is a percussive channel. Use the note
501 * number to select the proper patch (128 to 255) to play.
506 synth_devs
[dev
]->set_instr(dev
, voice
, 128 + note
);
507 synth_devs
[dev
]->chn_info
[chn
].pgm_num
= 128 + note
;
509 synth_devs
[dev
]->setup_voice(dev
, voice
, chn
);
511 synth_devs
[dev
]->start_note(dev
, voice
, note
, parm
);
517 synth_devs
[dev
]->kill_note(dev
, voice
, note
, parm
);
520 case MIDI_KEY_PRESSURE
:
523 synth_devs
[dev
]->aftertouch(dev
, voice
, parm
);
536 static void seq_chn_common_event(unsigned char *event_rec
)
538 unsigned char dev
= event_rec
[1];
539 unsigned char cmd
= event_rec
[2];
540 unsigned char chn
= event_rec
[3];
541 unsigned char p1
= event_rec
[4];
543 /* unsigned char p2 = event_rec[5]; */
544 unsigned short w14
= *(short *) &event_rec
[6];
546 if ((int) dev
> max_synthdev
|| synth_devs
[dev
] == NULL
)
548 if (!(synth_open_mask
& (1 << dev
)))
550 if (!synth_devs
[dev
])
555 case MIDI_PGM_CHANGE
:
556 if (seq_mode
== SEQ_2
)
558 synth_devs
[dev
]->chn_info
[chn
].pgm_num
= p1
;
559 if ((int) dev
>= num_synths
)
560 synth_devs
[dev
]->set_instr(dev
, chn
, p1
);
563 synth_devs
[dev
]->set_instr(dev
, chn
, p1
);
567 case MIDI_CTL_CHANGE
:
568 if (seq_mode
== SEQ_2
)
570 if (chn
> 15 || p1
> 127)
573 synth_devs
[dev
]->chn_info
[chn
].controllers
[p1
] = w14
& 0x7f;
575 if (p1
< 32) /* Setting MSB should clear LSB to 0 */
576 synth_devs
[dev
]->chn_info
[chn
].controllers
[p1
+ 32] = 0;
578 if ((int) dev
< num_synths
)
580 int val
= w14
& 0x7f;
583 if (p1
< 64) /* Combine MSB and LSB */
585 val
= ((synth_devs
[dev
]->
586 chn_info
[chn
].controllers
[p1
& ~32] & 0x7f) << 7)
588 chn_info
[chn
].controllers
[p1
| 32] & 0x7f);
591 /* Handle all playing notes on this channel */
593 key
= ((int) chn
<< 8);
595 for (i
= 0; i
< synth_devs
[dev
]->alloc
.max_voice
; i
++)
596 if ((synth_devs
[dev
]->alloc
.map
[i
] & 0xff00) == key
)
597 synth_devs
[dev
]->controller(dev
, i
, p1
, val
);
600 synth_devs
[dev
]->controller(dev
, chn
, p1
, w14
);
603 synth_devs
[dev
]->controller(dev
, chn
, p1
, w14
);
606 case MIDI_PITCH_BEND
:
607 if (seq_mode
== SEQ_2
)
609 synth_devs
[dev
]->chn_info
[chn
].bender_value
= w14
;
611 if ((int) dev
< num_synths
)
613 /* Handle all playing notes on this channel */
618 for (i
= 0; i
< synth_devs
[dev
]->alloc
.max_voice
; i
++)
619 if ((synth_devs
[dev
]->alloc
.map
[i
] & 0xff00) == key
)
620 synth_devs
[dev
]->bender(dev
, i
, w14
);
623 synth_devs
[dev
]->bender(dev
, chn
, w14
);
626 synth_devs
[dev
]->bender(dev
, chn
, w14
);
633 static int seq_timing_event(unsigned char *event_rec
)
635 unsigned char cmd
= event_rec
[1];
636 unsigned int parm
= *(int *) &event_rec
[4];
638 if (seq_mode
== SEQ_2
)
642 if ((ret
= tmr
->event(tmr_no
, event_rec
)) == TIMER_ARMED
)
643 if ((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
)
644 wake_up(&seq_sleeper
);
650 parm
+= prev_event_time
;
653 * NOTE! No break here. Execution of TMR_WAIT_REL continues in the
654 * next case (TMR_WAIT_ABS)
663 prev_event_time
= time
;
666 if (softsynthp
!= NULL
)
667 softsynthp(SSYN_REQUEST
, time
, 0, 0);
669 request_sound_timer(time
);
671 if ((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
)
672 wake_up(&seq_sleeper
);
678 if (softsynthp
!= NULL
)
680 softsynthp(SSYN_START
, 0, 0, 0);
699 if (seq_mode
== SEQ_2
)
700 seq_copy_to_input(event_rec
, 8);
703 parm
= (parm
<< 8 | SEQ_ECHO
);
704 seq_copy_to_input((unsigned char *) &parm
, 4);
711 return TIMER_NOT_ARMED
;
714 static void seq_local_event(unsigned char *event_rec
)
716 unsigned char cmd
= event_rec
[1];
717 unsigned int parm
= *((unsigned int *) &event_rec
[4]);
721 case LOCL_STARTAUDIO
:
723 DMAbuf_start_devices(parm
);
731 static void seq_sysex_message(unsigned char *event_rec
)
733 int dev
= event_rec
[1];
735 unsigned char *buf
= &event_rec
[2];
737 if ((int) dev
> max_synthdev
)
739 if (!(synth_open_mask
& (1 << dev
)))
741 if (!synth_devs
[dev
])
745 for (i
= 0; i
< 6 && buf
[i
] != 0xff; i
++)
748 if (!synth_devs
[dev
]->send_sysex
)
751 synth_devs
[dev
]->send_sysex(dev
, buf
, l
);
754 static int play_event(unsigned char *q
)
757 * NOTE! This routine returns
758 * 0 = normal event played.
759 * 1 = Timer armed. Suspend playback until timer callback.
760 * 2 = MIDI output buffer full. Restore queue and suspend until timer
767 if (synth_open_mask
& (1 << 0))
769 synth_devs
[0]->kill_note(0, q
[1], 255, q
[3]);
773 if (q
[4] < 128 || q
[4] == 255)
774 if (synth_open_mask
& (1 << 0))
776 synth_devs
[0]->start_note(0, q
[1], q
[2], q
[3]);
780 delay
= (unsigned int *) q
; /*
781 * Bytes 1 to 3 are containing the *
784 *delay
= (*delay
>> 8) & 0xffffff;
792 prev_event_time
= time
;
794 if (softsynthp
!= NULL
)
795 softsynthp(SSYN_REQUEST
, time
, 0, 0);
797 request_sound_timer(time
);
799 if ((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
)
800 wake_up(&seq_sleeper
);
802 * The timer is now active and will reinvoke this function
803 * after the timer expires. Return to the caller now.
810 if (synth_open_mask
& (1 << 0))
812 synth_devs
[0]->set_instr(0, q
[1], q
[2]);
815 case SEQ_SYNCTIMER
: /*
818 if (softsynthp
!= NULL
)
824 if (softsynthp
!= NULL
)
825 softsynthp(SSYN_START
, 0, 0, 0);
828 case SEQ_MIDIPUTC
: /*
829 * Put a midi character
831 if (midi_opened
[q
[2]])
837 if (dev
< 0 || dev
>= num_midis
|| midi_devs
[dev
] == NULL
)
840 if (!midi_devs
[dev
]->outputc(dev
, q
[1]))
843 * Output FIFO is full. Wait one timer cycle and try again.
847 if (softsynthp
!= NULL
)
848 softsynthp(SSYN_REQUEST
, -1, 0, 0);
850 request_sound_timer(-1);
854 midi_written
[dev
] = 1;
859 seq_copy_to_input(q
, 4); /*
860 * Echo back to the process
865 if ((int) q
[1] < max_synthdev
)
866 synth_devs
[q
[1]]->hw_control(q
[1], q
);
874 seq_chn_voice_event(q
);
878 seq_chn_common_event(q
);
882 if (seq_timing_event(q
) == TIMER_ARMED
)
893 seq_sysex_message(q
);
901 static void seq_startplay(void)
904 int this_one
, action
;
911 qhead
= ((this_one
= qhead
) + 1) % SEQ_MAX_QUEUE
;
913 restore_flags(flags
);
917 if ((action
= play_event(&queue
[this_one
* EV_SZ
])))
918 { /* Suspend playback. Next timer routine invokes this routine again */
930 if ((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
)
931 wake_up(&seq_sleeper
);
934 static void reset_controllers(int dev
, unsigned char *controller
, int update_dev
)
937 for (i
= 0; i
< 128; i
++)
938 controller
[i
] = ctrl_def_values
[i
];
941 static void setup_mode2(void)
945 max_synthdev
= num_synths
;
947 for (dev
= 0; dev
< num_midis
; dev
++)
949 if (midi_devs
[dev
] && midi_devs
[dev
]->converter
!= NULL
)
951 synth_devs
[max_synthdev
++] = midi_devs
[dev
]->converter
;
955 for (dev
= 0; dev
< max_synthdev
; dev
++)
959 synth_devs
[dev
]->sysex_ptr
= 0;
960 synth_devs
[dev
]->emulation
= 0;
962 for (chn
= 0; chn
< 16; chn
++)
964 synth_devs
[dev
]->chn_info
[chn
].pgm_num
= 0;
965 reset_controllers(dev
,
966 synth_devs
[dev
]->chn_info
[chn
].controllers
,0);
967 synth_devs
[dev
]->chn_info
[chn
].bender_value
= (1 << 7); /* Neutral */
968 synth_devs
[dev
]->chn_info
[chn
].bender_range
= 200;
975 int sequencer_open(int dev
, struct file
*file
)
984 level
= ((dev
& 0x0f) == SND_DEV_SEQ2
) ? 2 : 1;
987 mode
= translate_mode(file
);
989 DEB(printk("sequencer_open(dev=%d)\n", dev
));
993 /* printk("Sound card: sequencer not initialized\n");*/
996 if (dev
) /* Patch manager device (obsolete) */
999 if(synth_devs
[dev
] == NULL
)
1000 request_module("synth0");
1002 if (mode
== OPEN_READ
)
1006 /*printk("Sequencer: No MIDI devices. Input not possible\n");*/
1015 restore_flags(flags
);
1019 obsolete_api_used
= 0;
1020 restore_flags(flags
);
1022 max_mididev
= num_midis
;
1023 max_synthdev
= num_synths
;
1024 pre_event_timeout
= MAX_SCHEDULE_TIMEOUT
;
1027 if (pending_timer
!= -1)
1029 tmr_no
= pending_timer
;
1032 if (tmr_no
== -1) /* Not selected yet */
1037 for (i
= 0; i
< num_sound_timers
; i
++)
1038 if (sound_timer_devs
[i
] && sound_timer_devs
[i
]->priority
> best
)
1041 best
= sound_timer_devs
[i
]->priority
;
1043 if (tmr_no
== -1) /* Should not be */
1046 tmr
= sound_timer_devs
[tmr_no
];
1052 /*printk("sequencer: No timer for level 2\n");*/
1058 if (!max_synthdev
&& !max_mididev
)
1064 synth_open_mask
= 0;
1066 for (i
= 0; i
< max_mididev
; i
++)
1069 midi_written
[i
] = 0;
1072 for (i
= 0; i
< max_synthdev
; i
++)
1074 if (synth_devs
[i
]==NULL
)
1077 if ((tmp
= synth_devs
[i
]->open(i
, mode
)) < 0)
1079 printk(KERN_WARNING
"Sequencer: Warning! Cannot open synth device #%d (%d)\n", i
, tmp
);
1080 if (synth_devs
[i
]->midi_dev
)
1081 printk(KERN_WARNING
"(Maps to MIDI dev #%d)\n", synth_devs
[i
]->midi_dev
);
1085 synth_open_mask
|= (1 << i
);
1086 if (synth_devs
[i
]->midi_dev
)
1087 midi_opened
[synth_devs
[i
]->midi_dev
] = 1;
1091 if (softsynthp
!= NULL
)
1096 prev_input_time
= 0;
1097 prev_event_time
= 0;
1098 if (softsynthp
!= NULL
)
1099 softsynthp(SSYN_START
, 0, 0, 0);
1101 if (seq_mode
== SEQ_1
&& (mode
== OPEN_READ
|| mode
== OPEN_READWRITE
))
1104 * Initialize midi input devices
1107 for (i
= 0; i
< max_mididev
; i
++)
1108 if (!midi_opened
[i
] && midi_devs
[i
])
1110 if ((retval
= midi_devs
[i
]->open(i
, mode
,
1111 sequencer_midi_input
, sequencer_midi_output
)) >= 0)
1117 if (seq_mode
== SEQ_2
)
1118 tmr
->open(tmr_no
, seq_mode
);
1120 init_waitqueue_head(&seq_sleeper
);
1121 init_waitqueue_head(&midi_sleeper
);
1122 output_threshold
= SEQ_MAX_QUEUE
/ 2;
1127 void seq_drain_midi_queues(void)
1132 * Give the Midi drivers time to drain their output queues
1137 while (!signal_pending(current
) && n
)
1141 for (i
= 0; i
< max_mididev
; i
++)
1142 if (midi_opened
[i
] && midi_written
[i
])
1143 if (midi_devs
[i
]->buffer_status
!= NULL
)
1144 if (midi_devs
[i
]->buffer_status(i
))
1148 * Let's have a delay
1152 interruptible_sleep_on_timeout(&seq_sleeper
,
1157 void sequencer_release(int dev
, struct file
*file
)
1160 int mode
= translate_mode(file
);
1164 DEB(printk("sequencer_release(dev=%d)\n", dev
));
1167 * Wait until the queue is empty (if we don't have nonblock)
1170 if (mode
!= OPEN_READ
&& !(file
->f_flags
& O_NONBLOCK
))
1172 while (!signal_pending(current
) && qlen
> 0)
1175 interruptible_sleep_on_timeout(&seq_sleeper
,
1181 if (mode
!= OPEN_READ
)
1182 seq_drain_midi_queues(); /*
1183 * Ensure the output queues are empty
1186 if (mode
!= OPEN_READ
)
1187 seq_drain_midi_queues(); /*
1188 * Flush the all notes off messages
1191 for (i
= 0; i
< max_synthdev
; i
++)
1193 if (synth_open_mask
& (1 << i
)) /*
1198 synth_devs
[i
]->close(i
);
1200 if (synth_devs
[i
]->midi_dev
)
1201 midi_opened
[synth_devs
[i
]->midi_dev
] = 0;
1205 for (i
= 0; i
< max_mididev
; i
++)
1208 midi_devs
[i
]->close(i
);
1211 if (seq_mode
== SEQ_2
)
1214 if (obsolete_api_used
)
1215 printk(KERN_WARNING
"/dev/music: Obsolete (4 byte) API was used by %s\n", current
->comm
);
1219 static int seq_sync(void)
1221 unsigned long flags
;
1223 if (qlen
&& !seq_playing
&& !signal_pending(current
))
1229 interruptible_sleep_on_timeout(&seq_sleeper
, HZ
);
1230 restore_flags(flags
);
1234 static void midi_outc(int dev
, unsigned char data
)
1237 * NOTE! Calls sleep(). Don't call this from interrupt.
1241 unsigned long flags
;
1244 * This routine sends one byte to the Midi channel.
1245 * If the output FIFO is full, it waits until there
1246 * is space in the queue
1249 n
= 3 * HZ
; /* Timeout */
1253 while (n
&& !midi_devs
[dev
]->outputc(dev
, data
)) {
1254 interruptible_sleep_on_timeout(&seq_sleeper
, HZ
/25);
1257 restore_flags(flags
);
1260 static void seq_reset(void)
1263 * NOTE! Calls sleep(). Don't call this from interrupt.
1268 unsigned long flags
;
1270 if (softsynthp
!= NULL
)
1271 softsynthp(SSYN_STOP
, 0, 0, 0);
1276 prev_input_time
= 0;
1277 prev_event_time
= 0;
1279 qlen
= qhead
= qtail
= 0;
1280 iqlen
= iqhead
= iqtail
= 0;
1282 for (i
= 0; i
< max_synthdev
; i
++)
1283 if (synth_open_mask
& (1 << i
))
1285 synth_devs
[i
]->reset(i
);
1287 if (seq_mode
== SEQ_2
)
1289 for (chn
= 0; chn
< 16; chn
++)
1290 for (i
= 0; i
< max_synthdev
; i
++)
1291 if (synth_open_mask
& (1 << i
))
1294 synth_devs
[i
]->controller(i
, chn
, 123, 0); /* All notes off */
1295 synth_devs
[i
]->controller(i
, chn
, 121, 0); /* Reset all ctl */
1296 synth_devs
[i
]->bender(i
, chn
, 1 << 13); /* Bender off */
1299 else /* seq_mode == SEQ_1 */
1301 for (i
= 0; i
< max_mididev
; i
++)
1302 if (midi_written
[i
]) /*
1303 * Midi used. Some notes may still be playing
1307 * Sending just a ACTIVE SENSING message should be enough to stop all
1308 * playing notes. Since there are devices not recognizing the
1309 * active sensing, we have to send some all notes off messages also.
1313 for (chn
= 0; chn
< 16; chn
++)
1315 midi_outc(i
, (unsigned char) (0xb0 + (chn
& 0x0f))); /* control change */
1316 midi_outc(i
, 0x7b); /* All notes off */
1317 midi_outc(i
, 0); /* Dummy parameter */
1320 midi_devs
[i
]->close(i
);
1322 midi_written
[i
] = 0;
1332 if (waitqueue_active(&seq_sleeper
)) {
1333 /* printk( "Sequencer Warning: Unexpected sleeping process - Waking up\n"); */
1334 wake_up(&seq_sleeper
);
1336 restore_flags(flags
);
1339 static void seq_panic(void)
1342 * This routine is called by the application in case the user
1343 * wants to reset the system to the default state.
1349 * Since some of the devices don't recognize the active sensing and
1350 * all notes off messages, we have to shut all notes manually.
1352 * TO BE IMPLEMENTED LATER
1356 * Also return the controllers to their default states
1360 int sequencer_ioctl(int dev
, struct file
*file
, unsigned int cmd
, caddr_t arg
)
1362 int midi_dev
, orig_dev
, val
, err
;
1363 int mode
= translate_mode(file
);
1364 struct synth_info inf
;
1365 struct seq_event_rec event_rec
;
1366 unsigned long flags
;
1368 orig_dev
= dev
= dev
>> 4;
1372 case SNDCTL_TMR_TIMEBASE
:
1373 case SNDCTL_TMR_TEMPO
:
1374 case SNDCTL_TMR_START
:
1375 case SNDCTL_TMR_STOP
:
1376 case SNDCTL_TMR_CONTINUE
:
1377 case SNDCTL_TMR_METRONOME
:
1378 case SNDCTL_TMR_SOURCE
:
1379 if (seq_mode
!= SEQ_2
)
1381 return tmr
->ioctl(tmr_no
, cmd
, arg
);
1383 case SNDCTL_TMR_SELECT
:
1384 if (seq_mode
!= SEQ_2
)
1386 if (get_user(pending_timer
, (int *)arg
))
1388 if (pending_timer
< 0 || pending_timer
>= num_sound_timers
|| sound_timer_devs
[pending_timer
] == NULL
)
1393 val
= pending_timer
;
1396 case SNDCTL_SEQ_PANIC
:
1400 case SNDCTL_SEQ_SYNC
:
1401 if (mode
== OPEN_READ
)
1403 while (qlen
> 0 && !signal_pending(current
))
1405 return qlen
? -EINTR
: 0;
1407 case SNDCTL_SEQ_RESET
:
1411 case SNDCTL_SEQ_TESTMIDI
:
1412 if (__get_user(midi_dev
, (int *)arg
))
1414 if (midi_dev
< 0 || midi_dev
>= max_mididev
|| !midi_devs
[midi_dev
])
1417 if (!midi_opened
[midi_dev
] &&
1418 (err
= midi_devs
[midi_dev
]->open(midi_dev
, mode
, sequencer_midi_input
,
1419 sequencer_midi_output
)) < 0)
1421 midi_opened
[midi_dev
] = 1;
1424 case SNDCTL_SEQ_GETINCOUNT
:
1425 if (mode
== OPEN_WRITE
)
1430 case SNDCTL_SEQ_GETOUTCOUNT
:
1431 if (mode
== OPEN_READ
)
1433 val
= SEQ_MAX_QUEUE
- qlen
;
1436 case SNDCTL_SEQ_GETTIME
:
1437 if (seq_mode
== SEQ_2
)
1438 return tmr
->ioctl(tmr_no
, cmd
, arg
);
1439 if (softsynthp
!= NULL
)
1440 val
= softsynthp(SSYN_GETTIME
, 0, 0, 0);
1442 val
= jiffies
- seq_time
;
1445 case SNDCTL_SEQ_CTRLRATE
:
1447 * If *arg == 0, just return the current rate
1449 if (seq_mode
== SEQ_2
)
1450 return tmr
->ioctl(tmr_no
, cmd
, arg
);
1452 if (get_user(val
, (int *)arg
))
1459 case SNDCTL_SEQ_RESETSAMPLES
:
1460 case SNDCTL_SYNTH_REMOVESAMPLE
:
1461 case SNDCTL_SYNTH_CONTROL
:
1462 if (get_user(dev
, (int *)arg
))
1464 if (dev
< 0 || dev
>= num_synths
|| synth_devs
[dev
] == NULL
)
1466 if (!(synth_open_mask
& (1 << dev
)) && !orig_dev
)
1468 return synth_devs
[dev
]->ioctl(dev
, cmd
, arg
);
1470 case SNDCTL_SEQ_NRSYNTHS
:
1474 case SNDCTL_SEQ_NRMIDIS
:
1478 case SNDCTL_SYNTH_MEMAVL
:
1479 if (get_user(dev
, (int *)arg
))
1481 if (dev
< 0 || dev
>= num_synths
|| synth_devs
[dev
] == NULL
)
1483 if (!(synth_open_mask
& (1 << dev
)) && !orig_dev
)
1485 val
= synth_devs
[dev
]->ioctl(dev
, cmd
, arg
);
1488 case SNDCTL_FM_4OP_ENABLE
:
1489 if (get_user(dev
, (int *)arg
))
1491 if (dev
< 0 || dev
>= num_synths
|| synth_devs
[dev
] == NULL
)
1493 if (!(synth_open_mask
& (1 << dev
)))
1495 synth_devs
[dev
]->ioctl(dev
, cmd
, arg
);
1498 case SNDCTL_SYNTH_INFO
:
1499 if (get_user(dev
, (int *)(&(((struct synth_info
*)arg
)->device
))))
1501 if (dev
< 0 || dev
>= max_synthdev
)
1503 if (!(synth_open_mask
& (1 << dev
)) && !orig_dev
)
1505 return synth_devs
[dev
]->ioctl(dev
, cmd
, arg
);
1507 /* Like SYNTH_INFO but returns ID in the name field */
1508 case SNDCTL_SYNTH_ID
:
1509 if (get_user(dev
, (int *)(&(((struct synth_info
*)arg
)->device
))))
1511 if (dev
< 0 || dev
>= max_synthdev
)
1513 if (!(synth_open_mask
& (1 << dev
)) && !orig_dev
)
1515 memcpy(&inf
, synth_devs
[dev
]->info
, sizeof(inf
));
1516 strncpy(inf
.name
, synth_devs
[dev
]->id
, sizeof(inf
.name
));
1518 return copy_to_user(arg
, &inf
, sizeof(inf
))?-EFAULT
:0;
1520 case SNDCTL_SEQ_OUTOFBAND
:
1521 if (copy_from_user(&event_rec
, arg
, sizeof(event_rec
)))
1525 play_event(event_rec
.arr
);
1526 restore_flags(flags
);
1529 case SNDCTL_MIDI_INFO
:
1530 if (get_user(dev
, (int *)(&(((struct midi_info
*)arg
)->device
))))
1532 if (dev
< 0 || dev
>= max_mididev
|| !midi_devs
[dev
])
1534 midi_devs
[dev
]->info
.device
= dev
;
1535 return copy_to_user(arg
, &midi_devs
[dev
]->info
, sizeof(struct midi_info
))?-EFAULT
:0;
1537 case SNDCTL_SEQ_THRESHOLD
:
1538 if (get_user(val
, (int *)arg
))
1542 if (val
>= SEQ_MAX_QUEUE
)
1543 val
= SEQ_MAX_QUEUE
- 1;
1544 output_threshold
= val
;
1547 case SNDCTL_MIDI_PRETIME
:
1548 if (get_user(val
, (int *)arg
))
1552 val
= (HZ
* val
) / 10;
1553 pre_event_timeout
= val
;
1557 if (mode
== OPEN_READ
)
1561 if (!(synth_open_mask
& (1 << 0)))
1563 if (!synth_devs
[0]->ioctl
)
1565 return synth_devs
[0]->ioctl(0, cmd
, arg
);
1567 return put_user(val
, (int *)arg
);
1570 unsigned int sequencer_poll(int dev
, struct file
*file
, poll_table
* wait
)
1572 unsigned long flags
;
1573 unsigned int mask
= 0;
1580 poll_wait(file
, &midi_sleeper
, wait
);
1582 mask
|= POLLIN
| POLLRDNORM
;
1585 poll_wait(file
, &seq_sleeper
, wait
);
1586 if ((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
)
1587 mask
|= POLLOUT
| POLLWRNORM
;
1588 restore_flags(flags
);
1593 void sequencer_timer(unsigned long dummy
)
1598 int note_to_freq(int note_num
)
1602 * This routine converts a midi note to a frequency (multiplied by 1000)
1605 int note
, octave
, note_freq
;
1606 static int notes
[] =
1608 261632, 277189, 293671, 311132, 329632, 349232,
1609 369998, 391998, 415306, 440000, 466162, 493880
1612 #define BASE_OCTAVE 5
1614 octave
= note_num
/ 12;
1615 note
= note_num
% 12;
1617 note_freq
= notes
[note
];
1619 if (octave
< BASE_OCTAVE
)
1620 note_freq
>>= (BASE_OCTAVE
- octave
);
1621 else if (octave
> BASE_OCTAVE
)
1622 note_freq
<<= (octave
- BASE_OCTAVE
);
1631 unsigned long compute_finetune(unsigned long base_freq
, int bend
, int range
,
1634 unsigned long amount
;
1635 int negative
, semitones
, cents
, multiplier
= 1;
1648 bend
= bend
* range
/ 8192; /* Convert to cents */
1649 bend
+= vibrato_cents
;
1654 negative
= bend
< 0 ? 1 : 0;
1671 semitones
= bend
/ 100;
1676 amount
= (int) (semitone_tuning
[semitones
] * multiplier
* cent_tuning
[cents
]) / 10000;
1679 return (base_freq
* 10000) / amount
; /* Bend down */
1681 return (base_freq
* amount
) / 10000; /* Bend up */
1685 void sequencer_init(void)
1687 /* drag in sequencer_syms.o */
1689 extern char sequencer_syms_symbol
;
1690 sequencer_syms_symbol
= 0;
1698 queue
= (unsigned char *)vmalloc(SEQ_MAX_QUEUE
* EV_SZ
);
1701 printk(KERN_ERR
"sequencer: Can't allocate memory for sequencer output queue\n");
1704 iqueue
= (unsigned char *)vmalloc(SEQ_MAX_QUEUE
* IEV_SZ
);
1707 printk(KERN_ERR
"sequencer: Can't allocate memory for sequencer input queue\n");
1714 void sequencer_unload(void)