This source file includes following definitions.
- sequencer_read
- sequencer_midi_output
- seq_copy_to_input
- sequencer_midi_input
- seq_input_event
- sequencer_write
- seq_queue
- extended_event
- find_voice
- alloc_voice
- seq_chn_voice_event
- seq_chn_common_event
- seq_timing_event
- seq_local_event
- seq_sysex_message
- play_event
- seq_startplay
- reset_controllers
- setup_mode2
- sequencer_open
- seq_drain_midi_queues
- sequencer_release
- seq_sync
- midi_outc
- seq_reset
- seq_panic
- sequencer_ioctl
- sequencer_select
- sequencer_timer
- note_to_freq
- compute_finetune
- sequencer_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include <linux/config.h>
30
31
32 #define SEQUENCER_C
33 #include "sound_config.h"
34
35 #ifdef CONFIG_SEQUENCER
36
37 #include "midi_ctrl.h"
38
39 static int sequencer_ok = 0;
40 static struct sound_timer_operations *tmr;
41 static int tmr_no = -1;
42 static int pending_timer = -1;
43
44
45
46
47
48 static int max_mididev = 0;
49 static int max_synthdev = 0;
50
51
52
53
54
55
56
57 #define SEQ_1 1
58 #define SEQ_2 2
59 static int seq_mode = SEQ_1;
60
61 static wait_handle *seq_sleeper = NULL;
62 static volatile struct snd_wait seq_sleep_flag =
63 {0};
64 static wait_handle *midi_sleeper = NULL;
65 static volatile struct snd_wait midi_sleep_flag =
66 {0};
67
68 static int midi_opened[MAX_MIDI_DEV] =
69 {0};
70 static int midi_written[MAX_MIDI_DEV] =
71 {0};
72
73 unsigned long prev_input_time = 0;
74 int prev_event_time;
75 unsigned long seq_time = 0;
76
77 #include "tuning.h"
78
79 #define EV_SZ 8
80 #define IEV_SZ 8
81 static unsigned char *queue = NULL;
82 static unsigned char *iqueue = NULL;
83
84 static volatile int qhead = 0, qtail = 0, qlen = 0;
85 static volatile int iqhead = 0, iqtail = 0, iqlen = 0;
86 static volatile int seq_playing = 0;
87 static volatile int sequencer_busy = 0;
88 static int output_treshold;
89 static int pre_event_timeout;
90 static unsigned synth_open_mask;
91
92 static int seq_queue (unsigned char *note, char nonblock);
93 static void seq_startplay (void);
94 static int seq_sync (void);
95 static void seq_reset (void);
96 static int pmgr_present[MAX_SYNTH_DEV] =
97 {0};
98
99 #if MAX_SYNTH_DEV > 15
100 #error Too many synthesizer devices enabled.
101 #endif
102
103 int
104 sequencer_read (int dev, struct fileinfo *file, char *buf, int count)
105 {
106 int c = count, p = 0;
107 int ev_len;
108 unsigned long flags;
109
110 dev = dev >> 4;
111
112 ev_len = seq_mode == SEQ_1 ? 4 : 8;
113
114 if (dev)
115
116
117 return pmgr_read (dev - 1, file, buf, count);
118
119 save_flags (flags);
120 cli ();
121 if (!iqlen)
122 {
123 if ((file->flags & (O_NONBLOCK) ?
124 1 : 0))
125 {
126 restore_flags (flags);
127 return -EAGAIN;
128 }
129
130
131 {
132 unsigned long tl;
133
134 if (pre_event_timeout)
135 current_set_timeout (tl = jiffies + (pre_event_timeout));
136 else
137 tl = (unsigned long) -1;
138 midi_sleep_flag.mode = WK_SLEEP;
139 module_interruptible_sleep_on (&midi_sleeper);
140 if (!(midi_sleep_flag.mode & WK_WAKEUP))
141 {
142 if (jiffies >= tl)
143 midi_sleep_flag.mode |= WK_TIMEOUT;
144 }
145 midi_sleep_flag.mode &= ~WK_SLEEP;
146 };
147
148 if (!iqlen)
149 {
150 restore_flags (flags);
151 return 0;
152 }
153 }
154
155 while (iqlen && c >= ev_len)
156 {
157
158 memcpy_tofs (&((buf)[p]), (char *) &iqueue[iqhead * IEV_SZ], ev_len);
159 p += ev_len;
160 c -= ev_len;
161
162 iqhead = (iqhead + 1) % SEQ_MAX_QUEUE;
163 iqlen--;
164 }
165 restore_flags (flags);
166
167 return count - c;
168 }
169
170 static void
171 sequencer_midi_output (int dev)
172 {
173
174
175
176 }
177
178 void
179 seq_copy_to_input (unsigned char *event_rec, int len)
180 {
181 unsigned long flags;
182
183
184
185
186
187 if (len != 4 && len != 8)
188 return;
189 if ((seq_mode == SEQ_1) != (len == 4))
190 return;
191
192 if (iqlen >= (SEQ_MAX_QUEUE - 1))
193 return;
194
195 save_flags (flags);
196 cli ();
197 memcpy (&iqueue[iqtail * IEV_SZ], event_rec, len);
198 iqlen++;
199 iqtail = (iqtail + 1) % SEQ_MAX_QUEUE;
200
201 if ((midi_sleep_flag.mode & WK_SLEEP))
202 {
203 {
204 midi_sleep_flag.mode = WK_WAKEUP;
205 module_wake_up (&midi_sleeper);
206 };
207 }
208 restore_flags (flags);
209 }
210
211 static void
212 sequencer_midi_input (int dev, unsigned char data)
213 {
214 unsigned int tstamp;
215 unsigned char event_rec[4];
216
217 if (data == 0xfe)
218 return;
219
220 tstamp = jiffies - seq_time;
221 if (tstamp != prev_input_time)
222 {
223 tstamp = (tstamp << 8) | SEQ_WAIT;
224
225 seq_copy_to_input ((unsigned char *) &tstamp, 4);
226 prev_input_time = tstamp;
227 }
228
229 event_rec[0] = SEQ_MIDIPUTC;
230 event_rec[1] = data;
231 event_rec[2] = dev;
232 event_rec[3] = 0;
233
234 seq_copy_to_input (event_rec, 4);
235 }
236
237 void
238 seq_input_event (unsigned char *event_rec, int len)
239 {
240 unsigned long this_time;
241
242 if (seq_mode == SEQ_2)
243 this_time = tmr->get_time (tmr_no);
244 else
245 this_time = jiffies - seq_time;
246
247 if (this_time != prev_input_time)
248 {
249 unsigned char tmp_event[8];
250
251 tmp_event[0] = EV_TIMING;
252 tmp_event[1] = TMR_WAIT_ABS;
253 tmp_event[2] = 0;
254 tmp_event[3] = 0;
255 *(unsigned int *) &tmp_event[4] = this_time;
256
257 seq_copy_to_input (tmp_event, 8);
258 prev_input_time = this_time;
259 }
260
261 seq_copy_to_input (event_rec, len);
262 }
263
264 int
265 sequencer_write (int dev, struct fileinfo *file, const char *buf, int count)
266 {
267 unsigned char event_rec[EV_SZ], ev_code;
268 int p = 0, c, ev_size;
269 int err;
270 int mode = file->mode & O_ACCMODE;
271
272 dev = dev >> 4;
273
274 DEB (printk ("sequencer_write(dev=%d, count=%d)\n", dev, count));
275
276 if (mode == OPEN_READ)
277 return -EIO;
278
279 if (dev)
280 return pmgr_write (dev - 1, file, buf, count);
281
282 c = count;
283
284 while (c >= 4)
285 {
286 memcpy_fromfs ((char *) event_rec, &((buf)[p]), 4);
287 ev_code = event_rec[0];
288
289 if (ev_code == SEQ_FULLSIZE)
290 {
291 int err;
292
293 dev = *(unsigned short *) &event_rec[2];
294 if (dev < 0 || dev >= max_synthdev)
295 return -ENXIO;
296
297 if (!(synth_open_mask & (1 << dev)))
298 return -ENXIO;
299
300 err = synth_devs[dev]->load_patch (dev, *(short *) &event_rec[0], buf, p + 4, c, 0);
301 if (err < 0)
302 return err;
303
304 return err;
305 }
306
307 if (ev_code >= 128)
308 {
309 if (seq_mode == SEQ_2 && ev_code == SEQ_EXTENDED)
310 {
311 printk ("Sequencer: Invalid level 2 event %x\n", ev_code);
312 return -EINVAL;
313 }
314
315 ev_size = 8;
316
317 if (c < ev_size)
318 {
319 if (!seq_playing)
320 seq_startplay ();
321 return count - c;
322 }
323
324 memcpy_fromfs ((char *) &event_rec[4], &((buf)[p + 4]), 4);
325
326 }
327 else
328 {
329 if (seq_mode == SEQ_2)
330 {
331 printk ("Sequencer: 4 byte event in level 2 mode\n");
332 return -EINVAL;
333 }
334 ev_size = 4;
335 }
336
337 if (event_rec[0] == SEQ_MIDIPUTC)
338 {
339
340 if (!midi_opened[event_rec[2]])
341 {
342 int mode;
343 int dev = event_rec[2];
344
345 if (dev >= max_mididev)
346 {
347 printk ("Sequencer Error: Nonexistent MIDI device %d\n", dev);
348 return -ENXIO;
349 }
350
351 mode = file->mode & O_ACCMODE;
352
353 if ((err = midi_devs[dev]->open (dev, mode,
354 sequencer_midi_input, sequencer_midi_output)) < 0)
355 {
356 seq_reset ();
357 printk ("Sequencer Error: Unable to open Midi #%d\n", dev);
358 return err;
359 }
360
361 midi_opened[dev] = 1;
362 }
363
364 }
365
366 if (!seq_queue (event_rec, (file->flags & (O_NONBLOCK) ?
367 1 : 0)))
368 {
369 int processed = count - c;
370
371 if (!seq_playing)
372 seq_startplay ();
373
374 if (!processed && (file->flags & (O_NONBLOCK) ?
375 1 : 0))
376 return -EAGAIN;
377 else
378 return processed;
379 }
380
381 p += ev_size;
382 c -= ev_size;
383 }
384
385 if (!seq_playing)
386 seq_startplay ();
387
388 return count;
389
390
391 }
392
393 static int
394 seq_queue (unsigned char *note, char nonblock)
395 {
396
397
398
399
400
401 if (qlen >= SEQ_MAX_QUEUE)
402 if (!seq_playing)
403 seq_startplay ();
404
405
406
407 if (!nonblock && qlen >= SEQ_MAX_QUEUE && !(seq_sleep_flag.mode & WK_SLEEP))
408 {
409
410
411
412
413 seq_sleep_flag.mode = WK_SLEEP;
414 module_interruptible_sleep_on (&seq_sleeper);
415 seq_sleep_flag.mode &= ~WK_SLEEP;;
416 }
417
418 if (qlen >= SEQ_MAX_QUEUE)
419 {
420 return 0;
421
422
423 }
424 memcpy (&queue[qtail * EV_SZ], note, EV_SZ);
425
426 qtail = (qtail + 1) % SEQ_MAX_QUEUE;
427 qlen++;
428
429 return 1;
430 }
431
432 static int
433 extended_event (unsigned char *q)
434 {
435 int dev = q[2];
436
437 if (dev < 0 || dev >= max_synthdev)
438 return -ENXIO;
439
440 if (!(synth_open_mask & (1 << dev)))
441 return -ENXIO;
442
443 switch (q[1])
444 {
445 case SEQ_NOTEOFF:
446 synth_devs[dev]->kill_note (dev, q[3], q[4], q[5]);
447 break;
448
449 case SEQ_NOTEON:
450 if (q[4] > 127 && q[4] != 255)
451 return 0;
452
453 synth_devs[dev]->start_note (dev, q[3], q[4], q[5]);
454 break;
455
456 case SEQ_PGMCHANGE:
457 synth_devs[dev]->set_instr (dev, q[3], q[4]);
458 break;
459
460 case SEQ_AFTERTOUCH:
461 synth_devs[dev]->aftertouch (dev, q[3], q[4]);
462 break;
463
464 case SEQ_BALANCE:
465 synth_devs[dev]->panning (dev, q[3], (char) q[4]);
466 break;
467
468 case SEQ_CONTROLLER:
469 synth_devs[dev]->controller (dev, q[3], q[4], (short) (q[5] | (q[6] << 8)));
470 break;
471
472 case SEQ_VOLMODE:
473 if (synth_devs[dev]->volume_method != NULL)
474 synth_devs[dev]->volume_method (dev, q[3]);
475 break;
476
477 default:
478 return -EINVAL;
479 }
480
481 return 0;
482 }
483
484 static int
485 find_voice (int dev, int chn, int note)
486 {
487 unsigned short key;
488 int i;
489
490 key = (chn << 8) | (note + 1);
491
492 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
493 if (synth_devs[dev]->alloc.map[i] == key)
494 return i;
495
496 return -1;
497 }
498
499 static int
500 alloc_voice (int dev, int chn, int note)
501 {
502 unsigned short key;
503 int voice;
504
505 key = (chn << 8) | (note + 1);
506
507 voice = synth_devs[dev]->alloc_voice (dev, chn, note,
508 &synth_devs[dev]->alloc);
509 synth_devs[dev]->alloc.map[voice] = key;
510 synth_devs[dev]->alloc.alloc_times[voice] =
511 synth_devs[dev]->alloc.timestamp++;
512 return voice;
513 }
514
515 static void
516 seq_chn_voice_event (unsigned char *event_rec)
517 {
518 unsigned char dev = event_rec[1];
519 unsigned char cmd = event_rec[2];
520 unsigned char chn = event_rec[3];
521 unsigned char note = event_rec[4];
522 unsigned char parm = event_rec[5];
523 int voice = -1;
524
525 if ((int) dev > max_synthdev)
526 return;
527 if (!(synth_open_mask & (1 << dev)))
528 return;
529 if (!synth_devs[dev])
530 return;
531
532 if (seq_mode == SEQ_2)
533 {
534 if (synth_devs[dev]->alloc_voice)
535 voice = find_voice (dev, chn, note);
536
537 if (cmd == MIDI_NOTEON && parm == 0)
538 {
539 cmd = MIDI_NOTEOFF;
540 parm = 64;
541 }
542 }
543
544 switch (cmd)
545 {
546 case MIDI_NOTEON:
547 if (note > 127 && note != 255)
548 return;
549
550 if (voice == -1 && seq_mode == SEQ_2 && synth_devs[dev]->alloc_voice)
551 {
552 voice = alloc_voice (dev, chn, note);
553 }
554
555 if (voice == -1)
556 voice = chn;
557
558 if (seq_mode == SEQ_2 && (int) dev < num_synths)
559 {
560
561
562
563
564
565 if (chn == 9)
566 {
567 synth_devs[dev]->set_instr (dev, voice, 128 + note);
568 note = 60;
569
570 }
571 }
572
573 if (seq_mode == SEQ_2)
574 {
575 synth_devs[dev]->setup_voice (dev, voice, chn);
576 }
577
578 synth_devs[dev]->start_note (dev, voice, note, parm);
579 break;
580
581 case MIDI_NOTEOFF:
582 if (voice == -1)
583 voice = chn;
584 synth_devs[dev]->kill_note (dev, voice, note, parm);
585 break;
586
587 case MIDI_KEY_PRESSURE:
588 if (voice == -1)
589 voice = chn;
590 synth_devs[dev]->aftertouch (dev, voice, parm);
591 break;
592
593 default:;
594 }
595 }
596
597 static void
598 seq_chn_common_event (unsigned char *event_rec)
599 {
600 unsigned char dev = event_rec[1];
601 unsigned char cmd = event_rec[2];
602 unsigned char chn = event_rec[3];
603 unsigned char p1 = event_rec[4];
604
605
606 unsigned short w14 = *(short *) &event_rec[6];
607
608 if ((int) dev > max_synthdev)
609 return;
610 if (!(synth_open_mask & (1 << dev)))
611 return;
612 if (!synth_devs[dev])
613 return;
614
615 switch (cmd)
616 {
617 case MIDI_PGM_CHANGE:
618 if (seq_mode == SEQ_2)
619 {
620 synth_devs[dev]->chn_info[chn].pgm_num = p1;
621 if ((int) dev >= num_synths)
622 synth_devs[dev]->set_instr (dev, chn, p1);
623 }
624 else
625 synth_devs[dev]->set_instr (dev, chn, p1);
626
627 break;
628
629 case MIDI_CTL_CHANGE:
630 if (seq_mode == SEQ_2)
631 {
632 if (chn > 15 || p1 > 127)
633 break;
634
635 synth_devs[dev]->chn_info[chn].controllers[p1] = w14 & 0x7f;
636
637 if (p1 < 32)
638 synth_devs[dev]->chn_info[chn].controllers[p1 + 32] = 0;
639
640 if ((int) dev < num_synths)
641 {
642 int val = w14 & 0x7f;
643 int i, key;
644
645 if (p1 < 64)
646 {
647 val = ((synth_devs[dev]->
648 chn_info[chn].controllers[p1 & ~32] & 0x7f) << 7)
649 | (synth_devs[dev]->
650 chn_info[chn].controllers[p1 | 32] & 0x7f);
651 p1 &= ~32;
652 }
653
654
655
656 key = ((int) chn << 8);
657
658 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
659 if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
660 synth_devs[dev]->controller (dev, i, p1, val);
661 }
662 else
663 synth_devs[dev]->controller (dev, chn, p1, w14);
664 }
665 else
666 synth_devs[dev]->controller (dev, chn, p1, w14);
667 break;
668
669 case MIDI_PITCH_BEND:
670 if (seq_mode == SEQ_2)
671 {
672 synth_devs[dev]->chn_info[chn].bender_value = w14;
673
674 if ((int) dev < num_synths)
675 {
676 int i, key;
677
678 key = (chn << 8);
679
680 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
681 if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
682 synth_devs[dev]->bender (dev, i, w14);
683 }
684 else
685 synth_devs[dev]->bender (dev, chn, w14);
686 }
687 else
688 synth_devs[dev]->bender (dev, chn, w14);
689 break;
690
691 default:;
692 }
693 }
694
695 static int
696 seq_timing_event (unsigned char *event_rec)
697 {
698 unsigned char cmd = event_rec[1];
699 unsigned int parm = *(int *) &event_rec[4];
700
701 if (seq_mode == SEQ_2)
702 {
703 int ret;
704
705 if ((ret = tmr->event (tmr_no, event_rec)) == TIMER_ARMED)
706 {
707 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
708 {
709 unsigned long flags;
710
711 save_flags (flags);
712 cli ();
713 if ((seq_sleep_flag.mode & WK_SLEEP))
714 {
715 {
716 seq_sleep_flag.mode = WK_WAKEUP;
717 module_wake_up (&seq_sleeper);
718 };
719 }
720 restore_flags (flags);
721 }
722 }
723 return ret;
724 }
725
726 switch (cmd)
727 {
728 case TMR_WAIT_REL:
729 parm += prev_event_time;
730
731
732
733
734
735
736 case TMR_WAIT_ABS:
737 if (parm > 0)
738 {
739 long time;
740
741 seq_playing = 1;
742 time = parm;
743 prev_event_time = time;
744
745 request_sound_timer (time);
746
747 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
748 {
749 unsigned long flags;
750
751 save_flags (flags);
752 cli ();
753 if ((seq_sleep_flag.mode & WK_SLEEP))
754 {
755 {
756 seq_sleep_flag.mode = WK_WAKEUP;
757 module_wake_up (&seq_sleeper);
758 };
759 }
760 restore_flags (flags);
761 }
762
763 return TIMER_ARMED;
764 }
765 break;
766
767 case TMR_START:
768 seq_time = jiffies;
769 prev_input_time = 0;
770 prev_event_time = 0;
771 break;
772
773 case TMR_STOP:
774 break;
775
776 case TMR_CONTINUE:
777 break;
778
779 case TMR_TEMPO:
780 break;
781
782 case TMR_ECHO:
783 if (seq_mode == SEQ_2)
784 seq_copy_to_input (event_rec, 8);
785 else
786 {
787 parm = (parm << 8 | SEQ_ECHO);
788 seq_copy_to_input ((unsigned char *) &parm, 4);
789 }
790 break;
791
792 default:;
793 }
794
795 return TIMER_NOT_ARMED;
796 }
797
798 static void
799 seq_local_event (unsigned char *event_rec)
800 {
801 unsigned char cmd = event_rec[1];
802 unsigned int parm = *((unsigned int *) &event_rec[4]);
803
804 switch (cmd)
805 {
806 case LOCL_STARTAUDIO:
807 #ifdef CONFIG_AUDIO
808 DMAbuf_start_devices (parm);
809 #endif
810 break;
811
812 default:;
813 }
814 }
815
816 static void
817 seq_sysex_message (unsigned char *event_rec)
818 {
819 int dev = event_rec[1];
820 int i, l = 0;
821 unsigned char *buf = &event_rec[2];
822
823 if ((int) dev > max_synthdev)
824 return;
825 if (!(synth_open_mask & (1 << dev)))
826 return;
827 if (!synth_devs[dev])
828 return;
829 if (!synth_devs[dev]->send_sysex)
830 return;
831
832 l = 0;
833 for (i = 0; i < 6 && buf[i] != 0xff; i++)
834 l = i + 1;
835
836 if (l > 0)
837 synth_devs[dev]->send_sysex (dev, buf, l);
838 }
839
840 static int
841 play_event (unsigned char *q)
842 {
843
844
845
846
847
848
849 unsigned int *delay;
850
851 switch (q[0])
852 {
853 case SEQ_NOTEOFF:
854 if (synth_open_mask & (1 << 0))
855 if (synth_devs[0])
856 synth_devs[0]->kill_note (0, q[1], 255, q[3]);
857 break;
858
859 case SEQ_NOTEON:
860 if (q[4] < 128 || q[4] == 255)
861 if (synth_open_mask & (1 << 0))
862 if (synth_devs[0])
863 synth_devs[0]->start_note (0, q[1], q[2], q[3]);
864 break;
865
866 case SEQ_WAIT:
867 delay = (unsigned int *) q;
868
869
870
871 *delay = (*delay >> 8) & 0xffffff;
872
873 if (*delay > 0)
874 {
875 long time;
876
877 seq_playing = 1;
878 time = *delay;
879 prev_event_time = time;
880
881 request_sound_timer (time);
882
883 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
884 {
885 unsigned long flags;
886
887 save_flags (flags);
888 cli ();
889 if ((seq_sleep_flag.mode & WK_SLEEP))
890 {
891 {
892 seq_sleep_flag.mode = WK_WAKEUP;
893 module_wake_up (&seq_sleeper);
894 };
895 }
896 restore_flags (flags);
897 }
898
899
900
901
902 return 1;
903 }
904 break;
905
906 case SEQ_PGMCHANGE:
907 if (synth_open_mask & (1 << 0))
908 if (synth_devs[0])
909 synth_devs[0]->set_instr (0, q[1], q[2]);
910 break;
911
912 case SEQ_SYNCTIMER:
913
914
915 seq_time = jiffies;
916 prev_input_time = 0;
917 prev_event_time = 0;
918 break;
919
920 case SEQ_MIDIPUTC:
921
922
923 if (midi_opened[q[2]])
924 {
925 int dev;
926
927 dev = q[2];
928
929 if (!midi_devs[dev]->putc (dev, q[1]))
930 {
931
932
933
934
935 seq_playing = 1;
936 request_sound_timer (-1);
937 return 2;
938 }
939 else
940 midi_written[dev] = 1;
941 }
942 break;
943
944 case SEQ_ECHO:
945 seq_copy_to_input (q, 4);
946
947
948 break;
949
950 case SEQ_PRIVATE:
951 if ((int) q[1] < max_synthdev)
952 synth_devs[q[1]]->hw_control (q[1], q);
953 break;
954
955 case SEQ_EXTENDED:
956 extended_event (q);
957 break;
958
959 case EV_CHN_VOICE:
960 seq_chn_voice_event (q);
961 break;
962
963 case EV_CHN_COMMON:
964 seq_chn_common_event (q);
965 break;
966
967 case EV_TIMING:
968 if (seq_timing_event (q) == TIMER_ARMED)
969 {
970 return 1;
971 }
972 break;
973
974 case EV_SEQ_LOCAL:
975 seq_local_event (q);
976 break;
977
978 case EV_SYSEX:
979 seq_sysex_message (q);
980 break;
981
982 default:;
983 }
984
985 return 0;
986 }
987
988 static void
989 seq_startplay (void)
990 {
991 unsigned long flags;
992 int this_one, action;
993
994 while (qlen > 0)
995 {
996
997 save_flags (flags);
998 cli ();
999 qhead = ((this_one = qhead) + 1) % SEQ_MAX_QUEUE;
1000 qlen--;
1001 restore_flags (flags);
1002
1003 seq_playing = 1;
1004
1005 if ((action = play_event (&queue[this_one * EV_SZ])))
1006 {
1007 if (action == 2)
1008 {
1009 qlen++;
1010 qhead = this_one;
1011 }
1012 return;
1013 }
1014
1015 }
1016
1017 seq_playing = 0;
1018
1019 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
1020 {
1021 unsigned long flags;
1022
1023 save_flags (flags);
1024 cli ();
1025 if ((seq_sleep_flag.mode & WK_SLEEP))
1026 {
1027 {
1028 seq_sleep_flag.mode = WK_WAKEUP;
1029 module_wake_up (&seq_sleeper);
1030 };
1031 }
1032 restore_flags (flags);
1033 }
1034 }
1035
1036 static void
1037 reset_controllers (int dev, unsigned char *controller, int update_dev)
1038 {
1039
1040 int i;
1041
1042 for (i = 0; i < 128; i++)
1043 controller[i] = ctrl_def_values[i];
1044 }
1045
1046 static void
1047 setup_mode2 (void)
1048 {
1049 int dev;
1050
1051 max_synthdev = num_synths;
1052
1053 for (dev = 0; dev < num_midis; dev++)
1054 if (midi_devs[dev]->converter != NULL)
1055 {
1056 synth_devs[max_synthdev++] =
1057 midi_devs[dev]->converter;
1058 }
1059
1060 for (dev = 0; dev < max_synthdev; dev++)
1061 {
1062 int chn;
1063
1064 for (chn = 0; chn < 16; chn++)
1065 {
1066 synth_devs[dev]->chn_info[chn].pgm_num = 0;
1067 reset_controllers (dev,
1068 synth_devs[dev]->chn_info[chn].controllers,
1069 0);
1070 synth_devs[dev]->chn_info[chn].bender_value = (1 << 7);
1071 }
1072 }
1073
1074 max_mididev = 0;
1075 seq_mode = SEQ_2;
1076 }
1077
1078 int
1079 sequencer_open (int dev, struct fileinfo *file)
1080 {
1081 int retval, mode, i;
1082 int level, tmp;
1083 unsigned long flags;
1084
1085 level = ((dev & 0x0f) == SND_DEV_SEQ2) ? 2 : 1;
1086
1087 dev = dev >> 4;
1088 mode = file->mode & O_ACCMODE;
1089
1090 DEB (printk ("sequencer_open(dev=%d)\n", dev));
1091
1092 if (!sequencer_ok)
1093 {
1094 printk ("Soundcard: Sequencer not initialized\n");
1095 return -ENXIO;
1096 }
1097
1098 if (dev)
1099 {
1100 printk ("Patch manager interface is currently broken. Sorry\n");
1101 return -ENXIO;
1102 }
1103
1104 save_flags (flags);
1105 cli ();
1106 if (sequencer_busy)
1107 {
1108 printk ("Sequencer busy\n");
1109 restore_flags (flags);
1110 return -EBUSY;
1111 }
1112 sequencer_busy = 1;
1113 restore_flags (flags);
1114
1115 max_mididev = num_midis;
1116 max_synthdev = num_synths;
1117 pre_event_timeout = 0;
1118 seq_mode = SEQ_1;
1119
1120 if (pending_timer != -1)
1121 {
1122 tmr_no = pending_timer;
1123 pending_timer = -1;
1124 }
1125
1126 if (tmr_no == -1)
1127 {
1128 int i, best;
1129
1130 best = -1;
1131 for (i = 0; i < num_sound_timers; i++)
1132 if (sound_timer_devs[i]->priority > best)
1133 {
1134 tmr_no = i;
1135 best = sound_timer_devs[i]->priority;
1136 }
1137
1138 if (tmr_no == -1)
1139 tmr_no = 0;
1140 }
1141
1142 tmr = sound_timer_devs[tmr_no];
1143
1144 if (level == 2)
1145 {
1146 if (tmr == NULL)
1147 {
1148 printk ("sequencer: No timer for level 2\n");
1149 sequencer_busy = 0;
1150 return -ENXIO;
1151 }
1152 setup_mode2 ();
1153 }
1154
1155 if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
1156 if (!max_mididev)
1157 {
1158 printk ("Sequencer: No Midi devices. Input not possible\n");
1159 sequencer_busy = 0;
1160 return -ENXIO;
1161 }
1162
1163 if (!max_synthdev && !max_mididev)
1164 return -ENXIO;
1165
1166 synth_open_mask = 0;
1167
1168 for (i = 0; i < max_mididev; i++)
1169 {
1170 midi_opened[i] = 0;
1171 midi_written[i] = 0;
1172 }
1173
1174
1175
1176
1177 for (i = 0; i < max_synthdev; i++)
1178
1179
1180 if ((tmp = synth_devs[i]->open (i, mode)) < 0)
1181 {
1182 printk ("Sequencer: Warning! Cannot open synth device #%d (%d)\n", i, tmp);
1183 if (synth_devs[i]->midi_dev)
1184 printk ("(Maps to MIDI dev #%d)\n", synth_devs[i]->midi_dev);
1185 }
1186 else
1187 {
1188 synth_open_mask |= (1 << i);
1189 if (synth_devs[i]->midi_dev)
1190
1191
1192 midi_opened[synth_devs[i]->midi_dev] = 1;
1193 }
1194
1195 seq_time = jiffies;
1196 prev_input_time = 0;
1197 prev_event_time = 0;
1198
1199 if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
1200 {
1201
1202
1203 for (i = 0; i < max_mididev; i++)
1204 if (!midi_opened[i])
1205 {
1206 if ((retval = midi_devs[i]->open (i, mode,
1207 sequencer_midi_input, sequencer_midi_output)) >= 0)
1208 midi_opened[i] = 1;
1209 }
1210 }
1211
1212 if (seq_mode == SEQ_2)
1213 {
1214 tmr->open (tmr_no, seq_mode);
1215 }
1216
1217 seq_sleep_flag.mode = WK_NONE;
1218 midi_sleep_flag.mode = WK_NONE;
1219 output_treshold = SEQ_MAX_QUEUE / 2;
1220
1221 for (i = 0; i < num_synths; i++)
1222 if (pmgr_present[i])
1223 pmgr_inform (i, PM_E_OPENED, 0, 0, 0, 0);
1224
1225 return 0;
1226 }
1227
1228 void
1229 seq_drain_midi_queues (void)
1230 {
1231 int i, n;
1232
1233
1234
1235
1236
1237 n = 1;
1238
1239 while (!current_got_fatal_signal () && n)
1240 {
1241 n = 0;
1242
1243 for (i = 0; i < max_mididev; i++)
1244 if (midi_opened[i] && midi_written[i])
1245 if (midi_devs[i]->buffer_status != NULL)
1246 if (midi_devs[i]->buffer_status (i))
1247 n++;
1248
1249
1250
1251
1252 if (n)
1253 {
1254
1255 {
1256 unsigned long tl;
1257
1258 if (HZ / 10)
1259 current_set_timeout (tl = jiffies + (HZ / 10));
1260 else
1261 tl = (unsigned long) -1;
1262 seq_sleep_flag.mode = WK_SLEEP;
1263 module_interruptible_sleep_on (&seq_sleeper);
1264 if (!(seq_sleep_flag.mode & WK_WAKEUP))
1265 {
1266 if (jiffies >= tl)
1267 seq_sleep_flag.mode |= WK_TIMEOUT;
1268 }
1269 seq_sleep_flag.mode &= ~WK_SLEEP;
1270 };
1271 }
1272 }
1273 }
1274
1275 void
1276 sequencer_release (int dev, struct fileinfo *file)
1277 {
1278 int i;
1279 int mode = file->mode & O_ACCMODE;
1280
1281 dev = dev >> 4;
1282
1283 DEB (printk ("sequencer_release(dev=%d)\n", dev));
1284
1285 if (dev)
1286
1287
1288 {
1289 dev--;
1290 pmgr_release (dev);
1291 pmgr_present[dev] = 0;
1292 return;
1293 }
1294
1295
1296
1297
1298
1299 if (mode != OPEN_READ && !(file->flags & (O_NONBLOCK) ?
1300 1 : 0))
1301 while (!current_got_fatal_signal () && qlen)
1302 {
1303 seq_sync ();
1304 }
1305
1306 if (mode != OPEN_READ)
1307 seq_drain_midi_queues ();
1308
1309
1310 seq_reset ();
1311 if (mode != OPEN_READ)
1312 seq_drain_midi_queues ();
1313
1314
1315
1316 for (i = 0; i < max_synthdev; i++)
1317 if (synth_open_mask & (1 << i))
1318
1319
1320 if (synth_devs[i])
1321 {
1322 synth_devs[i]->close (i);
1323
1324 if (synth_devs[i]->midi_dev)
1325 midi_opened[synth_devs[i]->midi_dev] = 0;
1326 }
1327
1328 for (i = 0; i < num_synths; i++)
1329 if (pmgr_present[i])
1330 pmgr_inform (i, PM_E_CLOSED, 0, 0, 0, 0);
1331
1332 for (i = 0; i < max_mididev; i++)
1333 if (midi_opened[i])
1334 midi_devs[i]->close (i);
1335
1336 if (seq_mode == SEQ_2)
1337 tmr->close (tmr_no);
1338
1339 sequencer_busy = 0;
1340 }
1341
1342 static int
1343 seq_sync (void)
1344 {
1345 unsigned long flags;
1346
1347 if (qlen && !seq_playing && !current_got_fatal_signal ())
1348 seq_startplay ();
1349
1350 save_flags (flags);
1351 cli ();
1352 if (qlen && !(seq_sleep_flag.mode & WK_SLEEP))
1353 {
1354
1355 {
1356 unsigned long tl;
1357
1358 if (HZ)
1359 current_set_timeout (tl = jiffies + (HZ));
1360 else
1361 tl = (unsigned long) -1;
1362 seq_sleep_flag.mode = WK_SLEEP;
1363 module_interruptible_sleep_on (&seq_sleeper);
1364 if (!(seq_sleep_flag.mode & WK_WAKEUP))
1365 {
1366 if (jiffies >= tl)
1367 seq_sleep_flag.mode |= WK_TIMEOUT;
1368 }
1369 seq_sleep_flag.mode &= ~WK_SLEEP;
1370 };
1371 }
1372 restore_flags (flags);
1373
1374 return qlen;
1375 }
1376
1377 static void
1378 midi_outc (int dev, unsigned char data)
1379 {
1380
1381
1382
1383
1384 int n;
1385 unsigned long flags;
1386
1387
1388
1389
1390
1391
1392
1393 n = 3 * HZ;
1394
1395 save_flags (flags);
1396 cli ();
1397 while (n && !midi_devs[dev]->putc (dev, data))
1398 {
1399
1400 {
1401 unsigned long tl;
1402
1403 if (4)
1404 current_set_timeout (tl = jiffies + (4));
1405 else
1406 tl = (unsigned long) -1;
1407 seq_sleep_flag.mode = WK_SLEEP;
1408 module_interruptible_sleep_on (&seq_sleeper);
1409 if (!(seq_sleep_flag.mode & WK_WAKEUP))
1410 {
1411 if (jiffies >= tl)
1412 seq_sleep_flag.mode |= WK_TIMEOUT;
1413 }
1414 seq_sleep_flag.mode &= ~WK_SLEEP;
1415 };
1416 n--;
1417 }
1418 restore_flags (flags);
1419 }
1420
1421 static void
1422 seq_reset (void)
1423 {
1424
1425
1426
1427
1428 int i;
1429 int chn;
1430 unsigned long flags;
1431
1432 sound_stop_timer ();
1433 seq_time = jiffies;
1434 prev_input_time = 0;
1435 prev_event_time = 0;
1436
1437 qlen = qhead = qtail = 0;
1438 iqlen = iqhead = iqtail = 0;
1439
1440 for (i = 0; i < max_synthdev; i++)
1441 if (synth_open_mask & (1 << i))
1442 if (synth_devs[i])
1443 synth_devs[i]->reset (i);
1444
1445 if (seq_mode == SEQ_2)
1446 {
1447
1448 for (chn = 0; chn < 16; chn++)
1449 for (i = 0; i < max_synthdev; i++)
1450 if (synth_open_mask & (1 << i))
1451 if (synth_devs[i])
1452 {
1453 synth_devs[i]->controller (i, chn, 123, 0);
1454 synth_devs[i]->controller (i, chn, 121, 0);
1455 synth_devs[i]->bender (i, chn, 1 << 13);
1456 }
1457
1458 }
1459 else
1460
1461 {
1462 for (i = 0; i < max_mididev; i++)
1463 if (midi_written[i])
1464
1465
1466 {
1467
1468
1469
1470
1471
1472 midi_outc (i, 0xfe);
1473
1474 for (chn = 0; chn < 16; chn++)
1475 {
1476 midi_outc (i,
1477 (unsigned char) (0xb0 + (chn & 0x0f)));
1478 midi_outc (i, 0x7b);
1479 midi_outc (i, 0);
1480 }
1481
1482 midi_devs[i]->close (i);
1483
1484 midi_written[i] = 0;
1485 midi_opened[i] = 0;
1486 }
1487 }
1488
1489 seq_playing = 0;
1490
1491 save_flags (flags);
1492 cli ();
1493 if ((seq_sleep_flag.mode & WK_SLEEP))
1494 {
1495
1496 {
1497 seq_sleep_flag.mode = WK_WAKEUP;
1498 module_wake_up (&seq_sleeper);
1499 };
1500 }
1501 restore_flags (flags);
1502
1503 }
1504
1505 static void
1506 seq_panic (void)
1507 {
1508
1509
1510
1511
1512
1513 seq_reset ();
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525 }
1526
1527 int
1528 sequencer_ioctl (int dev, struct fileinfo *file,
1529 unsigned int cmd, caddr_t arg)
1530 {
1531 int midi_dev, orig_dev;
1532 int mode = file->mode & O_ACCMODE;
1533
1534 orig_dev = dev = dev >> 4;
1535
1536 switch (cmd)
1537 {
1538 case SNDCTL_TMR_TIMEBASE:
1539 case SNDCTL_TMR_TEMPO:
1540 case SNDCTL_TMR_START:
1541 case SNDCTL_TMR_STOP:
1542 case SNDCTL_TMR_CONTINUE:
1543 case SNDCTL_TMR_METRONOME:
1544 case SNDCTL_TMR_SOURCE:
1545 if (dev)
1546 return -EIO;
1547
1548 if (seq_mode != SEQ_2)
1549 return -EINVAL;
1550 return tmr->ioctl (tmr_no, cmd, arg);
1551 break;
1552
1553 case SNDCTL_TMR_SELECT:
1554 if (dev)
1555 return -EIO;
1556
1557 if (seq_mode != SEQ_2)
1558 return -EINVAL;
1559 pending_timer = get_fs_long ((long *) arg);
1560
1561 if (pending_timer < 0 || pending_timer >= num_sound_timers)
1562 {
1563 pending_timer = -1;
1564 return -EINVAL;
1565 }
1566
1567 return snd_ioctl_return ((int *) arg, pending_timer);
1568 break;
1569
1570 case SNDCTL_SEQ_PANIC:
1571 seq_panic ();
1572 break;
1573
1574 case SNDCTL_SEQ_SYNC:
1575 if (dev)
1576
1577
1578 return -EIO;
1579
1580 if (mode == OPEN_READ)
1581 return 0;
1582 while (qlen && !current_got_fatal_signal ())
1583 seq_sync ();
1584 if (qlen)
1585 return -EINTR;
1586 else
1587 return 0;
1588 break;
1589
1590 case SNDCTL_SEQ_RESET:
1591 if (dev)
1592
1593
1594 return -EIO;
1595
1596 seq_reset ();
1597 return 0;
1598 break;
1599
1600 case SNDCTL_SEQ_TESTMIDI:
1601 if (dev)
1602
1603
1604 return -EIO;
1605
1606 midi_dev = get_fs_long ((long *) arg);
1607 if (midi_dev >= max_mididev)
1608 return -ENXIO;
1609
1610 if (!midi_opened[midi_dev])
1611 {
1612 int err, mode;
1613
1614 mode = file->mode & O_ACCMODE;
1615 if ((err = midi_devs[midi_dev]->open (midi_dev, mode,
1616 sequencer_midi_input,
1617 sequencer_midi_output)) < 0)
1618 return err;
1619 }
1620
1621 midi_opened[midi_dev] = 1;
1622
1623 return 0;
1624 break;
1625
1626 case SNDCTL_SEQ_GETINCOUNT:
1627 if (dev)
1628
1629
1630 return -EIO;
1631
1632 if (mode == OPEN_WRITE)
1633 return 0;
1634 return snd_ioctl_return ((int *) arg, iqlen);
1635 break;
1636
1637 case SNDCTL_SEQ_GETOUTCOUNT:
1638
1639 if (mode == OPEN_READ)
1640 return 0;
1641 return snd_ioctl_return ((int *) arg, SEQ_MAX_QUEUE - qlen);
1642 break;
1643
1644 case SNDCTL_SEQ_CTRLRATE:
1645 if (dev)
1646 return -EIO;
1647
1648
1649
1650
1651 if (seq_mode == SEQ_2)
1652 return tmr->ioctl (tmr_no, cmd, arg);
1653
1654 if (get_fs_long ((long *) arg) != 0)
1655 return -EINVAL;
1656
1657 return snd_ioctl_return ((int *) arg, HZ);
1658 break;
1659
1660 case SNDCTL_SEQ_RESETSAMPLES:
1661 {
1662 int err;
1663
1664 dev = get_fs_long ((long *) arg);
1665 if (dev < 0 || dev >= num_synths)
1666 {
1667 return -ENXIO;
1668 }
1669
1670 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1671 {
1672 return -EBUSY;
1673 }
1674
1675 if (!orig_dev && pmgr_present[dev])
1676 pmgr_inform (dev, PM_E_PATCH_RESET, 0, 0, 0, 0);
1677
1678 err = synth_devs[dev]->ioctl (dev, cmd, arg);
1679 return err;
1680 }
1681 break;
1682
1683 case SNDCTL_SEQ_NRSYNTHS:
1684 return snd_ioctl_return ((int *) arg, max_synthdev);
1685 break;
1686
1687 case SNDCTL_SEQ_NRMIDIS:
1688 return snd_ioctl_return ((int *) arg, max_mididev);
1689 break;
1690
1691 case SNDCTL_SYNTH_MEMAVL:
1692 {
1693 int dev = get_fs_long ((long *) arg);
1694
1695 if (dev < 0 || dev >= num_synths)
1696 return -ENXIO;
1697
1698 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1699 return -EBUSY;
1700
1701 return snd_ioctl_return ((int *) arg, synth_devs[dev]->ioctl (dev, cmd, arg));
1702 }
1703 break;
1704
1705 case SNDCTL_FM_4OP_ENABLE:
1706 {
1707 int dev = get_fs_long ((long *) arg);
1708
1709 if (dev < 0 || dev >= num_synths)
1710 return -ENXIO;
1711
1712 if (!(synth_open_mask & (1 << dev)))
1713 return -ENXIO;
1714
1715 synth_devs[dev]->ioctl (dev, cmd, arg);
1716 return 0;
1717 }
1718 break;
1719
1720 case SNDCTL_SYNTH_INFO:
1721 {
1722 struct synth_info inf;
1723 int dev;
1724
1725 memcpy_fromfs ((char *) &inf, &(((char *) arg)[0]), sizeof (inf));
1726 dev = inf.device;
1727
1728 if (dev < 0 || dev >= max_synthdev)
1729 return -ENXIO;
1730
1731 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1732 return -EBUSY;
1733
1734 return synth_devs[dev]->ioctl (dev, cmd, arg);
1735 }
1736 break;
1737
1738 case SNDCTL_SEQ_OUTOFBAND:
1739 {
1740 struct seq_event_rec event_rec;
1741 unsigned long flags;
1742
1743 memcpy_fromfs ((char *) &event_rec, &(((char *) arg)[0]), sizeof (event_rec));
1744
1745 save_flags (flags);
1746 cli ();
1747 play_event (event_rec.arr);
1748 restore_flags (flags);
1749
1750 return 0;
1751 }
1752 break;
1753
1754 case SNDCTL_MIDI_INFO:
1755 {
1756 struct midi_info inf;
1757 int dev;
1758
1759 memcpy_fromfs ((char *) &inf, &(((char *) arg)[0]), sizeof (inf));
1760 dev = inf.device;
1761
1762 if (dev < 0 || dev >= max_mididev)
1763 return -ENXIO;
1764
1765 memcpy_tofs ((&((char *) arg)[0]), (char *) &(midi_devs[dev]->info), sizeof (inf));
1766 return 0;
1767 }
1768 break;
1769
1770 case SNDCTL_PMGR_IFACE:
1771 {
1772 struct patmgr_info *inf;
1773 int dev, err;
1774
1775 if ((inf = (struct patmgr_info *) kmalloc (sizeof (*inf), GFP_KERNEL)) == NULL)
1776 {
1777 printk ("patmgr: Can't allocate memory for a message\n");
1778 return -EIO;
1779 }
1780
1781 memcpy_fromfs ((char *) inf, &(((char *) arg)[0]), sizeof (*inf));
1782 dev = inf->device;
1783
1784 if (dev < 0 || dev >= num_synths)
1785 {
1786 kfree (inf);
1787 return -ENXIO;
1788 }
1789
1790 if (!synth_devs[dev]->pmgr_interface)
1791 {
1792 kfree (inf);
1793 return -ENXIO;
1794 }
1795
1796 if ((err = synth_devs[dev]->pmgr_interface (dev, inf)) == -1)
1797 {
1798 kfree (inf);
1799 return err;
1800 }
1801
1802 memcpy_tofs ((&((char *) arg)[0]), (char *) inf, sizeof (*inf));
1803 kfree (inf);
1804 return 0;
1805 }
1806 break;
1807
1808 case SNDCTL_PMGR_ACCESS:
1809 {
1810 struct patmgr_info *inf;
1811 int dev, err;
1812
1813 if ((inf = (struct patmgr_info *) kmalloc (sizeof (*inf), GFP_KERNEL)) == NULL)
1814 {
1815 printk ("patmgr: Can't allocate memory for a message\n");
1816 return -EIO;
1817 }
1818
1819 memcpy_fromfs ((char *) inf, &(((char *) arg)[0]), sizeof (*inf));
1820 dev = inf->device;
1821
1822 if (dev < 0 || dev >= num_synths)
1823 {
1824 kfree (inf);
1825 return -ENXIO;
1826 }
1827
1828 if (!pmgr_present[dev])
1829 {
1830 kfree (inf);
1831 return -ESRCH;
1832 }
1833
1834 if ((err = pmgr_access (dev, inf)) < 0)
1835 {
1836 kfree (inf);
1837 return err;
1838 }
1839
1840 memcpy_tofs ((&((char *) arg)[0]), (char *) inf, sizeof (*inf));
1841 kfree (inf);
1842 return 0;
1843 }
1844 break;
1845
1846 case SNDCTL_SEQ_THRESHOLD:
1847 {
1848 int tmp = get_fs_long ((long *) arg);
1849
1850 if (dev)
1851
1852
1853 return -EIO;
1854
1855 if (tmp < 1)
1856 tmp = 1;
1857 if (tmp >= SEQ_MAX_QUEUE)
1858 tmp = SEQ_MAX_QUEUE - 1;
1859 output_treshold = tmp;
1860 return 0;
1861 }
1862 break;
1863
1864 case SNDCTL_MIDI_PRETIME:
1865 {
1866 int val = get_fs_long ((long *) arg);
1867
1868 if (val < 0)
1869 val = 0;
1870
1871 val = (HZ * val) / 10;
1872 pre_event_timeout = val;
1873 return snd_ioctl_return ((int *) arg, val);
1874 }
1875 break;
1876
1877 default:
1878 if (dev)
1879
1880
1881 return -EIO;
1882
1883 if (mode == OPEN_READ)
1884 return -EIO;
1885
1886 if (!synth_devs[0])
1887 return -ENXIO;
1888 if (!(synth_open_mask & (1 << 0)))
1889 return -ENXIO;
1890 return synth_devs[0]->ioctl (0, cmd, arg);
1891 break;
1892 }
1893
1894 return -EINVAL;
1895 }
1896
1897 int
1898 sequencer_select (int dev, struct fileinfo *file, int sel_type, select_table_handle * wait)
1899 {
1900 unsigned long flags;
1901
1902 dev = dev >> 4;
1903
1904 switch (sel_type)
1905 {
1906 case SEL_IN:
1907 save_flags (flags);
1908 cli ();
1909 if (!iqlen)
1910 {
1911 midi_sleep_flag.mode = WK_SLEEP;
1912 module_select_wait (&midi_sleeper, wait);
1913 restore_flags (flags);
1914 return 0;
1915 }
1916 midi_sleep_flag.mode &= ~WK_SLEEP;
1917 restore_flags (flags);
1918 return 1;
1919 break;
1920
1921 case SEL_OUT:
1922 save_flags (flags);
1923 cli ();
1924 if ((SEQ_MAX_QUEUE - qlen) < output_treshold)
1925 {
1926 seq_sleep_flag.mode = WK_SLEEP;
1927 module_select_wait (&seq_sleeper, wait);
1928 restore_flags (flags);
1929 return 0;
1930 }
1931 seq_sleep_flag.mode &= ~WK_SLEEP;
1932 restore_flags (flags);
1933 return 1;
1934 break;
1935
1936 case SEL_EX:
1937 return 0;
1938 }
1939
1940 return 0;
1941 }
1942
1943
1944 void
1945 sequencer_timer (unsigned long dummy)
1946 {
1947 seq_startplay ();
1948 }
1949
1950 int
1951 note_to_freq (int note_num)
1952 {
1953
1954
1955
1956
1957
1958 int note, octave, note_freq;
1959 int notes[] =
1960 {
1961 261632, 277189, 293671, 311132, 329632, 349232,
1962 369998, 391998, 415306, 440000, 466162, 493880
1963 };
1964
1965 #define BASE_OCTAVE 5
1966
1967 octave = note_num / 12;
1968 note = note_num % 12;
1969
1970 note_freq = notes[note];
1971
1972 if (octave < BASE_OCTAVE)
1973 note_freq >>= (BASE_OCTAVE - octave);
1974 else if (octave > BASE_OCTAVE)
1975 note_freq <<= (octave - BASE_OCTAVE);
1976
1977
1978
1979
1980
1981 return note_freq;
1982 }
1983
1984 unsigned long
1985 compute_finetune (unsigned long base_freq, int bend, int range)
1986 {
1987 unsigned long amount;
1988 int negative, semitones, cents, multiplier = 1;
1989
1990 if (!bend)
1991 return base_freq;
1992 if (!range)
1993 return base_freq;
1994
1995 if (!base_freq)
1996 return base_freq;
1997
1998 if (range >= 8192)
1999 range = 8192;
2000
2001 bend = bend * range / 8192;
2002 if (!bend)
2003 return base_freq;
2004
2005 negative = bend < 0 ? 1 : 0;
2006
2007 if (bend < 0)
2008 bend *= -1;
2009 if (bend > range)
2010 bend = range;
2011
2012
2013
2014
2015
2016 while (bend > 2399)
2017 {
2018 multiplier *= 4;
2019 bend -= 2400;
2020 }
2021
2022 semitones = bend / 100;
2023 cents = bend % 100;
2024
2025 amount = (int) (semitone_tuning[semitones] * multiplier * cent_tuning[cents])
2026 / 10000;
2027
2028 if (negative)
2029 return (base_freq * 10000) / amount;
2030 else
2031 return (base_freq * amount) / 10000;
2032 }
2033
2034
2035 long
2036 sequencer_init (long mem_start)
2037 {
2038
2039
2040 queue = (unsigned char *) (sound_mem_blocks[sound_num_blocks] = kmalloc (SEQ_MAX_QUEUE * EV_SZ, GFP_KERNEL));
2041 if (sound_num_blocks < 1024)
2042 sound_num_blocks++;;
2043 if (queue == NULL)
2044 {
2045 printk ("Sound: Can't allocate memory for sequencer output queue\n");
2046 return mem_start;
2047 }
2048
2049
2050 iqueue = (unsigned char *) (sound_mem_blocks[sound_num_blocks] = kmalloc (SEQ_MAX_QUEUE * IEV_SZ, GFP_KERNEL));
2051 if (sound_num_blocks < 1024)
2052 sound_num_blocks++;;
2053 if (queue == NULL)
2054 {
2055 printk ("Sound: Can't allocate memory for sequencer input queue\n");
2056 return mem_start;
2057 }
2058
2059 sequencer_ok = 1;
2060
2061 return mem_start;
2062 }
2063
2064 #endif