2 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
4 * Copyright (c) 2003 Mathew Kanner
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (augustss@netbsd.org).
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Parts of this file started out as NetBSD: midi.c 1.31
35 * They are mostly gone. Still the most obvious will be the state
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/queue.h>
45 #include <sys/kernel.h>
47 #include <sys/mutex.h>
49 #include <sys/signalvar.h>
51 #include <sys/selinfo.h>
52 #include <sys/sysctl.h>
53 #include <sys/malloc.h>
56 #include <sys/fcntl.h>
57 #include <sys/types.h>
62 #include <sys/module.h>
64 #ifdef HAVE_KERNEL_OPTION_HEADERS
68 #include <dev/sound/midi/midi.h>
71 #include <dev/sound/midi/midiq.h>
73 MALLOC_DEFINE(M_MIDI, "midi buffers", "Midi data allocation area");
75 #ifndef KOBJMETHOD_END
76 #define KOBJMETHOD_END { NULL, NULL }
79 #define PCMMKMINOR(u, d, c) ((((c) & 0xff) << 16) | (((u) & 0x0f) << 4) | ((d) & 0x0f))
80 #define MIDIMKMINOR(u, d, c) PCMMKMINOR(u, d, c)
82 #define MIDI_DEV_RAW 2
83 #define MIDI_DEV_MIDICTL 12
86 MIDI_IN_START, MIDI_IN_SYSEX, MIDI_IN_DATA
90 * The MPU interface current has init() uninit() inqsize() outqsize()
91 * callback() : fiddle with the tx|rx status.
97 * /dev/rmidi Structure definitions
100 #define MIDI_NAMELEN 16
103 struct mtx lock; /* Protects all but queues */
106 int unit; /* Should only be used in midistat */
107 int channel; /* Should only be used in midistat */
110 int flags; /* File flags */
111 char name[MIDI_NAMELEN];
112 struct mtx qlock; /* Protects inq, outq and flags */
113 MIDIQ_HEAD(, char) inq, outq;
115 struct selinfo rsel, wsel;
116 int hiwat; /* QLEN(outq)>High-water -> disable
117 * writes from userland */
118 enum midi_states inq_state;
119 int inq_status, inq_left; /* Variables for the state machine in
120 * Midi_in, this is to provide that
121 * signals only get issued only
122 * complete command packets. */
125 struct synth_midi *synth;
127 TAILQ_ENTRY(snd_midi) link;
135 static synth_open_t midisynth_open;
136 static synth_close_t midisynth_close;
137 static synth_writeraw_t midisynth_writeraw;
138 static synth_killnote_t midisynth_killnote;
139 static synth_startnote_t midisynth_startnote;
140 static synth_setinstr_t midisynth_setinstr;
141 static synth_alloc_t midisynth_alloc;
142 static synth_controller_t midisynth_controller;
143 static synth_bender_t midisynth_bender;
146 static kobj_method_t midisynth_methods[] = {
147 KOBJMETHOD(synth_open, midisynth_open),
148 KOBJMETHOD(synth_close, midisynth_close),
149 KOBJMETHOD(synth_writeraw, midisynth_writeraw),
150 KOBJMETHOD(synth_setinstr, midisynth_setinstr),
151 KOBJMETHOD(synth_startnote, midisynth_startnote),
152 KOBJMETHOD(synth_killnote, midisynth_killnote),
153 KOBJMETHOD(synth_alloc, midisynth_alloc),
154 KOBJMETHOD(synth_controller, midisynth_controller),
155 KOBJMETHOD(synth_bender, midisynth_bender),
159 DEFINE_CLASS(midisynth, midisynth_methods, 0);
162 * Module Exports & Interface
164 * struct midi_chan *midi_init(MPU_CLASS cls, int unit, int chan,
166 * int midi_uninit(struct snd_midi *)
169 * EBUSY or other error
171 * int midi_in(struct snd_midi *, char *buf, int count)
172 * int midi_out(struct snd_midi *, char *buf, int count)
174 * midi_{in,out} return actual size transfered
180 * midi_devs tailq, holder of all rmidi instances protected by midistat_lock
183 TAILQ_HEAD(, snd_midi) midi_devs;
186 * /dev/midistat variables and declarations, protected by midistat_lock
189 static struct sx midistat_lock;
190 static int midistat_isopen = 0;
191 static struct sbuf midistat_sbuf;
192 static struct cdev *midistat_dev;
195 * /dev/midistat dev_t declarations
198 static d_open_t midistat_open;
199 static d_close_t midistat_close;
200 static d_read_t midistat_read;
202 static struct cdevsw midistat_cdevsw = {
203 .d_version = D_VERSION,
204 .d_open = midistat_open,
205 .d_close = midistat_close,
206 .d_read = midistat_read,
207 .d_name = "midistat",
212 * /dev/rmidi dev_t declarations, struct variable access is protected by
213 * locks contained within the structure.
216 static d_open_t midi_open;
217 static d_close_t midi_close;
218 static d_ioctl_t midi_ioctl;
219 static d_read_t midi_read;
220 static d_write_t midi_write;
221 static d_poll_t midi_poll;
223 static struct cdevsw midi_cdevsw = {
224 .d_version = D_VERSION,
226 .d_close = midi_close,
228 .d_write = midi_write,
229 .d_ioctl = midi_ioctl,
235 * Prototypes of library functions
238 static int midi_destroy(struct snd_midi *, int);
239 static int midistat_prepare(struct sbuf * s);
240 static int midi_load(void);
241 static int midi_unload(void);
246 SYSCTL_NODE(_hw, OID_AUTO, midi, CTLFLAG_RD, 0, "Midi driver");
247 static SYSCTL_NODE(_hw_midi, OID_AUTO, stat, CTLFLAG_RD, 0, "Status device");
250 /* XXX: should this be moved into debug.midi? */
251 SYSCTL_INT(_hw_midi, OID_AUTO, debug, CTLFLAG_RW, &midi_debug, 0, "");
254 SYSCTL_INT(_hw_midi, OID_AUTO, dumpraw, CTLFLAG_RW, &midi_dumpraw, 0, "");
257 SYSCTL_INT(_hw_midi, OID_AUTO, instroff, CTLFLAG_RW, &midi_instroff, 0, "");
259 int midistat_verbose;
260 SYSCTL_INT(_hw_midi_stat, OID_AUTO, verbose, CTLFLAG_RW,
261 &midistat_verbose, 0, "");
263 #define MIDI_DEBUG(l,a) if(midi_debug>=l) a
269 * Register a new rmidi device. cls midi_if interface unit == 0 means
270 * auto-assign new unit number unit != 0 already assigned a unit number, eg.
271 * not the first channel provided by this device. channel, sub-unit
272 * cookie is passed back on MPU calls Typical device drivers will call with
273 * unit=0, channel=1..(number of channels) and cookie=soft_c and won't care
274 * what unit number is used.
276 * It is an error to call midi_init with an already used unit/channel combo.
278 * Returns NULL on error
282 midi_init(kobj_class_t cls, int unit, int channel, void *cookie)
286 int inqsize, outqsize;
289 MIDI_DEBUG(1, printf("midiinit: unit %d/%d.\n", unit, channel));
290 sx_xlock(&midistat_lock);
292 * Protect against call with existing unit/channel or auto-allocate a
296 TAILQ_FOREACH(m, &midi_devs, link) {
299 if (m->unit == unit && m->channel == channel) {
300 mtx_unlock(&m->lock);
305 * Find a better unit number
310 mtx_unlock(&m->lock);
316 MIDI_DEBUG(1, printf("midiinit #2: unit %d/%d.\n", unit, channel));
317 m = malloc(sizeof(*m), M_MIDI, M_WAITOK | M_ZERO);
318 m->synth = malloc(sizeof(*m->synth), M_MIDI, M_WAITOK | M_ZERO);
319 kobj_init((kobj_t)m->synth, &midisynth_class);
321 kobj_init((kobj_t)m, cls);
322 inqsize = MPU_INQSIZE(m, cookie);
323 outqsize = MPU_OUTQSIZE(m, cookie);
325 MIDI_DEBUG(1, printf("midiinit queues %d/%d.\n", inqsize, outqsize));
326 if (!inqsize && !outqsize)
329 mtx_init(&m->lock, "raw midi", NULL, 0);
330 mtx_init(&m->qlock, "q raw midi", NULL, 0);
336 buf = malloc(sizeof(MIDI_TYPE) * inqsize, M_MIDI, M_NOWAIT);
340 MIDIQ_INIT(m->inq, buf, inqsize);
343 buf = malloc(sizeof(MIDI_TYPE) * outqsize, M_MIDI, M_NOWAIT);
346 m->hiwat = outqsize / 2;
348 MIDIQ_INIT(m->outq, buf, outqsize);
350 if ((inqsize && !MIDIQ_BUF(m->inq)) ||
351 (outqsize && !MIDIQ_BUF(m->outq)))
357 m->channel = channel;
360 if (MPU_INIT(m, cookie))
363 mtx_unlock(&m->lock);
364 mtx_unlock(&m->qlock);
366 TAILQ_INSERT_TAIL(&midi_devs, m, link);
368 sx_xunlock(&midistat_lock);
370 m->dev = make_dev(&midi_cdevsw,
371 MIDIMKMINOR(unit, MIDI_DEV_RAW, channel),
372 UID_ROOT, GID_WHEEL, 0666, "midi%d.%d", unit, channel);
378 mtx_destroy(&m->qlock);
379 mtx_destroy(&m->lock);
381 if (MIDIQ_BUF(m->inq))
382 free(MIDIQ_BUF(m->inq), M_MIDI);
383 if (MIDIQ_BUF(m->outq))
384 free(MIDIQ_BUF(m->outq), M_MIDI);
386 free(m->synth, M_MIDI);
389 sx_xunlock(&midistat_lock);
390 MIDI_DEBUG(1, printf("midi_init ended in error\n"));
395 * midi_uninit does not call MIDI_UNINIT, as since this is the implementors
396 * entry point. midi_uninit if fact, does not send any methods. A call to
397 * midi_uninit is a defacto promise that you won't manipulate ch anymore
402 midi_uninit(struct snd_midi *m)
407 sx_xlock(&midistat_lock);
410 if (!(m->rchan || m->wchan))
422 err = midi_destroy(m, 0);
427 mtx_unlock(&m->lock);
429 sx_xunlock(&midistat_lock);
434 * midi_in: process all data until the queue is full, then discards the rest.
435 * Since midi_in is a state machine, data discards can cause it to get out of
436 * whack. Process as much as possible. It calls, wakeup, selnotify and
437 * psignal at most once.
441 static int midi_lengths[] = {2, 2, 2, 2, 1, 1, 2, 0};
444 /* Number of bytes in a MIDI command */
445 #define MIDI_LENGTH(d) (midi_lengths[((d) >> 4) & 7])
446 #define MIDI_ACK 0xfe
447 #define MIDI_IS_STATUS(d) ((d) >= 0x80)
448 #define MIDI_IS_COMMON(d) ((d) >= 0xf0)
450 #define MIDI_SYSEX_START 0xF0
451 #define MIDI_SYSEX_END 0xF7
455 midi_in(struct snd_midi *m, MIDI_TYPE *buf, int size)
457 /* int i, sig, enq; */
460 /* MIDI_TYPE data; */
461 MIDI_DEBUG(5, printf("midi_in: m=%p size=%d\n", m, size));
466 if (!(m->flags & M_RX))
474 * Don't bother queuing if not in read mode. Discard everything and
475 * return size so the caller doesn't freak out.
478 if (!(m->flags & M_RX))
481 for (i = sig = 0; i < size; i++) {
485 if (data == MIDI_ACK)
488 switch (m->inq_state) {
490 if (MIDI_IS_STATUS(data)) {
492 case 0xf0: /* Sysex */
493 m->inq_state = MIDI_IN_SYSEX;
495 case 0xf1: /* MTC quarter frame */
496 case 0xf3: /* Song select */
497 m->inq_state = MIDI_IN_DATA;
501 case 0xf2: /* Song position pointer */
502 m->inq_state = MIDI_IN_DATA;
507 if (MIDI_IS_COMMON(data)) {
511 m->inq_state = MIDI_IN_DATA;
513 m->inq_status = data;
514 m->inq_left = MIDI_LENGTH(data);
518 } else if (MIDI_IS_STATUS(m->inq_status)) {
519 m->inq_state = MIDI_IN_DATA;
520 if (!MIDIQ_FULL(m->inq)) {
522 MIDIQ_ENQ(m->inq, &m->inq_status, 1);
525 m->inq_left = MIDI_LENGTH(m->inq_status) - 1;
529 * End of case MIDI_IN_START:
534 if (--m->inq_left <= 0)
535 sig = 1;/* deliver data */
538 if (data == MIDI_SYSEX_END)
539 m->inq_state = MIDI_IN_START;
544 if (!MIDIQ_FULL(m->inq)) {
545 MIDIQ_ENQ(m->inq, &data, 1);
549 * End of the state machines main "for loop"
554 MIDI_DEBUG(6, printf("midi_in: len %jd avail %jd\n",
555 (intmax_t)MIDIQ_LEN(m->inq),
556 (intmax_t)MIDIQ_AVAIL(m->inq)));
557 if (MIDIQ_AVAIL(m->inq) > size) {
559 MIDIQ_ENQ(m->inq, buf, size);
561 MIDI_DEBUG(4, printf("midi_in: Discarding data qu\n"));
562 mtx_unlock(&m->qlock);
572 kern_psignal(m->async, SIGIO);
573 PROC_UNLOCK(m->async);
578 mtx_unlock(&m->qlock);
583 * midi_out: The only clearer of the M_TXEN flag.
586 midi_out(struct snd_midi *m, MIDI_TYPE *buf, int size)
593 if (!(m->flags & M_TXEN))
596 MIDI_DEBUG(2, printf("midi_out: %p\n", m));
598 used = MIN(size, MIDIQ_LEN(m->outq));
599 MIDI_DEBUG(3, printf("midi_out: used %d\n", used));
601 MIDIQ_DEQ(m->outq, buf, used);
602 if (MIDIQ_EMPTY(m->outq)) {
604 MPU_CALLBACKP(m, m->cookie, m->flags);
606 if (used && MIDIQ_AVAIL(m->outq) > m->hiwat) {
614 kern_psignal(m->async, SIGIO);
615 PROC_UNLOCK(m->async);
618 mtx_unlock(&m->qlock);
624 * /dev/rmidi#.# device access functions
627 midi_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
629 struct snd_midi *m = i_dev->si_drv1;
632 MIDI_DEBUG(1, printf("midiopen %p %s %s\n", td,
633 flags & FREAD ? "M_RX" : "", flags & FWRITE ? "M_TX" : ""));
643 if (MIDIQ_SIZE(m->inq) == 0)
645 else if (m->flags & M_RX)
650 if (flags & FWRITE) {
651 if (MIDIQ_SIZE(m->outq) == 0)
653 else if (m->flags & M_TX)
665 m->flags |= M_RX | M_RXEN;
667 * Only clear the inq, the outq might still have data to drain
668 * from a previous session
676 MPU_CALLBACK(m, m->cookie, m->flags);
678 MIDI_DEBUG(2, printf("midi_open: opened.\n"));
680 err: mtx_unlock(&m->qlock);
681 mtx_unlock(&m->lock);
686 midi_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
688 struct snd_midi *m = i_dev->si_drv1;
692 MIDI_DEBUG(1, printf("midi_close %p %s %s\n", td,
693 flags & FREAD ? "M_RX" : "", flags & FWRITE ? "M_TX" : ""));
701 if ((flags & FREAD && !(m->flags & M_RX)) ||
702 (flags & FWRITE && !(m->flags & M_TX))) {
711 m->flags &= ~(M_RX | M_RXEN);
715 if ((m->flags & (M_TXEN | M_RXEN)) != (oldflags & (M_RXEN | M_TXEN)))
716 MPU_CALLBACK(m, m->cookie, m->flags);
718 MIDI_DEBUG(1, printf("midi_close: closed, busy = %d.\n", m->busy));
720 mtx_unlock(&m->qlock);
721 mtx_unlock(&m->lock);
727 * TODO: midi_read, per oss programmer's guide pg. 42 should return as soon
728 * as data is available.
731 midi_read(struct cdev *i_dev, struct uio *uio, int ioflag)
733 #define MIDI_RSIZE 32
734 struct snd_midi *m = i_dev->si_drv1;
737 char buf[MIDI_RSIZE];
739 MIDI_DEBUG(5, printf("midiread: count=%lu\n",
740 (unsigned long)uio->uio_resid));
750 if (!(m->flags & M_RX))
753 while (uio->uio_resid > 0) {
754 while (MIDIQ_EMPTY(m->inq)) {
755 retval = EWOULDBLOCK;
756 if (ioflag & O_NONBLOCK)
758 mtx_unlock(&m->lock);
760 retval = msleep(&m->rchan, &m->qlock,
761 PCATCH | PDROP, "midi RX", 0);
763 * We slept, maybe things have changed since last
768 if (m != i_dev->si_drv1)
770 /* if (retval && retval != ERESTART) */
779 MIDI_DEBUG(6, printf("midi_read start\n"));
781 * At this point, it is certain that m->inq has data
784 used = MIN(MIDIQ_LEN(m->inq), uio->uio_resid);
785 used = MIN(used, MIDI_RSIZE);
787 MIDI_DEBUG(6, printf("midiread: uiomove cc=%d\n", used));
788 MIDIQ_DEQ(m->inq, buf, used);
789 retval = uiomove(buf, used, uio);
795 * If we Made it here then transfer is good
798 err1: mtx_unlock(&m->qlock);
799 mtx_unlock(&m->lock);
800 err0: MIDI_DEBUG(4, printf("midi_read: ret %d\n", retval));
805 * midi_write: The only setter of M_TXEN
809 midi_write(struct cdev *i_dev, struct uio *uio, int ioflag)
811 #define MIDI_WSIZE 32
812 struct snd_midi *m = i_dev->si_drv1;
815 char buf[MIDI_WSIZE];
818 MIDI_DEBUG(4, printf("midi_write\n"));
826 if (!(m->flags & M_TX))
829 while (uio->uio_resid > 0) {
830 while (MIDIQ_AVAIL(m->outq) == 0) {
831 retval = EWOULDBLOCK;
832 if (ioflag & O_NONBLOCK)
834 mtx_unlock(&m->lock);
836 MIDI_DEBUG(3, printf("midi_write msleep\n"));
837 retval = msleep(&m->wchan, &m->qlock,
838 PCATCH | PDROP, "midi TX", 0);
840 * We slept, maybe things have changed since last
845 if (m != i_dev->si_drv1)
857 * We are certain than data can be placed on the queue
860 used = MIN(MIDIQ_AVAIL(m->outq), uio->uio_resid);
861 used = MIN(used, MIDI_WSIZE);
862 MIDI_DEBUG(5, printf("midiout: resid %zd len %jd avail %jd\n",
863 uio->uio_resid, (intmax_t)MIDIQ_LEN(m->outq),
864 (intmax_t)MIDIQ_AVAIL(m->outq)));
867 MIDI_DEBUG(5, printf("midi_write: uiomove cc=%d\n", used));
868 retval = uiomove(buf, used, uio);
871 MIDIQ_ENQ(m->outq, buf, used);
873 * Inform the bottom half that data can be written
875 if (!(m->flags & M_TXEN)) {
877 MPU_CALLBACK(m, m->cookie, m->flags);
881 * If we Made it here then transfer is good
884 err1: mtx_unlock(&m->qlock);
885 mtx_unlock(&m->lock);
890 midi_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
897 midi_poll(struct cdev *i_dev, int events, struct thread *td)
899 struct snd_midi *m = i_dev->si_drv1;
910 if (events & (POLLIN | POLLRDNORM))
911 if (!MIDIQ_EMPTY(m->inq))
912 events |= events & (POLLIN | POLLRDNORM);
914 if (events & (POLLOUT | POLLWRNORM))
915 if (MIDIQ_AVAIL(m->outq) < m->hiwat)
916 events |= events & (POLLOUT | POLLWRNORM);
919 if (events & (POLLIN | POLLRDNORM))
920 selrecord(td, &m->rsel);
922 if (events & (POLLOUT | POLLWRNORM))
923 selrecord(td, &m->wsel);
925 mtx_unlock(&m->lock);
926 mtx_unlock(&m->qlock);
932 * /dev/midistat device functions
936 midistat_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
940 MIDI_DEBUG(1, printf("midistat_open\n"));
942 sx_xlock(&midistat_lock);
943 if (midistat_isopen) {
944 sx_xunlock(&midistat_lock);
948 if (sbuf_new(&midistat_sbuf, NULL, 4096, SBUF_AUTOEXTEND) == NULL) {
952 error = (midistat_prepare(&midistat_sbuf) > 0) ? 0 : ENOMEM;
956 sx_xunlock(&midistat_lock);
961 midistat_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
963 MIDI_DEBUG(1, printf("midistat_close\n"));
964 sx_xlock(&midistat_lock);
965 if (!midistat_isopen) {
966 sx_xunlock(&midistat_lock);
969 sbuf_delete(&midistat_sbuf);
971 sx_xunlock(&midistat_lock);
976 midistat_read(struct cdev *i_dev, struct uio *uio, int flag)
981 MIDI_DEBUG(4, printf("midistat_read\n"));
982 sx_xlock(&midistat_lock);
983 if (!midistat_isopen) {
984 sx_xunlock(&midistat_lock);
987 if (uio->uio_offset < 0 || uio->uio_offset > sbuf_len(&midistat_sbuf)) {
988 sx_xunlock(&midistat_lock);
992 l = lmin(uio->uio_resid, sbuf_len(&midistat_sbuf) - uio->uio_offset);
994 err = uiomove(sbuf_data(&midistat_sbuf) + uio->uio_offset, l,
997 sx_xunlock(&midistat_lock);
1002 * Module library functions
1006 midistat_prepare(struct sbuf *s)
1010 sx_assert(&midistat_lock, SA_XLOCKED);
1012 sbuf_printf(s, "FreeBSD Midi Driver (midi2)\n");
1013 if (TAILQ_EMPTY(&midi_devs)) {
1014 sbuf_printf(s, "No devices installed.\n");
1018 sbuf_printf(s, "Installed devices:\n");
1020 TAILQ_FOREACH(m, &midi_devs, link) {
1022 sbuf_printf(s, "%s [%d/%d:%s]", m->name, m->unit, m->channel,
1023 MPU_PROVIDER(m, m->cookie));
1024 sbuf_printf(s, "%s", MPU_DESCR(m, m->cookie, midistat_verbose));
1025 sbuf_printf(s, "\n");
1026 mtx_unlock(&m->lock);
1035 * Convert IOCTL command to string for debugging
1039 midi_cmdname(int cmd)
1044 } *tab, cmdtab_midiioctl[] = {
1045 #define A(x) {x, ## x}
1047 * Once we have some real IOCTLs define, the following will
1050 * A(SNDCTL_MIDI_PRETIME), A(SNDCTL_MIDI_MPUMODE),
1051 * A(SNDCTL_MIDI_MPUCMD), A(SNDCTL_SYNTH_INFO),
1052 * A(SNDCTL_MIDI_INFO), A(SNDCTL_SYNTH_MEMAVL),
1053 * A(SNDCTL_FM_LOAD_INSTR), A(SNDCTL_FM_4OP_ENABLE),
1054 * A(MIOSPASSTHRU), A(MIOGPASSTHRU), A(AIONWRITE),
1055 * A(AIOGSIZE), A(AIOSSIZE), A(AIOGFMT), A(AIOSFMT),
1056 * A(AIOGMIX), A(AIOSMIX), A(AIOSTOP), A(AIOSYNC),
1065 for (tab = cmdtab_midiioctl; tab->cmd != cmd && tab->cmd != -1; tab++);
1077 midisynth_open(void *n, void *arg, int flags)
1079 struct snd_midi *m = ((struct synth_midi *)n)->m;
1082 MIDI_DEBUG(1, printf("midisynth_open %s %s\n",
1083 flags & FREAD ? "M_RX" : "", flags & FWRITE ? "M_TX" : ""));
1089 mtx_lock(&m->qlock);
1093 if (flags & FREAD) {
1094 if (MIDIQ_SIZE(m->inq) == 0)
1096 else if (m->flags & M_RX)
1101 if (flags & FWRITE) {
1102 if (MIDIQ_SIZE(m->outq) == 0)
1104 else if (m->flags & M_TX)
1112 * TODO: Consider m->async = 0;
1115 if (flags & FREAD) {
1116 m->flags |= M_RX | M_RXEN;
1118 * Only clear the inq, the outq might still have data to drain
1119 * from a previous session
1121 MIDIQ_CLEAR(m->inq);
1125 if (flags & FWRITE) {
1129 m->synth_flags = flags & (FREAD | FWRITE);
1131 MPU_CALLBACK(m, m->cookie, m->flags);
1134 err: mtx_unlock(&m->qlock);
1135 mtx_unlock(&m->lock);
1136 MIDI_DEBUG(2, printf("midisynth_open: return %d.\n", retval));
1141 midisynth_close(void *n)
1143 struct snd_midi *m = ((struct synth_midi *)n)->m;
1147 MIDI_DEBUG(1, printf("midisynth_close %s %s\n",
1148 m->synth_flags & FREAD ? "M_RX" : "",
1149 m->synth_flags & FWRITE ? "M_TX" : ""));
1155 mtx_lock(&m->qlock);
1157 if ((m->synth_flags & FREAD && !(m->flags & M_RX)) ||
1158 (m->synth_flags & FWRITE && !(m->flags & M_TX))) {
1164 oldflags = m->flags;
1166 if (m->synth_flags & FREAD)
1167 m->flags &= ~(M_RX | M_RXEN);
1168 if (m->synth_flags & FWRITE)
1171 if ((m->flags & (M_TXEN | M_RXEN)) != (oldflags & (M_RXEN | M_TXEN)))
1172 MPU_CALLBACK(m, m->cookie, m->flags);
1174 MIDI_DEBUG(1, printf("midi_close: closed, busy = %d.\n", m->busy));
1176 mtx_unlock(&m->qlock);
1177 mtx_unlock(&m->lock);
1187 midisynth_writeraw(void *n, uint8_t *buf, size_t len)
1189 struct snd_midi *m = ((struct synth_midi *)n)->m;
1194 MIDI_DEBUG(4, printf("midisynth_writeraw\n"));
1202 mtx_lock(&m->qlock);
1204 if (!(m->flags & M_TX))
1208 printf("midi dump: ");
1211 while (MIDIQ_AVAIL(m->outq) == 0) {
1212 if (!(m->flags & M_TXEN)) {
1214 MPU_CALLBACK(m, m->cookie, m->flags);
1216 mtx_unlock(&m->lock);
1218 MIDI_DEBUG(3, printf("midisynth_writeraw msleep\n"));
1219 retval = msleep(&m->wchan, &m->qlock,
1220 PCATCH | PDROP, "midi TX", 0);
1222 * We slept, maybe things have changed since last
1225 if (retval == EINTR)
1231 mtx_lock(&m->qlock);
1238 * We are certain than data can be placed on the queue
1241 used = MIN(MIDIQ_AVAIL(m->outq), len);
1242 used = MIN(used, MIDI_WSIZE);
1244 printf("midi_synth: resid %zu len %jd avail %jd\n",
1245 len, (intmax_t)MIDIQ_LEN(m->outq),
1246 (intmax_t)MIDIQ_AVAIL(m->outq)));
1249 for (i = 0; i < used; i++)
1250 printf("%x ", buf[i]);
1252 MIDIQ_ENQ(m->outq, buf, used);
1256 * Inform the bottom half that data can be written
1258 if (!(m->flags & M_TXEN)) {
1260 MPU_CALLBACK(m, m->cookie, m->flags);
1264 * If we Made it here then transfer is good
1270 err1: mtx_unlock(&m->qlock);
1271 mtx_unlock(&m->lock);
1272 err0: return retval;
1276 midisynth_killnote(void *n, uint8_t chn, uint8_t note, uint8_t vel)
1281 if (note > 127 || chn > 15)
1288 c[0] = 0x90 | (chn & 0x0f); /* Note on. */
1289 c[1] = (u_char)note;
1292 c[0] = 0x80 | (chn & 0x0f); /* Note off. */
1293 c[1] = (u_char)note;
1297 return midisynth_writeraw(n, c, 3);
1301 midisynth_setinstr(void *n, uint8_t chn, uint16_t instr)
1305 if (instr > 127 || chn > 15)
1308 c[0] = 0xc0 | (chn & 0x0f); /* Progamme change. */
1309 c[1] = instr + midi_instroff;
1311 return midisynth_writeraw(n, c, 2);
1315 midisynth_startnote(void *n, uint8_t chn, uint8_t note, uint8_t vel)
1319 if (note > 127 || chn > 15)
1325 c[0] = 0x90 | (chn & 0x0f); /* Note on. */
1326 c[1] = (u_char)note;
1329 return midisynth_writeraw(n, c, 3);
1332 midisynth_alloc(void *n, uint8_t chan, uint8_t note)
1338 midisynth_controller(void *n, uint8_t chn, uint8_t ctrlnum, uint16_t val)
1342 if (ctrlnum > 127 || chn > 15)
1345 c[0] = 0xb0 | (chn & 0x0f); /* Control Message. */
1348 return midisynth_writeraw(n, c, 3);
1352 midisynth_bender(void *n, uint8_t chn, uint16_t val)
1357 if (val > 16383 || chn > 15)
1360 c[0] = 0xe0 | (chn & 0x0f); /* Pitch bend. */
1361 c[1] = (u_char)val & 0x7f;
1362 c[2] = (u_char)(val >> 7) & 0x7f;
1364 return midisynth_writeraw(n, c, 3);
1368 * Single point of midi destructions.
1371 midi_destroy(struct snd_midi *m, int midiuninit)
1373 sx_assert(&midistat_lock, SA_XLOCKED);
1374 mtx_assert(&m->lock, MA_OWNED);
1376 MIDI_DEBUG(3, printf("midi_destroy\n"));
1377 m->dev->si_drv1 = NULL;
1378 mtx_unlock(&m->lock); /* XXX */
1379 destroy_dev(m->dev);
1380 TAILQ_REMOVE(&midi_devs, m, link);
1382 MPU_UNINIT(m, m->cookie);
1383 free(MIDIQ_BUF(m->inq), M_MIDI);
1384 free(MIDIQ_BUF(m->outq), M_MIDI);
1385 mtx_destroy(&m->qlock);
1386 mtx_destroy(&m->lock);
1387 free(m->synth, M_MIDI);
1393 * Load and unload functions, creates the /dev/midistat device
1399 sx_init(&midistat_lock, "midistat lock");
1400 TAILQ_INIT(&midi_devs);
1402 midistat_dev = make_dev(&midistat_cdevsw,
1403 MIDIMKMINOR(0, MIDI_DEV_MIDICTL, 0),
1404 UID_ROOT, GID_WHEEL, 0666, "midistat");
1412 struct snd_midi *m, *tmp;
1415 MIDI_DEBUG(1, printf("midi_unload()\n"));
1417 sx_xlock(&midistat_lock);
1418 if (midistat_isopen)
1421 TAILQ_FOREACH_SAFE(m, &midi_devs, link, tmp) {
1426 retval = midi_destroy(m, 1);
1430 sx_xunlock(&midistat_lock);
1431 destroy_dev(midistat_dev);
1434 * Made it here then unload is complete
1436 sx_destroy(&midistat_lock);
1440 mtx_unlock(&m->lock);
1442 sx_xunlock(&midistat_lock);
1444 MIDI_DEBUG(2, printf("midi_unload: failed\n"));
1448 extern int seq_modevent(module_t mod, int type, void *data);
1451 midi_modevent(module_t mod, int type, void *data)
1459 retval = midi_load();
1461 retval = seq_modevent(mod, type, data);
1465 retval = midi_unload();
1467 retval = seq_modevent(mod, type, data);
1478 midimapper_addseq(void *arg1, int *unit, void **cookie)
1482 return (kobj_t)arg1;
1486 midimapper_open(void *arg1, void **cookie)
1491 sx_xlock(&midistat_lock);
1492 TAILQ_FOREACH(m, &midi_devs, link) {
1495 sx_xunlock(&midistat_lock);
1500 midimapper_close(void *arg1, void *cookie)
1506 midimapper_fetch_synth(void *arg, void *cookie, int unit)
1511 sx_xlock(&midistat_lock);
1512 TAILQ_FOREACH(m, &midi_devs, link) {
1513 if (unit == retval) {
1514 sx_xunlock(&midistat_lock);
1515 return (kobj_t)m->synth;
1519 sx_xunlock(&midistat_lock);
1523 DEV_MODULE(midi, midi_modevent, NULL);
1524 MODULE_VERSION(midi, 1);