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"
23 static int sequencer_ok;
24 static struct sound_timer_operations *tmr;
25 static int tmr_no = -1; /* Currently selected timer */
26 static int pending_timer = -1; /* For timer change operation */
27 extern unsigned long seq_time;
29 static int obsolete_api_used;
30 static DEFINE_SPINLOCK(lock);
33 * Local counts for number of synth and MIDI devices. These are initialized
34 * by the sequencer_open.
36 static int max_mididev;
37 static int max_synthdev;
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 static DECLARE_WAIT_QUEUE_HEAD(seq_sleeper);
50 static DECLARE_WAIT_QUEUE_HEAD(midi_sleeper);
52 static int midi_opened[MAX_MIDI_DEV];
54 static int midi_written[MAX_MIDI_DEV];
56 static unsigned long prev_input_time;
57 static int prev_event_time;
64 static unsigned char *queue;
65 static unsigned char *iqueue;
67 static volatile int qhead, qtail, qlen;
68 static volatile int iqhead, iqtail, iqlen;
69 static volatile int seq_playing;
70 static volatile int sequencer_busy;
71 static int output_threshold;
72 static long pre_event_timeout;
73 static unsigned synth_open_mask;
75 static int seq_queue(unsigned char *note, char nonblock);
76 static void seq_startplay(void);
77 static int seq_sync(void);
78 static void seq_reset(void);
80 #if MAX_SYNTH_DEV > 15
81 #error Too many synthesizer devices enabled.
84 int sequencer_read(int dev, struct file *file, char __user *buf, int count)
92 ev_len = seq_mode == SEQ_1 ? 4 : 8;
94 spin_lock_irqsave(&lock,flags);
98 spin_unlock_irqrestore(&lock,flags);
99 if (file->f_flags & O_NONBLOCK) {
103 interruptible_sleep_on_timeout(&midi_sleeper,
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);
160 static void sequencer_midi_input(int dev, unsigned char data)
163 unsigned char event_rec[4];
165 if (data == 0xfe) /* Ignore active sensing */
168 tstamp = jiffies - seq_time;
170 if (tstamp != prev_input_time)
172 tstamp = (tstamp << 8) | SEQ_WAIT;
173 seq_copy_to_input((unsigned char *) &tstamp, 4);
174 prev_input_time = tstamp;
176 event_rec[0] = SEQ_MIDIPUTC;
181 seq_copy_to_input(event_rec, 4);
184 void seq_input_event(unsigned char *event_rec, int len)
186 unsigned long this_time;
188 if (seq_mode == SEQ_2)
189 this_time = tmr->get_time(tmr_no);
191 this_time = jiffies - seq_time;
193 if (this_time != prev_input_time)
195 unsigned char tmp_event[8];
197 tmp_event[0] = EV_TIMING;
198 tmp_event[1] = TMR_WAIT_ABS;
201 *(unsigned int *) &tmp_event[4] = this_time;
203 seq_copy_to_input(tmp_event, 8);
204 prev_input_time = this_time;
206 seq_copy_to_input(event_rec, len);
209 int sequencer_write(int dev, struct file *file, const char __user *buf, int count)
211 unsigned char event_rec[EV_SZ], ev_code;
212 int p = 0, c, ev_size;
214 int mode = translate_mode(file);
218 DEB(printk("sequencer_write(dev=%d, count=%d)\n", dev, count));
220 if (mode == OPEN_READ)
227 if (copy_from_user((char *) event_rec, &(buf)[p], 4))
229 ev_code = event_rec[0];
231 if (ev_code == SEQ_FULLSIZE)
235 dev = *(unsigned short *) &event_rec[2];
236 if (dev < 0 || dev >= max_synthdev || synth_devs[dev] == NULL)
239 if (!(synth_open_mask & (1 << dev)))
242 fmt = (*(short *) &event_rec[0]) & 0xffff;
243 err = synth_devs[dev]->load_patch(dev, fmt, buf, p + 4, c, 0);
251 if (seq_mode == SEQ_2 && ev_code == SEQ_EXTENDED)
253 printk(KERN_WARNING "Sequencer: Invalid level 2 event %x\n", ev_code);
264 if (copy_from_user((char *)&event_rec[4],
271 if (seq_mode == SEQ_2)
273 printk(KERN_WARNING "Sequencer: 4 byte event in level 2 mode\n");
278 if (event_rec[0] != SEQ_MIDIPUTC)
279 obsolete_api_used = 1;
282 if (event_rec[0] == SEQ_MIDIPUTC)
284 if (!midi_opened[event_rec[2]])
287 int dev = event_rec[2];
289 if (dev >= max_mididev || midi_devs[dev]==NULL)
291 /*printk("Sequencer Error: Nonexistent MIDI device %d\n", dev);*/
294 mode = translate_mode(file);
296 if ((err = midi_devs[dev]->open(dev, mode,
297 sequencer_midi_input, sequencer_midi_output)) < 0)
300 printk(KERN_WARNING "Sequencer Error: Unable to open Midi #%d\n", dev);
303 midi_opened[dev] = 1;
306 if (!seq_queue(event_rec, (file->f_flags & (O_NONBLOCK) ? 1 : 0)))
308 int processed = count - c;
313 if (!processed && (file->f_flags & O_NONBLOCK))
328 static int seq_queue(unsigned char *note, char nonblock)
332 * Test if there is space in the queue
335 if (qlen >= SEQ_MAX_QUEUE)
338 * Give chance to drain the queue
341 if (!nonblock && qlen >= SEQ_MAX_QUEUE && !waitqueue_active(&seq_sleeper)) {
343 * Sleep until there is enough space on the queue
345 interruptible_sleep_on(&seq_sleeper);
347 if (qlen >= SEQ_MAX_QUEUE)
353 memcpy(&queue[qtail * EV_SZ], note, EV_SZ);
355 qtail = (qtail + 1) % SEQ_MAX_QUEUE;
361 static int extended_event(unsigned char *q)
365 if (dev < 0 || dev >= max_synthdev)
368 if (!(synth_open_mask & (1 << dev)))
374 synth_devs[dev]->kill_note(dev, q[3], q[4], q[5]);
378 if (q[4] > 127 && q[4] != 255)
383 synth_devs[dev]->kill_note(dev, q[3], q[4], q[5]);
386 synth_devs[dev]->start_note(dev, q[3], q[4], q[5]);
390 synth_devs[dev]->set_instr(dev, q[3], q[4]);
394 synth_devs[dev]->aftertouch(dev, q[3], q[4]);
398 synth_devs[dev]->panning(dev, q[3], (char) q[4]);
402 synth_devs[dev]->controller(dev, q[3], q[4], (short) (q[5] | (q[6] << 8)));
406 if (synth_devs[dev]->volume_method != NULL)
407 synth_devs[dev]->volume_method(dev, q[3]);
416 static int find_voice(int dev, int chn, int note)
421 key = (chn << 8) | (note + 1);
422 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
423 if (synth_devs[dev]->alloc.map[i] == key)
428 static int alloc_voice(int dev, int chn, int note)
433 key = (chn << 8) | (note + 1);
435 voice = synth_devs[dev]->alloc_voice(dev, chn, note,
436 &synth_devs[dev]->alloc);
437 synth_devs[dev]->alloc.map[voice] = key;
438 synth_devs[dev]->alloc.alloc_times[voice] =
439 synth_devs[dev]->alloc.timestamp++;
443 static void seq_chn_voice_event(unsigned char *event_rec)
445 #define dev event_rec[1]
446 #define cmd event_rec[2]
447 #define chn event_rec[3]
448 #define note event_rec[4]
449 #define parm event_rec[5]
453 if ((int) dev > max_synthdev || synth_devs[dev] == NULL)
455 if (!(synth_open_mask & (1 << dev)))
457 if (!synth_devs[dev])
460 if (seq_mode == SEQ_2)
462 if (synth_devs[dev]->alloc_voice)
463 voice = find_voice(dev, chn, note);
465 if (cmd == MIDI_NOTEON && parm == 0)
475 if (note > 127 && note != 255) /* Not a seq2 feature */
478 if (voice == -1 && seq_mode == SEQ_2 && synth_devs[dev]->alloc_voice)
480 /* Internal synthesizer (FM, GUS, etc) */
481 voice = alloc_voice(dev, chn, note);
486 if (seq_mode == SEQ_2 && (int) dev < num_synths)
489 * The MIDI channel 10 is a percussive channel. Use the note
490 * number to select the proper patch (128 to 255) to play.
495 synth_devs[dev]->set_instr(dev, voice, 128 + note);
496 synth_devs[dev]->chn_info[chn].pgm_num = 128 + note;
498 synth_devs[dev]->setup_voice(dev, voice, chn);
500 synth_devs[dev]->start_note(dev, voice, note, parm);
506 synth_devs[dev]->kill_note(dev, voice, note, parm);
509 case MIDI_KEY_PRESSURE:
512 synth_devs[dev]->aftertouch(dev, voice, parm);
525 static void seq_chn_common_event(unsigned char *event_rec)
527 unsigned char dev = event_rec[1];
528 unsigned char cmd = event_rec[2];
529 unsigned char chn = event_rec[3];
530 unsigned char p1 = event_rec[4];
532 /* unsigned char p2 = event_rec[5]; */
533 unsigned short w14 = *(short *) &event_rec[6];
535 if ((int) dev > max_synthdev || synth_devs[dev] == NULL)
537 if (!(synth_open_mask & (1 << dev)))
539 if (!synth_devs[dev])
544 case MIDI_PGM_CHANGE:
545 if (seq_mode == SEQ_2)
547 synth_devs[dev]->chn_info[chn].pgm_num = p1;
548 if ((int) dev >= num_synths)
549 synth_devs[dev]->set_instr(dev, chn, p1);
552 synth_devs[dev]->set_instr(dev, chn, p1);
556 case MIDI_CTL_CHANGE:
557 if (seq_mode == SEQ_2)
559 if (chn > 15 || p1 > 127)
562 synth_devs[dev]->chn_info[chn].controllers[p1] = w14 & 0x7f;
564 if (p1 < 32) /* Setting MSB should clear LSB to 0 */
565 synth_devs[dev]->chn_info[chn].controllers[p1 + 32] = 0;
567 if ((int) dev < num_synths)
569 int val = w14 & 0x7f;
572 if (p1 < 64) /* Combine MSB and LSB */
574 val = ((synth_devs[dev]->
575 chn_info[chn].controllers[p1 & ~32] & 0x7f) << 7)
577 chn_info[chn].controllers[p1 | 32] & 0x7f);
580 /* Handle all playing notes on this channel */
582 key = ((int) chn << 8);
584 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
585 if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
586 synth_devs[dev]->controller(dev, i, p1, val);
589 synth_devs[dev]->controller(dev, chn, p1, w14);
592 synth_devs[dev]->controller(dev, chn, p1, w14);
595 case MIDI_PITCH_BEND:
596 if (seq_mode == SEQ_2)
598 synth_devs[dev]->chn_info[chn].bender_value = w14;
600 if ((int) dev < num_synths)
602 /* Handle all playing notes on this channel */
607 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
608 if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
609 synth_devs[dev]->bender(dev, i, w14);
612 synth_devs[dev]->bender(dev, chn, w14);
615 synth_devs[dev]->bender(dev, chn, w14);
622 static int seq_timing_event(unsigned char *event_rec)
624 unsigned char cmd = event_rec[1];
625 unsigned int parm = *(int *) &event_rec[4];
627 if (seq_mode == SEQ_2)
631 if ((ret = tmr->event(tmr_no, event_rec)) == TIMER_ARMED)
632 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
633 wake_up(&seq_sleeper);
639 parm += prev_event_time;
642 * NOTE! No break here. Execution of TMR_WAIT_REL continues in the
643 * next case (TMR_WAIT_ABS)
652 prev_event_time = time;
655 request_sound_timer(time);
657 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
658 wake_up(&seq_sleeper);
679 if (seq_mode == SEQ_2)
680 seq_copy_to_input(event_rec, 8);
683 parm = (parm << 8 | SEQ_ECHO);
684 seq_copy_to_input((unsigned char *) &parm, 4);
691 return TIMER_NOT_ARMED;
694 static void seq_local_event(unsigned char *event_rec)
696 unsigned char cmd = event_rec[1];
697 unsigned int parm = *((unsigned int *) &event_rec[4]);
701 case LOCL_STARTAUDIO:
702 DMAbuf_start_devices(parm);
709 static void seq_sysex_message(unsigned char *event_rec)
711 unsigned int dev = event_rec[1];
713 unsigned char *buf = &event_rec[2];
715 if (dev > max_synthdev)
717 if (!(synth_open_mask & (1 << dev)))
719 if (!synth_devs[dev])
723 for (i = 0; i < 6 && buf[i] != 0xff; i++)
726 if (!synth_devs[dev]->send_sysex)
729 synth_devs[dev]->send_sysex(dev, buf, l);
732 static int play_event(unsigned char *q)
735 * NOTE! This routine returns
736 * 0 = normal event played.
737 * 1 = Timer armed. Suspend playback until timer callback.
738 * 2 = MIDI output buffer full. Restore queue and suspend until timer
745 if (synth_open_mask & (1 << 0))
747 synth_devs[0]->kill_note(0, q[1], 255, q[3]);
751 if (q[4] < 128 || q[4] == 255)
752 if (synth_open_mask & (1 << 0))
754 synth_devs[0]->start_note(0, q[1], q[2], q[3]);
758 delay = (unsigned int *) q; /*
759 * Bytes 1 to 3 are containing the *
762 *delay = (*delay >> 8) & 0xffffff;
770 prev_event_time = time;
772 request_sound_timer(time);
774 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
775 wake_up(&seq_sleeper);
777 * The timer is now active and will reinvoke this function
778 * after the timer expires. Return to the caller now.
785 if (synth_open_mask & (1 << 0))
787 synth_devs[0]->set_instr(0, q[1], q[2]);
790 case SEQ_SYNCTIMER: /*
798 case SEQ_MIDIPUTC: /*
799 * Put a midi character
801 if (midi_opened[q[2]])
807 if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
810 if (!midi_devs[dev]->outputc(dev, q[1]))
813 * Output FIFO is full. Wait one timer cycle and try again.
817 request_sound_timer(-1);
821 midi_written[dev] = 1;
826 seq_copy_to_input(q, 4); /*
827 * Echo back to the process
832 if ((int) q[1] < max_synthdev)
833 synth_devs[q[1]]->hw_control(q[1], q);
841 seq_chn_voice_event(q);
845 seq_chn_common_event(q);
849 if (seq_timing_event(q) == TIMER_ARMED)
860 seq_sysex_message(q);
868 /* called also as timer in irq context */
869 static void seq_startplay(void)
871 int this_one, action;
877 spin_lock_irqsave(&lock,flags);
878 qhead = ((this_one = qhead) + 1) % SEQ_MAX_QUEUE;
880 spin_unlock_irqrestore(&lock,flags);
884 if ((action = play_event(&queue[this_one * EV_SZ])))
885 { /* Suspend playback. Next timer routine invokes this routine again */
897 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
898 wake_up(&seq_sleeper);
901 static void reset_controllers(int dev, unsigned char *controller, int update_dev)
904 for (i = 0; i < 128; i++)
905 controller[i] = ctrl_def_values[i];
908 static void setup_mode2(void)
912 max_synthdev = num_synths;
914 for (dev = 0; dev < num_midis; dev++)
916 if (midi_devs[dev] && midi_devs[dev]->converter != NULL)
918 synth_devs[max_synthdev++] = midi_devs[dev]->converter;
922 for (dev = 0; dev < max_synthdev; dev++)
926 synth_devs[dev]->sysex_ptr = 0;
927 synth_devs[dev]->emulation = 0;
929 for (chn = 0; chn < 16; chn++)
931 synth_devs[dev]->chn_info[chn].pgm_num = 0;
932 reset_controllers(dev,
933 synth_devs[dev]->chn_info[chn].controllers,0);
934 synth_devs[dev]->chn_info[chn].bender_value = (1 << 7); /* Neutral */
935 synth_devs[dev]->chn_info[chn].bender_range = 200;
942 int sequencer_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");*/
982 obsolete_api_used = 0;
984 max_mididev = num_midis;
985 max_synthdev = num_synths;
986 pre_event_timeout = MAX_SCHEDULE_TIMEOUT;
989 if (pending_timer != -1)
991 tmr_no = pending_timer;
994 if (tmr_no == -1) /* Not selected yet */
999 for (i = 0; i < num_sound_timers; i++)
1000 if (sound_timer_devs[i] && sound_timer_devs[i]->priority > best)
1003 best = sound_timer_devs[i]->priority;
1005 if (tmr_no == -1) /* Should not be */
1008 tmr = sound_timer_devs[tmr_no];
1014 /*printk("sequencer: No timer for level 2\n");*/
1020 if (!max_synthdev && !max_mididev)
1026 synth_open_mask = 0;
1028 for (i = 0; i < max_mididev; i++)
1031 midi_written[i] = 0;
1034 for (i = 0; i < max_synthdev; i++)
1036 if (synth_devs[i]==NULL)
1039 if (!try_module_get(synth_devs[i]->owner))
1042 if ((tmp = synth_devs[i]->open(i, mode)) < 0)
1044 printk(KERN_WARNING "Sequencer: Warning! Cannot open synth device #%d (%d)\n", i, tmp);
1045 if (synth_devs[i]->midi_dev)
1046 printk(KERN_WARNING "(Maps to MIDI dev #%d)\n", synth_devs[i]->midi_dev);
1050 synth_open_mask |= (1 << i);
1051 if (synth_devs[i]->midi_dev)
1052 midi_opened[synth_devs[i]->midi_dev] = 1;
1058 prev_input_time = 0;
1059 prev_event_time = 0;
1061 if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
1064 * Initialize midi input devices
1067 for (i = 0; i < max_mididev; i++)
1068 if (!midi_opened[i] && midi_devs[i])
1070 if (!try_module_get(midi_devs[i]->owner))
1073 if ((retval = midi_devs[i]->open(i, mode,
1074 sequencer_midi_input, sequencer_midi_output)) >= 0)
1081 if (seq_mode == SEQ_2) {
1082 if (try_module_get(tmr->owner))
1083 tmr->open(tmr_no, seq_mode);
1086 init_waitqueue_head(&seq_sleeper);
1087 init_waitqueue_head(&midi_sleeper);
1088 output_threshold = SEQ_MAX_QUEUE / 2;
1093 static void seq_drain_midi_queues(void)
1098 * Give the Midi drivers time to drain their output queues
1103 while (!signal_pending(current) && n)
1107 for (i = 0; i < max_mididev; i++)
1108 if (midi_opened[i] && midi_written[i])
1109 if (midi_devs[i]->buffer_status != NULL)
1110 if (midi_devs[i]->buffer_status(i))
1114 * Let's have a delay
1118 interruptible_sleep_on_timeout(&seq_sleeper,
1123 void sequencer_release(int dev, struct file *file)
1126 int mode = translate_mode(file);
1130 DEB(printk("sequencer_release(dev=%d)\n", dev));
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 interruptible_sleep_on_timeout(&seq_sleeper,
1147 if (mode != OPEN_READ)
1148 seq_drain_midi_queues(); /*
1149 * Ensure the output queues are empty
1152 if (mode != OPEN_READ)
1153 seq_drain_midi_queues(); /*
1154 * Flush the all notes off messages
1157 for (i = 0; i < max_synthdev; i++)
1159 if (synth_open_mask & (1 << i)) /*
1164 synth_devs[i]->close(i);
1166 module_put(synth_devs[i]->owner);
1168 if (synth_devs[i]->midi_dev)
1169 midi_opened[synth_devs[i]->midi_dev] = 0;
1173 for (i = 0; i < max_mididev; i++)
1175 if (midi_opened[i]) {
1176 midi_devs[i]->close(i);
1177 module_put(midi_devs[i]->owner);
1181 if (seq_mode == SEQ_2) {
1183 module_put(tmr->owner);
1186 if (obsolete_api_used)
1187 printk(KERN_WARNING "/dev/music: Obsolete (4 byte) API was used by %s\n", current->comm);
1191 static int seq_sync(void)
1193 if (qlen && !seq_playing && !signal_pending(current))
1197 interruptible_sleep_on_timeout(&seq_sleeper, HZ);
1201 static void midi_outc(int dev, unsigned char data)
1204 * NOTE! Calls sleep(). Don't call this from interrupt.
1208 unsigned long flags;
1211 * This routine sends one byte to the Midi channel.
1212 * If the output FIFO is full, it waits until there
1213 * is space in the queue
1216 n = 3 * HZ; /* Timeout */
1218 spin_lock_irqsave(&lock,flags);
1219 while (n && !midi_devs[dev]->outputc(dev, data)) {
1220 interruptible_sleep_on_timeout(&seq_sleeper, HZ/25);
1223 spin_unlock_irqrestore(&lock,flags);
1226 static void seq_reset(void)
1229 * NOTE! Calls sleep(). Don't call this from interrupt.
1234 unsigned long flags;
1239 prev_input_time = 0;
1240 prev_event_time = 0;
1242 qlen = qhead = qtail = 0;
1243 iqlen = iqhead = iqtail = 0;
1245 for (i = 0; i < max_synthdev; i++)
1246 if (synth_open_mask & (1 << i))
1248 synth_devs[i]->reset(i);
1250 if (seq_mode == SEQ_2)
1252 for (chn = 0; chn < 16; chn++)
1253 for (i = 0; i < max_synthdev; i++)
1254 if (synth_open_mask & (1 << i))
1257 synth_devs[i]->controller(i, chn, 123, 0); /* All notes off */
1258 synth_devs[i]->controller(i, chn, 121, 0); /* Reset all ctl */
1259 synth_devs[i]->bender(i, chn, 1 << 13); /* Bender off */
1262 else /* seq_mode == SEQ_1 */
1264 for (i = 0; i < max_mididev; i++)
1265 if (midi_written[i]) /*
1266 * Midi used. Some notes may still be playing
1270 * Sending just a ACTIVE SENSING message should be enough to stop all
1271 * playing notes. Since there are devices not recognizing the
1272 * active sensing, we have to send some all notes off messages also.
1276 for (chn = 0; chn < 16; chn++)
1278 midi_outc(i, (unsigned char) (0xb0 + (chn & 0x0f))); /* control change */
1279 midi_outc(i, 0x7b); /* All notes off */
1280 midi_outc(i, 0); /* Dummy parameter */
1283 midi_devs[i]->close(i);
1285 midi_written[i] = 0;
1292 spin_lock_irqsave(&lock,flags);
1294 if (waitqueue_active(&seq_sleeper)) {
1295 /* printk( "Sequencer Warning: Unexpected sleeping process - Waking up\n"); */
1296 wake_up(&seq_sleeper);
1298 spin_unlock_irqrestore(&lock,flags);
1301 static void seq_panic(void)
1304 * This routine is called by the application in case the user
1305 * wants to reset the system to the default state.
1311 * Since some of the devices don't recognize the active sensing and
1312 * all notes off messages, we have to shut all notes manually.
1314 * TO BE IMPLEMENTED LATER
1318 * Also return the controllers to their default states
1322 int sequencer_ioctl(int dev, struct file *file, unsigned int cmd, void __user *arg)
1324 int midi_dev, orig_dev, val, err;
1325 int mode = translate_mode(file);
1326 struct synth_info inf;
1327 struct seq_event_rec event_rec;
1328 unsigned long flags;
1329 int __user *p = arg;
1331 orig_dev = dev = dev >> 4;
1335 case SNDCTL_TMR_TIMEBASE:
1336 case SNDCTL_TMR_TEMPO:
1337 case SNDCTL_TMR_START:
1338 case SNDCTL_TMR_STOP:
1339 case SNDCTL_TMR_CONTINUE:
1340 case SNDCTL_TMR_METRONOME:
1341 case SNDCTL_TMR_SOURCE:
1342 if (seq_mode != SEQ_2)
1344 return tmr->ioctl(tmr_no, cmd, arg);
1346 case SNDCTL_TMR_SELECT:
1347 if (seq_mode != SEQ_2)
1349 if (get_user(pending_timer, p))
1351 if (pending_timer < 0 || pending_timer >= num_sound_timers || sound_timer_devs[pending_timer] == NULL)
1356 val = pending_timer;
1359 case SNDCTL_SEQ_PANIC:
1363 case SNDCTL_SEQ_SYNC:
1364 if (mode == OPEN_READ)
1366 while (qlen > 0 && !signal_pending(current))
1368 return qlen ? -EINTR : 0;
1370 case SNDCTL_SEQ_RESET:
1374 case SNDCTL_SEQ_TESTMIDI:
1375 if (__get_user(midi_dev, p))
1377 if (midi_dev < 0 || midi_dev >= max_mididev || !midi_devs[midi_dev])
1380 if (!midi_opened[midi_dev] &&
1381 (err = midi_devs[midi_dev]->open(midi_dev, mode, sequencer_midi_input,
1382 sequencer_midi_output)) < 0)
1384 midi_opened[midi_dev] = 1;
1387 case SNDCTL_SEQ_GETINCOUNT:
1388 if (mode == OPEN_WRITE)
1393 case SNDCTL_SEQ_GETOUTCOUNT:
1394 if (mode == OPEN_READ)
1396 val = SEQ_MAX_QUEUE - qlen;
1399 case SNDCTL_SEQ_GETTIME:
1400 if (seq_mode == SEQ_2)
1401 return tmr->ioctl(tmr_no, cmd, arg);
1402 val = jiffies - seq_time;
1405 case SNDCTL_SEQ_CTRLRATE:
1407 * If *arg == 0, just return the current rate
1409 if (seq_mode == SEQ_2)
1410 return tmr->ioctl(tmr_no, cmd, arg);
1412 if (get_user(val, p))
1419 case SNDCTL_SEQ_RESETSAMPLES:
1420 case SNDCTL_SYNTH_REMOVESAMPLE:
1421 case SNDCTL_SYNTH_CONTROL:
1422 if (get_user(dev, p))
1424 if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL)
1426 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1428 return synth_devs[dev]->ioctl(dev, cmd, arg);
1430 case SNDCTL_SEQ_NRSYNTHS:
1434 case SNDCTL_SEQ_NRMIDIS:
1438 case SNDCTL_SYNTH_MEMAVL:
1439 if (get_user(dev, p))
1441 if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL)
1443 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1445 val = synth_devs[dev]->ioctl(dev, cmd, arg);
1448 case SNDCTL_FM_4OP_ENABLE:
1449 if (get_user(dev, p))
1451 if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL)
1453 if (!(synth_open_mask & (1 << dev)))
1455 synth_devs[dev]->ioctl(dev, cmd, arg);
1458 case SNDCTL_SYNTH_INFO:
1459 if (get_user(dev, &((struct synth_info __user *)arg)->device))
1461 if (dev < 0 || dev >= max_synthdev)
1463 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1465 return synth_devs[dev]->ioctl(dev, cmd, arg);
1467 /* Like SYNTH_INFO but returns ID in the name field */
1468 case SNDCTL_SYNTH_ID:
1469 if (get_user(dev, &((struct synth_info __user *)arg)->device))
1471 if (dev < 0 || dev >= max_synthdev)
1473 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1475 memcpy(&inf, synth_devs[dev]->info, sizeof(inf));
1476 strlcpy(inf.name, synth_devs[dev]->id, sizeof(inf.name));
1478 return copy_to_user(arg, &inf, sizeof(inf))?-EFAULT:0;
1480 case SNDCTL_SEQ_OUTOFBAND:
1481 if (copy_from_user(&event_rec, arg, sizeof(event_rec)))
1483 spin_lock_irqsave(&lock,flags);
1484 play_event(event_rec.arr);
1485 spin_unlock_irqrestore(&lock,flags);
1488 case SNDCTL_MIDI_INFO:
1489 if (get_user(dev, &((struct midi_info __user *)arg)->device))
1491 if (dev < 0 || dev >= max_mididev || !midi_devs[dev])
1493 midi_devs[dev]->info.device = dev;
1494 return copy_to_user(arg, &midi_devs[dev]->info, sizeof(struct midi_info))?-EFAULT:0;
1496 case SNDCTL_SEQ_THRESHOLD:
1497 if (get_user(val, p))
1501 if (val >= SEQ_MAX_QUEUE)
1502 val = SEQ_MAX_QUEUE - 1;
1503 output_threshold = val;
1506 case SNDCTL_MIDI_PRETIME:
1507 if (get_user(val, p))
1511 val = (HZ * val) / 10;
1512 pre_event_timeout = val;
1516 if (mode == OPEN_READ)
1520 if (!(synth_open_mask & (1 << 0)))
1522 if (!synth_devs[0]->ioctl)
1524 return synth_devs[0]->ioctl(0, cmd, arg);
1526 return put_user(val, p);
1529 /* No kernel lock - we're using the global irq lock here */
1530 unsigned int sequencer_poll(int dev, struct file *file, poll_table * wait)
1532 unsigned long flags;
1533 unsigned int mask = 0;
1537 spin_lock_irqsave(&lock,flags);
1539 poll_wait(file, &midi_sleeper, wait);
1541 mask |= POLLIN | POLLRDNORM;
1544 poll_wait(file, &seq_sleeper, wait);
1545 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
1546 mask |= POLLOUT | POLLWRNORM;
1547 spin_unlock_irqrestore(&lock,flags);
1552 void sequencer_timer(unsigned long dummy)
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);
1590 unsigned long compute_finetune(unsigned long base_freq, int bend, int range,
1593 unsigned long amount;
1594 int negative, semitones, cents, multiplier = 1;
1607 bend = bend * range / 8192; /* Convert to cents */
1608 bend += vibrato_cents;
1613 negative = bend < 0 ? 1 : 0;
1630 semitones = bend / 100;
1635 amount = (int) (semitone_tuning[semitones] * multiplier * cent_tuning[cents]) / 10000;
1638 return (base_freq * 10000) / amount; /* Bend down */
1640 return (base_freq * amount) / 10000; /* Bend up */
1644 void sequencer_init(void)
1646 /* drag in sequencer_syms.o */
1648 extern char sequencer_syms_symbol;
1649 sequencer_syms_symbol = 0;
1655 queue = (unsigned char *)vmalloc(SEQ_MAX_QUEUE * EV_SZ);
1658 printk(KERN_ERR "sequencer: Can't allocate memory for sequencer output queue\n");
1661 iqueue = (unsigned char *)vmalloc(SEQ_MAX_QUEUE * IEV_SZ);
1664 printk(KERN_ERR "sequencer: Can't allocate memory for sequencer input queue\n");
1671 void sequencer_unload(void)
1675 queue = iqueue = NULL;