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;
145 static kobj_method_t midisynth_methods[] = {
146 KOBJMETHOD(synth_open, midisynth_open),
147 KOBJMETHOD(synth_close, midisynth_close),
148 KOBJMETHOD(synth_writeraw, midisynth_writeraw),
149 KOBJMETHOD(synth_setinstr, midisynth_setinstr),
150 KOBJMETHOD(synth_startnote, midisynth_startnote),
151 KOBJMETHOD(synth_killnote, midisynth_killnote),
152 KOBJMETHOD(synth_alloc, midisynth_alloc),
153 KOBJMETHOD(synth_controller, midisynth_controller),
154 KOBJMETHOD(synth_bender, midisynth_bender),
158 DEFINE_CLASS(midisynth, midisynth_methods, 0);
161 * Module Exports & Interface
163 * struct midi_chan *midi_init(MPU_CLASS cls, int unit, int chan,
165 * int midi_uninit(struct snd_midi *)
168 * EBUSY or other error
170 * int midi_in(struct snd_midi *, char *buf, int count)
171 * int midi_out(struct snd_midi *, char *buf, int count)
173 * midi_{in,out} return actual size transfered
178 * midi_devs tailq, holder of all rmidi instances protected by midistat_lock
181 TAILQ_HEAD(, snd_midi) midi_devs;
184 * /dev/midistat variables and declarations, protected by midistat_lock
187 static struct sx midistat_lock;
188 static int midistat_isopen = 0;
189 static struct sbuf midistat_sbuf;
190 static struct cdev *midistat_dev;
193 * /dev/midistat dev_t declarations
196 static d_open_t midistat_open;
197 static d_close_t midistat_close;
198 static d_read_t midistat_read;
200 static struct cdevsw midistat_cdevsw = {
201 .d_version = D_VERSION,
202 .d_open = midistat_open,
203 .d_close = midistat_close,
204 .d_read = midistat_read,
205 .d_name = "midistat",
209 * /dev/rmidi dev_t declarations, struct variable access is protected by
210 * locks contained within the structure.
213 static d_open_t midi_open;
214 static d_close_t midi_close;
215 static d_ioctl_t midi_ioctl;
216 static d_read_t midi_read;
217 static d_write_t midi_write;
218 static d_poll_t midi_poll;
220 static struct cdevsw midi_cdevsw = {
221 .d_version = D_VERSION,
223 .d_close = midi_close,
225 .d_write = midi_write,
226 .d_ioctl = midi_ioctl,
232 * Prototypes of library functions
235 static int midi_destroy(struct snd_midi *, int);
236 static int midistat_prepare(struct sbuf * s);
237 static int midi_load(void);
238 static int midi_unload(void);
243 SYSCTL_NODE(_hw, OID_AUTO, midi, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
245 static SYSCTL_NODE(_hw_midi, OID_AUTO, stat, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
249 /* XXX: should this be moved into debug.midi? */
250 SYSCTL_INT(_hw_midi, OID_AUTO, debug, CTLFLAG_RW, &midi_debug, 0, "");
253 SYSCTL_INT(_hw_midi, OID_AUTO, dumpraw, CTLFLAG_RW, &midi_dumpraw, 0, "");
256 SYSCTL_INT(_hw_midi, OID_AUTO, instroff, CTLFLAG_RW, &midi_instroff, 0, "");
258 int midistat_verbose;
259 SYSCTL_INT(_hw_midi_stat, OID_AUTO, verbose, CTLFLAG_RW,
260 &midistat_verbose, 0, "");
262 #define MIDI_DEBUG(l,a) if(midi_debug>=l) a
268 * Register a new rmidi device. cls midi_if interface unit == 0 means
269 * auto-assign new unit number unit != 0 already assigned a unit number, eg.
270 * not the first channel provided by this device. channel, sub-unit
271 * cookie is passed back on MPU calls Typical device drivers will call with
272 * unit=0, channel=1..(number of channels) and cookie=soft_c and won't care
273 * what unit number is used.
275 * It is an error to call midi_init with an already used unit/channel combo.
277 * Returns NULL on error
281 midi_init(kobj_class_t cls, int unit, int channel, void *cookie)
285 int inqsize, outqsize;
288 MIDI_DEBUG(1, printf("midiinit: unit %d/%d.\n", unit, channel));
289 sx_xlock(&midistat_lock);
291 * Protect against call with existing unit/channel or auto-allocate a
295 TAILQ_FOREACH(m, &midi_devs, link) {
298 if (m->unit == unit && m->channel == channel) {
299 mtx_unlock(&m->lock);
304 * Find a better unit number
309 mtx_unlock(&m->lock);
315 MIDI_DEBUG(1, printf("midiinit #2: unit %d/%d.\n", unit, channel));
316 m = malloc(sizeof(*m), M_MIDI, M_WAITOK | M_ZERO);
317 m->synth = malloc(sizeof(*m->synth), M_MIDI, M_WAITOK | M_ZERO);
318 kobj_init((kobj_t)m->synth, &midisynth_class);
320 kobj_init((kobj_t)m, cls);
321 inqsize = MPU_INQSIZE(m, cookie);
322 outqsize = MPU_OUTQSIZE(m, cookie);
324 MIDI_DEBUG(1, printf("midiinit queues %d/%d.\n", inqsize, outqsize));
325 if (!inqsize && !outqsize)
328 mtx_init(&m->lock, "raw midi", NULL, 0);
329 mtx_init(&m->qlock, "q raw midi", NULL, 0);
335 buf = malloc(sizeof(MIDI_TYPE) * inqsize, M_MIDI, M_NOWAIT);
339 MIDIQ_INIT(m->inq, buf, inqsize);
342 buf = malloc(sizeof(MIDI_TYPE) * outqsize, M_MIDI, M_NOWAIT);
345 m->hiwat = outqsize / 2;
347 MIDIQ_INIT(m->outq, buf, outqsize);
349 if ((inqsize && !MIDIQ_BUF(m->inq)) ||
350 (outqsize && !MIDIQ_BUF(m->outq)))
356 m->channel = channel;
359 if (MPU_INIT(m, cookie))
362 mtx_unlock(&m->lock);
363 mtx_unlock(&m->qlock);
365 TAILQ_INSERT_TAIL(&midi_devs, m, link);
367 sx_xunlock(&midistat_lock);
369 m->dev = make_dev(&midi_cdevsw,
370 MIDIMKMINOR(unit, MIDI_DEV_RAW, channel),
371 UID_ROOT, GID_WHEEL, 0666, "midi%d.%d", unit, channel);
377 mtx_destroy(&m->qlock);
378 mtx_destroy(&m->lock);
380 if (MIDIQ_BUF(m->inq))
381 free(MIDIQ_BUF(m->inq), M_MIDI);
382 if (MIDIQ_BUF(m->outq))
383 free(MIDIQ_BUF(m->outq), M_MIDI);
385 free(m->synth, M_MIDI);
388 sx_xunlock(&midistat_lock);
389 MIDI_DEBUG(1, printf("midi_init ended in error\n"));
394 * midi_uninit does not call MIDI_UNINIT, as since this is the implementors
395 * entry point. midi_uninit if fact, does not send any methods. A call to
396 * midi_uninit is a defacto promise that you won't manipulate ch anymore
401 midi_uninit(struct snd_midi *m)
406 sx_xlock(&midistat_lock);
409 if (!(m->rchan || m->wchan))
421 err = midi_destroy(m, 0);
426 mtx_unlock(&m->lock);
428 sx_xunlock(&midistat_lock);
433 * midi_in: process all data until the queue is full, then discards the rest.
434 * Since midi_in is a state machine, data discards can cause it to get out of
435 * whack. Process as much as possible. It calls, wakeup, selnotify and
436 * psignal at most once.
440 static int midi_lengths[] = {2, 2, 2, 2, 1, 1, 2, 0};
443 /* Number of bytes in a MIDI command */
444 #define MIDI_LENGTH(d) (midi_lengths[((d) >> 4) & 7])
445 #define MIDI_ACK 0xfe
446 #define MIDI_IS_STATUS(d) ((d) >= 0x80)
447 #define MIDI_IS_COMMON(d) ((d) >= 0xf0)
449 #define MIDI_SYSEX_START 0xF0
450 #define MIDI_SYSEX_END 0xF7
453 midi_in(struct snd_midi *m, MIDI_TYPE *buf, int size)
455 /* int i, sig, enq; */
458 /* MIDI_TYPE data; */
459 MIDI_DEBUG(5, printf("midi_in: m=%p size=%d\n", m, size));
464 if (!(m->flags & M_RX))
472 * Don't bother queuing if not in read mode. Discard everything and
473 * return size so the caller doesn't freak out.
476 if (!(m->flags & M_RX))
479 for (i = sig = 0; i < size; i++) {
482 if (data == MIDI_ACK)
485 switch (m->inq_state) {
487 if (MIDI_IS_STATUS(data)) {
489 case 0xf0: /* Sysex */
490 m->inq_state = MIDI_IN_SYSEX;
492 case 0xf1: /* MTC quarter frame */
493 case 0xf3: /* Song select */
494 m->inq_state = MIDI_IN_DATA;
498 case 0xf2: /* Song position pointer */
499 m->inq_state = MIDI_IN_DATA;
504 if (MIDI_IS_COMMON(data)) {
508 m->inq_state = MIDI_IN_DATA;
510 m->inq_status = data;
511 m->inq_left = MIDI_LENGTH(data);
515 } else if (MIDI_IS_STATUS(m->inq_status)) {
516 m->inq_state = MIDI_IN_DATA;
517 if (!MIDIQ_FULL(m->inq)) {
519 MIDIQ_ENQ(m->inq, &m->inq_status, 1);
522 m->inq_left = MIDI_LENGTH(m->inq_status) - 1;
526 * End of case MIDI_IN_START:
531 if (--m->inq_left <= 0)
532 sig = 1;/* deliver data */
535 if (data == MIDI_SYSEX_END)
536 m->inq_state = MIDI_IN_START;
541 if (!MIDIQ_FULL(m->inq)) {
542 MIDIQ_ENQ(m->inq, &data, 1);
546 * End of the state machines main "for loop"
551 MIDI_DEBUG(6, printf("midi_in: len %jd avail %jd\n",
552 (intmax_t)MIDIQ_LEN(m->inq),
553 (intmax_t)MIDIQ_AVAIL(m->inq)));
554 if (MIDIQ_AVAIL(m->inq) > size) {
556 MIDIQ_ENQ(m->inq, buf, size);
558 MIDI_DEBUG(4, printf("midi_in: Discarding data qu\n"));
559 mtx_unlock(&m->qlock);
569 kern_psignal(m->async, SIGIO);
570 PROC_UNLOCK(m->async);
575 mtx_unlock(&m->qlock);
580 * midi_out: The only clearer of the M_TXEN flag.
583 midi_out(struct snd_midi *m, MIDI_TYPE *buf, int size)
590 if (!(m->flags & M_TXEN))
593 MIDI_DEBUG(2, printf("midi_out: %p\n", m));
595 used = MIN(size, MIDIQ_LEN(m->outq));
596 MIDI_DEBUG(3, printf("midi_out: used %d\n", used));
598 MIDIQ_DEQ(m->outq, buf, used);
599 if (MIDIQ_EMPTY(m->outq)) {
601 MPU_CALLBACKP(m, m->cookie, m->flags);
603 if (used && MIDIQ_AVAIL(m->outq) > m->hiwat) {
611 kern_psignal(m->async, SIGIO);
612 PROC_UNLOCK(m->async);
615 mtx_unlock(&m->qlock);
620 * /dev/rmidi#.# device access functions
623 midi_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
625 struct snd_midi *m = i_dev->si_drv1;
628 MIDI_DEBUG(1, printf("midiopen %p %s %s\n", td,
629 flags & FREAD ? "M_RX" : "", flags & FWRITE ? "M_TX" : ""));
639 if (MIDIQ_SIZE(m->inq) == 0)
641 else if (m->flags & M_RX)
646 if (flags & FWRITE) {
647 if (MIDIQ_SIZE(m->outq) == 0)
649 else if (m->flags & M_TX)
661 m->flags |= M_RX | M_RXEN;
663 * Only clear the inq, the outq might still have data to drain
664 * from a previous session
672 MPU_CALLBACK(m, m->cookie, m->flags);
674 MIDI_DEBUG(2, printf("midi_open: opened.\n"));
676 err: mtx_unlock(&m->qlock);
677 mtx_unlock(&m->lock);
682 midi_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
684 struct snd_midi *m = i_dev->si_drv1;
688 MIDI_DEBUG(1, printf("midi_close %p %s %s\n", td,
689 flags & FREAD ? "M_RX" : "", flags & FWRITE ? "M_TX" : ""));
697 if ((flags & FREAD && !(m->flags & M_RX)) ||
698 (flags & FWRITE && !(m->flags & M_TX))) {
707 m->flags &= ~(M_RX | M_RXEN);
711 if ((m->flags & (M_TXEN | M_RXEN)) != (oldflags & (M_RXEN | M_TXEN)))
712 MPU_CALLBACK(m, m->cookie, m->flags);
714 MIDI_DEBUG(1, printf("midi_close: closed, busy = %d.\n", m->busy));
716 mtx_unlock(&m->qlock);
717 mtx_unlock(&m->lock);
723 * TODO: midi_read, per oss programmer's guide pg. 42 should return as soon
724 * as data is available.
727 midi_read(struct cdev *i_dev, struct uio *uio, int ioflag)
729 #define MIDI_RSIZE 32
730 struct snd_midi *m = i_dev->si_drv1;
733 char buf[MIDI_RSIZE];
735 MIDI_DEBUG(5, printf("midiread: count=%lu\n",
736 (unsigned long)uio->uio_resid));
746 if (!(m->flags & M_RX))
749 while (uio->uio_resid > 0) {
750 while (MIDIQ_EMPTY(m->inq)) {
751 retval = EWOULDBLOCK;
752 if (ioflag & O_NONBLOCK)
754 mtx_unlock(&m->lock);
756 retval = msleep(&m->rchan, &m->qlock,
757 PCATCH | PDROP, "midi RX", 0);
759 * We slept, maybe things have changed since last
764 if (m != i_dev->si_drv1)
766 /* if (retval && retval != ERESTART) */
775 MIDI_DEBUG(6, printf("midi_read start\n"));
777 * At this point, it is certain that m->inq has data
780 used = MIN(MIDIQ_LEN(m->inq), uio->uio_resid);
781 used = MIN(used, MIDI_RSIZE);
783 MIDI_DEBUG(6, printf("midiread: uiomove cc=%d\n", used));
784 MIDIQ_DEQ(m->inq, buf, used);
785 retval = uiomove(buf, used, uio);
791 * If we Made it here then transfer is good
794 err1: mtx_unlock(&m->qlock);
795 mtx_unlock(&m->lock);
796 err0: MIDI_DEBUG(4, printf("midi_read: ret %d\n", retval));
801 * midi_write: The only setter of M_TXEN
805 midi_write(struct cdev *i_dev, struct uio *uio, int ioflag)
807 #define MIDI_WSIZE 32
808 struct snd_midi *m = i_dev->si_drv1;
811 char buf[MIDI_WSIZE];
813 MIDI_DEBUG(4, printf("midi_write\n"));
821 if (!(m->flags & M_TX))
824 while (uio->uio_resid > 0) {
825 while (MIDIQ_AVAIL(m->outq) == 0) {
826 retval = EWOULDBLOCK;
827 if (ioflag & O_NONBLOCK)
829 mtx_unlock(&m->lock);
831 MIDI_DEBUG(3, printf("midi_write msleep\n"));
832 retval = msleep(&m->wchan, &m->qlock,
833 PCATCH | PDROP, "midi TX", 0);
835 * We slept, maybe things have changed since last
840 if (m != i_dev->si_drv1)
852 * We are certain than data can be placed on the queue
855 used = MIN(MIDIQ_AVAIL(m->outq), uio->uio_resid);
856 used = MIN(used, MIDI_WSIZE);
857 MIDI_DEBUG(5, printf("midiout: resid %zd len %jd avail %jd\n",
858 uio->uio_resid, (intmax_t)MIDIQ_LEN(m->outq),
859 (intmax_t)MIDIQ_AVAIL(m->outq)));
861 MIDI_DEBUG(5, printf("midi_write: uiomove cc=%d\n", used));
862 retval = uiomove(buf, used, uio);
865 MIDIQ_ENQ(m->outq, buf, used);
867 * Inform the bottom half that data can be written
869 if (!(m->flags & M_TXEN)) {
871 MPU_CALLBACK(m, m->cookie, m->flags);
875 * If we Made it here then transfer is good
878 err1: mtx_unlock(&m->qlock);
879 mtx_unlock(&m->lock);
884 midi_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
891 midi_poll(struct cdev *i_dev, int events, struct thread *td)
893 struct snd_midi *m = i_dev->si_drv1;
904 if (events & (POLLIN | POLLRDNORM))
905 if (!MIDIQ_EMPTY(m->inq))
906 events |= events & (POLLIN | POLLRDNORM);
908 if (events & (POLLOUT | POLLWRNORM))
909 if (MIDIQ_AVAIL(m->outq) < m->hiwat)
910 events |= events & (POLLOUT | POLLWRNORM);
913 if (events & (POLLIN | POLLRDNORM))
914 selrecord(td, &m->rsel);
916 if (events & (POLLOUT | POLLWRNORM))
917 selrecord(td, &m->wsel);
919 mtx_unlock(&m->lock);
920 mtx_unlock(&m->qlock);
926 * /dev/midistat device functions
930 midistat_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
934 MIDI_DEBUG(1, printf("midistat_open\n"));
936 sx_xlock(&midistat_lock);
937 if (midistat_isopen) {
938 sx_xunlock(&midistat_lock);
942 if (sbuf_new(&midistat_sbuf, NULL, 4096, SBUF_AUTOEXTEND) == NULL) {
946 error = (midistat_prepare(&midistat_sbuf) > 0) ? 0 : ENOMEM;
950 sx_xunlock(&midistat_lock);
955 midistat_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
957 MIDI_DEBUG(1, printf("midistat_close\n"));
958 sx_xlock(&midistat_lock);
959 if (!midistat_isopen) {
960 sx_xunlock(&midistat_lock);
963 sbuf_delete(&midistat_sbuf);
965 sx_xunlock(&midistat_lock);
970 midistat_read(struct cdev *i_dev, struct uio *uio, int flag)
975 MIDI_DEBUG(4, printf("midistat_read\n"));
976 sx_xlock(&midistat_lock);
977 if (!midistat_isopen) {
978 sx_xunlock(&midistat_lock);
981 if (uio->uio_offset < 0 || uio->uio_offset > sbuf_len(&midistat_sbuf)) {
982 sx_xunlock(&midistat_lock);
986 l = lmin(uio->uio_resid, sbuf_len(&midistat_sbuf) - uio->uio_offset);
988 err = uiomove(sbuf_data(&midistat_sbuf) + uio->uio_offset, l,
991 sx_xunlock(&midistat_lock);
996 * Module library functions
1000 midistat_prepare(struct sbuf *s)
1004 sx_assert(&midistat_lock, SA_XLOCKED);
1006 sbuf_printf(s, "FreeBSD Midi Driver (midi2)\n");
1007 if (TAILQ_EMPTY(&midi_devs)) {
1008 sbuf_printf(s, "No devices installed.\n");
1012 sbuf_printf(s, "Installed devices:\n");
1014 TAILQ_FOREACH(m, &midi_devs, link) {
1016 sbuf_printf(s, "%s [%d/%d:%s]", m->name, m->unit, m->channel,
1017 MPU_PROVIDER(m, m->cookie));
1018 sbuf_printf(s, "%s", MPU_DESCR(m, m->cookie, midistat_verbose));
1019 sbuf_printf(s, "\n");
1020 mtx_unlock(&m->lock);
1029 * Convert IOCTL command to string for debugging
1033 midi_cmdname(int cmd)
1038 } *tab, cmdtab_midiioctl[] = {
1039 #define A(x) {x, ## x}
1041 * Once we have some real IOCTLs define, the following will
1044 * A(SNDCTL_MIDI_PRETIME), A(SNDCTL_MIDI_MPUMODE),
1045 * A(SNDCTL_MIDI_MPUCMD), A(SNDCTL_SYNTH_INFO),
1046 * A(SNDCTL_MIDI_INFO), A(SNDCTL_SYNTH_MEMAVL),
1047 * A(SNDCTL_FM_LOAD_INSTR), A(SNDCTL_FM_4OP_ENABLE),
1048 * A(MIOSPASSTHRU), A(MIOGPASSTHRU), A(AIONWRITE),
1049 * A(AIOGSIZE), A(AIOSSIZE), A(AIOGFMT), A(AIOSFMT),
1050 * A(AIOGMIX), A(AIOSMIX), A(AIOSTOP), A(AIOSYNC),
1059 for (tab = cmdtab_midiioctl; tab->cmd != cmd && tab->cmd != -1; tab++);
1070 midisynth_open(void *n, void *arg, int flags)
1072 struct snd_midi *m = ((struct synth_midi *)n)->m;
1075 MIDI_DEBUG(1, printf("midisynth_open %s %s\n",
1076 flags & FREAD ? "M_RX" : "", flags & FWRITE ? "M_TX" : ""));
1082 mtx_lock(&m->qlock);
1086 if (flags & FREAD) {
1087 if (MIDIQ_SIZE(m->inq) == 0)
1089 else if (m->flags & M_RX)
1094 if (flags & FWRITE) {
1095 if (MIDIQ_SIZE(m->outq) == 0)
1097 else if (m->flags & M_TX)
1105 * TODO: Consider m->async = 0;
1108 if (flags & FREAD) {
1109 m->flags |= M_RX | M_RXEN;
1111 * Only clear the inq, the outq might still have data to drain
1112 * from a previous session
1114 MIDIQ_CLEAR(m->inq);
1118 if (flags & FWRITE) {
1122 m->synth_flags = flags & (FREAD | FWRITE);
1124 MPU_CALLBACK(m, m->cookie, m->flags);
1126 err: mtx_unlock(&m->qlock);
1127 mtx_unlock(&m->lock);
1128 MIDI_DEBUG(2, printf("midisynth_open: return %d.\n", retval));
1133 midisynth_close(void *n)
1135 struct snd_midi *m = ((struct synth_midi *)n)->m;
1139 MIDI_DEBUG(1, printf("midisynth_close %s %s\n",
1140 m->synth_flags & FREAD ? "M_RX" : "",
1141 m->synth_flags & FWRITE ? "M_TX" : ""));
1147 mtx_lock(&m->qlock);
1149 if ((m->synth_flags & FREAD && !(m->flags & M_RX)) ||
1150 (m->synth_flags & FWRITE && !(m->flags & M_TX))) {
1156 oldflags = m->flags;
1158 if (m->synth_flags & FREAD)
1159 m->flags &= ~(M_RX | M_RXEN);
1160 if (m->synth_flags & FWRITE)
1163 if ((m->flags & (M_TXEN | M_RXEN)) != (oldflags & (M_RXEN | M_TXEN)))
1164 MPU_CALLBACK(m, m->cookie, m->flags);
1166 MIDI_DEBUG(1, printf("midi_close: closed, busy = %d.\n", m->busy));
1168 mtx_unlock(&m->qlock);
1169 mtx_unlock(&m->lock);
1179 midisynth_writeraw(void *n, uint8_t *buf, size_t len)
1181 struct snd_midi *m = ((struct synth_midi *)n)->m;
1186 MIDI_DEBUG(4, printf("midisynth_writeraw\n"));
1194 mtx_lock(&m->qlock);
1196 if (!(m->flags & M_TX))
1200 printf("midi dump: ");
1203 while (MIDIQ_AVAIL(m->outq) == 0) {
1204 if (!(m->flags & M_TXEN)) {
1206 MPU_CALLBACK(m, m->cookie, m->flags);
1208 mtx_unlock(&m->lock);
1210 MIDI_DEBUG(3, printf("midisynth_writeraw msleep\n"));
1211 retval = msleep(&m->wchan, &m->qlock,
1212 PCATCH | PDROP, "midi TX", 0);
1214 * We slept, maybe things have changed since last
1217 if (retval == EINTR)
1223 mtx_lock(&m->qlock);
1230 * We are certain than data can be placed on the queue
1233 used = MIN(MIDIQ_AVAIL(m->outq), len);
1234 used = MIN(used, MIDI_WSIZE);
1236 printf("midi_synth: resid %zu len %jd avail %jd\n",
1237 len, (intmax_t)MIDIQ_LEN(m->outq),
1238 (intmax_t)MIDIQ_AVAIL(m->outq)));
1241 for (i = 0; i < used; i++)
1242 printf("%x ", buf[i]);
1244 MIDIQ_ENQ(m->outq, buf, used);
1248 * Inform the bottom half that data can be written
1250 if (!(m->flags & M_TXEN)) {
1252 MPU_CALLBACK(m, m->cookie, m->flags);
1256 * If we Made it here then transfer is good
1262 err1: mtx_unlock(&m->qlock);
1263 mtx_unlock(&m->lock);
1264 err0: return retval;
1268 midisynth_killnote(void *n, uint8_t chn, uint8_t note, uint8_t vel)
1272 if (note > 127 || chn > 15)
1279 c[0] = 0x90 | (chn & 0x0f); /* Note on. */
1280 c[1] = (u_char)note;
1283 c[0] = 0x80 | (chn & 0x0f); /* Note off. */
1284 c[1] = (u_char)note;
1288 return midisynth_writeraw(n, c, 3);
1292 midisynth_setinstr(void *n, uint8_t chn, uint16_t instr)
1296 if (instr > 127 || chn > 15)
1299 c[0] = 0xc0 | (chn & 0x0f); /* Progamme change. */
1300 c[1] = instr + midi_instroff;
1302 return midisynth_writeraw(n, c, 2);
1306 midisynth_startnote(void *n, uint8_t chn, uint8_t note, uint8_t vel)
1310 if (note > 127 || chn > 15)
1316 c[0] = 0x90 | (chn & 0x0f); /* Note on. */
1317 c[1] = (u_char)note;
1320 return midisynth_writeraw(n, c, 3);
1323 midisynth_alloc(void *n, uint8_t chan, uint8_t note)
1329 midisynth_controller(void *n, uint8_t chn, uint8_t ctrlnum, uint16_t val)
1333 if (ctrlnum > 127 || chn > 15)
1336 c[0] = 0xb0 | (chn & 0x0f); /* Control Message. */
1339 return midisynth_writeraw(n, c, 3);
1343 midisynth_bender(void *n, uint8_t chn, uint16_t val)
1347 if (val > 16383 || chn > 15)
1350 c[0] = 0xe0 | (chn & 0x0f); /* Pitch bend. */
1351 c[1] = (u_char)val & 0x7f;
1352 c[2] = (u_char)(val >> 7) & 0x7f;
1354 return midisynth_writeraw(n, c, 3);
1358 * Single point of midi destructions.
1361 midi_destroy(struct snd_midi *m, int midiuninit)
1363 sx_assert(&midistat_lock, SA_XLOCKED);
1364 mtx_assert(&m->lock, MA_OWNED);
1366 MIDI_DEBUG(3, printf("midi_destroy\n"));
1367 m->dev->si_drv1 = NULL;
1368 mtx_unlock(&m->lock); /* XXX */
1369 destroy_dev(m->dev);
1370 TAILQ_REMOVE(&midi_devs, m, link);
1372 MPU_UNINIT(m, m->cookie);
1373 free(MIDIQ_BUF(m->inq), M_MIDI);
1374 free(MIDIQ_BUF(m->outq), M_MIDI);
1375 mtx_destroy(&m->qlock);
1376 mtx_destroy(&m->lock);
1377 free(m->synth, M_MIDI);
1383 * Load and unload functions, creates the /dev/midistat device
1389 sx_init(&midistat_lock, "midistat lock");
1390 TAILQ_INIT(&midi_devs);
1392 midistat_dev = make_dev(&midistat_cdevsw,
1393 MIDIMKMINOR(0, MIDI_DEV_MIDICTL, 0),
1394 UID_ROOT, GID_WHEEL, 0666, "midistat");
1402 struct snd_midi *m, *tmp;
1405 MIDI_DEBUG(1, printf("midi_unload()\n"));
1407 sx_xlock(&midistat_lock);
1408 if (midistat_isopen)
1411 TAILQ_FOREACH_SAFE(m, &midi_devs, link, tmp) {
1416 retval = midi_destroy(m, 1);
1420 sx_xunlock(&midistat_lock);
1421 destroy_dev(midistat_dev);
1424 * Made it here then unload is complete
1426 sx_destroy(&midistat_lock);
1430 mtx_unlock(&m->lock);
1432 sx_xunlock(&midistat_lock);
1434 MIDI_DEBUG(2, printf("midi_unload: failed\n"));
1438 extern int seq_modevent(module_t mod, int type, void *data);
1441 midi_modevent(module_t mod, int type, void *data)
1449 retval = midi_load();
1451 retval = seq_modevent(mod, type, data);
1455 retval = midi_unload();
1457 retval = seq_modevent(mod, type, data);
1468 midimapper_addseq(void *arg1, int *unit, void **cookie)
1472 return (kobj_t)arg1;
1476 midimapper_open(void *arg1, void **cookie)
1481 sx_xlock(&midistat_lock);
1482 TAILQ_FOREACH(m, &midi_devs, link) {
1485 sx_xunlock(&midistat_lock);
1490 midimapper_close(void *arg1, void *cookie)
1496 midimapper_fetch_synth(void *arg, void *cookie, int unit)
1501 sx_xlock(&midistat_lock);
1502 TAILQ_FOREACH(m, &midi_devs, link) {
1503 if (unit == retval) {
1504 sx_xunlock(&midistat_lock);
1505 return (kobj_t)m->synth;
1509 sx_xunlock(&midistat_lock);
1513 DEV_MODULE(midi, midi_modevent, NULL);
1514 MODULE_VERSION(midi, 1);