Merge branch 'for-3.13/drivers' of git://git.kernel.dk/linux-block
[linux-2.6.git] / sound / oss / sequencer.c
blob4ff60a6427d9d8d3ea7cf095bc2766f2d49955fc
1 /*
2 * sound/oss/sequencer.c
4 * The sequencer personality manager.
5 */
6 /*
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
11 * for more info.
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)
45 #define SEQ_1 1
46 #define SEQ_2 2
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;
59 #include "tuning.h"
61 #define EV_SZ 8
62 #define IEV_SZ 8
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.
82 #endif
84 int sequencer_read(int dev, struct file *file, char __user *buf, int count)
86 int c = count, p = 0;
87 int ev_len;
88 unsigned long flags;
90 dev = dev >> 4;
92 ev_len = seq_mode == SEQ_1 ? 4 : 8;
94 spin_lock_irqsave(&lock,flags);
96 if (!iqlen)
98 spin_unlock_irqrestore(&lock,flags);
99 if (file->f_flags & O_NONBLOCK) {
100 return -EAGAIN;
103 interruptible_sleep_on_timeout(&midi_sleeper,
104 pre_event_timeout);
105 spin_lock_irqsave(&lock,flags);
106 if (!iqlen)
108 spin_unlock_irqrestore(&lock,flags);
109 return 0;
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))
117 return count - c;
118 p += ev_len;
119 c -= ev_len;
121 spin_lock_irqsave(&lock,flags);
122 iqhead = (iqhead + 1) % SEQ_MAX_QUEUE;
123 iqlen--;
125 spin_unlock_irqrestore(&lock,flags);
126 return count - c;
129 static void sequencer_midi_output(int dev)
132 * Currently NOP
136 void seq_copy_to_input(unsigned char *event_rec, int len)
138 unsigned long flags;
141 * Verify that the len is valid for the current mode.
144 if (len != 4 && len != 8)
145 return;
146 if ((seq_mode == SEQ_1) != (len == 4))
147 return;
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);
154 iqlen++;
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)
163 unsigned int tstamp;
164 unsigned char event_rec[4];
166 if (data == 0xfe) /* Ignore active sensing */
167 return;
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;
178 event_rec[1] = data;
179 event_rec[2] = dev;
180 event_rec[3] = 0;
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);
191 else
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;
200 tmp_event[2] = 0;
201 tmp_event[3] = 0;
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);
217 dev = dev >> 4;
219 DEB(printk("sequencer_write(dev=%d, count=%d)\n", dev, count));
221 if (mode == OPEN_READ)
222 return -EIO;
224 c = count;
226 while (c >= 4)
228 if (copy_from_user((char *) event_rec, &(buf)[p], 4))
229 goto out;
230 ev_code = event_rec[0];
232 if (ev_code == SEQ_FULLSIZE)
234 int err, fmt;
236 dev = *(unsigned short *) &event_rec[2];
237 if (dev < 0 || dev >= max_synthdev || synth_devs[dev] == NULL)
238 return -ENXIO;
240 if (!(synth_open_mask & (1 << dev)))
241 return -ENXIO;
243 fmt = (*(short *) &event_rec[0]) & 0xffff;
244 err = synth_devs[dev]->load_patch(dev, fmt, buf + p, c, 0);
245 if (err < 0)
246 return err;
248 return err;
250 if (ev_code >= 128)
252 if (seq_mode == SEQ_2 && ev_code == SEQ_EXTENDED)
254 printk(KERN_WARNING "Sequencer: Invalid level 2 event %x\n", ev_code);
255 return -EINVAL;
257 ev_size = 8;
259 if (c < ev_size)
261 if (!seq_playing)
262 seq_startplay();
263 return count - c;
265 if (copy_from_user((char *)&event_rec[4],
266 &(buf)[p + 4], 4))
267 goto out;
270 else
272 if (seq_mode == SEQ_2)
274 printk(KERN_WARNING "Sequencer: 4 byte event in level 2 mode\n");
275 return -EINVAL;
277 ev_size = 4;
279 if (event_rec[0] != SEQ_MIDIPUTC)
280 obsolete_api_used = 1;
283 if (event_rec[0] == SEQ_MIDIPUTC)
285 if (!midi_opened[event_rec[2]])
287 int err, mode;
288 int dev = event_rec[2];
290 if (dev >= max_mididev || midi_devs[dev]==NULL)
292 /*printk("Sequencer Error: Nonexistent MIDI device %d\n", dev);*/
293 return -ENXIO;
295 mode = translate_mode(file);
297 if ((err = midi_devs[dev]->open(dev, mode,
298 sequencer_midi_input, sequencer_midi_output)) < 0)
300 seq_reset();
301 printk(KERN_WARNING "Sequencer Error: Unable to open Midi #%d\n", dev);
302 return err;
304 midi_opened[dev] = 1;
307 if (!seq_queue(event_rec, (file->f_flags & (O_NONBLOCK) ? 1 : 0)))
309 int processed = count - c;
311 if (!seq_playing)
312 seq_startplay();
314 if (!processed && (file->f_flags & O_NONBLOCK))
315 return -EAGAIN;
316 else
317 return processed;
319 p += ev_size;
320 c -= ev_size;
323 if (!seq_playing)
324 seq_startplay();
325 out:
326 return count;
329 static int seq_queue(unsigned char *note, char nonblock)
333 * Test if there is space in the queue
336 if (qlen >= SEQ_MAX_QUEUE)
337 if (!seq_playing)
338 seq_startplay(); /*
339 * Give chance to drain the queue
342 if (!nonblock && qlen >= SEQ_MAX_QUEUE && !waitqueue_active(&seq_sleeper)) {
344 * Sleep until there is enough space on the queue
346 interruptible_sleep_on(&seq_sleeper);
348 if (qlen >= SEQ_MAX_QUEUE)
350 return 0; /*
351 * To be sure
354 memcpy(&queue[qtail * EV_SZ], note, EV_SZ);
356 qtail = (qtail + 1) % SEQ_MAX_QUEUE;
357 qlen++;
359 return 1;
362 static int extended_event(unsigned char *q)
364 int dev = q[2];
366 if (dev < 0 || dev >= max_synthdev)
367 return -ENXIO;
369 if (!(synth_open_mask & (1 << dev)))
370 return -ENXIO;
372 switch (q[1])
374 case SEQ_NOTEOFF:
375 synth_devs[dev]->kill_note(dev, q[3], q[4], q[5]);
376 break;
378 case SEQ_NOTEON:
379 if (q[4] > 127 && q[4] != 255)
380 return 0;
382 if (q[5] == 0)
384 synth_devs[dev]->kill_note(dev, q[3], q[4], q[5]);
385 break;
387 synth_devs[dev]->start_note(dev, q[3], q[4], q[5]);
388 break;
390 case SEQ_PGMCHANGE:
391 synth_devs[dev]->set_instr(dev, q[3], q[4]);
392 break;
394 case SEQ_AFTERTOUCH:
395 synth_devs[dev]->aftertouch(dev, q[3], q[4]);
396 break;
398 case SEQ_BALANCE:
399 synth_devs[dev]->panning(dev, q[3], (char) q[4]);
400 break;
402 case SEQ_CONTROLLER:
403 synth_devs[dev]->controller(dev, q[3], q[4], (short) (q[5] | (q[6] << 8)));
404 break;
406 case SEQ_VOLMODE:
407 if (synth_devs[dev]->volume_method != NULL)
408 synth_devs[dev]->volume_method(dev, q[3]);
409 break;
411 default:
412 return -EINVAL;
414 return 0;
417 static int find_voice(int dev, int chn, int note)
419 unsigned short key;
420 int i;
422 key = (chn << 8) | (note + 1);
423 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
424 if (synth_devs[dev]->alloc.map[i] == key)
425 return i;
426 return -1;
429 static int alloc_voice(int dev, int chn, int note)
431 unsigned short key;
432 int voice;
434 key = (chn << 8) | (note + 1);
436 voice = synth_devs[dev]->alloc_voice(dev, chn, note,
437 &synth_devs[dev]->alloc);
438 synth_devs[dev]->alloc.map[voice] = key;
439 synth_devs[dev]->alloc.alloc_times[voice] =
440 synth_devs[dev]->alloc.timestamp++;
441 return voice;
444 static void seq_chn_voice_event(unsigned char *event_rec)
446 #define dev event_rec[1]
447 #define cmd event_rec[2]
448 #define chn event_rec[3]
449 #define note event_rec[4]
450 #define parm event_rec[5]
452 int voice = -1;
454 if ((int) dev > max_synthdev || synth_devs[dev] == NULL)
455 return;
456 if (!(synth_open_mask & (1 << dev)))
457 return;
458 if (!synth_devs[dev])
459 return;
461 if (seq_mode == SEQ_2)
463 if (synth_devs[dev]->alloc_voice)
464 voice = find_voice(dev, chn, note);
466 if (cmd == MIDI_NOTEON && parm == 0)
468 cmd = MIDI_NOTEOFF;
469 parm = 64;
473 switch (cmd)
475 case MIDI_NOTEON:
476 if (note > 127 && note != 255) /* Not a seq2 feature */
477 return;
479 if (voice == -1 && seq_mode == SEQ_2 && synth_devs[dev]->alloc_voice)
481 /* Internal synthesizer (FM, GUS, etc) */
482 voice = alloc_voice(dev, chn, note);
484 if (voice == -1)
485 voice = chn;
487 if (seq_mode == SEQ_2 && (int) dev < num_synths)
490 * The MIDI channel 10 is a percussive channel. Use the note
491 * number to select the proper patch (128 to 255) to play.
494 if (chn == 9)
496 synth_devs[dev]->set_instr(dev, voice, 128 + note);
497 synth_devs[dev]->chn_info[chn].pgm_num = 128 + note;
499 synth_devs[dev]->setup_voice(dev, voice, chn);
501 synth_devs[dev]->start_note(dev, voice, note, parm);
502 break;
504 case MIDI_NOTEOFF:
505 if (voice == -1)
506 voice = chn;
507 synth_devs[dev]->kill_note(dev, voice, note, parm);
508 break;
510 case MIDI_KEY_PRESSURE:
511 if (voice == -1)
512 voice = chn;
513 synth_devs[dev]->aftertouch(dev, voice, parm);
514 break;
516 default:;
518 #undef dev
519 #undef cmd
520 #undef chn
521 #undef note
522 #undef parm
526 static void seq_chn_common_event(unsigned char *event_rec)
528 unsigned char dev = event_rec[1];
529 unsigned char cmd = event_rec[2];
530 unsigned char chn = event_rec[3];
531 unsigned char p1 = event_rec[4];
533 /* unsigned char p2 = event_rec[5]; */
534 unsigned short w14 = *(short *) &event_rec[6];
536 if ((int) dev > max_synthdev || synth_devs[dev] == NULL)
537 return;
538 if (!(synth_open_mask & (1 << dev)))
539 return;
540 if (!synth_devs[dev])
541 return;
543 switch (cmd)
545 case MIDI_PGM_CHANGE:
546 if (seq_mode == SEQ_2)
548 if (chn > 15)
549 break;
551 synth_devs[dev]->chn_info[chn].pgm_num = p1;
552 if ((int) dev >= num_synths)
553 synth_devs[dev]->set_instr(dev, chn, p1);
555 else
556 synth_devs[dev]->set_instr(dev, chn, p1);
558 break;
560 case MIDI_CTL_CHANGE:
561 if (seq_mode == SEQ_2)
563 if (chn > 15 || p1 > 127)
564 break;
566 synth_devs[dev]->chn_info[chn].controllers[p1] = w14 & 0x7f;
568 if (p1 < 32) /* Setting MSB should clear LSB to 0 */
569 synth_devs[dev]->chn_info[chn].controllers[p1 + 32] = 0;
571 if ((int) dev < num_synths)
573 int val = w14 & 0x7f;
574 int i, key;
576 if (p1 < 64) /* Combine MSB and LSB */
578 val = ((synth_devs[dev]->
579 chn_info[chn].controllers[p1 & ~32] & 0x7f) << 7)
580 | (synth_devs[dev]->
581 chn_info[chn].controllers[p1 | 32] & 0x7f);
582 p1 &= ~32;
584 /* Handle all playing notes on this channel */
586 key = ((int) chn << 8);
588 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
589 if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
590 synth_devs[dev]->controller(dev, i, p1, val);
592 else
593 synth_devs[dev]->controller(dev, chn, p1, w14);
595 else /* Mode 1 */
596 synth_devs[dev]->controller(dev, chn, p1, w14);
597 break;
599 case MIDI_PITCH_BEND:
600 if (seq_mode == SEQ_2)
602 if (chn > 15)
603 break;
605 synth_devs[dev]->chn_info[chn].bender_value = w14;
607 if ((int) dev < num_synths)
609 /* Handle all playing notes on this channel */
610 int i, key;
612 key = (chn << 8);
614 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
615 if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
616 synth_devs[dev]->bender(dev, i, w14);
618 else
619 synth_devs[dev]->bender(dev, chn, w14);
621 else /* MODE 1 */
622 synth_devs[dev]->bender(dev, chn, w14);
623 break;
625 default:;
629 static int seq_timing_event(unsigned char *event_rec)
631 unsigned char cmd = event_rec[1];
632 unsigned int parm = *(int *) &event_rec[4];
634 if (seq_mode == SEQ_2)
636 int ret;
638 if ((ret = tmr->event(tmr_no, event_rec)) == TIMER_ARMED)
639 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
640 wake_up(&seq_sleeper);
641 return ret;
643 switch (cmd)
645 case TMR_WAIT_REL:
646 parm += prev_event_time;
649 * NOTE! No break here. Execution of TMR_WAIT_REL continues in the
650 * next case (TMR_WAIT_ABS)
653 case TMR_WAIT_ABS:
654 if (parm > 0)
656 long time;
658 time = parm;
659 prev_event_time = time;
661 seq_playing = 1;
662 request_sound_timer(time);
664 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
665 wake_up(&seq_sleeper);
666 return TIMER_ARMED;
668 break;
670 case TMR_START:
671 seq_time = jiffies;
672 prev_input_time = 0;
673 prev_event_time = 0;
674 break;
676 case TMR_STOP:
677 break;
679 case TMR_CONTINUE:
680 break;
682 case TMR_TEMPO:
683 break;
685 case TMR_ECHO:
686 if (seq_mode == SEQ_2)
687 seq_copy_to_input(event_rec, 8);
688 else
690 parm = (parm << 8 | SEQ_ECHO);
691 seq_copy_to_input((unsigned char *) &parm, 4);
693 break;
695 default:;
698 return TIMER_NOT_ARMED;
701 static void seq_local_event(unsigned char *event_rec)
703 unsigned char cmd = event_rec[1];
704 unsigned int parm = *((unsigned int *) &event_rec[4]);
706 switch (cmd)
708 case LOCL_STARTAUDIO:
709 DMAbuf_start_devices(parm);
710 break;
712 default:;
716 static void seq_sysex_message(unsigned char *event_rec)
718 unsigned int dev = event_rec[1];
719 int i, l = 0;
720 unsigned char *buf = &event_rec[2];
722 if (dev > max_synthdev)
723 return;
724 if (!(synth_open_mask & (1 << dev)))
725 return;
726 if (!synth_devs[dev])
727 return;
729 l = 0;
730 for (i = 0; i < 6 && buf[i] != 0xff; i++)
731 l = i + 1;
733 if (!synth_devs[dev]->send_sysex)
734 return;
735 if (l > 0)
736 synth_devs[dev]->send_sysex(dev, buf, l);
739 static int play_event(unsigned char *q)
742 * NOTE! This routine returns
743 * 0 = normal event played.
744 * 1 = Timer armed. Suspend playback until timer callback.
745 * 2 = MIDI output buffer full. Restore queue and suspend until timer
747 unsigned int *delay;
749 switch (q[0])
751 case SEQ_NOTEOFF:
752 if (synth_open_mask & (1 << 0))
753 if (synth_devs[0])
754 synth_devs[0]->kill_note(0, q[1], 255, q[3]);
755 break;
757 case SEQ_NOTEON:
758 if (q[4] < 128 || q[4] == 255)
759 if (synth_open_mask & (1 << 0))
760 if (synth_devs[0])
761 synth_devs[0]->start_note(0, q[1], q[2], q[3]);
762 break;
764 case SEQ_WAIT:
765 delay = (unsigned int *) q; /*
766 * Bytes 1 to 3 are containing the *
767 * delay in 'ticks'
769 *delay = (*delay >> 8) & 0xffffff;
771 if (*delay > 0)
773 long time;
775 seq_playing = 1;
776 time = *delay;
777 prev_event_time = time;
779 request_sound_timer(time);
781 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
782 wake_up(&seq_sleeper);
784 * The timer is now active and will reinvoke this function
785 * after the timer expires. Return to the caller now.
787 return 1;
789 break;
791 case SEQ_PGMCHANGE:
792 if (synth_open_mask & (1 << 0))
793 if (synth_devs[0])
794 synth_devs[0]->set_instr(0, q[1], q[2]);
795 break;
797 case SEQ_SYNCTIMER: /*
798 * Reset timer
800 seq_time = jiffies;
801 prev_input_time = 0;
802 prev_event_time = 0;
803 break;
805 case SEQ_MIDIPUTC: /*
806 * Put a midi character
808 if (midi_opened[q[2]])
810 int dev;
812 dev = q[2];
814 if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
815 break;
817 if (!midi_devs[dev]->outputc(dev, q[1]))
820 * Output FIFO is full. Wait one timer cycle and try again.
823 seq_playing = 1;
824 request_sound_timer(-1);
825 return 2;
827 else
828 midi_written[dev] = 1;
830 break;
832 case SEQ_ECHO:
833 seq_copy_to_input(q, 4); /*
834 * Echo back to the process
836 break;
838 case SEQ_PRIVATE:
839 if ((int) q[1] < max_synthdev)
840 synth_devs[q[1]]->hw_control(q[1], q);
841 break;
843 case SEQ_EXTENDED:
844 extended_event(q);
845 break;
847 case EV_CHN_VOICE:
848 seq_chn_voice_event(q);
849 break;
851 case EV_CHN_COMMON:
852 seq_chn_common_event(q);
853 break;
855 case EV_TIMING:
856 if (seq_timing_event(q) == TIMER_ARMED)
858 return 1;
860 break;
862 case EV_SEQ_LOCAL:
863 seq_local_event(q);
864 break;
866 case EV_SYSEX:
867 seq_sysex_message(q);
868 break;
870 default:;
872 return 0;
875 /* called also as timer in irq context */
876 static void seq_startplay(void)
878 int this_one, action;
879 unsigned long flags;
881 while (qlen > 0)
884 spin_lock_irqsave(&lock,flags);
885 qhead = ((this_one = qhead) + 1) % SEQ_MAX_QUEUE;
886 qlen--;
887 spin_unlock_irqrestore(&lock,flags);
889 seq_playing = 1;
891 if ((action = play_event(&queue[this_one * EV_SZ])))
892 { /* Suspend playback. Next timer routine invokes this routine again */
893 if (action == 2)
895 qlen++;
896 qhead = this_one;
898 return;
902 seq_playing = 0;
904 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
905 wake_up(&seq_sleeper);
908 static void reset_controllers(int dev, unsigned char *controller, int update_dev)
910 int i;
911 for (i = 0; i < 128; i++)
912 controller[i] = ctrl_def_values[i];
915 static void setup_mode2(void)
917 int dev;
919 max_synthdev = num_synths;
921 for (dev = 0; dev < num_midis; dev++)
923 if (midi_devs[dev] && midi_devs[dev]->converter != NULL)
925 synth_devs[max_synthdev++] = midi_devs[dev]->converter;
929 for (dev = 0; dev < max_synthdev; dev++)
931 int chn;
933 synth_devs[dev]->sysex_ptr = 0;
934 synth_devs[dev]->emulation = 0;
936 for (chn = 0; chn < 16; chn++)
938 synth_devs[dev]->chn_info[chn].pgm_num = 0;
939 reset_controllers(dev,
940 synth_devs[dev]->chn_info[chn].controllers,0);
941 synth_devs[dev]->chn_info[chn].bender_value = (1 << 7); /* Neutral */
942 synth_devs[dev]->chn_info[chn].bender_range = 200;
945 max_mididev = 0;
946 seq_mode = SEQ_2;
949 int sequencer_open(int dev, struct file *file)
951 int retval, mode, i;
952 int level, tmp;
954 if (!sequencer_ok)
955 sequencer_init();
957 level = ((dev & 0x0f) == SND_DEV_SEQ2) ? 2 : 1;
959 dev = dev >> 4;
960 mode = translate_mode(file);
962 DEB(printk("sequencer_open(dev=%d)\n", dev));
964 if (!sequencer_ok)
966 /* printk("Sound card: sequencer not initialized\n");*/
967 return -ENXIO;
969 if (dev) /* Patch manager device (obsolete) */
970 return -ENXIO;
972 if(synth_devs[dev] == NULL)
973 request_module("synth0");
975 if (mode == OPEN_READ)
977 if (!num_midis)
979 /*printk("Sequencer: No MIDI devices. Input not possible\n");*/
980 sequencer_busy = 0;
981 return -ENXIO;
984 if (sequencer_busy)
986 return -EBUSY;
988 sequencer_busy = 1;
989 obsolete_api_used = 0;
991 max_mididev = num_midis;
992 max_synthdev = num_synths;
993 pre_event_timeout = MAX_SCHEDULE_TIMEOUT;
994 seq_mode = SEQ_1;
996 if (pending_timer != -1)
998 tmr_no = pending_timer;
999 pending_timer = -1;
1001 if (tmr_no == -1) /* Not selected yet */
1003 int i, best;
1005 best = -1;
1006 for (i = 0; i < num_sound_timers; i++)
1007 if (sound_timer_devs[i] && sound_timer_devs[i]->priority > best)
1009 tmr_no = i;
1010 best = sound_timer_devs[i]->priority;
1012 if (tmr_no == -1) /* Should not be */
1013 tmr_no = 0;
1015 tmr = sound_timer_devs[tmr_no];
1017 if (level == 2)
1019 if (tmr == NULL)
1021 /*printk("sequencer: No timer for level 2\n");*/
1022 sequencer_busy = 0;
1023 return -ENXIO;
1025 setup_mode2();
1027 if (!max_synthdev && !max_mididev)
1029 sequencer_busy=0;
1030 return -ENXIO;
1033 synth_open_mask = 0;
1035 for (i = 0; i < max_mididev; i++)
1037 midi_opened[i] = 0;
1038 midi_written[i] = 0;
1041 for (i = 0; i < max_synthdev; i++)
1043 if (synth_devs[i]==NULL)
1044 continue;
1046 if (!try_module_get(synth_devs[i]->owner))
1047 continue;
1049 if ((tmp = synth_devs[i]->open(i, mode)) < 0)
1051 printk(KERN_WARNING "Sequencer: Warning! Cannot open synth device #%d (%d)\n", i, tmp);
1052 if (synth_devs[i]->midi_dev)
1053 printk(KERN_WARNING "(Maps to MIDI dev #%d)\n", synth_devs[i]->midi_dev);
1055 else
1057 synth_open_mask |= (1 << i);
1058 if (synth_devs[i]->midi_dev)
1059 midi_opened[synth_devs[i]->midi_dev] = 1;
1063 seq_time = jiffies;
1065 prev_input_time = 0;
1066 prev_event_time = 0;
1068 if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
1071 * Initialize midi input devices
1074 for (i = 0; i < max_mididev; i++)
1075 if (!midi_opened[i] && midi_devs[i])
1077 if (!try_module_get(midi_devs[i]->owner))
1078 continue;
1080 if ((retval = midi_devs[i]->open(i, mode,
1081 sequencer_midi_input, sequencer_midi_output)) >= 0)
1083 midi_opened[i] = 1;
1088 if (seq_mode == SEQ_2) {
1089 if (try_module_get(tmr->owner))
1090 tmr->open(tmr_no, seq_mode);
1093 init_waitqueue_head(&seq_sleeper);
1094 init_waitqueue_head(&midi_sleeper);
1095 output_threshold = SEQ_MAX_QUEUE / 2;
1097 return 0;
1100 static void seq_drain_midi_queues(void)
1102 int i, n;
1105 * Give the Midi drivers time to drain their output queues
1108 n = 1;
1110 while (!signal_pending(current) && n)
1112 n = 0;
1114 for (i = 0; i < max_mididev; i++)
1115 if (midi_opened[i] && midi_written[i])
1116 if (midi_devs[i]->buffer_status != NULL)
1117 if (midi_devs[i]->buffer_status(i))
1118 n++;
1121 * Let's have a delay
1124 if (n)
1125 interruptible_sleep_on_timeout(&seq_sleeper,
1126 HZ/10);
1130 void sequencer_release(int dev, struct file *file)
1132 int i;
1133 int mode = translate_mode(file);
1135 dev = dev >> 4;
1137 DEB(printk("sequencer_release(dev=%d)\n", dev));
1140 * Wait until the queue is empty (if we don't have nonblock)
1143 if (mode != OPEN_READ && !(file->f_flags & O_NONBLOCK))
1145 while (!signal_pending(current) && qlen > 0)
1147 seq_sync();
1148 interruptible_sleep_on_timeout(&seq_sleeper,
1149 3*HZ);
1150 /* Extra delay */
1154 if (mode != OPEN_READ)
1155 seq_drain_midi_queues(); /*
1156 * Ensure the output queues are empty
1158 seq_reset();
1159 if (mode != OPEN_READ)
1160 seq_drain_midi_queues(); /*
1161 * Flush the all notes off messages
1164 for (i = 0; i < max_synthdev; i++)
1166 if (synth_open_mask & (1 << i)) /*
1167 * Actually opened
1169 if (synth_devs[i])
1171 synth_devs[i]->close(i);
1173 module_put(synth_devs[i]->owner);
1175 if (synth_devs[i]->midi_dev)
1176 midi_opened[synth_devs[i]->midi_dev] = 0;
1180 for (i = 0; i < max_mididev; i++)
1182 if (midi_opened[i]) {
1183 midi_devs[i]->close(i);
1184 module_put(midi_devs[i]->owner);
1188 if (seq_mode == SEQ_2) {
1189 tmr->close(tmr_no);
1190 module_put(tmr->owner);
1193 if (obsolete_api_used)
1194 printk(KERN_WARNING "/dev/music: Obsolete (4 byte) API was used by %s\n", current->comm);
1195 sequencer_busy = 0;
1198 static int seq_sync(void)
1200 if (qlen && !seq_playing && !signal_pending(current))
1201 seq_startplay();
1203 if (qlen > 0)
1204 interruptible_sleep_on_timeout(&seq_sleeper, HZ);
1205 return qlen;
1208 static void midi_outc(int dev, unsigned char data)
1211 * NOTE! Calls sleep(). Don't call this from interrupt.
1214 int n;
1215 unsigned long flags;
1218 * This routine sends one byte to the Midi channel.
1219 * If the output FIFO is full, it waits until there
1220 * is space in the queue
1223 n = 3 * HZ; /* Timeout */
1225 spin_lock_irqsave(&lock,flags);
1226 while (n && !midi_devs[dev]->outputc(dev, data)) {
1227 interruptible_sleep_on_timeout(&seq_sleeper, HZ/25);
1228 n--;
1230 spin_unlock_irqrestore(&lock,flags);
1233 static void seq_reset(void)
1236 * NOTE! Calls sleep(). Don't call this from interrupt.
1239 int i;
1240 int chn;
1241 unsigned long flags;
1243 sound_stop_timer();
1245 seq_time = jiffies;
1246 prev_input_time = 0;
1247 prev_event_time = 0;
1249 qlen = qhead = qtail = 0;
1250 iqlen = iqhead = iqtail = 0;
1252 for (i = 0; i < max_synthdev; i++)
1253 if (synth_open_mask & (1 << i))
1254 if (synth_devs[i])
1255 synth_devs[i]->reset(i);
1257 if (seq_mode == SEQ_2)
1259 for (chn = 0; chn < 16; chn++)
1260 for (i = 0; i < max_synthdev; i++)
1261 if (synth_open_mask & (1 << i))
1262 if (synth_devs[i])
1264 synth_devs[i]->controller(i, chn, 123, 0); /* All notes off */
1265 synth_devs[i]->controller(i, chn, 121, 0); /* Reset all ctl */
1266 synth_devs[i]->bender(i, chn, 1 << 13); /* Bender off */
1269 else /* seq_mode == SEQ_1 */
1271 for (i = 0; i < max_mididev; i++)
1272 if (midi_written[i]) /*
1273 * Midi used. Some notes may still be playing
1277 * Sending just a ACTIVE SENSING message should be enough to stop all
1278 * playing notes. Since there are devices not recognizing the
1279 * active sensing, we have to send some all notes off messages also.
1281 midi_outc(i, 0xfe);
1283 for (chn = 0; chn < 16; chn++)
1285 midi_outc(i, (unsigned char) (0xb0 + (chn & 0x0f))); /* control change */
1286 midi_outc(i, 0x7b); /* All notes off */
1287 midi_outc(i, 0); /* Dummy parameter */
1290 midi_devs[i]->close(i);
1292 midi_written[i] = 0;
1293 midi_opened[i] = 0;
1297 seq_playing = 0;
1299 spin_lock_irqsave(&lock,flags);
1301 if (waitqueue_active(&seq_sleeper)) {
1302 /* printk( "Sequencer Warning: Unexpected sleeping process - Waking up\n"); */
1303 wake_up(&seq_sleeper);
1305 spin_unlock_irqrestore(&lock,flags);
1308 static void seq_panic(void)
1311 * This routine is called by the application in case the user
1312 * wants to reset the system to the default state.
1315 seq_reset();
1318 * Since some of the devices don't recognize the active sensing and
1319 * all notes off messages, we have to shut all notes manually.
1321 * TO BE IMPLEMENTED LATER
1325 * Also return the controllers to their default states
1329 int sequencer_ioctl(int dev, struct file *file, unsigned int cmd, void __user *arg)
1331 int midi_dev, orig_dev, val, err;
1332 int mode = translate_mode(file);
1333 struct synth_info inf;
1334 struct seq_event_rec event_rec;
1335 unsigned long flags;
1336 int __user *p = arg;
1338 orig_dev = dev = dev >> 4;
1340 switch (cmd)
1342 case SNDCTL_TMR_TIMEBASE:
1343 case SNDCTL_TMR_TEMPO:
1344 case SNDCTL_TMR_START:
1345 case SNDCTL_TMR_STOP:
1346 case SNDCTL_TMR_CONTINUE:
1347 case SNDCTL_TMR_METRONOME:
1348 case SNDCTL_TMR_SOURCE:
1349 if (seq_mode != SEQ_2)
1350 return -EINVAL;
1351 return tmr->ioctl(tmr_no, cmd, arg);
1353 case SNDCTL_TMR_SELECT:
1354 if (seq_mode != SEQ_2)
1355 return -EINVAL;
1356 if (get_user(pending_timer, p))
1357 return -EFAULT;
1358 if (pending_timer < 0 || pending_timer >= num_sound_timers || sound_timer_devs[pending_timer] == NULL)
1360 pending_timer = -1;
1361 return -EINVAL;
1363 val = pending_timer;
1364 break;
1366 case SNDCTL_SEQ_PANIC:
1367 seq_panic();
1368 return -EINVAL;
1370 case SNDCTL_SEQ_SYNC:
1371 if (mode == OPEN_READ)
1372 return 0;
1373 while (qlen > 0 && !signal_pending(current))
1374 seq_sync();
1375 return qlen ? -EINTR : 0;
1377 case SNDCTL_SEQ_RESET:
1378 seq_reset();
1379 return 0;
1381 case SNDCTL_SEQ_TESTMIDI:
1382 if (__get_user(midi_dev, p))
1383 return -EFAULT;
1384 if (midi_dev < 0 || midi_dev >= max_mididev || !midi_devs[midi_dev])
1385 return -ENXIO;
1387 if (!midi_opened[midi_dev] &&
1388 (err = midi_devs[midi_dev]->open(midi_dev, mode, sequencer_midi_input,
1389 sequencer_midi_output)) < 0)
1390 return err;
1391 midi_opened[midi_dev] = 1;
1392 return 0;
1394 case SNDCTL_SEQ_GETINCOUNT:
1395 if (mode == OPEN_WRITE)
1396 return 0;
1397 val = iqlen;
1398 break;
1400 case SNDCTL_SEQ_GETOUTCOUNT:
1401 if (mode == OPEN_READ)
1402 return 0;
1403 val = SEQ_MAX_QUEUE - qlen;
1404 break;
1406 case SNDCTL_SEQ_GETTIME:
1407 if (seq_mode == SEQ_2)
1408 return tmr->ioctl(tmr_no, cmd, arg);
1409 val = jiffies - seq_time;
1410 break;
1412 case SNDCTL_SEQ_CTRLRATE:
1414 * If *arg == 0, just return the current rate
1416 if (seq_mode == SEQ_2)
1417 return tmr->ioctl(tmr_no, cmd, arg);
1419 if (get_user(val, p))
1420 return -EFAULT;
1421 if (val != 0)
1422 return -EINVAL;
1423 val = HZ;
1424 break;
1426 case SNDCTL_SEQ_RESETSAMPLES:
1427 case SNDCTL_SYNTH_REMOVESAMPLE:
1428 case SNDCTL_SYNTH_CONTROL:
1429 if (get_user(dev, p))
1430 return -EFAULT;
1431 if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL)
1432 return -ENXIO;
1433 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1434 return -EBUSY;
1435 return synth_devs[dev]->ioctl(dev, cmd, arg);
1437 case SNDCTL_SEQ_NRSYNTHS:
1438 val = max_synthdev;
1439 break;
1441 case SNDCTL_SEQ_NRMIDIS:
1442 val = max_mididev;
1443 break;
1445 case SNDCTL_SYNTH_MEMAVL:
1446 if (get_user(dev, p))
1447 return -EFAULT;
1448 if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL)
1449 return -ENXIO;
1450 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1451 return -EBUSY;
1452 val = synth_devs[dev]->ioctl(dev, cmd, arg);
1453 break;
1455 case SNDCTL_FM_4OP_ENABLE:
1456 if (get_user(dev, p))
1457 return -EFAULT;
1458 if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL)
1459 return -ENXIO;
1460 if (!(synth_open_mask & (1 << dev)))
1461 return -ENXIO;
1462 synth_devs[dev]->ioctl(dev, cmd, arg);
1463 return 0;
1465 case SNDCTL_SYNTH_INFO:
1466 if (get_user(dev, &((struct synth_info __user *)arg)->device))
1467 return -EFAULT;
1468 if (dev < 0 || dev >= max_synthdev)
1469 return -ENXIO;
1470 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1471 return -EBUSY;
1472 return synth_devs[dev]->ioctl(dev, cmd, arg);
1474 /* Like SYNTH_INFO but returns ID in the name field */
1475 case SNDCTL_SYNTH_ID:
1476 if (get_user(dev, &((struct synth_info __user *)arg)->device))
1477 return -EFAULT;
1478 if (dev < 0 || dev >= max_synthdev)
1479 return -ENXIO;
1480 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1481 return -EBUSY;
1482 memcpy(&inf, synth_devs[dev]->info, sizeof(inf));
1483 strlcpy(inf.name, synth_devs[dev]->id, sizeof(inf.name));
1484 inf.device = dev;
1485 return copy_to_user(arg, &inf, sizeof(inf))?-EFAULT:0;
1487 case SNDCTL_SEQ_OUTOFBAND:
1488 if (copy_from_user(&event_rec, arg, sizeof(event_rec)))
1489 return -EFAULT;
1490 spin_lock_irqsave(&lock,flags);
1491 play_event(event_rec.arr);
1492 spin_unlock_irqrestore(&lock,flags);
1493 return 0;
1495 case SNDCTL_MIDI_INFO:
1496 if (get_user(dev, &((struct midi_info __user *)arg)->device))
1497 return -EFAULT;
1498 if (dev < 0 || dev >= max_mididev || !midi_devs[dev])
1499 return -ENXIO;
1500 midi_devs[dev]->info.device = dev;
1501 return copy_to_user(arg, &midi_devs[dev]->info, sizeof(struct midi_info))?-EFAULT:0;
1503 case SNDCTL_SEQ_THRESHOLD:
1504 if (get_user(val, p))
1505 return -EFAULT;
1506 if (val < 1)
1507 val = 1;
1508 if (val >= SEQ_MAX_QUEUE)
1509 val = SEQ_MAX_QUEUE - 1;
1510 output_threshold = val;
1511 return 0;
1513 case SNDCTL_MIDI_PRETIME:
1514 if (get_user(val, p))
1515 return -EFAULT;
1516 if (val < 0)
1517 val = 0;
1518 val = (HZ * val) / 10;
1519 pre_event_timeout = val;
1520 break;
1522 default:
1523 if (mode == OPEN_READ)
1524 return -EIO;
1525 if (!synth_devs[0])
1526 return -ENXIO;
1527 if (!(synth_open_mask & (1 << 0)))
1528 return -ENXIO;
1529 if (!synth_devs[0]->ioctl)
1530 return -EINVAL;
1531 return synth_devs[0]->ioctl(0, cmd, arg);
1533 return put_user(val, p);
1536 /* No kernel lock - we're using the global irq lock here */
1537 unsigned int sequencer_poll(int dev, struct file *file, poll_table * wait)
1539 unsigned long flags;
1540 unsigned int mask = 0;
1542 dev = dev >> 4;
1544 spin_lock_irqsave(&lock,flags);
1545 /* input */
1546 poll_wait(file, &midi_sleeper, wait);
1547 if (iqlen)
1548 mask |= POLLIN | POLLRDNORM;
1550 /* output */
1551 poll_wait(file, &seq_sleeper, wait);
1552 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
1553 mask |= POLLOUT | POLLWRNORM;
1554 spin_unlock_irqrestore(&lock,flags);
1555 return mask;
1559 void sequencer_timer(unsigned long dummy)
1561 seq_startplay();
1563 EXPORT_SYMBOL(sequencer_timer);
1565 int note_to_freq(int note_num)
1569 * This routine converts a midi note to a frequency (multiplied by 1000)
1572 int note, octave, note_freq;
1573 static int notes[] =
1575 261632, 277189, 293671, 311132, 329632, 349232,
1576 369998, 391998, 415306, 440000, 466162, 493880
1579 #define BASE_OCTAVE 5
1581 octave = note_num / 12;
1582 note = note_num % 12;
1584 note_freq = notes[note];
1586 if (octave < BASE_OCTAVE)
1587 note_freq >>= (BASE_OCTAVE - octave);
1588 else if (octave > BASE_OCTAVE)
1589 note_freq <<= (octave - BASE_OCTAVE);
1592 * note_freq >>= 1;
1595 return note_freq;
1597 EXPORT_SYMBOL(note_to_freq);
1599 unsigned long compute_finetune(unsigned long base_freq, int bend, int range,
1600 int vibrato_cents)
1602 unsigned long amount;
1603 int negative, semitones, cents, multiplier = 1;
1605 if (!bend)
1606 return base_freq;
1607 if (!range)
1608 return base_freq;
1610 if (!base_freq)
1611 return base_freq;
1613 if (range >= 8192)
1614 range = 8192;
1616 bend = bend * range / 8192; /* Convert to cents */
1617 bend += vibrato_cents;
1619 if (!bend)
1620 return base_freq;
1622 negative = bend < 0 ? 1 : 0;
1624 if (bend < 0)
1625 bend *= -1;
1626 if (bend > range)
1627 bend = range;
1630 if (bend > 2399)
1631 bend = 2399;
1633 while (bend > 2399)
1635 multiplier *= 4;
1636 bend -= 2400;
1639 semitones = bend / 100;
1640 cents = bend % 100;
1642 amount = (int) (semitone_tuning[semitones] * multiplier * cent_tuning[cents]) / 10000;
1644 if (negative)
1645 return (base_freq * 10000) / amount; /* Bend down */
1646 else
1647 return (base_freq * amount) / 10000; /* Bend up */
1649 EXPORT_SYMBOL(compute_finetune);
1651 void sequencer_init(void)
1653 if (sequencer_ok)
1654 return;
1655 queue = vmalloc(SEQ_MAX_QUEUE * EV_SZ);
1656 if (queue == NULL)
1658 printk(KERN_ERR "sequencer: Can't allocate memory for sequencer output queue\n");
1659 return;
1661 iqueue = vmalloc(SEQ_MAX_QUEUE * IEV_SZ);
1662 if (iqueue == NULL)
1664 printk(KERN_ERR "sequencer: Can't allocate memory for sequencer input queue\n");
1665 vfree(queue);
1666 return;
1668 sequencer_ok = 1;
1670 EXPORT_SYMBOL(sequencer_init);
1672 void sequencer_unload(void)
1674 vfree(queue);
1675 vfree(iqueue);
1676 queue = iqueue = NULL;