1 /* $NetBSD: sequencer.c,v 1.51 2009/01/11 10:40:27 cegger Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss@NetBSD.org).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: sequencer.c,v 1.51 2009/01/11 10:40:27 cegger Exp $");
35 #include "sequencer.h"
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/fcntl.h>
40 #include <sys/vnode.h>
41 #include <sys/select.h>
43 #include <sys/malloc.h>
45 #include <sys/systm.h>
46 #include <sys/syslog.h>
47 #include <sys/kernel.h>
48 #include <sys/signalvar.h>
50 #include <sys/audioio.h>
51 #include <sys/midiio.h>
52 #include <sys/device.h>
55 #include <dev/midi_if.h>
56 #include <dev/midivar.h>
57 #include <dev/sequencervar.h>
59 #define ADDTIMEVAL(a, b) ( \
60 (a)->tv_sec += (b)->tv_sec, \
61 (a)->tv_usec += (b)->tv_usec, \
62 (a)->tv_usec > 1000000 ? ((a)->tv_sec++, (a)->tv_usec -= 1000000) : 0\
65 #define SUBTIMEVAL(a, b) ( \
66 (a)->tv_sec -= (b)->tv_sec, \
67 (a)->tv_usec -= (b)->tv_usec, \
68 (a)->tv_usec < 0 ? ((a)->tv_sec--, (a)->tv_usec += 1000000) : 0\
72 #define DPRINTF(x) if (sequencerdebug) printf x
73 #define DPRINTFN(n,x) if (sequencerdebug >= (n)) printf x
74 int sequencerdebug
= 0;
80 #define SEQ_NOTE_MAX 128
81 #define SEQ_NOTE_XXX 255
83 #define RECALC_USPERDIV(t) \
84 ((t)->usperdiv = 60*1000000L/((t)->tempo_beatpermin*(t)->timebase_divperbeat))
86 struct sequencer_softc seqdevs
[NSEQUENCER
];
88 void sequencerattach(int);
89 static void seq_reset(struct sequencer_softc
*);
90 static int seq_do_command(struct sequencer_softc
*, seq_event_t
*);
91 static int seq_do_chnvoice(struct sequencer_softc
*, seq_event_t
*);
92 static int seq_do_chncommon(struct sequencer_softc
*, seq_event_t
*);
93 static void seq_timer_waitabs(struct sequencer_softc
*, uint32_t);
94 static int seq_do_timing(struct sequencer_softc
*, seq_event_t
*);
95 static int seq_do_local(struct sequencer_softc
*, seq_event_t
*);
96 static int seq_do_sysex(struct sequencer_softc
*, seq_event_t
*);
97 static int seq_do_fullsize(struct sequencer_softc
*, seq_event_t
*, struct uio
*);
98 static int seq_input_event(struct sequencer_softc
*, seq_event_t
*);
99 static int seq_drain(struct sequencer_softc
*);
100 static void seq_startoutput(struct sequencer_softc
*);
101 static void seq_timeout(void *);
102 static int seq_to_new(seq_event_t
*, struct uio
*);
103 static int seq_sleep_timo(int *, const char *, int);
104 static int seq_sleep(int *, const char *);
105 static void seq_wakeup(int *);
106 static void seq_softintr(void *);
109 static int midiseq_out(struct midi_dev
*, u_char
*, u_int
, int);
110 static struct midi_dev
*midiseq_open(int, int);
111 static void midiseq_close(struct midi_dev
*);
112 static void midiseq_reset(struct midi_dev
*);
113 static int midiseq_noteon(struct midi_dev
*, int, int, seq_event_t
*);
114 static int midiseq_noteoff(struct midi_dev
*, int, int, seq_event_t
*);
115 static int midiseq_keypressure(struct midi_dev
*, int, int, seq_event_t
*);
116 static int midiseq_pgmchange(struct midi_dev
*, int, seq_event_t
*);
117 static int midiseq_chnpressure(struct midi_dev
*, int, seq_event_t
*);
118 static int midiseq_ctlchange(struct midi_dev
*, int, seq_event_t
*);
119 static int midiseq_pitchbend(struct midi_dev
*, int, seq_event_t
*);
120 static int midiseq_loadpatch(struct midi_dev
*, struct sysex_info
*, struct uio
*);
121 void midiseq_in(struct midi_dev
*, u_char
*, int);
123 static dev_type_open(sequenceropen
);
124 static dev_type_close(sequencerclose
);
125 static dev_type_read(sequencerread
);
126 static dev_type_write(sequencerwrite
);
127 static dev_type_ioctl(sequencerioctl
);
128 static dev_type_poll(sequencerpoll
);
129 static dev_type_kqfilter(sequencerkqfilter
);
131 const struct cdevsw sequencer_cdevsw
= {
132 sequenceropen
, sequencerclose
, sequencerread
, sequencerwrite
,
133 sequencerioctl
, nostop
, notty
, sequencerpoll
, nommap
,
134 sequencerkqfilter
, D_OTHER
,
138 sequencerattach(int n
)
140 struct sequencer_softc
*sc
;
142 for (n
= 0; n
< NSEQUENCER
; n
++) {
144 callout_init(&sc
->sc_callout
, 0);
145 sc
->sih
= softint_establish(SOFTINT_SERIAL
, seq_softintr
, sc
);
150 sequenceropen(dev_t dev
, int flags
, int ifmt
, struct lwp
*l
)
152 int unit
= SEQUENCERUNIT(dev
);
153 struct sequencer_softc
*sc
;
157 DPRINTF(("sequenceropen\n"));
159 if (unit
>= NSEQUENCER
)
164 if (SEQ_IS_OLD(unit
))
169 sc
->flags
= flags
& (FREAD
|FWRITE
);
174 sc
->input_stamp
= ~0;
177 nmidi
= midi_unit_count();
179 sc
->devs
= malloc(nmidi
* sizeof(struct midi_dev
*),
181 for (unit
= 0; unit
< nmidi
; unit
++) {
182 md
= midiseq_open(unit
, flags
);
184 sc
->devs
[sc
->nmidi
++] = md
;
190 sc
->timer
.timebase_divperbeat
= 100;
191 sc
->timer
.tempo_beatpermin
= 60;
192 RECALC_USPERDIV(&sc
->timer
);
193 sc
->timer
.divs_lastevent
= sc
->timer
.divs_lastchange
= 0;
194 microtime(&sc
->timer
.reftime
);
197 SEQ_QINIT(&sc
->outq
);
198 sc
->lowat
= SEQ_MAXQ
/ 2;
202 DPRINTF(("sequenceropen: mode=%d, nmidi=%d\n", sc
->mode
, sc
->nmidi
));
207 seq_sleep_timo(int *chan
, const char *label
, int timo
)
214 DPRINTFN(5, ("seq_sleep_timo: %p %s %d\n", chan
, label
, timo
));
216 st
= tsleep(chan
, PWAIT
| PCATCH
, label
, timo
);
220 printf("seq_sleep: %d\n", st
);
226 seq_sleep(int *chan
, const char *label
)
228 return seq_sleep_timo(chan
, label
, 0);
232 seq_wakeup(int *chan
)
235 DPRINTFN(5, ("seq_wakeup: %p\n", chan
));
242 seq_drain(struct sequencer_softc
*sc
)
246 DPRINTFN(3, ("seq_drain: %p, len=%d\n", sc
, SEQ_QLEN(&sc
->outq
)));
249 while(!SEQ_QEMPTY(&sc
->outq
) && !error
)
250 error
= seq_sleep_timo(&sc
->wchan
, "seq_dr", 60*hz
);
255 seq_timeout(void *addr
)
257 struct sequencer_softc
*sc
= addr
;
260 DPRINTFN(4, ("seq_timeout: %p\n", sc
));
263 if (SEQ_QLEN(&sc
->outq
) < sc
->lowat
) {
264 seq_wakeup(&sc
->wchan
);
265 selnotify(&sc
->wsel
, 0, 0);
266 if (sc
->async
!= NULL
) {
267 mutex_enter(proc_lock
);
268 if ((p
= sc
->async
) != NULL
)
270 mutex_exit(proc_lock
);
277 seq_startoutput(struct sequencer_softc
*sc
)
279 struct sequencer_queue
*q
= &sc
->outq
;
284 DPRINTFN(4, ("seq_startoutput: %p, len=%d\n", sc
, SEQ_QLEN(q
)));
285 while(!SEQ_QEMPTY(q
) && !sc
->timeout
) {
287 seq_do_command(sc
, &cmd
);
292 sequencerclose(dev_t dev
, int flags
, int ifmt
,
295 struct sequencer_softc
*sc
= &seqdevs
[SEQUENCERUNIT(dev
)];
298 DPRINTF(("sequencerclose: %p\n", sc
));
303 callout_stop(&sc
->sc_callout
);
308 for (n
= 0; n
< sc
->nmidi
; n
++)
309 midiseq_close(sc
->devs
[n
]);
310 free(sc
->devs
, M_DEVBUF
);
316 seq_softintr(void *cookie
)
318 struct sequencer_softc
*sc
= cookie
;
321 seq_wakeup(&sc
->rchan
);
322 selnotify(&sc
->rsel
, 0, 0);
323 if (sc
->async
!= NULL
) {
324 mutex_enter(proc_lock
);
325 if ((p
= sc
->async
) != NULL
)
327 mutex_exit(proc_lock
);
332 seq_input_event(struct sequencer_softc
*sc
, seq_event_t
*cmd
)
334 struct sequencer_queue
*q
= &sc
->inq
;
336 DPRINTFN(2, ("seq_input_event: %02x %02x %02x %02x %02x %02x %02x %02x\n",
338 cmd
->unknown
.byte
[0], cmd
->unknown
.byte
[1],
339 cmd
->unknown
.byte
[2], cmd
->unknown
.byte
[3],
340 cmd
->unknown
.byte
[4], cmd
->unknown
.byte
[5],
341 cmd
->unknown
.byte
[6]));
345 softint_schedule(sc
->sih
);
350 seq_event_intr(void *addr
, seq_event_t
*iev
)
352 struct sequencer_softc
*sc
= addr
;
359 if (!sc
->timer
.running
)
360 now
= sc
->timer
.stoptime
;
361 SUBTIMEVAL(&now
, &sc
->timer
.reftime
);
362 t
= now
.tv_sec
* 1000000 + now
.tv_usec
;
363 t
/= sc
->timer
.usperdiv
;
364 t
+= sc
->timer
.divs_lastchange
;
366 if (t
!= sc
->input_stamp
) {
367 seq_input_event(sc
, &SEQ_MK_TIMING(WAIT_ABS
, .divisions
=t
));
368 sc
->input_stamp
= t
; /* XXX wha hoppen if timer is reset? */
370 seq_input_event(sc
, iev
);
374 sequencerread(dev_t dev
, struct uio
*uio
, int ioflag
)
376 struct sequencer_softc
*sc
= &seqdevs
[SEQUENCERUNIT(dev
)];
377 struct sequencer_queue
*q
= &sc
->inq
;
381 DPRINTFN(20, ("sequencerread: %p, count=%d, ioflag=%x\n",
382 sc
, (int) uio
->uio_resid
, ioflag
));
384 if (sc
->mode
== SEQ_OLD
) {
385 DPRINTFN(-1,("sequencerread: old read\n"));
386 return (EINVAL
); /* XXX unimplemented */
390 while (SEQ_QEMPTY(q
)) {
391 if (ioflag
& IO_NDELAY
)
394 error
= seq_sleep(&sc
->rchan
, "seq rd");
400 while (uio
->uio_resid
>= sizeof ev
&& !error
&& !SEQ_QEMPTY(q
)) {
402 error
= uiomove(&ev
, sizeof ev
, uio
);
409 sequencerwrite(dev_t dev
, struct uio
*uio
, int ioflag
)
411 struct sequencer_softc
*sc
= &seqdevs
[SEQUENCERUNIT(dev
)];
412 struct sequencer_queue
*q
= &sc
->outq
;
417 DPRINTFN(2, ("sequencerwrite: %p, count=%d\n", sc
, (int) uio
->uio_resid
));
420 size
= sc
->mode
== SEQ_NEW
? sizeof cmdbuf
: SEQOLD_CMDSIZE
;
421 while (uio
->uio_resid
>= size
) {
422 error
= uiomove(&cmdbuf
, size
, uio
);
425 if (sc
->mode
== SEQ_OLD
)
426 if (seq_to_new(&cmdbuf
, uio
))
428 if (cmdbuf
.tag
== SEQ_FULLSIZE
) {
429 /* We do it like OSS does, asynchronously */
430 error
= seq_do_fullsize(sc
, &cmdbuf
, uio
);
435 while (SEQ_QFULL(q
)) {
438 if (ioflag
& IO_NDELAY
)
440 error
= seq_sleep(&sc
->wchan
, "seq_wr");
449 #ifdef SEQUENCER_DEBUG
451 DPRINTFN(2, ("sequencerwrite: error=%d\n", error
));
457 sequencerioctl(dev_t dev
, u_long cmd
, void *addr
, int flag
,
460 struct sequencer_softc
*sc
= &seqdevs
[SEQUENCERUNIT(dev
)];
461 struct synth_info
*si
;
468 DPRINTFN(2, ("sequencerioctl: %p cmd=0x%08lx\n", sc
, cmd
));
473 /* All handled in the upper FS layer. */
480 sc
->async
= l
->l_proc
;
481 DPRINTF(("sequencer_ioctl: FIOASYNC %p\n", l
));
486 case SEQUENCER_RESET
:
490 case SEQUENCER_PANIC
:
492 /* Do more? OSS doesn't */
496 if (sc
->flags
== FREAD
)
503 si
= (struct synth_info
*)addr
;
505 if (devno
< 0 || devno
>= sc
->nmidi
)
507 md
= sc
->devs
[devno
];
508 strncpy(si
->name
, md
->name
, sizeof si
->name
);
509 si
->synth_type
= SYNTH_TYPE_MIDI
;
510 si
->synth_subtype
= md
->subtype
;
511 si
->nr_voices
= md
->nr_voices
;
512 si
->instr_bank_size
= md
->instr_bank_size
;
513 si
->capabilities
= md
->capabilities
;
516 case SEQUENCER_NRSYNTHS
:
517 *(int *)addr
= sc
->nmidi
;
520 case SEQUENCER_NRMIDIS
:
521 *(int *)addr
= sc
->nmidi
;
524 case SEQUENCER_OUTOFBAND
:
525 DPRINTFN(3, ("sequencer_ioctl: OOB=%02x %02x %02x %02x %02x %02x %02x %02x\n",
526 *(u_char
*)addr
, *((u_char
*)addr
+1),
527 *((u_char
*)addr
+2), *((u_char
*)addr
+3),
528 *((u_char
*)addr
+4), *((u_char
*)addr
+5),
529 *((u_char
*)addr
+6), *((u_char
*)addr
+7)));
530 if ( !(sc
->flags
& FWRITE
) )
532 error
= seq_do_command(sc
, (seq_event_t
*)addr
);
535 case SEQUENCER_TMR_TIMEBASE
:
543 sc
->timer
.timebase_divperbeat
= t
;
544 sc
->timer
.divs_lastchange
= sc
->timer
.divs_lastevent
;
545 microtime(&sc
->timer
.reftime
);
546 RECALC_USPERDIV(&sc
->timer
);
550 case SEQUENCER_TMR_START
:
552 error
= seq_do_timing(sc
, &SEQ_MK_TIMING(START
));
556 case SEQUENCER_TMR_STOP
:
558 error
= seq_do_timing(sc
, &SEQ_MK_TIMING(STOP
));
562 case SEQUENCER_TMR_CONTINUE
:
564 error
= seq_do_timing(sc
, &SEQ_MK_TIMING(CONTINUE
));
568 case SEQUENCER_TMR_TEMPO
:
570 error
= seq_do_timing(sc
,
571 &SEQ_MK_TIMING(TEMPO
, .bpm
=*(int *)addr
));
574 *(int *)addr
= sc
->timer
.tempo_beatpermin
;
577 case SEQUENCER_TMR_SOURCE
:
578 *(int *)addr
= SEQUENCER_TMR_INTERNAL
;
581 case SEQUENCER_TMR_METRONOME
:
585 case SEQUENCER_THRESHOLD
:
586 t
= SEQ_MAXQ
- *(int *)addr
/ sizeof (seq_event_rec
);
594 case SEQUENCER_CTRLRATE
:
596 *(int *)addr
= (sc
->timer
.tempo_beatpermin
597 *sc
->timer
.timebase_divperbeat
+ 30) / 60;
601 case SEQUENCER_GETTIME
:
607 SUBTIMEVAL(&now
, &sc
->timer
.reftime
);
608 tx
= now
.tv_sec
* 1000000 + now
.tv_usec
;
609 tx
/= sc
->timer
.usperdiv
;
610 tx
+= sc
->timer
.divs_lastchange
;
617 DPRINTFN(-1,("sequencer_ioctl: unimpl %08lx\n", cmd
));
625 sequencerpoll(dev_t dev
, int events
, struct lwp
*l
)
627 struct sequencer_softc
*sc
= &seqdevs
[SEQUENCERUNIT(dev
)];
630 DPRINTF(("sequencerpoll: %p events=0x%x\n", sc
, events
));
632 if (events
& (POLLIN
| POLLRDNORM
))
633 if ((sc
->flags
&FREAD
) && !SEQ_QEMPTY(&sc
->inq
))
634 revents
|= events
& (POLLIN
| POLLRDNORM
);
636 if (events
& (POLLOUT
| POLLWRNORM
))
637 if ((sc
->flags
&FWRITE
) && SEQ_QLEN(&sc
->outq
) < sc
->lowat
)
638 revents
|= events
& (POLLOUT
| POLLWRNORM
);
641 if ((sc
->flags
&FREAD
) && (events
& (POLLIN
| POLLRDNORM
)))
642 selrecord(l
, &sc
->rsel
);
644 if ((sc
->flags
&FWRITE
) && (events
& (POLLOUT
| POLLWRNORM
)))
645 selrecord(l
, &sc
->wsel
);
652 filt_sequencerrdetach(struct knote
*kn
)
654 struct sequencer_softc
*sc
= kn
->kn_hook
;
658 SLIST_REMOVE(&sc
->rsel
.sel_klist
, kn
, knote
, kn_selnext
);
663 filt_sequencerread(struct knote
*kn
, long hint
)
665 struct sequencer_softc
*sc
= kn
->kn_hook
;
667 /* XXXLUKEM (thorpej): make sure this is correct */
669 if (SEQ_QEMPTY(&sc
->inq
))
671 kn
->kn_data
= sizeof(seq_event_rec
);
675 static const struct filterops sequencerread_filtops
=
676 { 1, NULL
, filt_sequencerrdetach
, filt_sequencerread
};
679 filt_sequencerwdetach(struct knote
*kn
)
681 struct sequencer_softc
*sc
= kn
->kn_hook
;
685 SLIST_REMOVE(&sc
->wsel
.sel_klist
, kn
, knote
, kn_selnext
);
690 filt_sequencerwrite(struct knote
*kn
, long hint
)
692 struct sequencer_softc
*sc
= kn
->kn_hook
;
694 /* XXXLUKEM (thorpej): make sure this is correct */
696 if (SEQ_QLEN(&sc
->outq
) >= sc
->lowat
)
698 kn
->kn_data
= sizeof(seq_event_rec
);
702 static const struct filterops sequencerwrite_filtops
=
703 { 1, NULL
, filt_sequencerwdetach
, filt_sequencerwrite
};
706 sequencerkqfilter(dev_t dev
, struct knote
*kn
)
708 struct sequencer_softc
*sc
= &seqdevs
[SEQUENCERUNIT(dev
)];
712 switch (kn
->kn_filter
) {
714 klist
= &sc
->rsel
.sel_klist
;
715 kn
->kn_fop
= &sequencerread_filtops
;
719 klist
= &sc
->wsel
.sel_klist
;
720 kn
->kn_fop
= &sequencerwrite_filtops
;
730 SLIST_INSERT_HEAD(klist
, kn
, kn_selnext
);
737 seq_reset(struct sequencer_softc
*sc
)
742 if ( !(sc
->flags
& FWRITE
) )
744 for (i
= 0; i
< sc
->nmidi
; i
++) {
747 for (chn
= 0; chn
< MAXCHAN
; chn
++) {
748 midiseq_ctlchange(md
, chn
, &SEQ_MK_CHN(CTL_CHANGE
,
749 .controller
=MIDI_CTRL_NOTES_OFF
));
750 midiseq_ctlchange(md
, chn
, &SEQ_MK_CHN(CTL_CHANGE
,
751 .controller
=MIDI_CTRL_RESET
));
752 midiseq_pitchbend(md
, chn
, &SEQ_MK_CHN(PITCH_BEND
,
753 .value
=MIDI_BEND_NEUTRAL
));
759 seq_do_command(struct sequencer_softc
*sc
, seq_event_t
*b
)
763 DPRINTFN(4, ("seq_do_command: %p cmd=0x%02x\n", sc
, b
->timing
.op
));
767 return seq_do_local(sc
, b
);
769 return seq_do_timing(sc
, b
);
771 return seq_do_chnvoice(sc
, b
);
773 return seq_do_chncommon(sc
, b
);
775 return seq_do_sysex(sc
, b
);
777 case SEQOLD_MIDIPUTC
:
778 dev
= b
->putc
.device
;
779 if (dev
< 0 || dev
>= sc
->nmidi
)
781 return midiseq_out(sc
->devs
[dev
], &b
->putc
.byte
, 1, 0);
783 DPRINTFN(-1,("seq_do_command: unimpl command %02x\n", b
->tag
));
789 seq_do_chnvoice(struct sequencer_softc
*sc
, seq_event_t
*b
)
795 dev
= b
->voice
.device
;
796 if (dev
< 0 || dev
>= sc
->nmidi
||
797 b
->voice
.channel
> 15 ||
798 b
->voice
.key
>= SEQ_NOTE_MAX
)
801 switch(b
->voice
.op
) {
802 case MIDI_NOTEON
: /* no need to special-case hidden noteoff here */
803 error
= midiseq_noteon(md
, b
->voice
.channel
, b
->voice
.key
, b
);
806 error
= midiseq_noteoff(md
, b
->voice
.channel
, b
->voice
.key
, b
);
808 case MIDI_KEY_PRESSURE
:
809 error
= midiseq_keypressure(md
,
810 b
->voice
.channel
, b
->voice
.key
, b
);
813 DPRINTFN(-1,("seq_do_chnvoice: unimpl command %02x\n",
822 seq_do_chncommon(struct sequencer_softc
*sc
, seq_event_t
*b
)
828 dev
= b
->common
.device
;
829 if (dev
< 0 || dev
>= sc
->nmidi
||
830 b
->common
.channel
> 15)
833 DPRINTFN(2,("seq_do_chncommon: %02x\n", b
->common
.op
));
836 switch(b
->common
.op
) {
837 case MIDI_PGM_CHANGE
:
838 error
= midiseq_pgmchange(md
, b
->common
.channel
, b
);
840 case MIDI_CTL_CHANGE
:
841 error
= midiseq_ctlchange(md
, b
->common
.channel
, b
);
843 case MIDI_PITCH_BEND
:
844 error
= midiseq_pitchbend(md
, b
->common
.channel
, b
);
846 case MIDI_CHN_PRESSURE
:
847 error
= midiseq_chnpressure(md
, b
->common
.channel
, b
);
850 DPRINTFN(-1,("seq_do_chncommon: unimpl command %02x\n",
859 seq_do_local(struct sequencer_softc
*sc
, seq_event_t
*b
)
865 seq_do_sysex(struct sequencer_softc
*sc
, seq_event_t
*b
)
869 uint8_t *bf
= b
->sysex
.buffer
;
871 dev
= b
->sysex
.device
;
872 if (dev
< 0 || dev
>= sc
->nmidi
)
874 DPRINTF(("seq_do_sysex: dev=%d\n", dev
));
877 if (!md
->doingsysex
) {
878 midiseq_out(md
, (uint8_t[]){MIDI_SYSEX_START
}, 1, 0);
882 for (i
= 0; i
< 6 && bf
[i
] != 0xff; i
++)
884 midiseq_out(md
, bf
, i
, 0);
885 if (i
< 6 || (i
> 0 && bf
[i
-1] == MIDI_SYSEX_END
))
891 seq_timer_waitabs(struct sequencer_softc
*sc
, uint32_t divs
)
899 t
->divs_lastevent
= divs
;
900 divs
-= t
->divs_lastchange
;
901 usec
= (long long)divs
* (long long)t
->usperdiv
; /* convert to usec */
902 when
.tv_sec
= usec
/ 1000000;
903 when
.tv_usec
= usec
% 1000000;
904 DPRINTFN(4, ("seq_timer_waitabs: adjdivs=%d, sleep when=%"PRId64
".%06"PRId64
,
905 divs
, when
.tv_sec
, (uint64_t)when
.tv_usec
));
906 ADDTIMEVAL(&when
, &t
->reftime
); /* abstime for end */
907 ticks
= tvhzto(&when
);
908 DPRINTFN(4, (" when+start=%"PRId64
".%06"PRId64
", tick=%d\n",
909 when
.tv_sec
, (uint64_t)when
.tv_usec
, ticks
));
912 if (ticks
> 20 * hz
) {
913 /* Waiting more than 20s */
914 printf("seq_timer_waitabs: funny ticks=%d, "
915 "usec=%lld\n", ticks
, usec
);
919 callout_reset(&sc
->sc_callout
, ticks
,
922 #ifdef SEQUENCER_DEBUG
924 DPRINTF(("seq_timer_waitabs: ticks = %d\n", ticks
));
929 seq_do_timing(struct sequencer_softc
*sc
, seq_event_t
*b
)
931 struct syn_timer
*t
= &sc
->timer
;
936 switch(b
->timing
.op
) {
938 seq_timer_waitabs(sc
,
939 b
->t_WAIT_REL
.divisions
+ t
->divs_lastevent
);
942 seq_timer_waitabs(sc
, b
->t_WAIT_ABS
.divisions
);
945 microtime(&t
->reftime
);
946 t
->divs_lastevent
= t
->divs_lastchange
= 0;
950 microtime(&t
->stoptime
);
957 SUBTIMEVAL(&when
, &t
->stoptime
);
958 ADDTIMEVAL(&t
->reftime
, &when
);
962 /* bpm is unambiguously MIDI clocks per minute / 24 */
963 /* (24 MIDI clocks are usually but not always a quarter note) */
964 if (b
->t_TEMPO
.bpm
< 8) /* where are these limits specified? */
965 t
->tempo_beatpermin
= 8;
966 else if (b
->t_TEMPO
.bpm
> 360) /* ? */
967 t
->tempo_beatpermin
= 360;
969 t
->tempo_beatpermin
= b
->t_TEMPO
.bpm
;
970 t
->divs_lastchange
= t
->divs_lastevent
;
971 microtime(&t
->reftime
);
975 error
= seq_input_event(sc
, b
);
978 t
->divs_lastevent
= t
->divs_lastchange
= 0;
979 microtime(&t
->reftime
);
983 DPRINTF(("seq_do_timing: unimplemented %02x\n", b
->timing
.op
));
984 error
= EINVAL
; /* not quite accurate... */
987 DPRINTF(("seq_timer: unknown %02x\n", b
->timing
.op
));
995 seq_do_fullsize(struct sequencer_softc
*sc
, seq_event_t
*b
, struct uio
*uio
)
997 struct sysex_info sysex
;
1001 if (sizeof(seq_event_rec
) != SEQ_SYSEX_HDRSIZE
) {
1002 printf("seq_do_fullsize: sysex size ??\n");
1006 memcpy(&sysex
, b
, sizeof sysex
);
1007 dev
= sysex
.device_no
;
1008 if (/* dev < 0 || */ dev
>= sc
->nmidi
)
1010 DPRINTFN(2, ("seq_do_fullsize: fmt=%04x, dev=%d, len=%d\n",
1011 sysex
.key
, dev
, sysex
.len
));
1012 return (midiseq_loadpatch(sc
->devs
[dev
], &sysex
, uio
));
1016 * Convert an old sequencer event to a new one.
1017 * NOTE: on entry, *ev may contain valid data only in the first 4 bytes.
1018 * That may be true even on exit (!) in the case of SEQOLD_MIDIPUTC; the
1019 * caller will only look at the first bytes in that case anyway. Ugly? Sure.
1022 seq_to_new(seq_event_t
*ev
, struct uio
*uio
)
1024 int cmd
, chan
, note
, parm
;
1030 bfp
= ev
->unknown
.byte
;
1034 DPRINTFN(3, ("seq_to_new: 0x%02x %d %d %d\n", cmd
, chan
, note
, parm
));
1037 /* Fill the event record */
1038 if (uio
->uio_resid
>= sizeof *ev
- SEQOLD_CMDSIZE
) {
1039 error
= uiomove(bfp
, sizeof *ev
- SEQOLD_CMDSIZE
, uio
);
1047 case SEQOLD_NOTEOFF
:
1049 * What's with the SEQ_NOTE_XXX? In OSS this seems to have
1050 * been undocumented magic for messing with the overall volume
1051 * of a 'voice', equated precariously with 'channel' and
1052 * pretty much unimplementable except by directly frobbing a
1053 * synth chip. For us, who treat everything as interfaced over
1054 * MIDI, this will just be unceremoniously discarded as
1055 * invalid in midiseq_noteoff, making the whole event an
1056 * elaborate no-op, and that doesn't seem to be any different
1057 * from what happens on linux with a MIDI-interfaced device,
1058 * by the way. The moral is ... use the new /dev/music API, ok?
1060 *ev
= SEQ_MK_CHN(NOTEOFF
, .device
=0, .channel
=chan
,
1061 .key
=SEQ_NOTE_XXX
, .velocity
=parm
);
1064 *ev
= SEQ_MK_CHN(NOTEON
,
1065 .device
=0, .channel
=chan
, .key
=note
, .velocity
=parm
);
1069 * This event cannot even /exist/ on non-littleendian machines,
1070 * and so help me, that's exactly the way OSS defined it.
1071 * Also, the OSS programmer's guide states (p. 74, v1.11)
1072 * that seqold time units are system clock ticks, unlike
1073 * the new 'divisions' which are determined by timebase. In
1074 * that case we would need to do scaling here - but no such
1075 * behavior is visible in linux either--which also treats this
1076 * value, surprisingly, as an absolute, not relative, time.
1077 * My guess is that this event has gone unused so long that
1078 * nobody could agree we got it wrong no matter what we do.
1080 tmp_delay
= *(uint32_t *)ev
>> 8;
1081 *ev
= SEQ_MK_TIMING(WAIT_ABS
, .divisions
=tmp_delay
);
1083 case SEQOLD_SYNCTIMER
:
1085 * The TMR_RESET event is not defined in any OSS materials
1086 * I can find; it may have been invented here just to provide
1087 * an accurate _to_new translation of this event.
1089 *ev
= SEQ_MK_TIMING(RESET
);
1091 case SEQOLD_PGMCHANGE
:
1092 *ev
= SEQ_MK_CHN(PGM_CHANGE
,
1093 .device
=0, .channel
=chan
, .program
=note
);
1095 case SEQOLD_MIDIPUTC
:
1096 break; /* interpret in normal mode */
1098 case SEQOLD_PRIVATE
:
1099 case SEQOLD_EXTENDED
:
1101 DPRINTF(("seq_to_new: not impl 0x%02x\n", cmd
));
1103 /* In case new-style events show up */
1106 case SEQ_CHN_COMMON
:
1113 /**********************************************/
1116 midiseq_in(struct midi_dev
*md
, u_char
*msg
, int len
)
1118 int unit
= md
->unit
;
1122 DPRINTFN(2, ("midiseq_in: %p %02x %02x %02x\n",
1123 md
, msg
[0], msg
[1], msg
[2]));
1125 status
= MIDI_GET_STATUS(msg
[0]);
1126 chan
= MIDI_GET_CHAN(msg
[0]);
1128 case MIDI_NOTEON
: /* midi(4) always canonicalizes hidden note-off */
1129 ev
= SEQ_MK_CHN(NOTEON
, .device
=unit
, .channel
=chan
,
1130 .key
=msg
[1], .velocity
=msg
[2]);
1133 ev
= SEQ_MK_CHN(NOTEOFF
, .device
=unit
, .channel
=chan
,
1134 .key
=msg
[1], .velocity
=msg
[2]);
1136 case MIDI_KEY_PRESSURE
:
1137 ev
= SEQ_MK_CHN(KEY_PRESSURE
, .device
=unit
, .channel
=chan
,
1138 .key
=msg
[1], .pressure
=msg
[2]);
1140 case MIDI_CTL_CHANGE
: /* XXX not correct for MSB */
1141 ev
= SEQ_MK_CHN(CTL_CHANGE
, .device
=unit
, .channel
=chan
,
1142 .controller
=msg
[1], .value
=msg
[2]);
1144 case MIDI_PGM_CHANGE
:
1145 ev
= SEQ_MK_CHN(PGM_CHANGE
, .device
=unit
, .channel
=chan
,
1148 case MIDI_CHN_PRESSURE
:
1149 ev
= SEQ_MK_CHN(CHN_PRESSURE
, .device
=unit
, .channel
=chan
,
1152 case MIDI_PITCH_BEND
:
1153 ev
= SEQ_MK_CHN(PITCH_BEND
, .device
=unit
, .channel
=chan
,
1154 .value
=(msg
[1] & 0x7f) | ((msg
[2] & 0x7f) << 7));
1156 default: /* this is now the point where MIDI_ACKs disappear */
1159 seq_event_intr(md
->seq
, &ev
);
1162 static struct midi_dev
*
1163 midiseq_open(int unit
, int flags
)
1165 extern struct cfdriver midi_cd
;
1167 struct midi_dev
*md
;
1168 struct midi_softc
*sc
;
1169 struct midi_info mi
;
1173 major
= devsw_name2chr("midi", NULL
, 0);
1174 dev
= makedev(major
, unit
);
1176 midi_getinfo(dev
, &mi
);
1177 if ( !(mi
.props
& MIDI_PROP_CAN_INPUT
) )
1179 if ( 0 == ( flags
& ( FREAD
| FWRITE
) ) )
1181 DPRINTFN(2, ("midiseq_open: %d %d\n", unit
, flags
));
1182 error
= cdev_open(dev
, flags
, 0, 0);
1185 sc
= device_lookup_private(&midi_cd
, unit
);
1187 md
= malloc(sizeof *md
, M_DEVBUF
, M_WAITOK
|M_ZERO
);
1193 md
->nr_voices
= 128; /* XXX */
1194 md
->instr_bank_size
= 128; /* XXX */
1195 if (mi
.props
& MIDI_PROP_CAN_INPUT
)
1196 md
->capabilities
|= SYNTH_CAP_INPUT
;
1201 midiseq_close(struct midi_dev
*md
)
1206 major
= devsw_name2chr("midi", NULL
, 0);
1207 dev
= makedev(major
, md
->unit
);
1209 DPRINTFN(2, ("midiseq_close: %d\n", md
->unit
));
1210 cdev_close(dev
, 0, 0, 0);
1215 midiseq_reset(struct midi_dev
*md
)
1217 /* XXX send GM reset? */
1218 DPRINTFN(3, ("midiseq_reset: %d\n", md
->unit
));
1222 midiseq_out(struct midi_dev
*md
, u_char
*bf
, u_int cc
, int chk
)
1224 DPRINTFN(5, ("midiseq_out: m=%p, unit=%d, bf[0]=0x%02x, cc=%d\n",
1225 md
->msc
, md
->unit
, bf
[0], cc
));
1227 /* midi(4) does running status compression where appropriate. */
1228 return midi_writebytes(md
->unit
, bf
, cc
);
1232 * If the writing process hands us a hidden note-off in a note-on event,
1233 * we will simply write it that way; no need to special case it here,
1234 * as midi(4) will always canonicalize or compress as appropriate anyway.
1237 midiseq_noteon(struct midi_dev
*md
, int chan
, int key
, seq_event_t
*ev
)
1239 return midiseq_out(md
, (uint8_t[]){
1240 MIDI_NOTEON
| chan
, key
, ev
->c_NOTEON
.velocity
& 0x7f}, 3, 1);
1244 midiseq_noteoff(struct midi_dev
*md
, int chan
, int key
, seq_event_t
*ev
)
1246 return midiseq_out(md
, (uint8_t[]){
1247 MIDI_NOTEOFF
| chan
, key
, ev
->c_NOTEOFF
.velocity
& 0x7f}, 3, 1);
1251 midiseq_keypressure(struct midi_dev
*md
, int chan
, int key
, seq_event_t
*ev
)
1253 return midiseq_out(md
, (uint8_t[]){
1254 MIDI_KEY_PRESSURE
| chan
, key
,
1255 ev
->c_KEY_PRESSURE
.pressure
& 0x7f}, 3, 1);
1259 midiseq_pgmchange(struct midi_dev
*md
, int chan
, seq_event_t
*ev
)
1261 if (ev
->c_PGM_CHANGE
.program
> 127)
1263 return midiseq_out(md
, (uint8_t[]){
1264 MIDI_PGM_CHANGE
| chan
, ev
->c_PGM_CHANGE
.program
}, 2, 1);
1268 midiseq_chnpressure(struct midi_dev
*md
, int chan
, seq_event_t
*ev
)
1270 if (ev
->c_CHN_PRESSURE
.pressure
> 127)
1272 return midiseq_out(md
, (uint8_t[]){
1273 MIDI_CHN_PRESSURE
| chan
, ev
->c_CHN_PRESSURE
.pressure
}, 2, 1);
1277 midiseq_ctlchange(struct midi_dev
*md
, int chan
, seq_event_t
*ev
)
1279 if (ev
->c_CTL_CHANGE
.controller
> 127)
1281 return midiseq_out( md
, (uint8_t[]){
1282 MIDI_CTL_CHANGE
| chan
, ev
->c_CTL_CHANGE
.controller
,
1283 ev
->c_CTL_CHANGE
.value
& 0x7f /* XXX this is SO wrong */
1288 midiseq_pitchbend(struct midi_dev
*md
, int chan
, seq_event_t
*ev
)
1290 return midiseq_out(md
, (uint8_t[]){
1291 MIDI_PITCH_BEND
| chan
,
1292 ev
->c_PITCH_BEND
.value
& 0x7f,
1293 (ev
->c_PITCH_BEND
.value
>> 7) & 0x7f}, 3, 1);
1297 midiseq_loadpatch(struct midi_dev
*md
,
1298 struct sysex_info
*sysex
, struct uio
*uio
)
1303 if (sysex
->key
!= SEQ_SYSEX_PATCH
) {
1304 DPRINTFN(-1,("midiseq_loadpatch: bad patch key 0x%04x\n",
1308 if (uio
->uio_resid
< sysex
->len
)
1309 /* adjust length, should be an error */
1310 sysex
->len
= uio
->uio_resid
;
1312 DPRINTFN(2, ("midiseq_loadpatch: len=%d\n", sysex
->len
));
1313 if (sysex
->len
== 0)
1315 error
= uiomove(&c
, 1, uio
);
1318 if (c
!= MIDI_SYSEX_START
) /* must start like this */
1320 error
= midiseq_out(md
, &c
, 1, 0);
1324 while (sysex
->len
> 0) {
1328 error
= uiomove(bf
, cc
, uio
);
1331 for(i
= 0; i
< cc
&& !MIDI_IS_STATUS(bf
[i
]); i
++)
1334 * XXX midi(4)'s buffer might not accommodate this, and the
1335 * function will not block us (though in this case we have
1336 * a process and could in principle block).
1338 error
= midiseq_out(md
, bf
, i
, 0);
1346 * Any leftover data in uio is rubbish;
1347 * the SYSEX should be one write ending in SYSEX_END.
1351 return midiseq_out(md
, &c
, 1, 0);
1356 static dev_type_open(midiopen
);
1357 static dev_type_close(midiclose
);
1359 const struct cdevsw midi_cdevsw
= {
1360 midiopen
, midiclose
, noread
, nowrite
, noioctl
,
1361 nostop
, notty
, nopoll
, nommap
, nokqfilter
, D_OTHER
1365 * If someone has a sequencer, but no midi devices there will
1366 * be unresolved references, so we provide little stubs.
1370 midi_unit_count(void)
1376 midiopen(dev_t dev
, int flags
, int ifmt
, struct lwp
*l
)
1381 struct cfdriver midi_cd
;
1384 midi_getinfo(dev_t dev
, struct midi_info
*mi
)
1386 mi
->name
= "Dummy MIDI device";
1391 midiclose(dev_t dev
, int flags
, int ifmt
, struct lwp
*l
)
1397 midi_writebytes(int unit
, u_char
*bf
, int cc
)
1401 #endif /* NMIDI == 0 */