forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequencer.c
1669 lines (1375 loc) · 33.6 KB
/
sequencer.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* sound/oss/sequencer.c
*
* The sequencer personality manager.
*/
/*
* Copyright (C) by Hannu Savolainen 1993-1997
*
* OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
* Version 2 (June 1991). See the "COPYING" file distributed with this software
* for more info.
*/
/*
* Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
* Alan Cox : reformatted and fixed a pair of null pointer bugs
*/
#include <linux/kmod.h>
#include <linux/spinlock.h>
#include "sound_config.h"
#include "midi_ctrl.h"
#include "sleep.h"
static int sequencer_ok;
static struct sound_timer_operations *tmr;
static int tmr_no = -1; /* Currently selected timer */
static int pending_timer = -1; /* For timer change operation */
extern unsigned long seq_time;
static int obsolete_api_used;
static DEFINE_SPINLOCK(lock);
/*
* Local counts for number of synth and MIDI devices. These are initialized
* by the sequencer_open.
*/
static int max_mididev;
static int max_synthdev;
/*
* The seq_mode gives the operating mode of the sequencer:
* 1 = level1 (the default)
* 2 = level2 (extended capabilities)
*/
#define SEQ_1 1
#define SEQ_2 2
static int seq_mode = SEQ_1;
static DECLARE_WAIT_QUEUE_HEAD(seq_sleeper);
static DECLARE_WAIT_QUEUE_HEAD(midi_sleeper);
static int midi_opened[MAX_MIDI_DEV];
static int midi_written[MAX_MIDI_DEV];
static unsigned long prev_input_time;
static int prev_event_time;
#include "tuning.h"
#define EV_SZ 8
#define IEV_SZ 8
static unsigned char *queue;
static unsigned char *iqueue;
static volatile int qhead, qtail, qlen;
static volatile int iqhead, iqtail, iqlen;
static volatile int seq_playing;
static volatile int sequencer_busy;
static int output_threshold;
static long pre_event_timeout;
static unsigned synth_open_mask;
static int seq_queue(unsigned char *note, char nonblock);
static void seq_startplay(void);
static int seq_sync(void);
static void seq_reset(void);
#if MAX_SYNTH_DEV > 15
#error Too many synthesizer devices enabled.
#endif
int sequencer_read(int dev, struct file *file, char __user *buf, int count)
{
int c = count, p = 0;
int ev_len;
unsigned long flags;
dev = dev >> 4;
ev_len = seq_mode == SEQ_1 ? 4 : 8;
spin_lock_irqsave(&lock,flags);
if (!iqlen)
{
spin_unlock_irqrestore(&lock,flags);
if (file->f_flags & O_NONBLOCK) {
return -EAGAIN;
}
oss_broken_sleep_on(&midi_sleeper, pre_event_timeout);
spin_lock_irqsave(&lock,flags);
if (!iqlen)
{
spin_unlock_irqrestore(&lock,flags);
return 0;
}
}
while (iqlen && c >= ev_len)
{
char *fixit = (char *) &iqueue[iqhead * IEV_SZ];
spin_unlock_irqrestore(&lock,flags);
if (copy_to_user(&(buf)[p], fixit, ev_len))
return count - c;
p += ev_len;
c -= ev_len;
spin_lock_irqsave(&lock,flags);
iqhead = (iqhead + 1) % SEQ_MAX_QUEUE;
iqlen--;
}
spin_unlock_irqrestore(&lock,flags);
return count - c;
}
static void sequencer_midi_output(int dev)
{
/*
* Currently NOP
*/
}
void seq_copy_to_input(unsigned char *event_rec, int len)
{
unsigned long flags;
/*
* Verify that the len is valid for the current mode.
*/
if (len != 4 && len != 8)
return;
if ((seq_mode == SEQ_1) != (len == 4))
return;
if (iqlen >= (SEQ_MAX_QUEUE - 1))
return; /* Overflow */
spin_lock_irqsave(&lock,flags);
memcpy(&iqueue[iqtail * IEV_SZ], event_rec, len);
iqlen++;
iqtail = (iqtail + 1) % SEQ_MAX_QUEUE;
wake_up(&midi_sleeper);
spin_unlock_irqrestore(&lock,flags);
}
EXPORT_SYMBOL(seq_copy_to_input);
static void sequencer_midi_input(int dev, unsigned char data)
{
unsigned int tstamp;
unsigned char event_rec[4];
if (data == 0xfe) /* Ignore active sensing */
return;
tstamp = jiffies - seq_time;
if (tstamp != prev_input_time)
{
tstamp = (tstamp << 8) | SEQ_WAIT;
seq_copy_to_input((unsigned char *) &tstamp, 4);
prev_input_time = tstamp;
}
event_rec[0] = SEQ_MIDIPUTC;
event_rec[1] = data;
event_rec[2] = dev;
event_rec[3] = 0;
seq_copy_to_input(event_rec, 4);
}
void seq_input_event(unsigned char *event_rec, int len)
{
unsigned long this_time;
if (seq_mode == SEQ_2)
this_time = tmr->get_time(tmr_no);
else
this_time = jiffies - seq_time;
if (this_time != prev_input_time)
{
unsigned char tmp_event[8];
tmp_event[0] = EV_TIMING;
tmp_event[1] = TMR_WAIT_ABS;
tmp_event[2] = 0;
tmp_event[3] = 0;
*(unsigned int *) &tmp_event[4] = this_time;
seq_copy_to_input(tmp_event, 8);
prev_input_time = this_time;
}
seq_copy_to_input(event_rec, len);
}
EXPORT_SYMBOL(seq_input_event);
int sequencer_write(int dev, struct file *file, const char __user *buf, int count)
{
unsigned char event_rec[EV_SZ], ev_code;
int p = 0, c, ev_size;
int mode = translate_mode(file);
dev = dev >> 4;
if (mode == OPEN_READ)
return -EIO;
c = count;
while (c >= 4)
{
if (copy_from_user((char *) event_rec, &(buf)[p], 4))
goto out;
ev_code = event_rec[0];
if (ev_code == SEQ_FULLSIZE)
{
int err, fmt;
dev = *(unsigned short *) &event_rec[2];
if (dev < 0 || dev >= max_synthdev || synth_devs[dev] == NULL)
return -ENXIO;
if (!(synth_open_mask & (1 << dev)))
return -ENXIO;
fmt = (*(short *) &event_rec[0]) & 0xffff;
err = synth_devs[dev]->load_patch(dev, fmt, buf + p, c, 0);
if (err < 0)
return err;
return err;
}
if (ev_code >= 128)
{
if (seq_mode == SEQ_2 && ev_code == SEQ_EXTENDED)
{
printk(KERN_WARNING "Sequencer: Invalid level 2 event %x\n", ev_code);
return -EINVAL;
}
ev_size = 8;
if (c < ev_size)
{
if (!seq_playing)
seq_startplay();
return count - c;
}
if (copy_from_user((char *)&event_rec[4],
&(buf)[p + 4], 4))
goto out;
}
else
{
if (seq_mode == SEQ_2)
{
printk(KERN_WARNING "Sequencer: 4 byte event in level 2 mode\n");
return -EINVAL;
}
ev_size = 4;
if (event_rec[0] != SEQ_MIDIPUTC)
obsolete_api_used = 1;
}
if (event_rec[0] == SEQ_MIDIPUTC)
{
if (!midi_opened[event_rec[2]])
{
int err, mode;
int dev = event_rec[2];
if (dev >= max_mididev || midi_devs[dev]==NULL)
{
/*printk("Sequencer Error: Nonexistent MIDI device %d\n", dev);*/
return -ENXIO;
}
mode = translate_mode(file);
if ((err = midi_devs[dev]->open(dev, mode,
sequencer_midi_input, sequencer_midi_output)) < 0)
{
seq_reset();
printk(KERN_WARNING "Sequencer Error: Unable to open Midi #%d\n", dev);
return err;
}
midi_opened[dev] = 1;
}
}
if (!seq_queue(event_rec, (file->f_flags & (O_NONBLOCK) ? 1 : 0)))
{
int processed = count - c;
if (!seq_playing)
seq_startplay();
if (!processed && (file->f_flags & O_NONBLOCK))
return -EAGAIN;
else
return processed;
}
p += ev_size;
c -= ev_size;
}
if (!seq_playing)
seq_startplay();
out:
return count;
}
static int seq_queue(unsigned char *note, char nonblock)
{
/*
* Test if there is space in the queue
*/
if (qlen >= SEQ_MAX_QUEUE)
if (!seq_playing)
seq_startplay(); /*
* Give chance to drain the queue
*/
if (!nonblock && qlen >= SEQ_MAX_QUEUE && !waitqueue_active(&seq_sleeper)) {
/*
* Sleep until there is enough space on the queue
*/
oss_broken_sleep_on(&seq_sleeper, MAX_SCHEDULE_TIMEOUT);
}
if (qlen >= SEQ_MAX_QUEUE)
{
return 0; /*
* To be sure
*/
}
memcpy(&queue[qtail * EV_SZ], note, EV_SZ);
qtail = (qtail + 1) % SEQ_MAX_QUEUE;
qlen++;
return 1;
}
static int extended_event(unsigned char *q)
{
int dev = q[2];
if (dev < 0 || dev >= max_synthdev)
return -ENXIO;
if (!(synth_open_mask & (1 << dev)))
return -ENXIO;
switch (q[1])
{
case SEQ_NOTEOFF:
synth_devs[dev]->kill_note(dev, q[3], q[4], q[5]);
break;
case SEQ_NOTEON:
if (q[4] > 127 && q[4] != 255)
return 0;
if (q[5] == 0)
{
synth_devs[dev]->kill_note(dev, q[3], q[4], q[5]);
break;
}
synth_devs[dev]->start_note(dev, q[3], q[4], q[5]);
break;
case SEQ_PGMCHANGE:
synth_devs[dev]->set_instr(dev, q[3], q[4]);
break;
case SEQ_AFTERTOUCH:
synth_devs[dev]->aftertouch(dev, q[3], q[4]);
break;
case SEQ_BALANCE:
synth_devs[dev]->panning(dev, q[3], (char) q[4]);
break;
case SEQ_CONTROLLER:
synth_devs[dev]->controller(dev, q[3], q[4], (short) (q[5] | (q[6] << 8)));
break;
case SEQ_VOLMODE:
if (synth_devs[dev]->volume_method != NULL)
synth_devs[dev]->volume_method(dev, q[3]);
break;
default:
return -EINVAL;
}
return 0;
}
static int find_voice(int dev, int chn, int note)
{
unsigned short key;
int i;
key = (chn << 8) | (note + 1);
for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
if (synth_devs[dev]->alloc.map[i] == key)
return i;
return -1;
}
static int alloc_voice(int dev, int chn, int note)
{
unsigned short key;
int voice;
key = (chn << 8) | (note + 1);
voice = synth_devs[dev]->alloc_voice(dev, chn, note,
&synth_devs[dev]->alloc);
synth_devs[dev]->alloc.map[voice] = key;
synth_devs[dev]->alloc.alloc_times[voice] =
synth_devs[dev]->alloc.timestamp++;
return voice;
}
static void seq_chn_voice_event(unsigned char *event_rec)
{
#define dev event_rec[1]
#define cmd event_rec[2]
#define chn event_rec[3]
#define note event_rec[4]
#define parm event_rec[5]
int voice = -1;
if ((int) dev > max_synthdev || synth_devs[dev] == NULL)
return;
if (!(synth_open_mask & (1 << dev)))
return;
if (!synth_devs[dev])
return;
if (seq_mode == SEQ_2)
{
if (synth_devs[dev]->alloc_voice)
voice = find_voice(dev, chn, note);
if (cmd == MIDI_NOTEON && parm == 0)
{
cmd = MIDI_NOTEOFF;
parm = 64;
}
}
switch (cmd)
{
case MIDI_NOTEON:
if (note > 127 && note != 255) /* Not a seq2 feature */
return;
if (voice == -1 && seq_mode == SEQ_2 && synth_devs[dev]->alloc_voice)
{
/* Internal synthesizer (FM, GUS, etc) */
voice = alloc_voice(dev, chn, note);
}
if (voice == -1)
voice = chn;
if (seq_mode == SEQ_2 && (int) dev < num_synths)
{
/*
* The MIDI channel 10 is a percussive channel. Use the note
* number to select the proper patch (128 to 255) to play.
*/
if (chn == 9)
{
synth_devs[dev]->set_instr(dev, voice, 128 + note);
synth_devs[dev]->chn_info[chn].pgm_num = 128 + note;
}
synth_devs[dev]->setup_voice(dev, voice, chn);
}
synth_devs[dev]->start_note(dev, voice, note, parm);
break;
case MIDI_NOTEOFF:
if (voice == -1)
voice = chn;
synth_devs[dev]->kill_note(dev, voice, note, parm);
break;
case MIDI_KEY_PRESSURE:
if (voice == -1)
voice = chn;
synth_devs[dev]->aftertouch(dev, voice, parm);
break;
default:;
}
#undef dev
#undef cmd
#undef chn
#undef note
#undef parm
}
static void seq_chn_common_event(unsigned char *event_rec)
{
unsigned char dev = event_rec[1];
unsigned char cmd = event_rec[2];
unsigned char chn = event_rec[3];
unsigned char p1 = event_rec[4];
/* unsigned char p2 = event_rec[5]; */
unsigned short w14 = *(short *) &event_rec[6];
if ((int) dev > max_synthdev || synth_devs[dev] == NULL)
return;
if (!(synth_open_mask & (1 << dev)))
return;
if (!synth_devs[dev])
return;
switch (cmd)
{
case MIDI_PGM_CHANGE:
if (seq_mode == SEQ_2)
{
if (chn > 15)
break;
synth_devs[dev]->chn_info[chn].pgm_num = p1;
if ((int) dev >= num_synths)
synth_devs[dev]->set_instr(dev, chn, p1);
}
else
synth_devs[dev]->set_instr(dev, chn, p1);
break;
case MIDI_CTL_CHANGE:
if (seq_mode == SEQ_2)
{
if (chn > 15 || p1 > 127)
break;
synth_devs[dev]->chn_info[chn].controllers[p1] = w14 & 0x7f;
if (p1 < 32) /* Setting MSB should clear LSB to 0 */
synth_devs[dev]->chn_info[chn].controllers[p1 + 32] = 0;
if ((int) dev < num_synths)
{
int val = w14 & 0x7f;
int i, key;
if (p1 < 64) /* Combine MSB and LSB */
{
val = ((synth_devs[dev]->
chn_info[chn].controllers[p1 & ~32] & 0x7f) << 7)
| (synth_devs[dev]->
chn_info[chn].controllers[p1 | 32] & 0x7f);
p1 &= ~32;
}
/* Handle all playing notes on this channel */
key = ((int) chn << 8);
for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
synth_devs[dev]->controller(dev, i, p1, val);
}
else
synth_devs[dev]->controller(dev, chn, p1, w14);
}
else /* Mode 1 */
synth_devs[dev]->controller(dev, chn, p1, w14);
break;
case MIDI_PITCH_BEND:
if (seq_mode == SEQ_2)
{
if (chn > 15)
break;
synth_devs[dev]->chn_info[chn].bender_value = w14;
if ((int) dev < num_synths)
{
/* Handle all playing notes on this channel */
int i, key;
key = (chn << 8);
for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
synth_devs[dev]->bender(dev, i, w14);
}
else
synth_devs[dev]->bender(dev, chn, w14);
}
else /* MODE 1 */
synth_devs[dev]->bender(dev, chn, w14);
break;
default:;
}
}
static int seq_timing_event(unsigned char *event_rec)
{
unsigned char cmd = event_rec[1];
unsigned int parm = *(int *) &event_rec[4];
if (seq_mode == SEQ_2)
{
int ret;
if ((ret = tmr->event(tmr_no, event_rec)) == TIMER_ARMED)
if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
wake_up(&seq_sleeper);
return ret;
}
switch (cmd)
{
case TMR_WAIT_REL:
parm += prev_event_time;
/*
* NOTE! No break here. Execution of TMR_WAIT_REL continues in the
* next case (TMR_WAIT_ABS)
*/
case TMR_WAIT_ABS:
if (parm > 0)
{
long time;
time = parm;
prev_event_time = time;
seq_playing = 1;
request_sound_timer(time);
if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
wake_up(&seq_sleeper);
return TIMER_ARMED;
}
break;
case TMR_START:
seq_time = jiffies;
prev_input_time = 0;
prev_event_time = 0;
break;
case TMR_STOP:
break;
case TMR_CONTINUE:
break;
case TMR_TEMPO:
break;
case TMR_ECHO:
if (seq_mode == SEQ_2)
seq_copy_to_input(event_rec, 8);
else
{
parm = (parm << 8 | SEQ_ECHO);
seq_copy_to_input((unsigned char *) &parm, 4);
}
break;
default:;
}
return TIMER_NOT_ARMED;
}
static void seq_local_event(unsigned char *event_rec)
{
unsigned char cmd = event_rec[1];
unsigned int parm = *((unsigned int *) &event_rec[4]);
switch (cmd)
{
case LOCL_STARTAUDIO:
DMAbuf_start_devices(parm);
break;
default:;
}
}
static void seq_sysex_message(unsigned char *event_rec)
{
unsigned int dev = event_rec[1];
int i, l = 0;
unsigned char *buf = &event_rec[2];
if (dev > max_synthdev)
return;
if (!(synth_open_mask & (1 << dev)))
return;
if (!synth_devs[dev])
return;
l = 0;
for (i = 0; i < 6 && buf[i] != 0xff; i++)
l = i + 1;
if (!synth_devs[dev]->send_sysex)
return;
if (l > 0)
synth_devs[dev]->send_sysex(dev, buf, l);
}
static int play_event(unsigned char *q)
{
/*
* NOTE! This routine returns
* 0 = normal event played.
* 1 = Timer armed. Suspend playback until timer callback.
* 2 = MIDI output buffer full. Restore queue and suspend until timer
*/
unsigned int *delay;
switch (q[0])
{
case SEQ_NOTEOFF:
if (synth_open_mask & (1 << 0))
if (synth_devs[0])
synth_devs[0]->kill_note(0, q[1], 255, q[3]);
break;
case SEQ_NOTEON:
if (q[4] < 128 || q[4] == 255)
if (synth_open_mask & (1 << 0))
if (synth_devs[0])
synth_devs[0]->start_note(0, q[1], q[2], q[3]);
break;
case SEQ_WAIT:
delay = (unsigned int *) q; /*
* Bytes 1 to 3 are containing the *
* delay in 'ticks'
*/
*delay = (*delay >> 8) & 0xffffff;
if (*delay > 0)
{
long time;
seq_playing = 1;
time = *delay;
prev_event_time = time;
request_sound_timer(time);
if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
wake_up(&seq_sleeper);
/*
* The timer is now active and will reinvoke this function
* after the timer expires. Return to the caller now.
*/
return 1;
}
break;
case SEQ_PGMCHANGE:
if (synth_open_mask & (1 << 0))
if (synth_devs[0])
synth_devs[0]->set_instr(0, q[1], q[2]);
break;
case SEQ_SYNCTIMER: /*
* Reset timer
*/
seq_time = jiffies;
prev_input_time = 0;
prev_event_time = 0;
break;
case SEQ_MIDIPUTC: /*
* Put a midi character
*/
if (midi_opened[q[2]])
{
int dev;
dev = q[2];
if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
break;
if (!midi_devs[dev]->outputc(dev, q[1]))
{
/*
* Output FIFO is full. Wait one timer cycle and try again.
*/
seq_playing = 1;
request_sound_timer(-1);
return 2;
}
else
midi_written[dev] = 1;
}
break;
case SEQ_ECHO:
seq_copy_to_input(q, 4); /*
* Echo back to the process
*/
break;
case SEQ_PRIVATE:
if ((int) q[1] < max_synthdev)
synth_devs[q[1]]->hw_control(q[1], q);
break;
case SEQ_EXTENDED:
extended_event(q);
break;
case EV_CHN_VOICE:
seq_chn_voice_event(q);
break;
case EV_CHN_COMMON:
seq_chn_common_event(q);
break;
case EV_TIMING:
if (seq_timing_event(q) == TIMER_ARMED)
{
return 1;
}
break;
case EV_SEQ_LOCAL:
seq_local_event(q);
break;
case EV_SYSEX:
seq_sysex_message(q);
break;
default:;
}
return 0;
}
/* called also as timer in irq context */
static void seq_startplay(void)
{
int this_one, action;
unsigned long flags;
while (qlen > 0)
{
spin_lock_irqsave(&lock,flags);
qhead = ((this_one = qhead) + 1) % SEQ_MAX_QUEUE;
qlen--;
spin_unlock_irqrestore(&lock,flags);
seq_playing = 1;
if ((action = play_event(&queue[this_one * EV_SZ])))
{ /* Suspend playback. Next timer routine invokes this routine again */
if (action == 2)
{
qlen++;
qhead = this_one;
}
return;
}
}
seq_playing = 0;
if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
wake_up(&seq_sleeper);
}
static void reset_controllers(int dev, unsigned char *controller, int update_dev)
{
int i;
for (i = 0; i < 128; i++)
controller[i] = ctrl_def_values[i];
}
static void setup_mode2(void)
{
int dev;
max_synthdev = num_synths;
for (dev = 0; dev < num_midis; dev++)
{
if (midi_devs[dev] && midi_devs[dev]->converter != NULL)
{
synth_devs[max_synthdev++] = midi_devs[dev]->converter;
}
}
for (dev = 0; dev < max_synthdev; dev++)
{
int chn;
synth_devs[dev]->sysex_ptr = 0;
synth_devs[dev]->emulation = 0;
for (chn = 0; chn < 16; chn++)
{
synth_devs[dev]->chn_info[chn].pgm_num = 0;
reset_controllers(dev,
synth_devs[dev]->chn_info[chn].controllers,0);
synth_devs[dev]->chn_info[chn].bender_value = (1 << 7); /* Neutral */
synth_devs[dev]->chn_info[chn].bender_range = 200;
}
}
max_mididev = 0;
seq_mode = SEQ_2;
}
int sequencer_open(int dev, struct file *file)
{
int retval, mode, i;
int level, tmp;
if (!sequencer_ok)
sequencer_init();
level = ((dev & 0x0f) == SND_DEV_SEQ2) ? 2 : 1;
dev = dev >> 4;
mode = translate_mode(file);
if (!sequencer_ok)
{
/* printk("Sound card: sequencer not initialized\n");*/
return -ENXIO;
}
if (dev) /* Patch manager device (obsolete) */
return -ENXIO;
if(synth_devs[dev] == NULL)
request_module("synth0");
if (mode == OPEN_READ)
{
if (!num_midis)
{
/*printk("Sequencer: No MIDI devices. Input not possible\n");*/
sequencer_busy = 0;
return -ENXIO;
}
}
if (sequencer_busy)
{
return -EBUSY;
}
sequencer_busy = 1;
obsolete_api_used = 0;
max_mididev = num_midis;
max_synthdev = num_synths;
pre_event_timeout = MAX_SCHEDULE_TIMEOUT;
seq_mode = SEQ_1;
if (pending_timer != -1)
{
tmr_no = pending_timer;
pending_timer = -1;
}
if (tmr_no == -1) /* Not selected yet */
{
int i, best;