2 * sound/oss/sequencer.c
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>
18 #include <linux/spinlock.h>
19 #include "sound_config.h"
21 #include "midi_ctrl.h"
24 static int sequencer_ok
;
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 extern unsigned long seq_time
;
30 static int obsolete_api_used
;
31 static DEFINE_SPINLOCK(lock
);
34 * Local counts for number of synth and MIDI devices. These are initialized
35 * by the sequencer_open.
37 static int max_mididev
;
38 static int max_synthdev
;
41 * The seq_mode gives the operating mode of the sequencer:
42 * 1 = level1 (the default)
43 * 2 = level2 (extended capabilities)
48 static int seq_mode
= SEQ_1
;
50 static DECLARE_WAIT_QUEUE_HEAD(seq_sleeper
);
51 static DECLARE_WAIT_QUEUE_HEAD(midi_sleeper
);
53 static int midi_opened
[MAX_MIDI_DEV
];
55 static int midi_written
[MAX_MIDI_DEV
];
57 static unsigned long prev_input_time
;
58 static int prev_event_time
;
65 static unsigned char *queue
;
66 static unsigned char *iqueue
;
68 static volatile int qhead
, qtail
, qlen
;
69 static volatile int iqhead
, iqtail
, iqlen
;
70 static volatile int seq_playing
;
71 static volatile int sequencer_busy
;
72 static int output_threshold
;
73 static long pre_event_timeout
;
74 static unsigned synth_open_mask
;
76 static int seq_queue(unsigned char *note
, char nonblock
);
77 static void seq_startplay(void);
78 static int seq_sync(void);
79 static void seq_reset(void);
81 #if MAX_SYNTH_DEV > 15
82 #error Too many synthesizer devices enabled.
85 int sequencer_read(int dev
, struct file
*file
, char __user
*buf
, int count
)
93 ev_len
= seq_mode
== SEQ_1
? 4 : 8;
95 spin_lock_irqsave(&lock
,flags
);
99 spin_unlock_irqrestore(&lock
,flags
);
100 if (file
->f_flags
& O_NONBLOCK
) {
104 oss_broken_sleep_on(&midi_sleeper
, pre_event_timeout
);
105 spin_lock_irqsave(&lock
,flags
);
108 spin_unlock_irqrestore(&lock
,flags
);
112 while (iqlen
&& c
>= ev_len
)
114 char *fixit
= (char *) &iqueue
[iqhead
* IEV_SZ
];
115 spin_unlock_irqrestore(&lock
,flags
);
116 if (copy_to_user(&(buf
)[p
], fixit
, ev_len
))
121 spin_lock_irqsave(&lock
,flags
);
122 iqhead
= (iqhead
+ 1) % SEQ_MAX_QUEUE
;
125 spin_unlock_irqrestore(&lock
,flags
);
129 static void sequencer_midi_output(int dev
)
136 void seq_copy_to_input(unsigned char *event_rec
, int len
)
141 * Verify that the len is valid for the current mode.
144 if (len
!= 4 && len
!= 8)
146 if ((seq_mode
== SEQ_1
) != (len
== 4))
149 if (iqlen
>= (SEQ_MAX_QUEUE
- 1))
150 return; /* Overflow */
152 spin_lock_irqsave(&lock
,flags
);
153 memcpy(&iqueue
[iqtail
* IEV_SZ
], event_rec
, len
);
155 iqtail
= (iqtail
+ 1) % SEQ_MAX_QUEUE
;
156 wake_up(&midi_sleeper
);
157 spin_unlock_irqrestore(&lock
,flags
);
159 EXPORT_SYMBOL(seq_copy_to_input
);
161 static void sequencer_midi_input(int dev
, unsigned char data
)
164 unsigned char event_rec
[4];
166 if (data
== 0xfe) /* Ignore active sensing */
169 tstamp
= jiffies
- seq_time
;
171 if (tstamp
!= prev_input_time
)
173 tstamp
= (tstamp
<< 8) | SEQ_WAIT
;
174 seq_copy_to_input((unsigned char *) &tstamp
, 4);
175 prev_input_time
= tstamp
;
177 event_rec
[0] = SEQ_MIDIPUTC
;
182 seq_copy_to_input(event_rec
, 4);
185 void seq_input_event(unsigned char *event_rec
, int len
)
187 unsigned long this_time
;
189 if (seq_mode
== SEQ_2
)
190 this_time
= tmr
->get_time(tmr_no
);
192 this_time
= jiffies
- seq_time
;
194 if (this_time
!= prev_input_time
)
196 unsigned char tmp_event
[8];
198 tmp_event
[0] = EV_TIMING
;
199 tmp_event
[1] = TMR_WAIT_ABS
;
202 *(unsigned int *) &tmp_event
[4] = this_time
;
204 seq_copy_to_input(tmp_event
, 8);
205 prev_input_time
= this_time
;
207 seq_copy_to_input(event_rec
, len
);
209 EXPORT_SYMBOL(seq_input_event
);
211 int sequencer_write(int dev
, struct file
*file
, const char __user
*buf
, int count
)
213 unsigned char event_rec
[EV_SZ
], ev_code
;
214 int p
= 0, c
, ev_size
;
215 int mode
= translate_mode(file
);
219 if (mode
== OPEN_READ
)
226 if (copy_from_user((char *) event_rec
, &(buf
)[p
], 4))
228 ev_code
= event_rec
[0];
230 if (ev_code
== SEQ_FULLSIZE
)
234 dev
= *(unsigned short *) &event_rec
[2];
235 if (dev
< 0 || dev
>= max_synthdev
|| synth_devs
[dev
] == NULL
)
238 if (!(synth_open_mask
& (1 << dev
)))
241 fmt
= (*(short *) &event_rec
[0]) & 0xffff;
242 err
= synth_devs
[dev
]->load_patch(dev
, fmt
, buf
+ p
, c
, 0);
250 if (seq_mode
== SEQ_2
&& ev_code
== SEQ_EXTENDED
)
252 printk(KERN_WARNING
"Sequencer: Invalid level 2 event %x\n", ev_code
);
263 if (copy_from_user((char *)&event_rec
[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
);
302 midi_opened
[dev
] = 1;
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 int seq_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 oss_broken_sleep_on(&seq_sleeper
, MAX_SCHEDULE_TIMEOUT
);
346 if (qlen
>= SEQ_MAX_QUEUE
)
352 memcpy(&queue
[qtail
* EV_SZ
], note
, EV_SZ
);
354 qtail
= (qtail
+ 1) % SEQ_MAX_QUEUE
;
360 static int extended_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 int find_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 int alloc_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 void seq_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
)))
456 if (!synth_devs
[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 void seq_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
)))
538 if (!synth_devs
[dev
])
543 case MIDI_PGM_CHANGE
:
544 if (seq_mode
== SEQ_2
)
549 synth_devs
[dev
]->chn_info
[chn
].pgm_num
= p1
;
550 if ((int) dev
>= num_synths
)
551 synth_devs
[dev
]->set_instr(dev
, chn
, p1
);
554 synth_devs
[dev
]->set_instr(dev
, chn
, p1
);
558 case MIDI_CTL_CHANGE
:
559 if (seq_mode
== SEQ_2
)
561 if (chn
> 15 || p1
> 127)
564 synth_devs
[dev
]->chn_info
[chn
].controllers
[p1
] = w14
& 0x7f;
566 if (p1
< 32) /* Setting MSB should clear LSB to 0 */
567 synth_devs
[dev
]->chn_info
[chn
].controllers
[p1
+ 32] = 0;
569 if ((int) dev
< num_synths
)
571 int val
= w14
& 0x7f;
574 if (p1
< 64) /* Combine MSB and LSB */
576 val
= ((synth_devs
[dev
]->
577 chn_info
[chn
].controllers
[p1
& ~32] & 0x7f) << 7)
579 chn_info
[chn
].controllers
[p1
| 32] & 0x7f);
582 /* Handle all playing notes on this channel */
584 key
= ((int) chn
<< 8);
586 for (i
= 0; i
< synth_devs
[dev
]->alloc
.max_voice
; i
++)
587 if ((synth_devs
[dev
]->alloc
.map
[i
] & 0xff00) == key
)
588 synth_devs
[dev
]->controller(dev
, i
, p1
, val
);
591 synth_devs
[dev
]->controller(dev
, chn
, p1
, w14
);
594 synth_devs
[dev
]->controller(dev
, chn
, p1
, w14
);
597 case MIDI_PITCH_BEND
:
598 if (seq_mode
== SEQ_2
)
603 synth_devs
[dev
]->chn_info
[chn
].bender_value
= w14
;
605 if ((int) dev
< num_synths
)
607 /* Handle all playing notes on this channel */
612 for (i
= 0; i
< synth_devs
[dev
]->alloc
.max_voice
; i
++)
613 if ((synth_devs
[dev
]->alloc
.map
[i
] & 0xff00) == key
)
614 synth_devs
[dev
]->bender(dev
, i
, w14
);
617 synth_devs
[dev
]->bender(dev
, chn
, w14
);
620 synth_devs
[dev
]->bender(dev
, chn
, w14
);
627 static int seq_timing_event(unsigned char *event_rec
)
629 unsigned char cmd
= event_rec
[1];
630 unsigned int parm
= *(int *) &event_rec
[4];
632 if (seq_mode
== SEQ_2
)
636 if ((ret
= tmr
->event(tmr_no
, event_rec
)) == TIMER_ARMED
)
637 if ((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
)
638 wake_up(&seq_sleeper
);
644 parm
+= prev_event_time
;
647 * NOTE! No break here. Execution of TMR_WAIT_REL continues in the
648 * next case (TMR_WAIT_ABS)
657 prev_event_time
= time
;
660 request_sound_timer(time
);
662 if ((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
)
663 wake_up(&seq_sleeper
);
684 if (seq_mode
== SEQ_2
)
685 seq_copy_to_input(event_rec
, 8);
688 parm
= (parm
<< 8 | SEQ_ECHO
);
689 seq_copy_to_input((unsigned char *) &parm
, 4);
696 return TIMER_NOT_ARMED
;
699 static void seq_local_event(unsigned char *event_rec
)
701 unsigned char cmd
= event_rec
[1];
702 unsigned int parm
= *((unsigned int *) &event_rec
[4]);
706 case LOCL_STARTAUDIO
:
707 DMAbuf_start_devices(parm
);
714 static void seq_sysex_message(unsigned char *event_rec
)
716 unsigned int dev
= event_rec
[1];
718 unsigned char *buf
= &event_rec
[2];
720 if (dev
> max_synthdev
)
722 if (!(synth_open_mask
& (1 << dev
)))
724 if (!synth_devs
[dev
])
728 for (i
= 0; i
< 6 && buf
[i
] != 0xff; i
++)
731 if (!synth_devs
[dev
]->send_sysex
)
734 synth_devs
[dev
]->send_sysex(dev
, buf
, l
);
737 static int play_event(unsigned char *q
)
740 * NOTE! This routine returns
741 * 0 = normal event played.
742 * 1 = Timer armed. Suspend playback until timer callback.
743 * 2 = MIDI output buffer full. Restore queue and suspend until timer
750 if (synth_open_mask
& (1 << 0))
752 synth_devs
[0]->kill_note(0, q
[1], 255, q
[3]);
756 if (q
[4] < 128 || q
[4] == 255)
757 if (synth_open_mask
& (1 << 0))
759 synth_devs
[0]->start_note(0, q
[1], q
[2], q
[3]);
763 delay
= (unsigned int *) q
; /*
764 * Bytes 1 to 3 are containing the *
767 *delay
= (*delay
>> 8) & 0xffffff;
775 prev_event_time
= time
;
777 request_sound_timer(time
);
779 if ((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
)
780 wake_up(&seq_sleeper
);
782 * The timer is now active and will reinvoke this function
783 * after the timer expires. Return to the caller now.
790 if (synth_open_mask
& (1 << 0))
792 synth_devs
[0]->set_instr(0, q
[1], q
[2]);
795 case SEQ_SYNCTIMER
: /*
803 case SEQ_MIDIPUTC
: /*
804 * Put a midi character
806 if (midi_opened
[q
[2]])
812 if (dev
< 0 || dev
>= num_midis
|| midi_devs
[dev
] == NULL
)
815 if (!midi_devs
[dev
]->outputc(dev
, q
[1]))
818 * Output FIFO is full. Wait one timer cycle and try again.
822 request_sound_timer(-1);
826 midi_written
[dev
] = 1;
831 seq_copy_to_input(q
, 4); /*
832 * Echo back to the process
837 if ((int) q
[1] < max_synthdev
)
838 synth_devs
[q
[1]]->hw_control(q
[1], q
);
846 seq_chn_voice_event(q
);
850 seq_chn_common_event(q
);
854 if (seq_timing_event(q
) == TIMER_ARMED
)
865 seq_sysex_message(q
);
873 /* called also as timer in irq context */
874 static void seq_startplay(void)
876 int this_one
, action
;
882 spin_lock_irqsave(&lock
,flags
);
883 qhead
= ((this_one
= qhead
) + 1) % SEQ_MAX_QUEUE
;
885 spin_unlock_irqrestore(&lock
,flags
);
889 if ((action
= play_event(&queue
[this_one
* EV_SZ
])))
890 { /* Suspend playback. Next timer routine invokes this routine again */
902 if ((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
)
903 wake_up(&seq_sleeper
);
906 static void reset_controllers(int dev
, unsigned char *controller
, int update_dev
)
909 for (i
= 0; i
< 128; i
++)
910 controller
[i
] = ctrl_def_values
[i
];
913 static void setup_mode2(void)
917 max_synthdev
= num_synths
;
919 for (dev
= 0; dev
< num_midis
; dev
++)
921 if (midi_devs
[dev
] && midi_devs
[dev
]->converter
!= NULL
)
923 synth_devs
[max_synthdev
++] = midi_devs
[dev
]->converter
;
927 for (dev
= 0; dev
< max_synthdev
; dev
++)
931 synth_devs
[dev
]->sysex_ptr
= 0;
932 synth_devs
[dev
]->emulation
= 0;
934 for (chn
= 0; chn
< 16; chn
++)
936 synth_devs
[dev
]->chn_info
[chn
].pgm_num
= 0;
937 reset_controllers(dev
,
938 synth_devs
[dev
]->chn_info
[chn
].controllers
,0);
939 synth_devs
[dev
]->chn_info
[chn
].bender_value
= (1 << 7); /* Neutral */
940 synth_devs
[dev
]->chn_info
[chn
].bender_range
= 200;
947 int sequencer_open(int dev
, struct file
*file
)
955 level
= ((dev
& 0x0f) == SND_DEV_SEQ2
) ? 2 : 1;
958 mode
= translate_mode(file
);
962 /* printk("Sound card: sequencer not initialized\n");*/
965 if (dev
) /* Patch manager device (obsolete) */
968 if(synth_devs
[dev
] == NULL
)
969 request_module("synth0");
971 if (mode
== OPEN_READ
)
975 /*printk("Sequencer: No MIDI devices. Input not possible\n");*/
985 obsolete_api_used
= 0;
987 max_mididev
= num_midis
;
988 max_synthdev
= num_synths
;
989 pre_event_timeout
= MAX_SCHEDULE_TIMEOUT
;
992 if (pending_timer
!= -1)
994 tmr_no
= pending_timer
;
997 if (tmr_no
== -1) /* Not selected yet */
1002 for (i
= 0; i
< num_sound_timers
; i
++)
1003 if (sound_timer_devs
[i
] && sound_timer_devs
[i
]->priority
> best
)
1006 best
= sound_timer_devs
[i
]->priority
;
1008 if (tmr_no
== -1) /* Should not be */
1011 tmr
= sound_timer_devs
[tmr_no
];
1017 /*printk("sequencer: No timer for level 2\n");*/
1023 if (!max_synthdev
&& !max_mididev
)
1029 synth_open_mask
= 0;
1031 for (i
= 0; i
< max_mididev
; i
++)
1034 midi_written
[i
] = 0;
1037 for (i
= 0; i
< max_synthdev
; i
++)
1039 if (synth_devs
[i
]==NULL
)
1042 if (!try_module_get(synth_devs
[i
]->owner
))
1045 if ((tmp
= synth_devs
[i
]->open(i
, mode
)) < 0)
1047 printk(KERN_WARNING
"Sequencer: Warning! Cannot open synth device #%d (%d)\n", i
, tmp
);
1048 if (synth_devs
[i
]->midi_dev
)
1049 printk(KERN_WARNING
"(Maps to MIDI dev #%d)\n", synth_devs
[i
]->midi_dev
);
1053 synth_open_mask
|= (1 << i
);
1054 if (synth_devs
[i
]->midi_dev
)
1055 midi_opened
[synth_devs
[i
]->midi_dev
] = 1;
1061 prev_input_time
= 0;
1062 prev_event_time
= 0;
1064 if (seq_mode
== SEQ_1
&& (mode
== OPEN_READ
|| mode
== OPEN_READWRITE
))
1067 * Initialize midi input devices
1070 for (i
= 0; i
< max_mididev
; i
++)
1071 if (!midi_opened
[i
] && midi_devs
[i
])
1073 if (!try_module_get(midi_devs
[i
]->owner
))
1076 if ((retval
= midi_devs
[i
]->open(i
, mode
,
1077 sequencer_midi_input
, sequencer_midi_output
)) >= 0)
1084 if (seq_mode
== SEQ_2
) {
1085 if (try_module_get(tmr
->owner
))
1086 tmr
->open(tmr_no
, seq_mode
);
1089 init_waitqueue_head(&seq_sleeper
);
1090 init_waitqueue_head(&midi_sleeper
);
1091 output_threshold
= SEQ_MAX_QUEUE
/ 2;
1096 static void seq_drain_midi_queues(void)
1101 * Give the Midi drivers time to drain their output queues
1106 while (!signal_pending(current
) && n
)
1110 for (i
= 0; i
< max_mididev
; i
++)
1111 if (midi_opened
[i
] && midi_written
[i
])
1112 if (midi_devs
[i
]->buffer_status
!= NULL
)
1113 if (midi_devs
[i
]->buffer_status(i
))
1117 * Let's have a delay
1121 oss_broken_sleep_on(&seq_sleeper
, HZ
/10);
1125 void sequencer_release(int dev
, struct file
*file
)
1128 int mode
= translate_mode(file
);
1133 * Wait until the queue is empty (if we don't have nonblock)
1136 if (mode
!= OPEN_READ
&& !(file
->f_flags
& O_NONBLOCK
))
1138 while (!signal_pending(current
) && qlen
> 0)
1141 oss_broken_sleep_on(&seq_sleeper
, 3*HZ
);
1146 if (mode
!= OPEN_READ
)
1147 seq_drain_midi_queues(); /*
1148 * Ensure the output queues are empty
1151 if (mode
!= OPEN_READ
)
1152 seq_drain_midi_queues(); /*
1153 * Flush the all notes off messages
1156 for (i
= 0; i
< max_synthdev
; i
++)
1158 if (synth_open_mask
& (1 << i
)) /*
1163 synth_devs
[i
]->close(i
);
1165 module_put(synth_devs
[i
]->owner
);
1167 if (synth_devs
[i
]->midi_dev
)
1168 midi_opened
[synth_devs
[i
]->midi_dev
] = 0;
1172 for (i
= 0; i
< max_mididev
; i
++)
1174 if (midi_opened
[i
]) {
1175 midi_devs
[i
]->close(i
);
1176 module_put(midi_devs
[i
]->owner
);
1180 if (seq_mode
== SEQ_2
) {
1182 module_put(tmr
->owner
);
1185 if (obsolete_api_used
)
1186 printk(KERN_WARNING
"/dev/music: Obsolete (4 byte) API was used by %s\n", current
->comm
);
1190 static int seq_sync(void)
1192 if (qlen
&& !seq_playing
&& !signal_pending(current
))
1196 oss_broken_sleep_on(&seq_sleeper
, HZ
);
1200 static void midi_outc(int dev
, unsigned char data
)
1203 * NOTE! Calls sleep(). Don't call this from interrupt.
1207 unsigned long flags
;
1210 * This routine sends one byte to the Midi channel.
1211 * If the output FIFO is full, it waits until there
1212 * is space in the queue
1215 n
= 3 * HZ
; /* Timeout */
1217 spin_lock_irqsave(&lock
,flags
);
1218 while (n
&& !midi_devs
[dev
]->outputc(dev
, data
)) {
1219 oss_broken_sleep_on(&seq_sleeper
, HZ
/25);
1222 spin_unlock_irqrestore(&lock
,flags
);
1225 static void seq_reset(void)
1228 * NOTE! Calls sleep(). Don't call this from interrupt.
1233 unsigned long flags
;
1238 prev_input_time
= 0;
1239 prev_event_time
= 0;
1241 qlen
= qhead
= qtail
= 0;
1242 iqlen
= iqhead
= iqtail
= 0;
1244 for (i
= 0; i
< max_synthdev
; i
++)
1245 if (synth_open_mask
& (1 << i
))
1247 synth_devs
[i
]->reset(i
);
1249 if (seq_mode
== SEQ_2
)
1251 for (chn
= 0; chn
< 16; chn
++)
1252 for (i
= 0; i
< max_synthdev
; i
++)
1253 if (synth_open_mask
& (1 << i
))
1256 synth_devs
[i
]->controller(i
, chn
, 123, 0); /* All notes off */
1257 synth_devs
[i
]->controller(i
, chn
, 121, 0); /* Reset all ctl */
1258 synth_devs
[i
]->bender(i
, chn
, 1 << 13); /* Bender off */
1261 else /* seq_mode == SEQ_1 */
1263 for (i
= 0; i
< max_mididev
; i
++)
1264 if (midi_written
[i
]) /*
1265 * Midi used. Some notes may still be playing
1269 * Sending just a ACTIVE SENSING message should be enough to stop all
1270 * playing notes. Since there are devices not recognizing the
1271 * active sensing, we have to send some all notes off messages also.
1275 for (chn
= 0; chn
< 16; chn
++)
1277 midi_outc(i
, (unsigned char) (0xb0 + (chn
& 0x0f))); /* control change */
1278 midi_outc(i
, 0x7b); /* All notes off */
1279 midi_outc(i
, 0); /* Dummy parameter */
1282 midi_devs
[i
]->close(i
);
1284 midi_written
[i
] = 0;
1291 spin_lock_irqsave(&lock
,flags
);
1293 if (waitqueue_active(&seq_sleeper
)) {
1294 /* printk( "Sequencer Warning: Unexpected sleeping process - Waking up\n"); */
1295 wake_up(&seq_sleeper
);
1297 spin_unlock_irqrestore(&lock
,flags
);
1300 static void seq_panic(void)
1303 * This routine is called by the application in case the user
1304 * wants to reset the system to the default state.
1310 * Since some of the devices don't recognize the active sensing and
1311 * all notes off messages, we have to shut all notes manually.
1313 * TO BE IMPLEMENTED LATER
1317 * Also return the controllers to their default states
1321 int sequencer_ioctl(int dev
, struct file
*file
, unsigned int cmd
, void __user
*arg
)
1323 int midi_dev
, orig_dev
, val
, err
;
1324 int mode
= translate_mode(file
);
1325 struct synth_info inf
;
1326 struct seq_event_rec event_rec
;
1327 unsigned long flags
;
1328 int __user
*p
= arg
;
1330 orig_dev
= dev
= dev
>> 4;
1334 case SNDCTL_TMR_TIMEBASE
:
1335 case SNDCTL_TMR_TEMPO
:
1336 case SNDCTL_TMR_START
:
1337 case SNDCTL_TMR_STOP
:
1338 case SNDCTL_TMR_CONTINUE
:
1339 case SNDCTL_TMR_METRONOME
:
1340 case SNDCTL_TMR_SOURCE
:
1341 if (seq_mode
!= SEQ_2
)
1343 return tmr
->ioctl(tmr_no
, cmd
, arg
);
1345 case SNDCTL_TMR_SELECT
:
1346 if (seq_mode
!= SEQ_2
)
1348 if (get_user(pending_timer
, p
))
1350 if (pending_timer
< 0 || pending_timer
>= num_sound_timers
|| sound_timer_devs
[pending_timer
] == NULL
)
1355 val
= pending_timer
;
1358 case SNDCTL_SEQ_PANIC
:
1362 case SNDCTL_SEQ_SYNC
:
1363 if (mode
== OPEN_READ
)
1365 while (qlen
> 0 && !signal_pending(current
))
1367 return qlen
? -EINTR
: 0;
1369 case SNDCTL_SEQ_RESET
:
1373 case SNDCTL_SEQ_TESTMIDI
:
1374 if (__get_user(midi_dev
, p
))
1376 if (midi_dev
< 0 || midi_dev
>= max_mididev
|| !midi_devs
[midi_dev
])
1379 if (!midi_opened
[midi_dev
] &&
1380 (err
= midi_devs
[midi_dev
]->open(midi_dev
, mode
, sequencer_midi_input
,
1381 sequencer_midi_output
)) < 0)
1383 midi_opened
[midi_dev
] = 1;
1386 case SNDCTL_SEQ_GETINCOUNT
:
1387 if (mode
== OPEN_WRITE
)
1392 case SNDCTL_SEQ_GETOUTCOUNT
:
1393 if (mode
== OPEN_READ
)
1395 val
= SEQ_MAX_QUEUE
- qlen
;
1398 case SNDCTL_SEQ_GETTIME
:
1399 if (seq_mode
== SEQ_2
)
1400 return tmr
->ioctl(tmr_no
, cmd
, arg
);
1401 val
= jiffies
- seq_time
;
1404 case SNDCTL_SEQ_CTRLRATE
:
1406 * If *arg == 0, just return the current rate
1408 if (seq_mode
== SEQ_2
)
1409 return tmr
->ioctl(tmr_no
, cmd
, arg
);
1411 if (get_user(val
, p
))
1418 case SNDCTL_SEQ_RESETSAMPLES
:
1419 case SNDCTL_SYNTH_REMOVESAMPLE
:
1420 case SNDCTL_SYNTH_CONTROL
:
1421 if (get_user(dev
, p
))
1423 if (dev
< 0 || dev
>= num_synths
|| synth_devs
[dev
] == NULL
)
1425 if (!(synth_open_mask
& (1 << dev
)) && !orig_dev
)
1427 return synth_devs
[dev
]->ioctl(dev
, cmd
, arg
);
1429 case SNDCTL_SEQ_NRSYNTHS
:
1433 case SNDCTL_SEQ_NRMIDIS
:
1437 case SNDCTL_SYNTH_MEMAVL
:
1438 if (get_user(dev
, p
))
1440 if (dev
< 0 || dev
>= num_synths
|| synth_devs
[dev
] == NULL
)
1442 if (!(synth_open_mask
& (1 << dev
)) && !orig_dev
)
1444 val
= synth_devs
[dev
]->ioctl(dev
, cmd
, arg
);
1447 case SNDCTL_FM_4OP_ENABLE
:
1448 if (get_user(dev
, p
))
1450 if (dev
< 0 || dev
>= num_synths
|| synth_devs
[dev
] == NULL
)
1452 if (!(synth_open_mask
& (1 << dev
)))
1454 synth_devs
[dev
]->ioctl(dev
, cmd
, arg
);
1457 case SNDCTL_SYNTH_INFO
:
1458 if (get_user(dev
, &((struct synth_info __user
*)arg
)->device
))
1460 if (dev
< 0 || dev
>= max_synthdev
)
1462 if (!(synth_open_mask
& (1 << dev
)) && !orig_dev
)
1464 return synth_devs
[dev
]->ioctl(dev
, cmd
, arg
);
1466 /* Like SYNTH_INFO but returns ID in the name field */
1467 case SNDCTL_SYNTH_ID
:
1468 if (get_user(dev
, &((struct synth_info __user
*)arg
)->device
))
1470 if (dev
< 0 || dev
>= max_synthdev
)
1472 if (!(synth_open_mask
& (1 << dev
)) && !orig_dev
)
1474 memcpy(&inf
, synth_devs
[dev
]->info
, sizeof(inf
));
1475 strlcpy(inf
.name
, synth_devs
[dev
]->id
, sizeof(inf
.name
));
1477 return copy_to_user(arg
, &inf
, sizeof(inf
))?-EFAULT
:0;
1479 case SNDCTL_SEQ_OUTOFBAND
:
1480 if (copy_from_user(&event_rec
, arg
, sizeof(event_rec
)))
1482 spin_lock_irqsave(&lock
,flags
);
1483 play_event(event_rec
.arr
);
1484 spin_unlock_irqrestore(&lock
,flags
);
1487 case SNDCTL_MIDI_INFO
:
1488 if (get_user(dev
, &((struct midi_info __user
*)arg
)->device
))
1490 if (dev
< 0 || dev
>= max_mididev
|| !midi_devs
[dev
])
1492 midi_devs
[dev
]->info
.device
= dev
;
1493 return copy_to_user(arg
, &midi_devs
[dev
]->info
, sizeof(struct midi_info
))?-EFAULT
:0;
1495 case SNDCTL_SEQ_THRESHOLD
:
1496 if (get_user(val
, p
))
1500 if (val
>= SEQ_MAX_QUEUE
)
1501 val
= SEQ_MAX_QUEUE
- 1;
1502 output_threshold
= val
;
1505 case SNDCTL_MIDI_PRETIME
:
1506 if (get_user(val
, p
))
1510 val
= (HZ
* val
) / 10;
1511 pre_event_timeout
= val
;
1515 if (mode
== OPEN_READ
)
1519 if (!(synth_open_mask
& (1 << 0)))
1521 if (!synth_devs
[0]->ioctl
)
1523 return synth_devs
[0]->ioctl(0, cmd
, arg
);
1525 return put_user(val
, p
);
1528 /* No kernel lock - we're using the global irq lock here */
1529 unsigned int sequencer_poll(int dev
, struct file
*file
, poll_table
* wait
)
1531 unsigned long flags
;
1532 unsigned int mask
= 0;
1536 spin_lock_irqsave(&lock
,flags
);
1538 poll_wait(file
, &midi_sleeper
, wait
);
1540 mask
|= POLLIN
| POLLRDNORM
;
1543 poll_wait(file
, &seq_sleeper
, wait
);
1544 if ((SEQ_MAX_QUEUE
- qlen
) >= output_threshold
)
1545 mask
|= POLLOUT
| POLLWRNORM
;
1546 spin_unlock_irqrestore(&lock
,flags
);
1551 void sequencer_timer(unsigned long dummy
)
1555 EXPORT_SYMBOL(sequencer_timer
);
1557 int note_to_freq(int note_num
)
1561 * This routine converts a midi note to a frequency (multiplied by 1000)
1564 int note
, octave
, note_freq
;
1565 static int notes
[] =
1567 261632, 277189, 293671, 311132, 329632, 349232,
1568 369998, 391998, 415306, 440000, 466162, 493880
1571 #define BASE_OCTAVE 5
1573 octave
= note_num
/ 12;
1574 note
= note_num
% 12;
1576 note_freq
= notes
[note
];
1578 if (octave
< BASE_OCTAVE
)
1579 note_freq
>>= (BASE_OCTAVE
- octave
);
1580 else if (octave
> BASE_OCTAVE
)
1581 note_freq
<<= (octave
- BASE_OCTAVE
);
1589 EXPORT_SYMBOL(note_to_freq
);
1591 unsigned long compute_finetune(unsigned long base_freq
, int bend
, int range
,
1594 unsigned long amount
;
1595 int negative
, semitones
, cents
, multiplier
= 1;
1608 bend
= bend
* range
/ 8192; /* Convert to cents */
1609 bend
+= vibrato_cents
;
1614 negative
= bend
< 0 ? 1 : 0;
1631 semitones
= bend
/ 100;
1634 amount
= (int) (semitone_tuning
[semitones
] * multiplier
* cent_tuning
[cents
]) / 10000;
1637 return (base_freq
* 10000) / amount
; /* Bend down */
1639 return (base_freq
* amount
) / 10000; /* Bend up */
1641 EXPORT_SYMBOL(compute_finetune
);
1643 void sequencer_init(void)
1647 queue
= vmalloc(SEQ_MAX_QUEUE
* EV_SZ
);
1650 printk(KERN_ERR
"sequencer: Can't allocate memory for sequencer output queue\n");
1653 iqueue
= vmalloc(SEQ_MAX_QUEUE
* IEV_SZ
);
1656 printk(KERN_ERR
"sequencer: Can't allocate memory for sequencer input queue\n");
1662 EXPORT_SYMBOL(sequencer_init
);
1664 void sequencer_unload(void)
1668 queue
= iqueue
= NULL
;