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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
|
/* $OpenBSD: sequencer.c,v 1.22 2011/07/02 22:20:07 nicm Exp $ */
/* $NetBSD: sequencer.c,v 1.13 1998/11/25 22:17:07 augustss Exp $ */
/*
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Lennart Augustsson (augustss@netbsd.org).
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <sys/vnode.h>
#include <sys/selinfo.h>
#include <sys/poll.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/syslog.h>
#include <sys/kernel.h>
#include <sys/signalvar.h>
#include <sys/conf.h>
#include <sys/audioio.h>
#include <sys/midiio.h>
#include <sys/device.h>
#include <dev/midi_if.h>
#include <dev/midivar.h>
#include <dev/sequencervar.h>
#ifndef splaudio
#define splaudio() splbio() /* XXX found in audio_if.h normally */
#endif
#define ADDTIMEVAL(a, b) ( \
(a)->tv_sec += (b)->tv_sec, \
(a)->tv_usec += (b)->tv_usec, \
(a)->tv_usec >= 1000000 ? ((a)->tv_sec++, (a)->tv_usec -= 1000000) : 0\
)
#define SUBTIMEVAL(a, b) ( \
(a)->tv_sec -= (b)->tv_sec, \
(a)->tv_usec -= (b)->tv_usec, \
(a)->tv_usec < 0 ? ((a)->tv_sec--, (a)->tv_usec += 1000000) : 0\
)
#ifdef AUDIO_DEBUG
#define DPRINTF(x) if (sequencerdebug) printf x
#define DPRINTFN(n,x) if (sequencerdebug >= (n)) printf x
int sequencerdebug = 0;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
#endif
#define SEQ_CMD(b) ((b)->arr[0])
#define SEQ_EDEV(b) ((b)->arr[1])
#define SEQ_ECMD(b) ((b)->arr[2])
#define SEQ_ECHAN(b) ((b)->arr[3])
#define SEQ_ENOTE(b) ((b)->arr[4])
#define SEQ_EPARM(b) ((b)->arr[5])
#define SEQ_EP1(b) ((b)->arr[4])
#define SEQ_EP2(b) ((b)->arr[5])
#define SEQ_XCMD(b) ((b)->arr[1])
#define SEQ_XDEV(b) ((b)->arr[2])
#define SEQ_XCHAN(b) ((b)->arr[3])
#define SEQ_XNOTE(b) ((b)->arr[4])
#define SEQ_XVEL(b) ((b)->arr[5])
#define SEQ_TCMD(b) ((b)->arr[1])
#define SEQ_TPARM(b) ((b)->arr[4])
#define SEQ_NOTE_MAX 128
#define SEQ_NOTE_XXX 255
#define SEQ_VEL_OFF 0
#define RECALC_TICK(t) ((t)->tick = 60 * 1000000L / ((t)->tempo * (t)->timebase))
struct sequencer_softc seqdevs[NSEQUENCER];
void sequencerattach(int);
void seq_reset(struct sequencer_softc *);
int seq_do_command(struct sequencer_softc *, seq_event_rec *);
int seq_do_extcommand(struct sequencer_softc *, seq_event_rec *);
int seq_do_chnvoice(struct sequencer_softc *, seq_event_rec *);
int seq_do_chncommon(struct sequencer_softc *, seq_event_rec *);
int seq_do_timing(struct sequencer_softc *, seq_event_rec *);
int seq_do_local(struct sequencer_softc *, seq_event_rec *);
int seq_do_sysex(struct sequencer_softc *, seq_event_rec *);
int seq_do_fullsize(struct sequencer_softc *, seq_event_rec *,
struct uio *);
int seq_timer(struct sequencer_softc *, int, int, seq_event_rec *);
static int seq_input_event(struct sequencer_softc *, seq_event_rec *);
int seq_drain(struct sequencer_softc *);
void seq_startoutput(struct sequencer_softc *);
void seq_timeout(void *);
int seq_to_new(seq_event_rec *, struct uio *);
static int seq_sleep_timo(int *, char *, int);
static int seq_sleep(int *, char *);
static void seq_wakeup(int *);
struct midi_softc;
int midiseq_out(struct midi_dev *, u_char *, u_int, int);
struct midi_dev *midiseq_open(int, int);
void midiseq_close(struct midi_dev *);
void midiseq_reset(struct midi_dev *);
int midiseq_noteon(struct midi_dev *, int, int, int);
int midiseq_noteoff(struct midi_dev *, int, int, int);
int midiseq_keypressure(struct midi_dev *, int, int, int);
int midiseq_pgmchange(struct midi_dev *, int, int);
int midiseq_chnpressure(struct midi_dev *, int, int);
int midiseq_ctlchange(struct midi_dev *, int, int, int);
int midiseq_pitchbend(struct midi_dev *, int, int);
int midiseq_loadpatch(struct midi_dev *, struct sysex_info *,
struct uio *);
int midiseq_putc(struct midi_dev *, int);
void midiseq_in(struct midi_dev *, u_char *, int);
void
sequencerattach(int n)
{
}
int
sequenceropen(dev_t dev, int flags, int ifmt, struct proc *p)
{
int unit = SEQUENCERUNIT(dev);
struct sequencer_softc *sc;
struct midi_dev *md;
int nmidi;
DPRINTF(("sequenceropen\n"));
if (unit >= NSEQUENCER)
return (ENXIO);
sc = &seqdevs[unit];
if (sc->isopen)
return (EBUSY);
if (SEQ_IS_OLD(dev))
sc->mode = SEQ_OLD;
else
sc->mode = SEQ_NEW;
sc->isopen++;
sc->flags = flags & (FREAD|FWRITE);
sc->rchan = 0;
sc->wchan = 0;
sc->pbus = 0;
sc->async = 0;
sc->input_stamp = ~0;
sc->nmidi = 0;
nmidi = midi_unit_count();
sc->devs = malloc(nmidi * sizeof(struct midi_dev *),
M_DEVBUF, M_WAITOK);
for (unit = 0; unit < nmidi; unit++) {
md = midiseq_open(unit, flags);
if (md) {
sc->devs[sc->nmidi++] = md;
md->seq = sc;
}
}
sc->timer.timebase = 100;
sc->timer.tempo = 60;
sc->doingsysex = 0;
RECALC_TICK(&sc->timer);
sc->timer.last = 0;
microtime(&sc->timer.start);
SEQ_QINIT(&sc->inq);
SEQ_QINIT(&sc->outq);
sc->lowat = SEQ_MAXQ / 2;
timeout_set(&sc->timo, seq_timeout, sc);
seq_reset(sc);
DPRINTF(("sequenceropen: mode=%d, nmidi=%d\n", sc->mode, sc->nmidi));
return (0);
}
static int
seq_sleep_timo(int *chan, char *label, int timo)
{
int st;
if (!label)
label = "seq";
DPRINTFN(5, ("seq_sleep_timo: %p %s %d\n", chan, label, timo));
*chan = 1;
st = tsleep(chan, PWAIT | PCATCH, label, timo);
*chan = 0;
#ifdef MIDI_DEBUG
if (st != 0)
printf("seq_sleep: %d\n", st);
#endif
return (st);
}
static int
seq_sleep(int *chan, char *label)
{
return (seq_sleep_timo(chan, label, 0));
}
static void
seq_wakeup(int *chan)
{
if (*chan) {
DPRINTFN(5, ("seq_wakeup: %p\n", chan));
wakeup(chan);
*chan = 0;
}
}
int
seq_drain(struct sequencer_softc *sc)
{
int error;
DPRINTFN(3, ("seq_drain: %p, len=%d\n", sc, SEQ_QLEN(&sc->outq)));
seq_startoutput(sc);
error = 0;
while(!SEQ_QEMPTY(&sc->outq) && !error)
error = seq_sleep_timo(&sc->wchan, "seq_dr", 60*hz);
return (error);
}
void
seq_timeout(void *addr)
{
struct sequencer_softc *sc = addr;
DPRINTFN(4, ("seq_timeout: %p\n", sc));
sc->timeout = 0;
seq_startoutput(sc);
if (SEQ_QLEN(&sc->outq) < sc->lowat) {
seq_wakeup(&sc->wchan);
selwakeup(&sc->wsel);
if (sc->async)
psignal(sc->async, SIGIO);
}
}
void
seq_startoutput(struct sequencer_softc *sc)
{
struct sequencer_queue *q = &sc->outq;
seq_event_rec cmd;
if (sc->timeout)
return;
DPRINTFN(4, ("seq_startoutput: %p, len=%d\n", sc, SEQ_QLEN(q)));
while(!SEQ_QEMPTY(q) && !sc->timeout) {
SEQ_QGET(q, cmd);
seq_do_command(sc, &cmd);
}
}
int
sequencerclose(dev_t dev, int flags, int ifmt, struct proc *p)
{
struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
int n, s;
DPRINTF(("sequencerclose: %p\n", sc));
seq_drain(sc);
s = splaudio();
if (sc->timeout) {
timeout_del(&sc->timo);
sc->timeout = 0;
}
splx(s);
for (n = 0; n < sc->nmidi; n++)
midiseq_close(sc->devs[n]);
free(sc->devs, M_DEVBUF);
sc->isopen = 0;
return (0);
}
static int
seq_input_event(struct sequencer_softc *sc, seq_event_rec *cmd)
{
struct sequencer_queue *q = &sc->inq;
DPRINTFN(2, ("seq_input_event: %02x %02x %02x %02x %02x %02x %02x %02x\n",
cmd->arr[0], cmd->arr[1], cmd->arr[2], cmd->arr[3],
cmd->arr[4], cmd->arr[5], cmd->arr[6], cmd->arr[7]));
if (SEQ_QFULL(q))
return (ENOMEM);
SEQ_QPUT(q, *cmd);
seq_wakeup(&sc->rchan);
selwakeup(&sc->rsel);
if (sc->async)
psignal(sc->async, SIGIO);
return (0);
}
void
seq_event_intr(void *addr, seq_event_rec *iev)
{
struct sequencer_softc *sc = addr;
union {
u_int32_t l;
u_int8_t b[4];
} u;
u_long t;
struct timeval now;
seq_event_rec ev;
microtime(&now);
SUBTIMEVAL(&now, &sc->timer.start);
t = now.tv_sec * 1000000 + now.tv_usec;
t /= sc->timer.tick;
if (t != sc->input_stamp) {
ev.arr[0] = SEQ_TIMING;
ev.arr[1] = TMR_WAIT_ABS;
ev.arr[2] = 0;
ev.arr[3] = 0;
u.l = t;
ev.arr[4] = u.b[0];
ev.arr[5] = u.b[1];
ev.arr[6] = u.b[2];
ev.arr[7] = u.b[3];
seq_input_event(sc, &ev);
sc->input_stamp = t;
}
seq_input_event(sc, iev);
}
int
sequencerread(dev_t dev, struct uio *uio, int ioflag)
{
struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
struct sequencer_queue *q = &sc->inq;
seq_event_rec ev;
int error, s;
DPRINTFN(20, ("sequencerread: %p, count=%d, ioflag=%x\n",
sc, uio->uio_resid, ioflag));
if (sc->mode == SEQ_OLD) {
DPRINTF(("sequencerread: old read\n"));
return (EINVAL); /* XXX unimplemented */
}
error = 0;
while (SEQ_QEMPTY(q)) {
if (ioflag & IO_NDELAY)
return (EWOULDBLOCK);
else {
error = seq_sleep(&sc->rchan, "seq rd");
if (error)
return (error);
}
}
s = splaudio();
while (uio->uio_resid >= sizeof ev && !error && !SEQ_QEMPTY(q)) {
SEQ_QGET(q, ev);
error = uiomove((caddr_t)&ev, sizeof ev, uio);
}
splx(s);
return (error);
}
int
sequencerwrite(dev_t dev, struct uio *uio, int ioflag)
{
struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
struct sequencer_queue *q = &sc->outq;
int error;
seq_event_rec cmdbuf;
int size;
DPRINTFN(2, ("sequencerwrite: %p, count=%d\n", sc, uio->uio_resid));
error = 0;
size = sc->mode == SEQ_NEW ? sizeof cmdbuf : SEQOLD_CMDSIZE;
while (uio->uio_resid >= size) {
error = uiomove((caddr_t)&cmdbuf, size, uio);
if (error)
break;
if (sc->mode == SEQ_OLD)
if (seq_to_new(&cmdbuf, uio))
continue;
if (SEQ_CMD(&cmdbuf) == SEQ_FULLSIZE) {
/* We do it like OSS does, asynchronously */
error = seq_do_fullsize(sc, &cmdbuf, uio);
if (error)
break;
continue;
}
while (SEQ_QFULL(q)) {
seq_startoutput(sc);
if (SEQ_QFULL(q)) {
if (ioflag & IO_NDELAY)
return (EWOULDBLOCK);
error = seq_sleep(&sc->wchan, "seq_wr");
if (error)
return (error);
}
}
SEQ_QPUT(q, cmdbuf);
}
seq_startoutput(sc);
#ifdef SEQUENCER_DEBUG
if (error)
DPRINTFN(2, ("sequencerwrite: error=%d\n", error));
#endif
return (error);
}
int
sequencerioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
{
struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
struct synth_info *si;
struct midi_dev *md;
int devno;
int error;
int t;
DPRINTFN(2, ("sequencerioctl: %p cmd=0x%08lx\n", sc, cmd));
error = 0;
switch (cmd) {
case FIONBIO:
/* All handled in the upper FS layer. */
break;
case FIOASYNC:
if (*(int *)addr) {
if (sc->async)
return (EBUSY);
sc->async = p;
DPRINTF(("sequencer_ioctl: FIOASYNC %p\n", p));
} else
sc->async = 0;
break;
case SEQUENCER_RESET:
seq_reset(sc);
break;
case SEQUENCER_PANIC:
seq_reset(sc);
/* Do more? OSS doesn't */
break;
case SEQUENCER_SYNC:
if (sc->flags == FREAD)
return (0);
seq_drain(sc);
error = 0;
break;
case SEQUENCER_INFO:
si = (struct synth_info*)addr;
devno = si->device;
if (devno < 0 || devno >= sc->nmidi)
return (EINVAL);
md = sc->devs[devno];
strncpy(si->name, md->name, sizeof si->name);
si->synth_type = SYNTH_TYPE_MIDI;
si->synth_subtype = md->subtype;
si->nr_voices = md->nr_voices;
si->instr_bank_size = md->instr_bank_size;
si->capabilities = md->capabilities;
break;
case SEQUENCER_NRSYNTHS:
*(int *)addr = sc->nmidi;
break;
case SEQUENCER_NRMIDIS:
*(int *)addr = sc->nmidi;
break;
case SEQUENCER_OUTOFBAND:
DPRINTFN(3, ("sequencer_ioctl: OOB=%02x %02x %02x %02x %02x %02x %02x %02x\n",
*(u_char *)addr, *(u_char *)(addr+1),
*(u_char *)(addr+2), *(u_char *)(addr+3),
*(u_char *)(addr+4), *(u_char *)(addr+5),
*(u_char *)(addr+6), *(u_char *)(addr+7)));
error = seq_do_command(sc, (seq_event_rec *)addr);
break;
case SEQUENCER_TMR_TIMEBASE:
t = *(int *)addr;
if (t < 1)
t = 1;
if (t > 1000)
t = 1000;
sc->timer.timebase = t;
*(int *)addr = t;
RECALC_TICK(&sc->timer);
break;
case SEQUENCER_TMR_START:
error = seq_timer(sc, TMR_START, 0, 0);
break;
case SEQUENCER_TMR_STOP:
error = seq_timer(sc, TMR_STOP, 0, 0);
break;
case SEQUENCER_TMR_CONTINUE:
error = seq_timer(sc, TMR_CONTINUE, 0, 0);
break;
case SEQUENCER_TMR_TEMPO:
t = *(int *)addr;
if (t < 8)
t = 8;
if (t > 250)
t = 250;
sc->timer.tempo = t;
*(int *)addr = t;
RECALC_TICK(&sc->timer);
break;
case SEQUENCER_TMR_SOURCE:
*(int *)addr = SEQUENCER_TMR_INTERNAL;
break;
case SEQUENCER_TMR_METRONOME:
/* noop */
break;
case SEQUENCER_THRESHOLD:
t = SEQ_MAXQ - *(int *)addr / sizeof (seq_event_rec);
if (t < 1)
t = 1;
if (t > SEQ_MAXQ)
t = SEQ_MAXQ;
sc->lowat = t;
break;
case SEQUENCER_CTRLRATE:
*(int *)addr = (sc->timer.tempo*sc->timer.timebase + 30) / 60;
break;
case SEQUENCER_GETTIME:
{
struct timeval now;
u_long t;
microtime(&now);
SUBTIMEVAL(&now, &sc->timer.start);
t = now.tv_sec * 1000000 + now.tv_usec;
t /= sc->timer.tick;
*(int *)addr = t;
break;
}
default:
DPRINTF(("sequencer_ioctl: unimpl %08lx\n", cmd));
error = ENOTTY;
break;
}
return (error);
}
int
sequencerpoll(dev_t dev, int events, struct proc *p)
{
struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
int revents = 0;
DPRINTF(("sequencerpoll: %p rw=0x%x\n", sc, events));
if (events & (POLLIN | POLLRDNORM)) {
if (!SEQ_QEMPTY(&sc->inq))
revents |= events & (POLLIN | POLLRDNORM);
}
if (events & (POLLOUT | POLLWRNORM)) {
if (SEQ_QLEN(&sc->outq) < sc->lowat)
revents |= events & (POLLOUT | POLLWRNORM);
}
if (revents == 0) {
if (events & (POLLIN | POLLRDNORM))
selrecord(p, &sc->rsel);
if (events & (POLLOUT | POLLWRNORM))
selrecord(p, &sc->wsel);
}
return (revents);
}
int
sequencerkqfilter(dev_t dev, struct knote *kn)
{
return (ENXIO);
}
void
seq_reset(struct sequencer_softc *sc)
{
int i, chn;
struct midi_dev *md;
for (i = 0; i < sc->nmidi; i++) {
md = sc->devs[i];
midiseq_reset(md);
for (chn = 0; chn < MAXCHAN; chn++) {
midiseq_ctlchange(md, chn, MIDI_CTRL_ALLOFF, 0);
midiseq_ctlchange(md, chn, MIDI_CTRL_RESET, 0);
midiseq_pitchbend(md, chn, MIDI_BEND_NEUTRAL);
}
}
}
int
seq_do_command(struct sequencer_softc *sc, seq_event_rec *b)
{
int dev;
DPRINTFN(4, ("seq_do_command: %p cmd=0x%02x\n", sc, SEQ_CMD(b)));
switch(SEQ_CMD(b)) {
case SEQ_LOCAL:
return (seq_do_local(sc, b));
case SEQ_TIMING:
return (seq_do_timing(sc, b));
case SEQ_CHN_VOICE:
return (seq_do_chnvoice(sc, b));
case SEQ_CHN_COMMON:
return (seq_do_chncommon(sc, b));
case SEQ_SYSEX:
return (seq_do_sysex(sc, b));
/* COMPAT */
case SEQOLD_MIDIPUTC:
dev = b->arr[2];
if (dev < 0 || dev >= sc->nmidi)
return (ENXIO);
return (midiseq_putc(sc->devs[dev], b->arr[1]));
default:
DPRINTF(("seq_do_command: unimpl command %02x\n",
SEQ_CMD(b)));
return (EINVAL);
}
}
int
seq_do_chnvoice(struct sequencer_softc *sc, seq_event_rec *b)
{
int cmd, dev, chan, note, parm, voice;
int error;
struct midi_dev *md;
dev = SEQ_EDEV(b);
if (dev < 0 || dev >= sc->nmidi)
return (ENXIO);
md = sc->devs[dev];
cmd = SEQ_ECMD(b);
chan = SEQ_ECHAN(b);
note = SEQ_ENOTE(b);
parm = SEQ_EPARM(b);
DPRINTFN(2,("seq_do_chnvoice: cmd=%02x dev=%d chan=%d note=%d parm=%d\n",
cmd, dev, chan, note, parm));
voice = chan;
if (cmd == MIDI_NOTEON && parm == 0) {
cmd = MIDI_NOTEOFF;
parm = MIDI_HALF_VEL;
}
switch(cmd) {
case MIDI_NOTEON:
DPRINTFN(5, ("seq_do_chnvoice: noteon %p %d %d %d\n",
md, voice, note, parm));
error = midiseq_noteon(md, voice, note, parm);
break;
case MIDI_NOTEOFF:
error = midiseq_noteoff(md, voice, note, parm);
break;
case MIDI_KEY_PRESSURE:
error = midiseq_keypressure(md, voice, note, parm);
break;
default:
DPRINTF(("seq_do_chnvoice: unimpl command %02x\n", cmd));
error = EINVAL;
break;
}
return (error);
}
int
seq_do_chncommon(struct sequencer_softc *sc, seq_event_rec *b)
{
int cmd, dev, chan, p1, w14;
int error;
struct midi_dev *md;
union {
int16_t s;
u_int8_t b[2];
} u;
dev = SEQ_EDEV(b);
if (dev < 0 || dev >= sc->nmidi)
return (ENXIO);
md = sc->devs[dev];
cmd = SEQ_ECMD(b);
chan = SEQ_ECHAN(b);
p1 = SEQ_EP1(b);
u.b[0] = b->arr[6];
u.b[1] = b->arr[7];
w14 = u.s;
DPRINTFN(2,("seq_do_chncommon: %02x\n", cmd));
error = 0;
switch(cmd) {
case MIDI_PGM_CHANGE:
error = midiseq_pgmchange(md, chan, p1);
break;
case MIDI_CTL_CHANGE:
if (chan > 15 || p1 > 127)
return (0); /* EINVAL */
error = midiseq_ctlchange(md, chan, p1, w14);
break;
case MIDI_PITCH_BEND:
error = midiseq_pitchbend(md, chan, w14);
break;
case MIDI_CHN_PRESSURE:
error = midiseq_chnpressure(md, chan, p1);
break;
default:
DPRINTF(("seq_do_chncommon: unimpl command %02x\n", cmd));
error = EINVAL;
break;
}
return (error);
}
int
seq_do_timing(struct sequencer_softc *sc, seq_event_rec *b)
{
union {
int32_t i;
u_int8_t b[4];
} u;
u.b[0] = b->arr[4];
u.b[1] = b->arr[5];
u.b[2] = b->arr[6];
u.b[3] = b->arr[7];
return (seq_timer(sc, SEQ_TCMD(b), u.i, b));
}
int
seq_do_local(struct sequencer_softc *sc, seq_event_rec *b)
{
return (EINVAL);
}
int
seq_do_sysex(struct sequencer_softc *sc, seq_event_rec *b)
{
int dev, i;
struct midi_dev *md;
u_int8_t c, *buf = &b->arr[2];
dev = SEQ_EDEV(b);
if (dev < 0 || dev >= sc->nmidi)
return (ENXIO);
DPRINTF(("seq_do_sysex: dev=%d\n", dev));
md = sc->devs[dev];
if (!sc->doingsysex) {
c = MIDI_SYSEX_START;
midiseq_out(md, &c, 1, 0);
sc->doingsysex = 1;
}
for (i = 0; i < 6 && buf[i] != 0xff; i++)
;
midiseq_out(md, buf, i, 0);
if (i < 6 || (i > 0 && buf[i-1] == MIDI_SYSEX_END))
sc->doingsysex = 0;
return (0);
}
int
seq_timer(struct sequencer_softc *sc, int cmd, int parm, seq_event_rec *b)
{
struct syn_timer *t = &sc->timer;
struct timeval when;
int ticks;
int error;
long long usec;
DPRINTFN(2,("seq_timer: %02x %d\n", cmd, parm));
error = 0;
switch(cmd) {
case TMR_WAIT_REL:
parm += t->last;
/* FALLTHROUGH */
case TMR_WAIT_ABS:
t->last = parm;
usec = (long long)parm * (long long)t->tick; /* convert to usec */
when.tv_sec = usec / 1000000;
when.tv_usec = usec % 1000000;
DPRINTFN(4, ("seq_timer: parm=%d, sleep when=%ld.%06ld", parm,
when.tv_sec, when.tv_usec));
ADDTIMEVAL(&when, &t->start); /* abstime for end */
ticks = hzto(&when);
DPRINTFN(4, (" when+start=%ld.%06ld, ticks=%d\n",
when.tv_sec, when.tv_usec, ticks));
if (ticks > 0) {
#ifdef DIAGNOSTIC
if (ticks > 20 * hz) {
/* Waiting more than 20s */
printf("seq_timer: funny ticks=%d, usec=%lld, parm=%d, tick=%ld\n",
ticks, usec, parm, t->tick);
}
#endif
sc->timeout = 1;
timeout_add(&sc->timo, ticks);
}
#ifdef SEQUENCER_DEBUG
else if (ticks < 0)
DPRINTF(("seq_timer: ticks = %d\n", ticks));
#endif
break;
case TMR_START:
microtime(&t->start);
t->running = 1;
break;
case TMR_STOP:
microtime(&t->stop);
t->running = 0;
break;
case TMR_CONTINUE:
microtime(&when);
SUBTIMEVAL(&when, &t->stop);
ADDTIMEVAL(&t->start, &when);
t->running = 1;
break;
case TMR_TEMPO:
/* parm is ticks per minute / timebase */
if (parm < 8)
parm = 8;
if (parm > 360)
parm = 360;
t->tempo = parm;
RECALC_TICK(t);
break;
case TMR_ECHO:
error = seq_input_event(sc, b);
break;
case TMR_RESET:
t->last = 0;
microtime(&t->start);
break;
default:
DPRINTF(("seq_timer: unknown %02x\n", cmd));
error = EINVAL;
break;
}
return (error);
}
int
seq_do_fullsize(struct sequencer_softc *sc, seq_event_rec *b, struct uio *uio)
{
struct sysex_info sysex;
u_int dev;
#ifdef DIAGNOSTIC
if (sizeof(seq_event_rec) != SEQ_SYSEX_HDRSIZE) {
printf("seq_do_fullsize: sysex size ??\n");
return (EINVAL);
}
#endif
memcpy(&sysex, b, sizeof sysex);
dev = sysex.device_no;
DPRINTFN(2, ("seq_do_fullsize: fmt=%04x, dev=%d, len=%d\n",
sysex.key, dev, sysex.len));
return (midiseq_loadpatch(sc->devs[dev], &sysex, uio));
}
/* Convert an old sequencer event to a new one. */
int
seq_to_new(seq_event_rec *ev, struct uio *uio)
{
int cmd, chan, note, parm;
u_int32_t delay;
int error;
cmd = SEQ_CMD(ev);
chan = ev->arr[1];
note = ev->arr[2];
parm = ev->arr[3];
DPRINTFN(3, ("seq_to_new: 0x%02x %d %d %d\n", cmd, chan, note, parm));
if (cmd >= 0x80) {
/* Fill the event record */
if (uio->uio_resid >= sizeof *ev - SEQOLD_CMDSIZE) {
error = uiomove(&ev->arr[SEQOLD_CMDSIZE],
sizeof *ev - SEQOLD_CMDSIZE, uio);
if (error)
return (error);
} else
return (EINVAL);
}
switch(cmd) {
case SEQOLD_NOTEOFF:
note = 255;
SEQ_ECMD(ev) = MIDI_NOTEOFF;
goto onoff;
case SEQOLD_NOTEON:
SEQ_ECMD(ev) = MIDI_NOTEON;
onoff:
SEQ_CMD(ev) = SEQ_CHN_VOICE;
SEQ_EDEV(ev) = 0;
SEQ_ECHAN(ev) = chan;
SEQ_ENOTE(ev) = note;
SEQ_EPARM(ev) = parm;
break;
case SEQOLD_WAIT:
delay = *(u_int32_t *)ev->arr >> 8;
SEQ_CMD(ev) = SEQ_TIMING;
SEQ_TCMD(ev) = TMR_WAIT_REL;
*(u_int32_t *)&ev->arr[4] = delay;
break;
case SEQOLD_SYNCTIMER:
SEQ_CMD(ev) = SEQ_TIMING;
SEQ_TCMD(ev) = TMR_RESET;
break;
case SEQOLD_PGMCHANGE:
SEQ_ECMD(ev) = MIDI_PGM_CHANGE;
SEQ_CMD(ev) = SEQ_CHN_COMMON;
SEQ_EDEV(ev) = 0;
SEQ_ECHAN(ev) = chan;
SEQ_EP1(ev) = note;
break;
case SEQOLD_MIDIPUTC:
break; /* interpret in normal mode */
case SEQOLD_ECHO:
case SEQOLD_PRIVATE:
case SEQOLD_EXTENDED:
default:
DPRINTF(("seq_to_new: not impl 0x%02x\n", cmd));
return (EINVAL);
/* In case new events show up */
case SEQ_TIMING:
case SEQ_CHN_VOICE:
case SEQ_CHN_COMMON:
case SEQ_FULLSIZE:
break;
}
return (0);
}
/**********************************************/
void
midiseq_in(struct midi_dev *md, u_char *msg, int len)
{
int unit = md->unit;
seq_event_rec ev;
int status, chan;
DPRINTFN(2, ("midiseq_in: %p %02x %02x %02x\n",
md, msg[0], msg[1], msg[2]));
status = MIDI_GET_STATUS(msg[0]);
chan = MIDI_GET_CHAN(msg[0]);
switch (status) {
case MIDI_NOTEON:
if (msg[2] == 0) {
status = MIDI_NOTEOFF;
msg[2] = MIDI_HALF_VEL;
}
/* FALLTHROUGH */
case MIDI_NOTEOFF:
case MIDI_KEY_PRESSURE:
SEQ_MK_CHN_VOICE(&ev, unit, status, chan, msg[1], msg[2]);
break;
case MIDI_CTL_CHANGE:
SEQ_MK_CHN_COMMON(&ev, unit, status, chan, msg[1], 0, msg[2]);
break;
case MIDI_PGM_CHANGE:
case MIDI_CHN_PRESSURE:
SEQ_MK_CHN_COMMON(&ev, unit, status, chan, msg[1], 0, 0);
break;
case MIDI_PITCH_BEND:
SEQ_MK_CHN_COMMON(&ev, unit, status, chan, 0, 0,
(msg[1] & 0x7f) | ((msg[2] & 0x7f) << 7));
break;
default:
return;
}
seq_event_intr(md->seq, &ev);
}
struct midi_dev *
midiseq_open(int unit, int flags)
{
extern struct cfdriver midi_cd;
int error;
struct midi_dev *md;
struct midi_softc *sc;
struct midi_info mi;
DPRINTFN(2, ("midiseq_open: %d %d\n", unit, flags));
error = midiopen(makedev(0, unit), flags, 0, 0);
if (error)
return (0);
sc = midi_cd.cd_devs[unit];
sc->seqopen = 1;
md = malloc(sizeof *md, M_DEVBUF, M_WAITOK | M_ZERO);
sc->seq_md = md;
md->msc = sc;
midi_getinfo(makedev(0, unit), &mi);
md->unit = unit;
md->name = mi.name;
md->subtype = 0;
md->nr_voices = 128; /* XXX */
md->instr_bank_size = 128; /* XXX */
if (mi.props & MIDI_PROP_CAN_INPUT)
md->capabilities |= SYNTH_CAP_INPUT;
return (md);
}
void
midiseq_close(struct midi_dev *md)
{
DPRINTFN(2, ("midiseq_close: %d\n", md->unit));
midiclose(makedev(0, md->unit), 0, 0, 0);
free(md, M_DEVBUF);
}
void
midiseq_reset(struct midi_dev *md)
{
/* XXX send GM reset? */
DPRINTFN(3, ("midiseq_reset: %d\n", md->unit));
}
int
midiseq_out(struct midi_dev *md, u_char *buf, u_int cc, int chk)
{
DPRINTFN(5, ("midiseq_out: m=%p, unit=%d, buf[0]=0x%02x, cc=%d\n",
md->msc, md->unit, buf[0], cc));
/* The MIDI "status" byte does not have to be repeated. */
if (chk && md->last_cmd == buf[0])
buf++, cc--;
else
md->last_cmd = buf[0];
return (midi_writebytes(md->unit, buf, cc));
}
int
midiseq_noteon(struct midi_dev *md, int chan, int note, int vel)
{
u_char buf[3];
DPRINTFN(6, ("midiseq_noteon 0x%02x %d %d\n",
MIDI_NOTEON | chan, note, vel));
if (chan < 0 || chan > 15 ||
note < 0 || note > 127)
return (EINVAL);
if (vel < 0) vel = 0;
if (vel > 127) vel = 127;
buf[0] = MIDI_NOTEON | chan;
buf[1] = note;
buf[2] = vel;
return (midiseq_out(md, buf, 3, 1));
}
int
midiseq_noteoff(struct midi_dev *md, int chan, int note, int vel)
{
u_char buf[3];
if (chan < 0 || chan > 15 ||
note < 0 || note > 127)
return (EINVAL);
if (vel < 0) vel = 0;
if (vel > 127) vel = 127;
buf[0] = MIDI_NOTEOFF | chan;
buf[1] = note;
buf[2] = vel;
return (midiseq_out(md, buf, 3, 1));
}
int
midiseq_keypressure(struct midi_dev *md, int chan, int note, int vel)
{
u_char buf[3];
if (chan < 0 || chan > 15 ||
note < 0 || note > 127)
return (EINVAL);
if (vel < 0) vel = 0;
if (vel > 127) vel = 127;
buf[0] = MIDI_KEY_PRESSURE | chan;
buf[1] = note;
buf[2] = vel;
return (midiseq_out(md, buf, 3, 1));
}
int
midiseq_pgmchange(struct midi_dev *md, int chan, int parm)
{
u_char buf[2];
if (chan < 0 || chan > 15 ||
parm < 0 || parm > 127)
return (EINVAL);
buf[0] = MIDI_PGM_CHANGE | chan;
buf[1] = parm;
return (midiseq_out(md, buf, 2, 1));
}
int
midiseq_chnpressure(struct midi_dev *md, int chan, int parm)
{
u_char buf[2];
if (chan < 0 || chan > 15 ||
parm < 0 || parm > 127)
return (EINVAL);
buf[0] = MIDI_CHN_PRESSURE | chan;
buf[1] = parm;
return (midiseq_out(md, buf, 2, 1));
}
int
midiseq_ctlchange(struct midi_dev *md, int chan, int parm, int w14)
{
u_char buf[3];
if (chan < 0 || chan > 15 ||
parm < 0 || parm > 127)
return (EINVAL);
buf[0] = MIDI_CTL_CHANGE | chan;
buf[1] = parm;
buf[2] = w14 & 0x7f;
return (midiseq_out(md, buf, 3, 1));
}
int
midiseq_pitchbend(struct midi_dev *md, int chan, int parm)
{
u_char buf[3];
if (chan < 0 || chan > 15)
return (EINVAL);
buf[0] = MIDI_PITCH_BEND | chan;
buf[1] = parm & 0x7f;
buf[2] = (parm >> 7) & 0x7f;
return (midiseq_out(md, buf, 3, 1));
}
int
midiseq_loadpatch(struct midi_dev *md, struct sysex_info *sysex, struct uio *uio)
{
u_char c, buf[128];
int i, cc, error;
if (sysex->key != SEQ_SYSEX_PATCH) {
DPRINTF(("midiseq_loadpatch: bad patch key 0x%04x\n",
sysex->key));
return (EINVAL);
}
if (uio->uio_resid < sysex->len)
/* adjust length, should be an error */
sysex->len = uio->uio_resid;
DPRINTFN(2, ("midiseq_loadpatch: len=%d\n", sysex->len));
if (sysex->len == 0)
return (EINVAL);
error = uiomove(&c, 1, uio);
if (error)
return error;
if (c != MIDI_SYSEX_START) /* must start like this */
return (EINVAL);
error = midiseq_out(md, &c, 1, 0);
if (error)
return (error);
--sysex->len;
while (sysex->len > 0) {
cc = sysex->len;
if (cc > sizeof buf)
cc = sizeof buf;
error = uiomove(buf, cc, uio);
if (error)
break;
for(i = 0; i < cc && !MIDI_IS_STATUS(buf[i]); i++)
;
error = midiseq_out(md, buf, i, 0);
if (error)
break;
sysex->len -= i;
if (i != cc)
break;
}
/* Any leftover data in uio is rubbish;
* the SYSEX should be one write ending in SYSEX_END.
*/
uio->uio_resid = 0;
c = MIDI_SYSEX_END;
return (midiseq_out(md, &c, 1, 0));
}
int
midiseq_putc(struct midi_dev *md, int data)
{
u_char c = data;
DPRINTFN(4,("midiseq_putc: 0x%02x\n", data));
return (midiseq_out(md, &c, 1, 0));
}
#include "midi.h"
#if NMIDI == 0
/*
* If someone has a sequencer, but no midi devices there will
* be unresolved references, so we provide little stubs.
*/
int
midi_unit_count(void)
{
return (0);
}
int
midiopen(dev_t dev, int flags, int ifmt, struct proc *p)
{
return (ENXIO);
}
struct cfdriver midi_cd;
void
midi_getinfo(dev_t dev, struct midi_info *mi)
{
}
int
midiclose(dev_t dev, int flags, int ifmt, struct proc *p)
{
return (ENXIO);
}
int
midi_writebytes(int unit, u_char *buf, int cc)
{
return (ENXIO);
}
#endif /* NMIDI == 0 */
|