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);
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;
216 int mode = translate_mode(file);
220 DEB(printk("sequencer_write(dev=%d, count=%d)\n", dev, count));
222 if (mode == OPEN_READ)
229 if (copy_from_user((char *) event_rec, &(buf)[p], 4))
231 ev_code = event_rec[0];
233 if (ev_code == SEQ_FULLSIZE)
237 dev = *(unsigned short *) &event_rec[2];
238 if (dev < 0 || dev >= max_synthdev || synth_devs[dev] == NULL)
241 if (!(synth_open_mask & (1 << dev)))
244 fmt = (*(short *) &event_rec[0]) & 0xffff;
245 err = synth_devs[dev]->load_patch(dev, fmt, buf, p + 4, c, 0);
253 if (seq_mode == SEQ_2 && ev_code == SEQ_EXTENDED)
255 printk(KERN_WARNING "Sequencer: Invalid level 2 event %x\n", ev_code);
266 if (copy_from_user((char *)&event_rec[4],
273 if (seq_mode == SEQ_2)
275 printk(KERN_WARNING "Sequencer: 4 byte event in level 2 mode\n");
280 if (event_rec[0] != SEQ_MIDIPUTC)
281 obsolete_api_used = 1;
284 if (event_rec[0] == SEQ_MIDIPUTC)
286 if (!midi_opened[event_rec[2]])
289 int dev = event_rec[2];
291 if (dev >= max_mididev || midi_devs[dev]==NULL)
293 /*printk("Sequencer Error: Nonexistent MIDI device %d\n", dev);*/
296 mode = translate_mode(file);
298 if ((err = midi_devs[dev]->open(dev, mode,
299 sequencer_midi_input, sequencer_midi_output)) < 0)
302 printk(KERN_WARNING "Sequencer Error: Unable to open Midi #%d\n", dev);
305 midi_opened[dev] = 1;
308 if (!seq_queue(event_rec, (file->f_flags & (O_NONBLOCK) ? 1 : 0)))
310 int processed = count - c;
315 if (!processed && (file->f_flags & O_NONBLOCK))
330 static int seq_queue(unsigned char *note, char nonblock)
334 * Test if there is space in the queue
337 if (qlen >= SEQ_MAX_QUEUE)
340 * Give chance to drain the queue
343 if (!nonblock && qlen >= SEQ_MAX_QUEUE && !waitqueue_active(&seq_sleeper)) {
345 * Sleep until there is enough space on the queue
347 interruptible_sleep_on(&seq_sleeper);
349 if (qlen >= SEQ_MAX_QUEUE)
355 memcpy(&queue[qtail * EV_SZ], note, EV_SZ);
357 qtail = (qtail + 1) % SEQ_MAX_QUEUE;
363 static int extended_event(unsigned char *q)
367 if (dev < 0 || dev >= max_synthdev)
370 if (!(synth_open_mask & (1 << dev)))
376 synth_devs[dev]->kill_note(dev, q[3], q[4], q[5]);
380 if (q[4] > 127 && q[4] != 255)
385 synth_devs[dev]->kill_note(dev, q[3], q[4], q[5]);
388 synth_devs[dev]->start_note(dev, q[3], q[4], q[5]);
392 synth_devs[dev]->set_instr(dev, q[3], q[4]);
396 synth_devs[dev]->aftertouch(dev, q[3], q[4]);
400 synth_devs[dev]->panning(dev, q[3], (char) q[4]);
404 synth_devs[dev]->controller(dev, q[3], q[4], (short) (q[5] | (q[6] << 8)));
408 if (synth_devs[dev]->volume_method != NULL)
409 synth_devs[dev]->volume_method(dev, q[3]);
418 static int find_voice(int dev, int chn, int note)
423 key = (chn << 8) | (note + 1);
424 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
425 if (synth_devs[dev]->alloc.map[i] == key)
430 static int alloc_voice(int dev, int chn, int note)
435 key = (chn << 8) | (note + 1);
437 voice = synth_devs[dev]->alloc_voice(dev, chn, note,
438 &synth_devs[dev]->alloc);
439 synth_devs[dev]->alloc.map[voice] = key;
440 synth_devs[dev]->alloc.alloc_times[voice] =
441 synth_devs[dev]->alloc.timestamp++;
445 static void seq_chn_voice_event(unsigned char *event_rec)
447 #define dev event_rec[1]
448 #define cmd event_rec[2]
449 #define chn event_rec[3]
450 #define note event_rec[4]
451 #define parm event_rec[5]
455 if ((int) dev > max_synthdev || synth_devs[dev] == NULL)
457 if (!(synth_open_mask & (1 << dev)))
459 if (!synth_devs[dev])
462 if (seq_mode == SEQ_2)
464 if (synth_devs[dev]->alloc_voice)
465 voice = find_voice(dev, chn, note);
467 if (cmd == MIDI_NOTEON && parm == 0)
477 if (note > 127 && note != 255) /* Not a seq2 feature */
480 if (voice == -1 && seq_mode == SEQ_2 && synth_devs[dev]->alloc_voice)
482 /* Internal synthesizer (FM, GUS, etc) */
483 voice = alloc_voice(dev, chn, note);
488 if (seq_mode == SEQ_2 && (int) dev < num_synths)
491 * The MIDI channel 10 is a percussive channel. Use the note
492 * number to select the proper patch (128 to 255) to play.
497 synth_devs[dev]->set_instr(dev, voice, 128 + note);
498 synth_devs[dev]->chn_info[chn].pgm_num = 128 + note;
500 synth_devs[dev]->setup_voice(dev, voice, chn);
502 synth_devs[dev]->start_note(dev, voice, note, parm);
508 synth_devs[dev]->kill_note(dev, voice, note, parm);
511 case MIDI_KEY_PRESSURE:
514 synth_devs[dev]->aftertouch(dev, voice, parm);
527 static void seq_chn_common_event(unsigned char *event_rec)
529 unsigned char dev = event_rec[1];
530 unsigned char cmd = event_rec[2];
531 unsigned char chn = event_rec[3];
532 unsigned char p1 = event_rec[4];
534 /* unsigned char p2 = event_rec[5]; */
535 unsigned short w14 = *(short *) &event_rec[6];
537 if ((int) dev > max_synthdev || synth_devs[dev] == NULL)
539 if (!(synth_open_mask & (1 << dev)))
541 if (!synth_devs[dev])
546 case MIDI_PGM_CHANGE:
547 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)
600 synth_devs[dev]->chn_info[chn].bender_value = w14;
602 if ((int) dev < num_synths)
604 /* Handle all playing notes on this channel */
609 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
610 if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
611 synth_devs[dev]->bender(dev, i, w14);
614 synth_devs[dev]->bender(dev, chn, w14);
617 synth_devs[dev]->bender(dev, chn, w14);
624 static int seq_timing_event(unsigned char *event_rec)
626 unsigned char cmd = event_rec[1];
627 unsigned int parm = *(int *) &event_rec[4];
629 if (seq_mode == SEQ_2)
633 if ((ret = tmr->event(tmr_no, event_rec)) == TIMER_ARMED)
634 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
635 wake_up(&seq_sleeper);
641 parm += prev_event_time;
644 * NOTE! No break here. Execution of TMR_WAIT_REL continues in the
645 * next case (TMR_WAIT_ABS)
654 prev_event_time = time;
657 request_sound_timer(time);
659 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
660 wake_up(&seq_sleeper);
681 if (seq_mode == SEQ_2)
682 seq_copy_to_input(event_rec, 8);
685 parm = (parm << 8 | SEQ_ECHO);
686 seq_copy_to_input((unsigned char *) &parm, 4);
693 return TIMER_NOT_ARMED;
696 static void seq_local_event(unsigned char *event_rec)
698 unsigned char cmd = event_rec[1];
699 unsigned int parm = *((unsigned int *) &event_rec[4]);
703 case LOCL_STARTAUDIO:
704 DMAbuf_start_devices(parm);
711 static void seq_sysex_message(unsigned char *event_rec)
713 unsigned int dev = event_rec[1];
715 unsigned char *buf = &event_rec[2];
717 if (dev > max_synthdev)
719 if (!(synth_open_mask & (1 << dev)))
721 if (!synth_devs[dev])
725 for (i = 0; i < 6 && buf[i] != 0xff; i++)
728 if (!synth_devs[dev]->send_sysex)
731 synth_devs[dev]->send_sysex(dev, buf, l);
734 static int play_event(unsigned char *q)
737 * NOTE! This routine returns
738 * 0 = normal event played.
739 * 1 = Timer armed. Suspend playback until timer callback.
740 * 2 = MIDI output buffer full. Restore queue and suspend until timer
747 if (synth_open_mask & (1 << 0))
749 synth_devs[0]->kill_note(0, q[1], 255, q[3]);
753 if (q[4] < 128 || q[4] == 255)
754 if (synth_open_mask & (1 << 0))
756 synth_devs[0]->start_note(0, q[1], q[2], q[3]);
760 delay = (unsigned int *) q; /*
761 * Bytes 1 to 3 are containing the *
764 *delay = (*delay >> 8) & 0xffffff;
772 prev_event_time = time;
774 request_sound_timer(time);
776 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
777 wake_up(&seq_sleeper);
779 * The timer is now active and will reinvoke this function
780 * after the timer expires. Return to the caller now.
787 if (synth_open_mask & (1 << 0))
789 synth_devs[0]->set_instr(0, q[1], q[2]);
792 case SEQ_SYNCTIMER: /*
800 case SEQ_MIDIPUTC: /*
801 * Put a midi character
803 if (midi_opened[q[2]])
809 if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
812 if (!midi_devs[dev]->outputc(dev, q[1]))
815 * Output FIFO is full. Wait one timer cycle and try again.
819 request_sound_timer(-1);
823 midi_written[dev] = 1;
828 seq_copy_to_input(q, 4); /*
829 * Echo back to the process
834 if ((int) q[1] < max_synthdev)
835 synth_devs[q[1]]->hw_control(q[1], q);
843 seq_chn_voice_event(q);
847 seq_chn_common_event(q);
851 if (seq_timing_event(q) == TIMER_ARMED)
862 seq_sysex_message(q);
870 /* called also as timer in irq context */
871 static void seq_startplay(void)
873 int this_one, action;
879 spin_lock_irqsave(&lock,flags);
880 qhead = ((this_one = qhead) + 1) % SEQ_MAX_QUEUE;
882 spin_unlock_irqrestore(&lock,flags);
886 if ((action = play_event(&queue[this_one * EV_SZ])))
887 { /* Suspend playback. Next timer routine invokes this routine again */
899 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
900 wake_up(&seq_sleeper);
903 static void reset_controllers(int dev, unsigned char *controller, int update_dev)
906 for (i = 0; i < 128; i++)
907 controller[i] = ctrl_def_values[i];
910 static void setup_mode2(void)
914 max_synthdev = num_synths;
916 for (dev = 0; dev < num_midis; dev++)
918 if (midi_devs[dev] && midi_devs[dev]->converter != NULL)
920 synth_devs[max_synthdev++] = midi_devs[dev]->converter;
924 for (dev = 0; dev < max_synthdev; dev++)
928 synth_devs[dev]->sysex_ptr = 0;
929 synth_devs[dev]->emulation = 0;
931 for (chn = 0; chn < 16; chn++)
933 synth_devs[dev]->chn_info[chn].pgm_num = 0;
934 reset_controllers(dev,
935 synth_devs[dev]->chn_info[chn].controllers,0);
936 synth_devs[dev]->chn_info[chn].bender_value = (1 << 7); /* Neutral */
937 synth_devs[dev]->chn_info[chn].bender_range = 200;
944 int sequencer_open(int dev, struct file *file)
952 level = ((dev & 0x0f) == SND_DEV_SEQ2) ? 2 : 1;
955 mode = translate_mode(file);
957 DEB(printk("sequencer_open(dev=%d)\n", dev));
961 /* printk("Sound card: sequencer not initialized\n");*/
964 if (dev) /* Patch manager device (obsolete) */
967 if(synth_devs[dev] == NULL)
968 request_module("synth0");
970 if (mode == OPEN_READ)
974 /*printk("Sequencer: No MIDI devices. Input not possible\n");*/
984 obsolete_api_used = 0;
986 max_mididev = num_midis;
987 max_synthdev = num_synths;
988 pre_event_timeout = MAX_SCHEDULE_TIMEOUT;
991 if (pending_timer != -1)
993 tmr_no = pending_timer;
996 if (tmr_no == -1) /* Not selected yet */
1001 for (i = 0; i < num_sound_timers; i++)
1002 if (sound_timer_devs[i] && sound_timer_devs[i]->priority > best)
1005 best = sound_timer_devs[i]->priority;
1007 if (tmr_no == -1) /* Should not be */
1010 tmr = sound_timer_devs[tmr_no];
1016 /*printk("sequencer: No timer for level 2\n");*/
1022 if (!max_synthdev && !max_mididev)
1028 synth_open_mask = 0;
1030 for (i = 0; i < max_mididev; i++)
1033 midi_written[i] = 0;
1036 for (i = 0; i < max_synthdev; i++)
1038 if (synth_devs[i]==NULL)
1041 if (!try_module_get(synth_devs[i]->owner))
1044 if ((tmp = synth_devs[i]->open(i, mode)) < 0)
1046 printk(KERN_WARNING "Sequencer: Warning! Cannot open synth device #%d (%d)\n", i, tmp);
1047 if (synth_devs[i]->midi_dev)
1048 printk(KERN_WARNING "(Maps to MIDI dev #%d)\n", synth_devs[i]->midi_dev);
1052 synth_open_mask |= (1 << i);
1053 if (synth_devs[i]->midi_dev)
1054 midi_opened[synth_devs[i]->midi_dev] = 1;
1060 prev_input_time = 0;
1061 prev_event_time = 0;
1063 if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
1066 * Initialize midi input devices
1069 for (i = 0; i < max_mididev; i++)
1070 if (!midi_opened[i] && midi_devs[i])
1072 if (!try_module_get(midi_devs[i]->owner))
1075 if ((retval = midi_devs[i]->open(i, mode,
1076 sequencer_midi_input, sequencer_midi_output)) >= 0)
1083 if (seq_mode == SEQ_2) {
1084 if (try_module_get(tmr->owner))
1085 tmr->open(tmr_no, seq_mode);
1088 init_waitqueue_head(&seq_sleeper);
1089 init_waitqueue_head(&midi_sleeper);
1090 output_threshold = SEQ_MAX_QUEUE / 2;
1095 static void seq_drain_midi_queues(void)
1100 * Give the Midi drivers time to drain their output queues
1105 while (!signal_pending(current) && n)
1109 for (i = 0; i < max_mididev; i++)
1110 if (midi_opened[i] && midi_written[i])
1111 if (midi_devs[i]->buffer_status != NULL)
1112 if (midi_devs[i]->buffer_status(i))
1116 * Let's have a delay
1120 interruptible_sleep_on_timeout(&seq_sleeper,
1125 void sequencer_release(int dev, struct file *file)
1128 int mode = translate_mode(file);
1132 DEB(printk("sequencer_release(dev=%d)\n", dev));
1135 * Wait until the queue is empty (if we don't have nonblock)
1138 if (mode != OPEN_READ && !(file->f_flags & O_NONBLOCK))
1140 while (!signal_pending(current) && qlen > 0)
1143 interruptible_sleep_on_timeout(&seq_sleeper,
1149 if (mode != OPEN_READ)
1150 seq_drain_midi_queues(); /*
1151 * Ensure the output queues are empty
1154 if (mode != OPEN_READ)
1155 seq_drain_midi_queues(); /*
1156 * Flush the all notes off messages
1159 for (i = 0; i < max_synthdev; i++)
1161 if (synth_open_mask & (1 << i)) /*
1166 synth_devs[i]->close(i);
1168 module_put(synth_devs[i]->owner);
1170 if (synth_devs[i]->midi_dev)
1171 midi_opened[synth_devs[i]->midi_dev] = 0;
1175 for (i = 0; i < max_mididev; i++)
1177 if (midi_opened[i]) {
1178 midi_devs[i]->close(i);
1179 module_put(midi_devs[i]->owner);
1183 if (seq_mode == SEQ_2) {
1185 module_put(tmr->owner);
1188 if (obsolete_api_used)
1189 printk(KERN_WARNING "/dev/music: Obsolete (4 byte) API was used by %s\n", current->comm);
1193 static int seq_sync(void)
1195 if (qlen && !seq_playing && !signal_pending(current))
1199 interruptible_sleep_on_timeout(&seq_sleeper, HZ);
1203 static void midi_outc(int dev, unsigned char data)
1206 * NOTE! Calls sleep(). Don't call this from interrupt.
1210 unsigned long flags;
1213 * This routine sends one byte to the Midi channel.
1214 * If the output FIFO is full, it waits until there
1215 * is space in the queue
1218 n = 3 * HZ; /* Timeout */
1220 spin_lock_irqsave(&lock,flags);
1221 while (n && !midi_devs[dev]->outputc(dev, data)) {
1222 interruptible_sleep_on_timeout(&seq_sleeper, HZ/25);
1225 spin_unlock_irqrestore(&lock,flags);
1228 static void seq_reset(void)
1231 * NOTE! Calls sleep(). Don't call this from interrupt.
1236 unsigned long flags;
1241 prev_input_time = 0;
1242 prev_event_time = 0;
1244 qlen = qhead = qtail = 0;
1245 iqlen = iqhead = iqtail = 0;
1247 for (i = 0; i < max_synthdev; i++)
1248 if (synth_open_mask & (1 << i))
1250 synth_devs[i]->reset(i);
1252 if (seq_mode == SEQ_2)
1254 for (chn = 0; chn < 16; chn++)
1255 for (i = 0; i < max_synthdev; i++)
1256 if (synth_open_mask & (1 << i))
1259 synth_devs[i]->controller(i, chn, 123, 0); /* All notes off */
1260 synth_devs[i]->controller(i, chn, 121, 0); /* Reset all ctl */
1261 synth_devs[i]->bender(i, chn, 1 << 13); /* Bender off */
1264 else /* seq_mode == SEQ_1 */
1266 for (i = 0; i < max_mididev; i++)
1267 if (midi_written[i]) /*
1268 * Midi used. Some notes may still be playing
1272 * Sending just a ACTIVE SENSING message should be enough to stop all
1273 * playing notes. Since there are devices not recognizing the
1274 * active sensing, we have to send some all notes off messages also.
1278 for (chn = 0; chn < 16; chn++)
1280 midi_outc(i, (unsigned char) (0xb0 + (chn & 0x0f))); /* control change */
1281 midi_outc(i, 0x7b); /* All notes off */
1282 midi_outc(i, 0); /* Dummy parameter */
1285 midi_devs[i]->close(i);
1287 midi_written[i] = 0;
1294 spin_lock_irqsave(&lock,flags);
1296 if (waitqueue_active(&seq_sleeper)) {
1297 /* printk( "Sequencer Warning: Unexpected sleeping process - Waking up\n"); */
1298 wake_up(&seq_sleeper);
1300 spin_unlock_irqrestore(&lock,flags);
1303 static void seq_panic(void)
1306 * This routine is called by the application in case the user
1307 * wants to reset the system to the default state.
1313 * Since some of the devices don't recognize the active sensing and
1314 * all notes off messages, we have to shut all notes manually.
1316 * TO BE IMPLEMENTED LATER
1320 * Also return the controllers to their default states
1324 int sequencer_ioctl(int dev, struct file *file, unsigned int cmd, void __user *arg)
1326 int midi_dev, orig_dev, val, err;
1327 int mode = translate_mode(file);
1328 struct synth_info inf;
1329 struct seq_event_rec event_rec;
1330 unsigned long flags;
1331 int __user *p = arg;
1333 orig_dev = dev = dev >> 4;
1337 case SNDCTL_TMR_TIMEBASE:
1338 case SNDCTL_TMR_TEMPO:
1339 case SNDCTL_TMR_START:
1340 case SNDCTL_TMR_STOP:
1341 case SNDCTL_TMR_CONTINUE:
1342 case SNDCTL_TMR_METRONOME:
1343 case SNDCTL_TMR_SOURCE:
1344 if (seq_mode != SEQ_2)
1346 return tmr->ioctl(tmr_no, cmd, arg);
1348 case SNDCTL_TMR_SELECT:
1349 if (seq_mode != SEQ_2)
1351 if (get_user(pending_timer, p))
1353 if (pending_timer < 0 || pending_timer >= num_sound_timers || sound_timer_devs[pending_timer] == NULL)
1358 val = pending_timer;
1361 case SNDCTL_SEQ_PANIC:
1365 case SNDCTL_SEQ_SYNC:
1366 if (mode == OPEN_READ)
1368 while (qlen > 0 && !signal_pending(current))
1370 return qlen ? -EINTR : 0;
1372 case SNDCTL_SEQ_RESET:
1376 case SNDCTL_SEQ_TESTMIDI:
1377 if (__get_user(midi_dev, p))
1379 if (midi_dev < 0 || midi_dev >= max_mididev || !midi_devs[midi_dev])
1382 if (!midi_opened[midi_dev] &&
1383 (err = midi_devs[midi_dev]->open(midi_dev, mode, sequencer_midi_input,
1384 sequencer_midi_output)) < 0)
1386 midi_opened[midi_dev] = 1;
1389 case SNDCTL_SEQ_GETINCOUNT:
1390 if (mode == OPEN_WRITE)
1395 case SNDCTL_SEQ_GETOUTCOUNT:
1396 if (mode == OPEN_READ)
1398 val = SEQ_MAX_QUEUE - qlen;
1401 case SNDCTL_SEQ_GETTIME:
1402 if (seq_mode == SEQ_2)
1403 return tmr->ioctl(tmr_no, cmd, arg);
1404 val = jiffies - seq_time;
1407 case SNDCTL_SEQ_CTRLRATE:
1409 * If *arg == 0, just return the current rate
1411 if (seq_mode == SEQ_2)
1412 return tmr->ioctl(tmr_no, cmd, arg);
1414 if (get_user(val, p))
1421 case SNDCTL_SEQ_RESETSAMPLES:
1422 case SNDCTL_SYNTH_REMOVESAMPLE:
1423 case SNDCTL_SYNTH_CONTROL:
1424 if (get_user(dev, p))
1426 if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL)
1428 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1430 return synth_devs[dev]->ioctl(dev, cmd, arg);
1432 case SNDCTL_SEQ_NRSYNTHS:
1436 case SNDCTL_SEQ_NRMIDIS:
1440 case SNDCTL_SYNTH_MEMAVL:
1441 if (get_user(dev, p))
1443 if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL)
1445 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1447 val = synth_devs[dev]->ioctl(dev, cmd, arg);
1450 case SNDCTL_FM_4OP_ENABLE:
1451 if (get_user(dev, p))
1453 if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL)
1455 if (!(synth_open_mask & (1 << dev)))
1457 synth_devs[dev]->ioctl(dev, cmd, arg);
1460 case SNDCTL_SYNTH_INFO:
1461 if (get_user(dev, &((struct synth_info __user *)arg)->device))
1463 if (dev < 0 || dev >= max_synthdev)
1465 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1467 return synth_devs[dev]->ioctl(dev, cmd, arg);
1469 /* Like SYNTH_INFO but returns ID in the name field */
1470 case SNDCTL_SYNTH_ID:
1471 if (get_user(dev, &((struct synth_info __user *)arg)->device))
1473 if (dev < 0 || dev >= max_synthdev)
1475 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1477 memcpy(&inf, synth_devs[dev]->info, sizeof(inf));
1478 strlcpy(inf.name, synth_devs[dev]->id, sizeof(inf.name));
1480 return copy_to_user(arg, &inf, sizeof(inf))?-EFAULT:0;
1482 case SNDCTL_SEQ_OUTOFBAND:
1483 if (copy_from_user(&event_rec, arg, sizeof(event_rec)))
1485 spin_lock_irqsave(&lock,flags);
1486 play_event(event_rec.arr);
1487 spin_unlock_irqrestore(&lock,flags);
1490 case SNDCTL_MIDI_INFO:
1491 if (get_user(dev, &((struct midi_info __user *)arg)->device))
1493 if (dev < 0 || dev >= max_mididev || !midi_devs[dev])
1495 midi_devs[dev]->info.device = dev;
1496 return copy_to_user(arg, &midi_devs[dev]->info, sizeof(struct midi_info))?-EFAULT:0;
1498 case SNDCTL_SEQ_THRESHOLD:
1499 if (get_user(val, p))
1503 if (val >= SEQ_MAX_QUEUE)
1504 val = SEQ_MAX_QUEUE - 1;
1505 output_threshold = val;
1508 case SNDCTL_MIDI_PRETIME:
1509 if (get_user(val, p))
1513 val = (HZ * val) / 10;
1514 pre_event_timeout = val;
1518 if (mode == OPEN_READ)
1522 if (!(synth_open_mask & (1 << 0)))
1524 if (!synth_devs[0]->ioctl)
1526 return synth_devs[0]->ioctl(0, cmd, arg);
1528 return put_user(val, p);
1531 /* No kernel lock - we're using the global irq lock here */
1532 unsigned int sequencer_poll(int dev, struct file *file, poll_table * wait)
1534 unsigned long flags;
1535 unsigned int mask = 0;
1539 spin_lock_irqsave(&lock,flags);
1541 poll_wait(file, &midi_sleeper, wait);
1543 mask |= POLLIN | POLLRDNORM;
1546 poll_wait(file, &seq_sleeper, wait);
1547 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
1548 mask |= POLLOUT | POLLWRNORM;
1549 spin_unlock_irqrestore(&lock,flags);
1554 void sequencer_timer(unsigned long dummy)
1558 EXPORT_SYMBOL(sequencer_timer);
1560 int note_to_freq(int note_num)
1564 * This routine converts a midi note to a frequency (multiplied by 1000)
1567 int note, octave, note_freq;
1568 static int notes[] =
1570 261632, 277189, 293671, 311132, 329632, 349232,
1571 369998, 391998, 415306, 440000, 466162, 493880
1574 #define BASE_OCTAVE 5
1576 octave = note_num / 12;
1577 note = note_num % 12;
1579 note_freq = notes[note];
1581 if (octave < BASE_OCTAVE)
1582 note_freq >>= (BASE_OCTAVE - octave);
1583 else if (octave > BASE_OCTAVE)
1584 note_freq <<= (octave - BASE_OCTAVE);
1592 EXPORT_SYMBOL(note_to_freq);
1594 unsigned long compute_finetune(unsigned long base_freq, int bend, int range,
1597 unsigned long amount;
1598 int negative, semitones, cents, multiplier = 1;
1611 bend = bend * range / 8192; /* Convert to cents */
1612 bend += vibrato_cents;
1617 negative = bend < 0 ? 1 : 0;
1634 semitones = bend / 100;
1639 amount = (int) (semitone_tuning[semitones] * multiplier * cent_tuning[cents]) / 10000;
1642 return (base_freq * 10000) / amount; /* Bend down */
1644 return (base_freq * amount) / 10000; /* Bend up */
1646 EXPORT_SYMBOL(compute_finetune);
1648 void sequencer_init(void)
1652 queue = (unsigned char *)vmalloc(SEQ_MAX_QUEUE * EV_SZ);
1655 printk(KERN_ERR "sequencer: Can't allocate memory for sequencer output queue\n");
1658 iqueue = (unsigned char *)vmalloc(SEQ_MAX_QUEUE * IEV_SZ);
1661 printk(KERN_ERR "sequencer: Can't allocate memory for sequencer input queue\n");
1667 EXPORT_SYMBOL(sequencer_init);
1669 void sequencer_unload(void)
1673 queue = iqueue = NULL;