2 * Copyright (c) 2003 Mathew Kanner
3 * Copyright (c) 1993 Hannu Savolainen
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * The sequencer personality manager.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: head/sys/dev/sound/midi/sequencer.c 274035 2014-11-03 11:11:45Z bapt $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/filio.h>
40 #include <sys/sockio.h>
41 #include <sys/fcntl.h>
43 #include <sys/sysctl.h>
45 #include <sys/kernel.h> /* for DATA_SET */
47 #include <sys/module.h>
51 #include <sys/syslog.h>
52 #include <sys/errno.h>
53 #include <sys/malloc.h>
55 #include <machine/clock.h> /* for DELAY */
56 #include <sys/soundcard.h>
60 #include <sys/condvar.h>
61 #include <sys/kthread.h>
62 #include <sys/unistd.h>
63 #include <sys/device.h>
65 #ifdef HAVE_KERNEL_OPTION_HEADERS
69 #include <dev/sound/midi/midi.h>
70 #include <dev/sound/midi/midiq.h>
73 #include <dev/sound/midi/sequencer.h>
75 #define TMR_TIMERBASE 13
77 #define SND_DEV_SEQ 1 /* Sequencer output /dev/sequencer (FM
78 * synthesizer and MIDI output) */
79 #define SND_DEV_MUSIC 8 /* /dev/music, level 2 interface */
81 /* Length of a sequencer event. */
86 #define LOOKUP_EXIST (0)
87 #define LOOKUP_OPEN (1)
88 #define LOOKUP_CLOSE (2)
90 #define PCMMKMINOR(u, d, c) \
91 ((((c) & 0xff) << 16) | (((u) & 0x0f) << 4) | ((d) & 0x0f))
92 #define MIDIMKMINOR(u, d, c) PCMMKMINOR(u, d, c)
93 #define MIDIUNIT(y) ((dev2unit(y) >> 4) & 0x0f)
94 #define MIDIDEV(y) (dev2unit(y) & 0x0f)
96 /* These are the entries to the sequencer driver. */
97 static d_open_t seq_open
;
98 static d_close_t seq_close
;
99 static d_ioctl_t seq_ioctl
;
100 static d_read_t seq_read
;
101 static d_write_t seq_write
;
102 static d_kqfilter_t seq_kqfilter
;
104 static void seq_filter_detach(struct knote
*);
105 static int seq_filter_read(struct knote
*, long);
106 static int seq_filter_write(struct knote
*, long);
108 static struct dev_ops seq_ops
= {
109 { "sequencer", 0, D_MPSAFE
},
111 .d_close
= seq_close
,
113 .d_write
= seq_write
,
114 .d_ioctl
= seq_ioctl
,
115 .d_kqfilter
= seq_kqfilter
,
118 static struct filterops seq_read_filterops
=
119 { FILTEROP_ISFD
, NULL
, seq_filter_detach
, seq_filter_read
};
120 static struct filterops seq_write_filterops
=
121 { FILTEROP_ISFD
, NULL
, seq_filter_detach
, seq_filter_write
};
126 struct lock seq_lock
, q_lock
;
127 struct cv empty_cv
, reset_cv
, in_cv
, out_cv
, state_cv
, th_cv
;
129 MIDIQ_HEAD(, u_char
) in_q
, out_q
;
132 /* Flags (protected by flag_mtx of mididev_info) */
133 int fflags
; /* Access mode */
136 int out_water
; /* Sequence output threshould */
137 snd_sync_parm sync_parm
; /* AIOSYNC parameter set */
138 struct thread
*sync_thread
; /* AIOSYNCing thread */
139 struct kqinfo in_kq
, out_kq
;
141 struct cdev
*seqdev
, *musicdev
;
148 struct timeval timerstop
, timersub
;
149 int timerbase
, tempo
;
155 int pre_event_timeout
;
160 * Module specific stuff, including how many sequecers
164 SYSCTL_NODE(_hw_midi
, OID_AUTO
, seq
, CTLFLAG_RD
, 0, "Midi sequencer");
167 /* XXX: should this be moved into debug.midi? */
168 SYSCTL_INT(_hw_midi_seq
, OID_AUTO
, debug
, CTLFLAG_RW
, &seq_debug
, 0, "");
170 midi_cmdtab cmdtab_seqevent
[] = {
171 {SEQ_NOTEOFF
, "SEQ_NOTEOFF"},
172 {SEQ_NOTEON
, "SEQ_NOTEON"},
173 {SEQ_WAIT
, "SEQ_WAIT"},
174 {SEQ_PGMCHANGE
, "SEQ_PGMCHANGE"},
175 {SEQ_SYNCTIMER
, "SEQ_SYNCTIMER"},
176 {SEQ_MIDIPUTC
, "SEQ_MIDIPUTC"},
177 {SEQ_DRUMON
, "SEQ_DRUMON"},
178 {SEQ_DRUMOFF
, "SEQ_DRUMOFF"},
179 {SEQ_ECHO
, "SEQ_ECHO"},
180 {SEQ_AFTERTOUCH
, "SEQ_AFTERTOUCH"},
181 {SEQ_CONTROLLER
, "SEQ_CONTROLLER"},
182 {SEQ_BALANCE
, "SEQ_BALANCE"},
183 {SEQ_VOLMODE
, "SEQ_VOLMODE"},
184 {SEQ_FULLSIZE
, "SEQ_FULLSIZE"},
185 {SEQ_PRIVATE
, "SEQ_PRIVATE"},
186 {SEQ_EXTENDED
, "SEQ_EXTENDED"},
187 {EV_SEQ_LOCAL
, "EV_SEQ_LOCAL"},
188 {EV_TIMING
, "EV_TIMING"},
189 {EV_CHN_COMMON
, "EV_CHN_COMMON"},
190 {EV_CHN_VOICE
, "EV_CHN_VOICE"},
191 {EV_SYSEX
, "EV_SYSEX"},
195 midi_cmdtab cmdtab_seqioctl
[] = {
196 {SNDCTL_SEQ_RESET
, "SNDCTL_SEQ_RESET"},
197 {SNDCTL_SEQ_SYNC
, "SNDCTL_SEQ_SYNC"},
198 {SNDCTL_SYNTH_INFO
, "SNDCTL_SYNTH_INFO"},
199 {SNDCTL_SEQ_CTRLRATE
, "SNDCTL_SEQ_CTRLRATE"},
200 {SNDCTL_SEQ_GETOUTCOUNT
, "SNDCTL_SEQ_GETOUTCOUNT"},
201 {SNDCTL_SEQ_GETINCOUNT
, "SNDCTL_SEQ_GETINCOUNT"},
202 {SNDCTL_SEQ_PERCMODE
, "SNDCTL_SEQ_PERCMODE"},
203 {SNDCTL_FM_LOAD_INSTR
, "SNDCTL_FM_LOAD_INSTR"},
204 {SNDCTL_SEQ_TESTMIDI
, "SNDCTL_SEQ_TESTMIDI"},
205 {SNDCTL_SEQ_RESETSAMPLES
, "SNDCTL_SEQ_RESETSAMPLES"},
206 {SNDCTL_SEQ_NRSYNTHS
, "SNDCTL_SEQ_NRSYNTHS"},
207 {SNDCTL_SEQ_NRMIDIS
, "SNDCTL_SEQ_NRMIDIS"},
208 {SNDCTL_SEQ_GETTIME
, "SNDCTL_SEQ_GETTIME"},
209 {SNDCTL_MIDI_INFO
, "SNDCTL_MIDI_INFO"},
210 {SNDCTL_SEQ_THRESHOLD
, "SNDCTL_SEQ_THRESHOLD"},
211 {SNDCTL_SYNTH_MEMAVL
, "SNDCTL_SYNTH_MEMAVL"},
212 {SNDCTL_FM_4OP_ENABLE
, "SNDCTL_FM_4OP_ENABLE"},
213 {SNDCTL_PMGR_ACCESS
, "SNDCTL_PMGR_ACCESS"},
214 {SNDCTL_SEQ_PANIC
, "SNDCTL_SEQ_PANIC"},
215 {SNDCTL_SEQ_OUTOFBAND
, "SNDCTL_SEQ_OUTOFBAND"},
216 {SNDCTL_TMR_TIMEBASE
, "SNDCTL_TMR_TIMEBASE"},
217 {SNDCTL_TMR_START
, "SNDCTL_TMR_START"},
218 {SNDCTL_TMR_STOP
, "SNDCTL_TMR_STOP"},
219 {SNDCTL_TMR_CONTINUE
, "SNDCTL_TMR_CONTINUE"},
220 {SNDCTL_TMR_TEMPO
, "SNDCTL_TMR_TEMPO"},
221 {SNDCTL_TMR_SOURCE
, "SNDCTL_TMR_SOURCE"},
222 {SNDCTL_TMR_METRONOME
, "SNDCTL_TMR_METRONOME"},
223 {SNDCTL_TMR_SELECT
, "SNDCTL_TMR_SELECT"},
224 {SNDCTL_MIDI_PRETIME
, "SNDCTL_MIDI_PRETIME"},
225 {AIONWRITE
, "AIONWRITE"},
226 {AIOGSIZE
, "AIOGSIZE"},
227 {AIOSSIZE
, "AIOSSIZE"},
228 {AIOGFMT
, "AIOGFMT"},
229 {AIOSFMT
, "AIOSFMT"},
230 {AIOGMIX
, "AIOGMIX"},
231 {AIOSMIX
, "AIOSMIX"},
232 {AIOSTOP
, "AIOSTOP"},
233 {AIOSYNC
, "AIOSYNC"},
234 {AIOGCAP
, "AIOGCAP"},
238 midi_cmdtab cmdtab_timer
[] = {
239 {TMR_WAIT_REL
, "TMR_WAIT_REL"},
240 {TMR_WAIT_ABS
, "TMR_WAIT_ABS"},
241 {TMR_STOP
, "TMR_STOP"},
242 {TMR_START
, "TMR_START"},
243 {TMR_CONTINUE
, "TMR_CONTINUE"},
244 {TMR_TEMPO
, "TMR_TEMPO"},
245 {TMR_ECHO
, "TMR_ECHO"},
246 {TMR_CLOCK
, "TMR_CLOCK"},
247 {TMR_SPP
, "TMR_SPP"},
248 {TMR_TIMESIG
, "TMR_TIMESIG"},
252 midi_cmdtab cmdtab_seqcv
[] = {
253 {MIDI_NOTEOFF
, "MIDI_NOTEOFF"},
254 {MIDI_NOTEON
, "MIDI_NOTEON"},
255 {MIDI_KEY_PRESSURE
, "MIDI_KEY_PRESSURE"},
259 midi_cmdtab cmdtab_seqccmn
[] = {
260 {MIDI_CTL_CHANGE
, "MIDI_CTL_CHANGE"},
261 {MIDI_PGM_CHANGE
, "MIDI_PGM_CHANGE"},
262 {MIDI_CHN_PRESSURE
, "MIDI_CHN_PRESSURE"},
263 {MIDI_PITCH_BEND
, "MIDI_PITCH_BEND"},
264 {MIDI_SYSTEM_PREFIX
, "MIDI_SYSTEM_PREFIX"},
268 #ifndef KOBJMETHOD_END
269 #define KOBJMETHOD_END { NULL, NULL }
273 * static const char *mpu401_mprovider(kobj_t obj, struct mpu401 *m);
276 static kobj_method_t seq_methods
[] = {
277 /* KOBJMETHOD(mpu_provider,mpu401_mprovider), */
281 DEFINE_CLASS(sequencer
, seq_methods
, 0);
283 /* The followings are the local function. */
284 static int seq_convertold(u_char
*event
, u_char
*out
);
287 * static void seq_midiinput(struct seq_softc * scp, void *md);
289 static void seq_reset(struct seq_softc
*scp
);
290 static int seq_sync(struct seq_softc
*scp
);
292 static int seq_processevent(struct seq_softc
*scp
, u_char
*event
);
294 static int seq_timing(struct seq_softc
*scp
, u_char
*event
);
295 static int seq_local(struct seq_softc
*scp
, u_char
*event
);
297 static int seq_chnvoice(struct seq_softc
*scp
, kobj_t md
, u_char
*event
);
298 static int seq_chncommon(struct seq_softc
*scp
, kobj_t md
, u_char
*event
);
299 static int seq_sysex(struct seq_softc
*scp
, kobj_t md
, u_char
*event
);
301 static int seq_fetch_mid(struct seq_softc
*scp
, int unit
, kobj_t
*md
);
302 void seq_copytoinput(struct seq_softc
*scp
, u_char
*event
, int len
);
303 int seq_modevent(module_t mod
, int type
, void *data
);
304 struct seq_softc
*seqs
[10];
305 static struct lock seqinfo_lock
;
306 static u_long nseq
= 0;
308 static void timer_start(struct seq_softc
*t
);
309 static void timer_stop(struct seq_softc
*t
);
310 static void timer_setvals(struct seq_softc
*t
, int tempo
, int timerbase
);
311 static void timer_wait(struct seq_softc
*t
, int ticks
, int wait_abs
);
312 static int timer_now(struct seq_softc
*t
);
316 timer_start(struct seq_softc
*t
)
319 getmicrotime(&t
->timersub
);
323 timer_continue(struct seq_softc
*t
)
327 if (t
->timerrun
== 1)
331 timevalsub(&now
, &t
->timerstop
);
332 timevaladd(&t
->timersub
, &now
);
336 timer_stop(struct seq_softc
*t
)
339 getmicrotime(&t
->timerstop
);
343 timer_setvals(struct seq_softc
*t
, int tempo
, int timerbase
)
346 t
->timerbase
= timerbase
;
350 timer_wait(struct seq_softc
*t
, int ticks
, int wait_abs
)
352 struct timeval now
, when
;
354 unsigned long long i
;
356 while (t
->timerrun
== 0) {
357 SEQ_DEBUG(2, kprintf("Timer wait when timer isn't running\n"));
359 * The old sequencer used timeouts that only increased
360 * the timer when the timer was running.
361 * Hence the sequencer would stick (?) if the
362 * timer was disabled.
364 cv_wait(&t
->reset_cv
, &t
->seq_lock
);
369 i
= ticks
* 60ull * 1000000ull / (t
->tempo
* t
->timerbase
);
371 when
.tv_sec
= i
/ 1000000;
372 when
.tv_usec
= i
% 1000000;
375 kprintf("timer_wait tempo %d timerbase %d ticks %d abs %d u_sec %llu\n",
376 t
->tempo
, t
->timerbase
, ticks
, wait_abs
, i
);
381 timevalsub(&now
, &t
->timersub
);
382 timevalsub(&when
, &now
);
384 if (when
.tv_sec
< 0 || when
.tv_usec
< 0) {
386 kprintf("seq_timer error negative time %lds.%06lds\n",
387 (long)when
.tv_sec
, (long)when
.tv_usec
));
390 i
= when
.tv_sec
* 1000000ull;
395 kprintf("seq_timer usec %llu ticks %llu\n",
396 when
.tv_sec
* 1000000ull + when
.tv_usec
, i
);
399 ret
= cv_timedwait(&t
->reset_cv
, &t
->seq_lock
, i
+ 1);
402 if (ret
!= EWOULDBLOCK
)
403 SEQ_DEBUG(3, kprintf("seq_timer didn't timeout\n"));
408 timer_now(struct seq_softc
*t
)
411 unsigned long long i
;
414 if (t
->timerrun
== 0)
419 timevalsub(&now
, &t
->timersub
);
421 i
= now
.tv_sec
* 1000000ull;
429 * kprintf("timer_now: %llu %d\n", i, ret);
436 seq_eventthread(void *arg
)
438 struct seq_softc
*scp
= arg
;
441 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
442 SEQ_DEBUG(2, kprintf("seq_eventthread started\n"));
443 while (scp
->done
== 0) {
445 while (scp
->playing
== 0) {
446 cv_wait(&scp
->state_cv
, &scp
->seq_lock
);
451 while (MIDIQ_EMPTY(scp
->out_q
)) {
452 cv_broadcast(&scp
->empty_cv
);
453 cv_wait(&scp
->out_cv
, &scp
->seq_lock
);
454 if (scp
->playing
== 0)
460 MIDIQ_DEQ(scp
->out_q
, event
, EV_SZ
);
462 if (MIDIQ_AVAIL(scp
->out_q
) < scp
->out_water
) {
463 cv_broadcast(&scp
->out_cv
);
464 KNOTE(&scp
->out_kq
.ki_note
, 0);
466 seq_processevent(scp
, event
);
470 cv_broadcast(&scp
->th_cv
);
471 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
472 SEQ_DEBUG(2, kprintf("seq_eventthread finished\n"));
477 * seq_processevent: This maybe called by the event thread or the IOCTL
478 * handler for queued and out of band events respectively.
481 seq_processevent(struct seq_softc
*scp
, u_char
*event
)
488 if (event
[0] == EV_SEQ_LOCAL
)
489 ret
= seq_local(scp
, event
);
490 else if (event
[0] == EV_TIMING
)
491 ret
= seq_timing(scp
, event
);
492 else if (event
[0] != EV_CHN_VOICE
&&
493 event
[0] != EV_CHN_COMMON
&&
494 event
[0] != EV_SYSEX
&&
495 event
[0] != SEQ_MIDIPUTC
) {
497 SEQ_DEBUG(2, kprintf("seq_processevent not known %d\n",
499 } else if (seq_fetch_mid(scp
, event
[1], &m
) != 0) {
501 SEQ_DEBUG(2, kprintf("seq_processevent midi unit not found %d\n",
506 ret
= seq_chnvoice(scp
, m
, event
);
509 ret
= seq_chncommon(scp
, m
, event
);
512 ret
= seq_sysex(scp
, m
, event
);
515 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
516 ret
= SYNTH_WRITERAW(m
, &event
[2], 1);
517 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
526 struct seq_softc
*scp
;
530 /* Allocate the softc. */
532 scp
= kmalloc(sizeof(*scp
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
533 kobj_init((kobj_t
)scp
, &sequencer_class
);
535 buf
= kmalloc(sizeof(*buf
) * EV_SZ
* 1024, M_TEMP
, M_WAITOK
| M_ZERO
);
536 MIDIQ_INIT(scp
->in_q
, buf
, EV_SZ
* 1024);
537 buf
= kmalloc(sizeof(*buf
) * EV_SZ
* 1024, M_TEMP
, M_WAITOK
| M_ZERO
);
538 MIDIQ_INIT(scp
->out_q
, buf
, EV_SZ
* 1024);
541 scp
->midis
= kmalloc(sizeof(kobj_t
) * 32, M_TEMP
, M_WAITOK
| M_ZERO
);
542 scp
->midi_flags
= kmalloc(sizeof(*scp
->midi_flags
) * 32, M_TEMP
,
547 lockinit(&scp
->seq_lock
, "seqflq", 0, LK_CANRECURSE
);
548 cv_init(&scp
->state_cv
, "seqstate");
549 cv_init(&scp
->empty_cv
, "seqempty");
550 cv_init(&scp
->reset_cv
, "seqtimer");
551 cv_init(&scp
->out_cv
, "seqqout");
552 cv_init(&scp
->in_cv
, "seqqin");
553 cv_init(&scp
->th_cv
, "seqstart");
556 * Init the damn timer
559 scp
->mapper
= midimapper_addseq(scp
, &scp
->unit
, &scp
->mapper_cookie
);
560 if (scp
->mapper
== NULL
)
563 scp
->seqdev
= make_dev(&seq_ops
,
564 MIDIMKMINOR(scp
->unit
, SND_DEV_SEQ
, 0), UID_ROOT
,
565 GID_WHEEL
, 0666, "sequencer%d", scp
->unit
);
567 scp
->musicdev
= make_dev(&seq_ops
,
568 MIDIMKMINOR(scp
->unit
, SND_DEV_MUSIC
, 0), UID_ROOT
,
569 GID_WHEEL
, 0666, "music%d", scp
->unit
);
571 if (scp
->seqdev
== NULL
|| scp
->musicdev
== NULL
)
574 * TODO: Add to list of sequencers this module provides
579 (seq_eventthread
, scp
, NULL
,
580 "sequencer %02d", scp
->unit
);
585 scp
->seqdev
->si_drv1
= scp
->musicdev
->si_drv1
= scp
;
587 SEQ_DEBUG(2, kprintf("sequencer %d created scp %p\n", scp
->unit
, scp
));
591 lockmgr(&seqinfo_lock
, LK_EXCLUSIVE
);
593 lockmgr(&seqinfo_lock
, LK_RELEASE
);
599 if (scp
->seqdev
!= NULL
)
600 destroy_dev(scp
->seqdev
);
601 if (scp
->musicdev
!= NULL
)
602 destroy_dev(scp
->musicdev
);
604 * TODO: Destroy mutex and cv
606 if (scp
->midis
!= NULL
)
607 kfree(scp
->midis
, M_TEMP
);
608 if (scp
->midi_flags
!= NULL
)
609 kfree(scp
->midi_flags
, M_TEMP
);
611 kfree(scp
->out_q
.b
, M_TEMP
);
613 kfree(scp
->in_q
.b
, M_TEMP
);
614 kfree(scp
, M_DEVBUF
);
621 seq_delunit(int unit
)
623 struct seq_softc
*scp
= seqs
[unit
];
626 //SEQ_DEBUG(4, kprintf("seq_delunit: %d\n", unit));
627 SEQ_DEBUG(1, kprintf("seq_delunit: 1 \n"));
628 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
632 cv_broadcast(&scp
->out_cv
);
633 cv_broadcast(&scp
->state_cv
);
634 cv_broadcast(&scp
->reset_cv
);
635 SEQ_DEBUG(1, kprintf("seq_delunit: 2 \n"));
636 cv_wait(&scp
->th_cv
, &scp
->seq_lock
);
637 SEQ_DEBUG(1, kprintf("seq_delunit: 3.0 \n"));
638 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
639 SEQ_DEBUG(1, kprintf("seq_delunit: 3.1 \n"));
641 cv_destroy(&scp
->state_cv
);
642 SEQ_DEBUG(1, kprintf("seq_delunit: 4 \n"));
643 cv_destroy(&scp
->empty_cv
);
644 SEQ_DEBUG(1, kprintf("seq_delunit: 5 \n"));
645 cv_destroy(&scp
->reset_cv
);
646 SEQ_DEBUG(1, kprintf("seq_delunit: 6 \n"));
647 cv_destroy(&scp
->out_cv
);
648 SEQ_DEBUG(1, kprintf("seq_delunit: 7 \n"));
649 cv_destroy(&scp
->in_cv
);
650 SEQ_DEBUG(1, kprintf("seq_delunit: 8 \n"));
651 cv_destroy(&scp
->th_cv
);
653 SEQ_DEBUG(1, kprintf("seq_delunit: 10 \n"));
655 destroy_dev(scp
->seqdev
);
656 SEQ_DEBUG(1, kprintf("seq_delunit: 11 \n"));
658 destroy_dev(scp
->musicdev
);
659 SEQ_DEBUG(1, kprintf("seq_delunit: 12 \n"));
660 scp
->seqdev
= scp
->musicdev
= NULL
;
661 if (scp
->midis
!= NULL
)
662 kfree(scp
->midis
, M_TEMP
);
663 SEQ_DEBUG(1, kprintf("seq_delunit: 13 \n"));
664 if (scp
->midi_flags
!= NULL
)
665 kfree(scp
->midi_flags
, M_TEMP
);
666 SEQ_DEBUG(1, kprintf("seq_delunit: 14 \n"));
667 kfree(scp
->out_q
.b
, M_TEMP
);
668 SEQ_DEBUG(1, kprintf("seq_delunit: 15 \n"));
669 kfree(scp
->in_q
.b
, M_TEMP
);
671 SEQ_DEBUG(1, kprintf("seq_delunit: 16 \n"));
673 lockuninit(&scp
->seq_lock
);
674 SEQ_DEBUG(1, kprintf("seq_delunit: 17 \n"));
675 kfree(scp
, M_DEVBUF
);
677 lockmgr(&seqinfo_lock
, LK_EXCLUSIVE
);
678 for (i
= unit
; i
< (nseq
- 1); i
++)
679 seqs
[i
] = seqs
[i
+ 1];
681 lockmgr(&seqinfo_lock
, LK_RELEASE
);
687 seq_modevent(module_t mod
, int type
, void *data
)
695 lockinit(&seqinfo_lock
, "seqmod", 0, LK_CANRECURSE
);
696 retval
= seq_addunit();
701 r
= seq_delunit(nseq
- 1);
709 lockuninit(&seqinfo_lock
);
721 seq_fetch_mid(struct seq_softc
*scp
, int unit
, kobj_t
*md
)
724 if (unit
> scp
->midi_number
|| unit
< 0)
727 *md
= scp
->midis
[unit
];
733 seq_open(struct dev_open_args
*ap
)
735 cdev_t i_dev
= ap
->a_head
.a_dev
;
736 int flags
= ap
->a_oflags
;
737 struct seq_softc
*scp
= i_dev
->si_drv1
;
743 SEQ_DEBUG(3, kprintf("seq_open: scp %p unit %d, flags 0x%x.\n",
744 scp
, scp
->unit
, flags
));
747 * Mark this device busy.
750 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
752 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
753 SEQ_DEBUG(2, kprintf("seq_open: unit %d is busy.\n", scp
->unit
));
758 if ((scp->fflags & O_NONBLOCK) != 0)
759 scp->flags |= SEQ_F_NBIO;
761 scp
->music
= MIDIDEV(i_dev
) == SND_DEV_MUSIC
;
764 * Enumerate the available midi devices
766 scp
->midi_number
= 0;
767 scp
->maxunits
= midimapper_open(scp
->mapper
, &scp
->mapper_cookie
);
769 if (scp
->maxunits
== 0)
770 SEQ_DEBUG(2, kprintf("seq_open: no midi devices\n"));
772 for (i
= 0; i
< scp
->maxunits
; i
++) {
773 scp
->midis
[scp
->midi_number
] =
774 midimapper_fetch_synth(scp
->mapper
, scp
->mapper_cookie
, i
);
775 if (scp
->midis
[scp
->midi_number
]) {
776 if (SYNTH_OPEN(scp
->midis
[scp
->midi_number
], scp
,
778 scp
->midis
[scp
->midi_number
] = NULL
;
780 scp
->midi_flags
[scp
->midi_number
] =
781 SYNTH_QUERY(scp
->midis
[scp
->midi_number
]);
787 timer_setvals(scp
, 60, 100);
792 * actually, if we're in rdonly mode, we should start the timer
795 * TODO: Handle recording now
798 scp
->out_water
= MIDIQ_SIZE(scp
->out_q
) / 2;
801 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
803 SEQ_DEBUG(2, kprintf("seq_open: opened, mode %s.\n",
804 scp
->music
? "music" : "sequencer"));
806 kprintf("Sequencer %d %p opened maxunits %d midi_number %d:\n",
807 scp
->unit
, scp
, scp
->maxunits
, scp
->midi_number
));
808 for (i
= 0; i
< scp
->midi_number
; i
++)
809 SEQ_DEBUG(3, kprintf(" midi %d %p\n", i
, scp
->midis
[i
]));
818 seq_close(struct dev_close_args
*ap
)
820 cdev_t i_dev
= ap
->a_head
.a_dev
;
822 struct seq_softc
*scp
= i_dev
->si_drv1
;
828 SEQ_DEBUG(2, kprintf("seq_close: unit %d.\n", scp
->unit
));
830 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
839 for (i
= 0; i
< scp
->midi_number
; i
++)
841 SYNTH_CLOSE(scp
->midis
[i
]);
843 midimapper_close(scp
->mapper
, scp
->mapper_cookie
);
851 SEQ_DEBUG(3, kprintf("seq_close: closed ret = %d.\n", ret
));
852 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
857 seq_read(struct dev_read_args
*ap
)
859 cdev_t i_dev
= ap
->a_head
.a_dev
;
860 struct uio
*uio
= ap
->a_uio
;
861 int ioflag
= ap
->a_ioflag
;
863 struct seq_softc
*scp
= i_dev
->si_drv1
;
866 u_char buf
[SEQ_RSIZE
];
871 SEQ_DEBUG(7, kprintf("seq_read: unit %d, resid %zd.\n",
872 scp
->unit
, uio
->uio_resid
));
874 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
875 if ((scp
->fflags
& FREAD
) == 0) {
876 SEQ_DEBUG(2, kprintf("seq_read: unit %d is not for reading.\n",
885 * if ((scp->flags & SEQ_F_READING) == 0)
888 * TODO, start recording if not alread
892 * I think the semantics are to return as soon
894 * Second thought, it doens't seem like midimoutain
895 * expects that at all.
896 * TODO: Look up in some sort of spec
899 while (uio
->uio_resid
> 0) {
900 while (MIDIQ_EMPTY(scp
->in_q
)) {
901 retval
= EWOULDBLOCK
;
903 * I wish I knew which one to care about
906 if (scp
->fflags
& O_NONBLOCK
)
908 if (ioflag
& O_NONBLOCK
)
911 retval
= cv_wait_sig(&scp
->in_cv
, &scp
->seq_lock
);
916 used
= MIN(MIDIQ_LEN(scp
->in_q
), uio
->uio_resid
);
917 used
= MIN(used
, SEQ_RSIZE
);
919 SEQ_DEBUG(8, kprintf("midiread: uiomove cc=%d\n", used
));
920 MIDIQ_DEQ(scp
->in_q
, buf
, used
);
921 retval
= uiomove(buf
, used
, uio
);
928 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
929 SEQ_DEBUG(6, kprintf("seq_read: ret %d, resid %zd.\n",
930 retval
, uio
->uio_resid
));
936 seq_write(struct dev_write_args
*ap
)
938 cdev_t i_dev
= ap
->a_head
.a_dev
;
939 struct uio
*uio
= ap
->a_uio
;
940 int ioflag
= ap
->a_ioflag
;
941 u_char event
[EV_SZ
], newevent
[EV_SZ
], ev_code
;
942 struct seq_softc
*scp
= i_dev
->si_drv1
;
946 SEQ_DEBUG(7, kprintf("seq_write: unit %d, resid %zd.\n",
947 scp
->unit
, uio
->uio_resid
));
952 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
954 if ((scp
->fflags
& FWRITE
) == 0) {
955 SEQ_DEBUG(2, kprintf("seq_write: unit %d is not for writing.\n",
960 while (uio
->uio_resid
> 0) {
961 while (MIDIQ_AVAIL(scp
->out_q
) == 0) {
962 retval
= EWOULDBLOCK
;
963 if (scp
->fflags
& O_NONBLOCK
)
965 if (ioflag
& O_NONBLOCK
)
967 SEQ_DEBUG(8, kprintf("seq_write cvwait\n"));
970 cv_broadcast(&scp
->out_cv
);
971 cv_broadcast(&scp
->state_cv
);
973 retval
= cv_wait_sig(&scp
->out_cv
, &scp
->seq_lock
);
975 * We slept, maybe things have changed since last
984 if (scp
!= i_dev
->si_drv1
)
989 used
= MIN(uio
->uio_resid
, 4);
991 SEQ_DEBUG(8, kprintf("seqout: resid %zd len %jd avail %jd\n",
992 uio
->uio_resid
, (intmax_t)MIDIQ_LEN(scp
->out_q
),
993 (intmax_t)MIDIQ_AVAIL(scp
->out_q
)));
999 retval
= uiomove(event
, used
, uio
);
1004 SEQ_DEBUG(8, kprintf("seq_write: unit %d, event %s.\n",
1005 scp
->unit
, midi_cmdname(ev_code
, cmdtab_seqevent
)));
1007 /* Have a look at the event code. */
1008 if (ev_code
== SEQ_FULLSIZE
) {
1011 * TODO: restore code for SEQ_FULLSIZE
1015 * A long event, these are the patches/samples for a
1018 midiunit
= *(u_short
*)&event
[2];
1019 lockmgr(&sd
->seq_lock
, LK_EXCLUSIVE
);
1020 ret
= lookup_mididev(scp
, midiunit
, LOOKUP_OPEN
, &md
);
1021 lockmgr(&sd
->seq_lock
, LK_RELEASE
);
1025 SEQ_DEBUG(kprintf("seq_write: loading a patch to the unit %d.\n", midiunit
));
1027 ret
= md
->synth
.loadpatch(md
, *(short *)&event
[0], buf
,
1032 * For now, just flush the darn buffer
1035 kprintf("seq_write: SEQ_FULLSIZE flusing buffer.\n"));
1036 while (uio
->uio_resid
> 0) {
1037 retval
= uiomove(event
, EV_SZ
, uio
);
1047 if (ev_code
>= 128) {
1050 * Some sort of an extended event. The size is eight
1051 * bytes. scoop extra info.
1053 if (scp
->music
&& ev_code
== SEQ_EXTENDED
) {
1054 SEQ_DEBUG(2, kprintf("seq_write: invalid level two event %x.\n", ev_code
));
1057 if (uiomove((caddr_t
)&event
[4], 4, uio
)) {
1059 kprintf("seq_write: user memory mangled?\n"));
1067 SEQ_DEBUG(2, kprintf("seq_write: four byte event in music mode.\n"));
1071 if (ev_code
== SEQ_MIDIPUTC
) {
1073 * TODO: event[2] is unit number to receive char.
1078 #ifdef not_ever_ever
1079 if (event
[0] == EV_TIMING
&&
1080 (event
[1] == TMR_START
|| event
[1] == TMR_STOP
)) {
1082 * For now, try to make midimoutain work by
1083 * forcing these events to be processed
1086 seq_processevent(scp
, event
);
1088 MIDIQ_ENQ(scp
->out_q
, event
, EV_SZ
);
1090 MIDIQ_ENQ(scp
->out_q
, event
, EV_SZ
);
1093 if (seq_convertold(event
, newevent
) > 0)
1094 MIDIQ_ENQ(scp
->out_q
, newevent
, EV_SZ
);
1104 cv_broadcast(&scp
->state_cv
);
1105 cv_broadcast(&scp
->out_cv
);
1111 kprintf("seq_write done: leftover buffer length %zd retval %d\n",
1112 uio
->uio_resid
, retval
));
1113 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1118 seq_ioctl(struct dev_ioctl_args
*ap
)
1120 cdev_t i_dev
= ap
->a_head
.a_dev
;
1121 u_long cmd
= ap
->a_cmd
;
1122 caddr_t arg
= ap
->a_data
;
1123 int mode
= ap
->a_fflag
;
1124 int midiunit
, ret
, tmp
;
1125 struct seq_softc
*scp
= i_dev
->si_drv1
;
1126 struct synth_info
*synthinfo
;
1127 struct midi_info
*midiinfo
;
1128 u_char event
[EV_SZ
];
1129 u_char newevent
[EV_SZ
];
1134 * struct snd_size *sndsize;
1140 SEQ_DEBUG(6, kprintf("seq_ioctl: unit %d, cmd %s.\n",
1141 scp
->unit
, midi_cmdname(cmd
, cmdtab_seqioctl
)));
1146 case SNDCTL_SEQ_GETTIME
:
1148 * ioctl needed by libtse
1150 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1151 *(int *)arg
= timer_now(scp
);
1152 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1153 SEQ_DEBUG(6, kprintf("seq_ioctl: gettime %d.\n", *(int *)arg
));
1156 case SNDCTL_TMR_METRONOME
:
1158 case SNDCTL_TMR_SOURCE
:
1164 case SNDCTL_TMR_TEMPO
:
1165 event
[1] = TMR_TEMPO
;
1166 event
[4] = *(int *)arg
& 0xFF;
1167 event
[5] = (*(int *)arg
>> 8) & 0xFF;
1168 event
[6] = (*(int *)arg
>> 16) & 0xFF;
1169 event
[7] = (*(int *)arg
>> 24) & 0xFF;
1171 case SNDCTL_TMR_TIMEBASE
:
1172 event
[1] = TMR_TIMERBASE
;
1173 event
[4] = *(int *)arg
& 0xFF;
1174 event
[5] = (*(int *)arg
>> 8) & 0xFF;
1175 event
[6] = (*(int *)arg
>> 16) & 0xFF;
1176 event
[7] = (*(int *)arg
>> 24) & 0xFF;
1178 case SNDCTL_TMR_START
:
1179 event
[1] = TMR_START
;
1181 case SNDCTL_TMR_STOP
:
1182 event
[1] = TMR_STOP
;
1184 case SNDCTL_TMR_CONTINUE
:
1185 event
[1] = TMR_CONTINUE
;
1187 event
[0] = EV_TIMING
;
1188 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1191 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1194 seq_processevent(scp
, event
);
1195 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1197 case SNDCTL_TMR_SELECT
:
1199 kprintf("seq_ioctl: SNDCTL_TMR_SELECT not supported\n"));
1202 case SNDCTL_SEQ_SYNC
:
1203 if (mode
== O_RDONLY
) {
1207 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1208 ret
= seq_sync(scp
);
1209 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1211 case SNDCTL_SEQ_PANIC
:
1213 case SNDCTL_SEQ_RESET
:
1215 * SNDCTL_SEQ_PANIC == SNDCTL_SEQ_RESET
1217 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1219 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1222 case SNDCTL_SEQ_TESTMIDI
:
1223 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1225 * TODO: SNDCTL_SEQ_TESTMIDI now means "can I write to the
1228 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1231 case SNDCTL_SEQ_GETINCOUNT
:
1232 if (mode
== O_WRONLY
)
1235 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1236 *(int *)arg
= scp
->in_q
.rl
;
1237 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1238 SEQ_DEBUG(kprintf("seq_ioctl: incount %d.\n",
1243 case SNDCTL_SEQ_GETOUTCOUNT
:
1244 if (mode
== O_RDONLY
)
1247 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1248 *(int *)arg
= scp
->out_q
.fl
;
1249 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1250 SEQ_DEBUG(kprintf("seq_ioctl: outcount %d.\n",
1256 case SNDCTL_SEQ_CTRLRATE
:
1257 if (*(int *)arg
!= 0) {
1261 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1262 *(int *)arg
= scp
->timerbase
;
1263 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1264 SEQ_DEBUG(3, kprintf("seq_ioctl: ctrlrate %d.\n", *(int *)arg
));
1268 * TODO: ioctl SNDCTL_SEQ_RESETSAMPLES
1271 case SNDCTL_SEQ_RESETSAMPLES
:
1272 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1273 ret
= lookup_mididev(scp
, *(int *)arg
, LOOKUP_OPEN
, &md
);
1274 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1277 ret
= midi_ioctl(MIDIMKDEV(major(i_dev
), *(int *)arg
,
1278 SND_DEV_MIDIN
), cmd
, arg
, mode
, td
);
1281 case SNDCTL_SEQ_NRSYNTHS
:
1282 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1283 *(int *)arg
= scp
->midi_number
;
1284 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1285 SEQ_DEBUG(3, kprintf("seq_ioctl: synths %d.\n", *(int *)arg
));
1288 case SNDCTL_SEQ_NRMIDIS
:
1289 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1294 * TODO: count the numbder of devices that can WRITERAW
1296 *(int *)arg
= scp
->midi_number
;
1298 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1299 SEQ_DEBUG(3, kprintf("seq_ioctl: midis %d.\n", *(int *)arg
));
1303 * TODO: ioctl SNDCTL_SYNTH_MEMAVL
1306 case SNDCTL_SYNTH_MEMAVL
:
1307 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1308 ret
= lookup_mididev(scp
, *(int *)arg
, LOOKUP_OPEN
, &md
);
1309 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1312 ret
= midi_ioctl(MIDIMKDEV(major(i_dev
), *(int *)arg
,
1313 SND_DEV_MIDIN
), cmd
, arg
, mode
, td
);
1316 case SNDCTL_SEQ_OUTOFBAND
:
1317 for (ret
= 0; ret
< EV_SZ
; ret
++)
1318 event
[ret
] = (u_char
)arg
[0];
1320 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1322 ret
= seq_processevent(scp
, event
);
1324 if (seq_convertold(event
, newevent
) > 0)
1325 ret
= seq_processevent(scp
, newevent
);
1329 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1331 case SNDCTL_SYNTH_INFO
:
1332 synthinfo
= (struct synth_info
*)arg
;
1333 midiunit
= synthinfo
->device
;
1334 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1335 if (seq_fetch_mid(scp
, midiunit
, &md
) == 0) {
1336 bzero(synthinfo
, sizeof(*synthinfo
));
1337 synthinfo
->name
[0] = 'f';
1338 synthinfo
->name
[1] = 'a';
1339 synthinfo
->name
[2] = 'k';
1340 synthinfo
->name
[3] = 'e';
1341 synthinfo
->name
[4] = 's';
1342 synthinfo
->name
[5] = 'y';
1343 synthinfo
->name
[6] = 'n';
1344 synthinfo
->name
[7] = 't';
1345 synthinfo
->name
[8] = 'h';
1346 synthinfo
->device
= midiunit
;
1347 synthinfo
->synth_type
= SYNTH_TYPE_MIDI
;
1348 synthinfo
->capabilities
= scp
->midi_flags
[midiunit
];
1352 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1354 case SNDCTL_MIDI_INFO
:
1355 midiinfo
= (struct midi_info
*)arg
;
1356 midiunit
= midiinfo
->device
;
1357 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1358 if (seq_fetch_mid(scp
, midiunit
, &md
) == 0) {
1359 bzero(midiinfo
, sizeof(*midiinfo
));
1360 midiinfo
->name
[0] = 'f';
1361 midiinfo
->name
[1] = 'a';
1362 midiinfo
->name
[2] = 'k';
1363 midiinfo
->name
[3] = 'e';
1364 midiinfo
->name
[4] = 'm';
1365 midiinfo
->name
[5] = 'i';
1366 midiinfo
->name
[6] = 'd';
1367 midiinfo
->name
[7] = 'i';
1368 midiinfo
->device
= midiunit
;
1369 midiinfo
->capabilities
= scp
->midi_flags
[midiunit
];
1371 * TODO: What devtype?
1373 midiinfo
->dev_type
= 0x01;
1377 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1379 case SNDCTL_SEQ_THRESHOLD
:
1380 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1381 RANGE(*(int *)arg
, 1, MIDIQ_SIZE(scp
->out_q
) - 1);
1382 scp
->out_water
= *(int *)arg
;
1383 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1384 SEQ_DEBUG(3, kprintf("seq_ioctl: water %d.\n", *(int *)arg
));
1387 case SNDCTL_MIDI_PRETIME
:
1391 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1392 scp
->pre_event_timeout
= (hz
* tmp
) / 10;
1393 *(int *)arg
= scp
->pre_event_timeout
;
1394 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1395 SEQ_DEBUG(3, kprintf("seq_ioctl: pretime %d.\n", *(int *)arg
));
1398 case SNDCTL_FM_4OP_ENABLE
:
1399 case SNDCTL_PMGR_IFACE
:
1400 case SNDCTL_PMGR_ACCESS
:
1402 * Patch manager and fm are ded, ded, ded.
1407 * TODO: Consider ioctl default case.
1409 * if ((scp->fflags & O_ACCMODE) == FREAD) {
1413 * Then pass on the ioctl to device 0
1416 kprintf("seq_ioctl: unsupported IOCTL %ld.\n", cmd
));
1425 seq_kqfilter(struct dev_kqfilter_args
*ap
)
1427 cdev_t dev
= ap
->a_head
.a_dev
;
1428 struct knote
*kn
= ap
->a_kn
;
1429 struct seq_softc
*scp
;
1430 struct klist
*klist
;
1435 switch (kn
->kn_filter
) {
1437 kn
->kn_fop
= &seq_read_filterops
;
1438 kn
->kn_hook
= (caddr_t
)scp
;
1439 klist
= &scp
->in_kq
.ki_note
;
1442 kn
->kn_fop
= &seq_write_filterops
;
1443 kn
->kn_hook
= (caddr_t
)scp
;
1444 klist
= &scp
->out_kq
.ki_note
;
1447 ap
->a_result
= EOPNOTSUPP
;
1451 knote_insert(klist
, kn
);
1457 seq_filter_detach(struct knote
*kn
)
1459 struct seq_softc
*scp
= (struct seq_softc
*)kn
->kn_hook
;
1460 struct klist
*in_klist
= &scp
->in_kq
.ki_note
;
1461 struct klist
*out_klist
= &scp
->out_kq
.ki_note
;
1463 knote_remove(in_klist
, kn
);
1464 knote_remove(out_klist
, kn
);
1468 seq_filter_read(struct knote
*kn
, long hint
)
1470 struct seq_softc
*scp
= (struct seq_softc
*)kn
->kn_hook
;
1474 SEQ_DEBUG(3, kprintf("seq_filter_read: unit %d.\n", scp
->unit
));
1475 SEQ_DEBUG(1, kprintf("seq_filter_read: unit %d.\n", scp
->unit
));
1477 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1479 /* Look up the apropriate queue and select it. */
1481 /* TODO: Start recording. */
1483 /* Find out the boundary. */
1485 if (MIDIQ_LEN(scp
->in_q
) >= lim
) {
1486 /* We can read now. */
1489 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1495 seq_filter_write(struct knote
*kn
, long hint
)
1497 struct seq_softc
*scp
= (struct seq_softc
*)kn
->kn_hook
;
1501 SEQ_DEBUG(3, kprintf("seq_filter_write: unit %d.\n", scp
->unit
));
1502 SEQ_DEBUG(1, kprintf("seq_filter_write: unit %d.\n", scp
->unit
));
1504 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1506 /* Look up the apropriate queue and select it. */
1508 /* Start playing. */
1510 cv_broadcast(&scp
->state_cv
);
1511 cv_broadcast(&scp
->out_cv
);
1513 lim
= scp
->out_water
;
1515 if (MIDIQ_AVAIL(scp
->out_q
) >= lim
) {
1516 /* We can write now. */
1519 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1526 sein_qtr(void *p
, void /* mididev_info */ *md
)
1528 struct seq_softc
*scp
;
1530 scp
= (struct seq_softc
*)p
;
1532 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1534 /* Restart playing if we have the data to output. */
1535 if (scp
->queueout_pending
)
1536 seq_callback(scp
, SEQ_CB_START
| SEQ_CB_WR
);
1537 /* Check the midi device if we are reading. */
1538 if ((scp
->flags
& SEQ_F_READING
) != 0)
1539 seq_midiinput(scp
, md
);
1541 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1547 * Was the old playevent. Use this to convert and old
1548 * style /dev/sequencer event to a /dev/music event
1551 seq_convertold(u_char
*event
, u_char
*out
)
1554 u_char dev
, chn
, note
, vel
;
1556 out
[0] = out
[1] = out
[2] = out
[3] = out
[4] = out
[5] = out
[6] =
1568 * TODO: Debug statement
1587 out
[0] = EV_CHN_VOICE
;
1589 out
[2] = MIDI_NOTEOFF
;
1597 out
[0] = EV_CHN_VOICE
;
1599 out
[2] = MIDI_NOTEON
;
1607 * wait delay = (event[2] << 16) + (event[3] << 8) + event[4]
1611 out
[0] = EV_CHN_COMMON
;
1613 out
[2] = MIDI_PGM_CHANGE
;
1622 out[2] = MIDI_PGM_CHANGE;
1626 SEQ_DEBUG(4,kprintf("seq_playevent: synctimer\n"));
1632 kprintf("seq_playevent: put data 0x%02x, unit %d.\n",
1633 event
[1], event
[2]));
1635 * Pass through to the midi device.
1639 out
[0] = SEQ_MIDIPUTC
;
1647 * This isn't handled here yet because I don't know if I can
1648 * just use four bytes events. There might be consequences
1649 * in the _read routing
1651 if (seq_copytoinput(scp
, event
, 4) == EAGAIN
) {
1667 case SEQ_AFTERTOUCH
:
1669 * SYNTH_AFTERTOUCH(md, event[3], event[4])
1673 * SYNTH_PANNING(md, event[3], (char)event[4])
1675 case SEQ_CONTROLLER
:
1677 * SYNTH_CONTROLLER(md, event[3], event[4], *(short *)&event[5])
1681 * SYNTH_VOLUMEMETHOD(md, event[3])
1685 kprintf("seq_convertold: SEQ_EXTENDED type %d"
1686 "not handled\n", event
[1]));
1692 out
[1] = TMR_WAIT_REL
;
1697 SEQ_DEBUG(5, kprintf("SEQ_WAIT %d",
1698 event
[2] + (event
[3] << 8) + (event
[4] << 24)));
1708 kprintf("seq_convertold: event type %d not handled %d %d %d\n",
1709 event
[0], event
[1], event
[2], event
[3]));
1716 * Writting to the sequencer buffer never blocks and drops
1717 * input which cannot be queued
1720 seq_copytoinput(struct seq_softc
*scp
, u_char
*event
, int len
)
1723 KKASSERT(lockowned(&scp
->seq_lock
));
1725 if (MIDIQ_AVAIL(scp
->in_q
) < len
) {
1727 * ENOROOM? EINPUTDROPPED? ETOUGHLUCK?
1729 SEQ_DEBUG(2, kprintf("seq_copytoinput: queue full\n"));
1731 MIDIQ_ENQ(scp
->in_q
, event
, len
);
1732 KNOTE(&scp
->in_kq
.ki_note
, 0);
1733 cv_broadcast(&scp
->in_cv
);
1739 seq_chnvoice(struct seq_softc
*scp
, kobj_t md
, u_char
*event
)
1742 u_char cmd
, chn
, note
, parm
;
1750 KKASSERT(lockowned(&scp
->seq_lock
));
1752 SEQ_DEBUG(5, kprintf("seq_chnvoice: unit %d, dev %d, cmd %s,"
1753 " chn %d, note %d, parm %d.\n", scp
->unit
, event
[1],
1754 midi_cmdname(cmd
, cmdtab_seqcv
), chn
, note
, parm
));
1756 voice
= SYNTH_ALLOC(md
, chn
, note
);
1758 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1762 if (note
< 128 || note
== 255) {
1764 if (scp
->music
&& chn
== 9) {
1766 * This channel is a percussion. The note
1767 * number is the patch number.
1770 lockmgr(&scp->seq_lock, LK_RELEASE);
1771 if (SYNTH_SETINSTR(md, voice, 128 + note)
1773 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1776 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1778 note
= 60; /* Middle C. */
1783 lockmgr(&scp->seq_lock, LK_RELEASE);
1784 if (SYNTH_SETUPVOICE(md, voice, chn)
1786 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1789 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1792 SYNTH_STARTNOTE(md
, voice
, note
, parm
);
1796 SYNTH_KILLNOTE(md
, voice
, note
, parm
);
1798 case MIDI_KEY_PRESSURE
:
1799 SYNTH_AFTERTOUCH(md
, voice
, parm
);
1803 SEQ_DEBUG(2, kprintf("seq_chnvoice event type %d not handled\n",
1808 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1813 seq_chncommon(struct seq_softc
*scp
, kobj_t md
, u_char
*event
)
1817 u_char cmd
, chn
, p1
;
1823 w14
= *(u_short
*)&event
[6];
1825 SEQ_DEBUG(5, kprintf("seq_chncommon: unit %d, dev %d, cmd %s, chn %d,"
1826 " p1 %d, w14 %d.\n", scp
->unit
, event
[1],
1827 midi_cmdname(cmd
, cmdtab_seqccmn
), chn
, p1
, w14
));
1828 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1830 case MIDI_PGM_CHANGE
:
1831 SEQ_DEBUG(4, kprintf("seq_chncommon pgmchn chn %d pg %d\n",
1833 SYNTH_SETINSTR(md
, chn
, p1
);
1835 case MIDI_CTL_CHANGE
:
1836 SEQ_DEBUG(4, kprintf("seq_chncommon ctlch chn %d pg %d %d\n",
1838 SYNTH_CONTROLLER(md
, chn
, p1
, w14
);
1840 case MIDI_PITCH_BEND
:
1843 * TODO: MIDI_PITCH_BEND
1846 lockmgr(&md
->synth
.vc_lock
, LK_EXCLUSIVE
);
1847 md
->synth
.chn_info
[chn
].bender_value
= w14
;
1848 if (md
->midiunit
>= 0) {
1850 * Handle all of the notes playing on this
1853 key
= ((int)chn
<< 8);
1854 for (i
= 0; i
< md
->synth
.alloc
.max_voice
; i
++)
1855 if ((md
->synth
.alloc
.map
[i
] & 0xff00) == key
) {
1856 lockmgr(&md
->synth
.vc_lock
, LK_RELEASE
);
1857 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1858 if (md
->synth
.bender(md
, i
, w14
) == EAGAIN
) {
1859 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1862 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1865 lockmgr(&md
->synth
.vc_lock
, LK_RELEASE
);
1866 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
1867 if (md
->synth
.bender(md
, chn
, w14
) == EAGAIN
) {
1868 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1871 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1875 SYNTH_BENDER(md
, chn
, w14
);
1880 kprintf("seq_chncommon event type %d not handled.\n",
1885 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
1890 seq_timing(struct seq_softc
*scp
, u_char
*event
)
1896 param
= event
[4] + (event
[5] << 8) +
1897 (event
[6] << 16) + (event
[7] << 24);
1899 SEQ_DEBUG(5, kprintf("seq_timing: unit %d, cmd %d, param %d.\n",
1900 scp
->unit
, event
[1], param
));
1903 timer_wait(scp
, param
, 0);
1906 timer_wait(scp
, param
, 1);
1910 cv_broadcast(&scp
->reset_cv
);
1915 * The following cv_broadcast isn't needed since we only
1916 * wait for 0->1 transitions. It probably won't hurt
1918 cv_broadcast(&scp
->reset_cv
);
1921 timer_continue(scp
);
1922 cv_broadcast(&scp
->reset_cv
);
1929 SEQ_DEBUG(4, kprintf("Timer set tempo %d\n", param
));
1930 timer_setvals(scp
, param
, scp
->timerbase
);
1937 SEQ_DEBUG(4, kprintf("Timer set timerbase %d\n", param
));
1938 timer_setvals(scp
, scp
->tempo
, param
);
1942 * TODO: Consider making 4-byte events for /dev/sequencer
1943 * PRO: Maybe needed by legacy apps
1944 * CON: soundcard.h has been warning for a while many years
1945 * to expect 8 byte events.
1949 seq_copytoinput(scp
, event
, 8);
1951 param
= (param
<< 8 | SEQ_ECHO
);
1952 seq_copytoinput(scp
, (u_char
*)¶m
, 4);
1955 seq_copytoinput(scp
, event
, 8);
1959 SEQ_DEBUG(2, kprintf("seq_timing event type %d not handled.\n",
1968 seq_local(struct seq_softc
*scp
, u_char
*event
)
1973 KKASSERT(lockowned(&scp
->seq_lock
));
1975 SEQ_DEBUG(5, kprintf("seq_local: unit %d, cmd %d\n", scp
->unit
,
1979 SEQ_DEBUG(1, kprintf("seq_local event type %d not handled\n",
1988 seq_sysex(struct seq_softc
*scp
, kobj_t md
, u_char
*event
)
1992 KKASSERT(lockowned(&scp
->seq_lock
));
1993 SEQ_DEBUG(5, kprintf("seq_sysex: unit %d device %d\n", scp
->unit
,
1996 for (i
= 0; i
< 6 && event
[i
+ 2] != 0xff; i
++)
1999 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
2000 if (SYNTH_SENDSYSEX(md
, &event
[2], l
) == EAGAIN
) {
2001 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
2004 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
2010 * Reset no longer closes the raw devices nor seq_sync's
2011 * Callers are IOCTL and seq_close
2014 seq_reset(struct seq_softc
*scp
)
2019 KKASSERT(lockowned(&scp
->seq_lock
));
2021 SEQ_DEBUG(5, kprintf("seq_reset: unit %d.\n", scp
->unit
));
2024 * Stop reading and writing.
2027 /* scp->recording = 0; */
2029 cv_broadcast(&scp
->state_cv
);
2030 cv_broadcast(&scp
->out_cv
);
2031 cv_broadcast(&scp
->reset_cv
);
2034 * For now, don't reset the timers.
2036 MIDIQ_CLEAR(scp
->in_q
);
2037 MIDIQ_CLEAR(scp
->out_q
);
2039 for (i
= 0; i
< scp
->midi_number
; i
++) {
2041 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
2043 for (chn
= 0; chn
< 16; chn
++) {
2044 SYNTH_CONTROLLER(m
, chn
, 123, 0);
2045 SYNTH_CONTROLLER(m
, chn
, 121, 0);
2046 SYNTH_BENDER(m
, chn
, 1 << 13);
2048 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
2054 * *really* flush the output queue
2055 * flush the event queue, then flush the synthsisers.
2056 * Callers are IOCTL and close
2059 #define SEQ_SYNC_TIMEOUT 8
2061 seq_sync(struct seq_softc
*scp
)
2063 int i
, rl
, sync
[16], done
;
2065 KKASSERT(lockowned(&scp
->seq_lock
));
2067 SEQ_DEBUG(4, kprintf("seq_sync: unit %d.\n", scp
->unit
));
2070 * Wait until output queue is empty. Check every so often to see if
2071 * the queue is moving along. If it isn't just abort.
2073 while (!MIDIQ_EMPTY(scp
->out_q
)) {
2075 if (!scp
->playing
) {
2077 cv_broadcast(&scp
->state_cv
);
2078 cv_broadcast(&scp
->out_cv
);
2080 rl
= MIDIQ_LEN(scp
->out_q
);
2082 i
= cv_timedwait_sig(&scp
->out_cv
,
2083 &scp
->seq_lock
, SEQ_SYNC_TIMEOUT
* hz
);
2085 if (i
== EINTR
|| i
== ERESTART
) {
2088 * XXX: I don't know why we stop playing
2091 cv_broadcast(&scp
->out_cv
);
2095 if (i
== EWOULDBLOCK
&& rl
== MIDIQ_LEN(scp
->out_q
) &&
2096 scp
->waiting
== 0) {
2098 * A queue seems to be stuck up. Give up and clear
2101 MIDIQ_CLEAR(scp
->out_q
);
2103 cv_broadcast(&scp
->state_cv
);
2104 cv_broadcast(&scp
->out_cv
);
2105 cv_broadcast(&scp
->reset_cv
);
2108 * TODO: Consider if the raw devices need to be flushed
2111 SEQ_DEBUG(1, kprintf("seq_sync queue stuck, aborting\n"));
2119 * Since syncing a midi device might block, unlock scp->seq_lock.
2122 lockmgr(&scp
->seq_lock
, LK_RELEASE
);
2123 for (i
= 0; i
< scp
->midi_number
; i
++)
2128 for (i
= 0; i
< scp
->midi_number
; i
++)
2130 if (SYNTH_INSYNC(scp
->midis
[i
]) == 0)
2140 lockmgr(&scp
->seq_lock
, LK_EXCLUSIVE
);
2145 midi_cmdname(int cmd
, midi_cmdtab
*tab
)
2147 while (tab
->name
!= NULL
) {
2148 if (cmd
== tab
->cmd
)