2 * Copyright (c) 2003 Mathew Kanner
3 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Lennart Augustsson (augustss@netbsd.org).
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 * Parts of this file started out as NetBSD: midi.c 1.31
33 * They are mostly gone. Still the most obvious will be the state
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/kernel.h>
44 #include <sys/mutex.h>
46 #include <sys/signalvar.h>
48 #include <sys/selinfo.h>
49 #include <sys/sysctl.h>
50 #include <sys/types.h>
51 #include <sys/malloc.h>
52 #include <sys/param.h>
53 #include <sys/systm.h>
55 #include <sys/fcntl.h>
56 #include <sys/types.h>
61 #include <sys/module.h>
63 #ifdef HAVE_KERNEL_OPTION_HEADERS
67 #include <dev/sound/midi/midi.h>
70 #include <dev/sound/midi/midiq.h>
72 MALLOC_DEFINE(M_MIDI, "midi buffers", "Midi data allocation area");
74 #ifndef KOBJMETHOD_END
75 #define KOBJMETHOD_END { NULL, NULL }
78 #define PCMMKMINOR(u, d, c) ((((c) & 0xff) << 16) | (((u) & 0x0f) << 4) | ((d) & 0x0f))
79 #define MIDIMKMINOR(u, d, c) PCMMKMINOR(u, d, c)
81 #define MIDI_DEV_RAW 2
82 #define MIDI_DEV_MIDICTL 12
85 MIDI_IN_START, MIDI_IN_SYSEX, MIDI_IN_DATA
89 * The MPU interface current has init() uninit() inqsize(( outqsize()
90 * callback() : fiddle with the tx|rx status.
96 * /dev/rmidi Structure definitions
99 #define MIDI_NAMELEN 16
102 struct mtx lock; /* Protects all but queues */
105 int unit; /* Should only be used in midistat */
106 int channel; /* Should only be used in midistat */
109 int flags; /* File flags */
110 char name[MIDI_NAMELEN];
111 struct mtx qlock; /* Protects inq, outq and flags */
112 MIDIQ_HEAD(, char) inq, outq;
114 struct selinfo rsel, wsel;
115 int hiwat; /* QLEN(outq)>High-water -> disable
116 * writes from userland */
117 enum midi_states inq_state;
118 int inq_status, inq_left; /* Variables for the state machine in
119 * Midi_in, this is to provide that
120 * signals only get issued only
121 * complete command packets. */
124 struct synth_midi *synth;
126 TAILQ_ENTRY(snd_midi) link;
134 static synth_open_t midisynth_open;
135 static synth_close_t midisynth_close;
136 static synth_writeraw_t midisynth_writeraw;
137 static synth_killnote_t midisynth_killnote;
138 static synth_startnote_t midisynth_startnote;
139 static synth_setinstr_t midisynth_setinstr;
140 static synth_alloc_t midisynth_alloc;
141 static synth_controller_t midisynth_controller;
142 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) int
164 * midi_uninit(struct snd_midi *) 0 == no error EBUSY or other error int
165 * Midi_in(struct midi_chan *, char *buf, int count) int Midi_out(struct
166 * midi_chan *, char *buf, int count)
168 * midi_{in,out} return actual size transfered
174 * midi_devs tailq, holder of all rmidi instances protected by midistat_lock
177 TAILQ_HEAD(, snd_midi) midi_devs;
180 * /dev/midistat variables and declarations, protected by midistat_lock
183 static struct mtx midistat_lock;
184 static int midistat_isopen = 0;
185 static struct sbuf midistat_sbuf;
186 static int midistat_bufptr;
187 static struct cdev *midistat_dev;
190 * /dev/midistat dev_t declarations
193 static d_open_t midistat_open;
194 static d_close_t midistat_close;
195 static d_read_t midistat_read;
197 static struct cdevsw midistat_cdevsw = {
198 .d_version = D_VERSION,
199 .d_open = midistat_open,
200 .d_close = midistat_close,
201 .d_read = midistat_read,
202 .d_name = "midistat",
207 * /dev/rmidi dev_t declarations, struct variable access is protected by
208 * locks contained within the structure.
211 static d_open_t midi_open;
212 static d_close_t midi_close;
213 static d_ioctl_t midi_ioctl;
214 static d_read_t midi_read;
215 static d_write_t midi_write;
216 static d_poll_t midi_poll;
218 static struct cdevsw midi_cdevsw = {
219 .d_version = D_VERSION,
221 .d_close = midi_close,
223 .d_write = midi_write,
224 .d_ioctl = midi_ioctl,
230 * Prototypes of library functions
233 static int midi_destroy(struct snd_midi *, int);
234 static int midistat_prepare(struct sbuf * s);
235 static int midi_load(void);
236 static int midi_unload(void);
241 SYSCTL_NODE(_hw, OID_AUTO, midi, CTLFLAG_RD, 0, "Midi driver");
242 static SYSCTL_NODE(_hw_midi, OID_AUTO, stat, CTLFLAG_RD, 0, "Status device");
245 /* XXX: should this be moved into debug.midi? */
246 SYSCTL_INT(_hw_midi, OID_AUTO, debug, CTLFLAG_RW, &midi_debug, 0, "");
249 SYSCTL_INT(_hw_midi, OID_AUTO, dumpraw, CTLFLAG_RW, &midi_dumpraw, 0, "");
252 SYSCTL_INT(_hw_midi, OID_AUTO, instroff, CTLFLAG_RW, &midi_instroff, 0, "");
254 int midistat_verbose;
255 SYSCTL_INT(_hw_midi_stat, OID_AUTO, verbose, CTLFLAG_RW,
256 &midistat_verbose, 0, "");
258 #define MIDI_DEBUG(l,a) if(midi_debug>=l) a
264 * Register a new rmidi device. cls midi_if interface unit == 0 means
265 * auto-assign new unit number unit != 0 already assigned a unit number, eg.
266 * not the first channel provided by this device. channel, sub-unit
267 * cookie is passed back on MPU calls Typical device drivers will call with
268 * unit=0, channel=1..(number of channels) and cookie=soft_c and won't care
269 * what unit number is used.
271 * It is an error to call midi_init with an already used unit/channel combo.
273 * Returns NULL on error
277 midi_init(kobj_class_t cls, int unit, int channel, void *cookie)
281 int inqsize, outqsize;
284 MIDI_DEBUG(1, printf("midiinit: unit %d/%d.\n", unit, channel));
285 mtx_lock(&midistat_lock);
287 * Protect against call with existing unit/channel or auto-allocate a
291 TAILQ_FOREACH(m, &midi_devs, link) {
294 if (m->unit == unit && m->channel == channel) {
295 mtx_unlock(&m->lock);
300 * Find a better unit number
305 mtx_unlock(&m->lock);
311 MIDI_DEBUG(1, printf("midiinit #2: unit %d/%d.\n", unit, channel));
312 m = malloc(sizeof(*m), M_MIDI, M_NOWAIT | M_ZERO);
316 m->synth = malloc(sizeof(*m->synth), M_MIDI, M_NOWAIT | M_ZERO);
317 kobj_init((kobj_t)m->synth, &midisynth_class);
319 kobj_init((kobj_t)m, cls);
320 inqsize = MPU_INQSIZE(m, cookie);
321 outqsize = MPU_OUTQSIZE(m, cookie);
323 MIDI_DEBUG(1, printf("midiinit queues %d/%d.\n", inqsize, outqsize));
324 if (!inqsize && !outqsize)
327 mtx_init(&m->lock, "raw midi", NULL, 0);
328 mtx_init(&m->qlock, "q raw midi", NULL, 0);
334 buf = malloc(sizeof(MIDI_TYPE) * inqsize, M_MIDI, M_NOWAIT);
338 MIDIQ_INIT(m->inq, buf, inqsize);
341 buf = malloc(sizeof(MIDI_TYPE) * outqsize, M_MIDI, M_NOWAIT);
344 m->hiwat = outqsize / 2;
346 MIDIQ_INIT(m->outq, buf, outqsize);
348 if ((inqsize && !MIDIQ_BUF(m->inq)) ||
349 (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 mtx_unlock(&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);
376 err2: mtx_destroy(&m->qlock);
377 mtx_destroy(&m->lock);
379 if (MIDIQ_BUF(m->inq))
380 free(MIDIQ_BUF(m->inq), M_MIDI);
381 if (MIDIQ_BUF(m->outq))
382 free(MIDIQ_BUF(m->outq), M_MIDI);
383 err1: free(m, M_MIDI);
384 err0: mtx_unlock(&midistat_lock);
385 MIDI_DEBUG(1, printf("midi_init ended in error\n"));
390 * midi_uninit does not call MIDI_UNINIT, as since this is the implementors
391 * entry point. midi_unint if fact, does not send any methods. A call to
392 * midi_uninit is a defacto promise that you won't manipulate ch anymore
397 midi_uninit(struct snd_midi *m)
402 mtx_lock(&midistat_lock);
405 if (!(m->rchan || m->wchan))
417 err = midi_destroy(m, 0);
421 err: mtx_unlock(&m->lock);
422 exit: mtx_unlock(&midistat_lock);
427 * midi_in: process all data until the queue is full, then discards the rest.
428 * Since midi_in is a state machine, data discards can cause it to get out of
429 * whack. Process as much as possible. It calls, wakeup, selnotify and
430 * psignal at most once.
434 static int midi_lengths[] = {2, 2, 2, 2, 1, 1, 2, 0};
437 /* Number of bytes in a MIDI command */
438 #define MIDI_LENGTH(d) (midi_lengths[((d) >> 4) & 7])
439 #define MIDI_ACK 0xfe
440 #define MIDI_IS_STATUS(d) ((d) >= 0x80)
441 #define MIDI_IS_COMMON(d) ((d) >= 0xf0)
443 #define MIDI_SYSEX_START 0xF0
444 #define MIDI_SYSEX_END 0xF7
448 midi_in(struct snd_midi *m, MIDI_TYPE *buf, int size)
450 /* int i, sig, enq; */
453 /* MIDI_TYPE data; */
454 MIDI_DEBUG(5, printf("midi_in: m=%p size=%d\n", m, size));
459 if (!(m->flags & M_RX))
467 * Don't bother queuing if not in read mode. Discard everything and
468 * return size so the caller doesn't freak out.
471 if (!(m->flags & M_RX))
474 for (i = sig = 0; i < size; i++) {
478 if (data == MIDI_ACK)
481 switch (m->inq_state) {
483 if (MIDI_IS_STATUS(data)) {
485 case 0xf0: /* Sysex */
486 m->inq_state = MIDI_IN_SYSEX;
488 case 0xf1: /* MTC quarter frame */
489 case 0xf3: /* Song select */
490 m->inq_state = MIDI_IN_DATA;
494 case 0xf2: /* Song position pointer */
495 m->inq_state = MIDI_IN_DATA;
500 if (MIDI_IS_COMMON(data)) {
504 m->inq_state = MIDI_IN_DATA;
506 m->inq_status = data;
507 m->inq_left = MIDI_LENGTH(data);
511 } else if (MIDI_IS_STATUS(m->inq_status)) {
512 m->inq_state = MIDI_IN_DATA;
513 if (!MIDIQ_FULL(m->inq)) {
515 MIDIQ_ENQ(m->inq, &m->inq_status, 1);
518 m->inq_left = MIDI_LENGTH(m->inq_status) - 1;
522 * End of case MIDI_IN_START:
527 if (--m->inq_left <= 0)
528 sig = 1;/* deliver data */
531 if (data == MIDI_SYSEX_END)
532 m->inq_state = MIDI_IN_START;
537 if (!MIDIQ_FULL(m->inq)) {
538 MIDIQ_ENQ(m->inq, &data, 1);
542 * End of the state machines main "for loop"
547 MIDI_DEBUG(6, printf("midi_in: len %jd avail %jd\n",
548 (intmax_t)MIDIQ_LEN(m->inq),
549 (intmax_t)MIDIQ_AVAIL(m->inq)));
550 if (MIDIQ_AVAIL(m->inq) > size) {
552 MIDIQ_ENQ(m->inq, buf, size);
554 MIDI_DEBUG(4, printf("midi_in: Discarding data qu\n"));
555 mtx_unlock(&m->qlock);
565 kern_psignal(m->async, SIGIO);
566 PROC_UNLOCK(m->async);
571 mtx_unlock(&m->qlock);
576 * midi_out: The only clearer of the M_TXEN flag.
579 midi_out(struct snd_midi *m, MIDI_TYPE *buf, int size)
586 if (!(m->flags & M_TXEN))
589 MIDI_DEBUG(2, printf("midi_out: %p\n", m));
591 used = MIN(size, MIDIQ_LEN(m->outq));
592 MIDI_DEBUG(3, printf("midi_out: used %d\n", used));
594 MIDIQ_DEQ(m->outq, buf, used);
595 if (MIDIQ_EMPTY(m->outq)) {
597 MPU_CALLBACKP(m, m->cookie, m->flags);
599 if (used && MIDIQ_AVAIL(m->outq) > m->hiwat) {
607 kern_psignal(m->async, SIGIO);
608 PROC_UNLOCK(m->async);
611 mtx_unlock(&m->qlock);
617 * /dev/rmidi#.# device access functions
620 midi_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
622 struct snd_midi *m = i_dev->si_drv1;
625 MIDI_DEBUG(1, printf("midiopen %p %s %s\n", td,
626 flags & FREAD ? "M_RX" : "", flags & FWRITE ? "M_TX" : ""));
636 if (MIDIQ_SIZE(m->inq) == 0)
638 else if (m->flags & M_RX)
643 if (flags & FWRITE) {
644 if (MIDIQ_SIZE(m->outq) == 0)
646 else if (m->flags & M_TX)
658 m->flags |= M_RX | M_RXEN;
660 * Only clear the inq, the outq might still have data to drain
661 * from a previous session
669 MPU_CALLBACK(m, m->cookie, m->flags);
671 MIDI_DEBUG(2, printf("midi_open: opened.\n"));
673 err: mtx_unlock(&m->qlock);
674 mtx_unlock(&m->lock);
679 midi_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
681 struct snd_midi *m = i_dev->si_drv1;
685 MIDI_DEBUG(1, printf("midi_close %p %s %s\n", td,
686 flags & FREAD ? "M_RX" : "", flags & FWRITE ? "M_TX" : ""));
694 if ((flags & FREAD && !(m->flags & M_RX)) ||
695 (flags & FWRITE && !(m->flags & M_TX))) {
704 m->flags &= ~(M_RX | M_RXEN);
708 if ((m->flags & (M_TXEN | M_RXEN)) != (oldflags & (M_RXEN | M_TXEN)))
709 MPU_CALLBACK(m, m->cookie, m->flags);
711 MIDI_DEBUG(1, printf("midi_close: closed, busy = %d.\n", m->busy));
713 mtx_unlock(&m->qlock);
714 mtx_unlock(&m->lock);
720 * TODO: midi_read, per oss programmer's guide pg. 42 should return as soon
721 * as data is available.
724 midi_read(struct cdev *i_dev, struct uio *uio, int ioflag)
726 #define MIDI_RSIZE 32
727 struct snd_midi *m = i_dev->si_drv1;
730 char buf[MIDI_RSIZE];
732 MIDI_DEBUG(5, printf("midiread: count=%lu\n",
733 (unsigned long)uio->uio_resid));
743 if (!(m->flags & M_RX))
746 while (uio->uio_resid > 0) {
747 while (MIDIQ_EMPTY(m->inq)) {
748 retval = EWOULDBLOCK;
749 if (ioflag & O_NONBLOCK)
751 mtx_unlock(&m->lock);
753 retval = msleep(&m->rchan, &m->qlock,
754 PCATCH | PDROP, "midi RX", 0);
756 * We slept, maybe things have changed since last
761 if (m != i_dev->si_drv1)
763 /* if (retval && retval != ERESTART) */
772 MIDI_DEBUG(6, printf("midi_read start\n"));
774 * At this point, it is certain that m->inq has data
777 used = MIN(MIDIQ_LEN(m->inq), uio->uio_resid);
778 used = MIN(used, MIDI_RSIZE);
780 MIDI_DEBUG(6, printf("midiread: uiomove cc=%d\n", used));
781 MIDIQ_DEQ(m->inq, buf, used);
782 retval = uiomove(buf, used, uio);
788 * If we Made it here then transfer is good
791 err1: mtx_unlock(&m->qlock);
792 mtx_unlock(&m->lock);
793 err0: MIDI_DEBUG(4, printf("midi_read: ret %d\n", retval));
798 * midi_write: The only setter of M_TXEN
802 midi_write(struct cdev *i_dev, struct uio *uio, int ioflag)
804 #define MIDI_WSIZE 32
805 struct snd_midi *m = i_dev->si_drv1;
808 char buf[MIDI_WSIZE];
811 MIDI_DEBUG(4, printf("midi_write\n"));
819 if (!(m->flags & M_TX))
822 while (uio->uio_resid > 0) {
823 while (MIDIQ_AVAIL(m->outq) == 0) {
824 retval = EWOULDBLOCK;
825 if (ioflag & O_NONBLOCK)
827 mtx_unlock(&m->lock);
829 MIDI_DEBUG(3, printf("midi_write msleep\n"));
830 retval = msleep(&m->wchan, &m->qlock,
831 PCATCH | PDROP, "midi TX", 0);
833 * We slept, maybe things have changed since last
838 if (m != i_dev->si_drv1)
850 * We are certain than data can be placed on the queue
853 used = MIN(MIDIQ_AVAIL(m->outq), uio->uio_resid);
854 used = MIN(used, MIDI_WSIZE);
855 MIDI_DEBUG(5, printf("midiout: resid %zd len %jd avail %jd\n",
856 uio->uio_resid, (intmax_t)MIDIQ_LEN(m->outq),
857 (intmax_t)MIDIQ_AVAIL(m->outq)));
860 MIDI_DEBUG(5, printf("midi_write: uiomove cc=%d\n", used));
861 retval = uiomove(buf, used, uio);
864 MIDIQ_ENQ(m->outq, buf, used);
866 * Inform the bottom half that data can be written
868 if (!(m->flags & M_TXEN)) {
870 MPU_CALLBACK(m, m->cookie, m->flags);
874 * If we Made it here then transfer is good
877 err1: mtx_unlock(&m->qlock);
878 mtx_unlock(&m->lock);
883 midi_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
890 midi_poll(struct cdev *i_dev, int events, struct thread *td)
892 struct snd_midi *m = i_dev->si_drv1;
903 if (events & (POLLIN | POLLRDNORM))
904 if (!MIDIQ_EMPTY(m->inq))
905 events |= events & (POLLIN | POLLRDNORM);
907 if (events & (POLLOUT | POLLWRNORM))
908 if (MIDIQ_AVAIL(m->outq) < m->hiwat)
909 events |= events & (POLLOUT | POLLWRNORM);
912 if (events & (POLLIN | POLLRDNORM))
913 selrecord(td, &m->rsel);
915 if (events & (POLLOUT | POLLWRNORM))
916 selrecord(td, &m->wsel);
918 mtx_unlock(&m->lock);
919 mtx_unlock(&m->qlock);
925 * /dev/midistat device functions
929 midistat_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
933 MIDI_DEBUG(1, printf("midistat_open\n"));
934 mtx_lock(&midistat_lock);
936 if (midistat_isopen) {
937 mtx_unlock(&midistat_lock);
941 mtx_unlock(&midistat_lock);
943 if (sbuf_new(&midistat_sbuf, NULL, 4096, SBUF_AUTOEXTEND) == NULL) {
945 mtx_lock(&midistat_lock);
948 mtx_lock(&midistat_lock);
950 error = (midistat_prepare(&midistat_sbuf) > 0) ? 0 : ENOMEM;
954 mtx_unlock(&midistat_lock);
959 midistat_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
961 MIDI_DEBUG(1, printf("midistat_close\n"));
962 mtx_lock(&midistat_lock);
963 if (!midistat_isopen) {
964 mtx_unlock(&midistat_lock);
967 sbuf_delete(&midistat_sbuf);
970 mtx_unlock(&midistat_lock);
975 midistat_read(struct cdev *i_dev, struct uio *buf, int flag)
979 MIDI_DEBUG(4, printf("midistat_read\n"));
980 mtx_lock(&midistat_lock);
981 if (!midistat_isopen) {
982 mtx_unlock(&midistat_lock);
985 l = min(buf->uio_resid, sbuf_len(&midistat_sbuf) - midistat_bufptr);
988 mtx_unlock(&midistat_lock);
989 err = uiomove(sbuf_data(&midistat_sbuf) + midistat_bufptr, l,
991 mtx_lock(&midistat_lock);
994 midistat_bufptr += l;
995 mtx_unlock(&midistat_lock);
1000 * Module library functions
1004 midistat_prepare(struct sbuf *s)
1008 mtx_assert(&midistat_lock, MA_OWNED);
1010 sbuf_printf(s, "FreeBSD Midi Driver (midi2)\n");
1011 if (TAILQ_EMPTY(&midi_devs)) {
1012 sbuf_printf(s, "No devices installed.\n");
1016 sbuf_printf(s, "Installed devices:\n");
1018 TAILQ_FOREACH(m, &midi_devs, link) {
1020 sbuf_printf(s, "%s [%d/%d:%s]", m->name, m->unit, m->channel,
1021 MPU_PROVIDER(m, m->cookie));
1022 sbuf_printf(s, "%s", MPU_DESCR(m, m->cookie, midistat_verbose));
1023 sbuf_printf(s, "\n");
1024 mtx_unlock(&m->lock);
1033 * Convert IOCTL command to string for debugging
1037 midi_cmdname(int cmd)
1042 } *tab, cmdtab_midiioctl[] = {
1043 #define A(x) {x, ## x}
1045 * Once we have some real IOCTLs define, the following will
1048 * A(SNDCTL_MIDI_PRETIME), A(SNDCTL_MIDI_MPUMODE),
1049 * A(SNDCTL_MIDI_MPUCMD), A(SNDCTL_SYNTH_INFO),
1050 * A(SNDCTL_MIDI_INFO), A(SNDCTL_SYNTH_MEMAVL),
1051 * A(SNDCTL_FM_LOAD_INSTR), A(SNDCTL_FM_4OP_ENABLE),
1052 * A(MIOSPASSTHRU), A(MIOGPASSTHRU), A(AIONWRITE),
1053 * A(AIOGSIZE), A(AIOSSIZE), A(AIOGFMT), A(AIOSFMT),
1054 * A(AIOGMIX), A(AIOSMIX), A(AIOSTOP), A(AIOSYNC),
1063 for (tab = cmdtab_midiioctl; tab->cmd != cmd && tab->cmd != -1; tab++);
1075 midisynth_open(void *n, void *arg, int flags)
1077 struct snd_midi *m = ((struct synth_midi *)n)->m;
1080 MIDI_DEBUG(1, printf("midisynth_open %s %s\n",
1081 flags & FREAD ? "M_RX" : "", flags & FWRITE ? "M_TX" : ""));
1087 mtx_lock(&m->qlock);
1091 if (flags & FREAD) {
1092 if (MIDIQ_SIZE(m->inq) == 0)
1094 else if (m->flags & M_RX)
1099 if (flags & FWRITE) {
1100 if (MIDIQ_SIZE(m->outq) == 0)
1102 else if (m->flags & M_TX)
1110 * TODO: Consider m->async = 0;
1113 if (flags & FREAD) {
1114 m->flags |= M_RX | M_RXEN;
1116 * Only clear the inq, the outq might still have data to drain
1117 * from a previous session
1119 MIDIQ_CLEAR(m->inq);
1123 if (flags & FWRITE) {
1127 m->synth_flags = flags & (FREAD | FWRITE);
1129 MPU_CALLBACK(m, m->cookie, m->flags);
1132 err: mtx_unlock(&m->qlock);
1133 mtx_unlock(&m->lock);
1134 MIDI_DEBUG(2, printf("midisynth_open: return %d.\n", retval));
1139 midisynth_close(void *n)
1141 struct snd_midi *m = ((struct synth_midi *)n)->m;
1145 MIDI_DEBUG(1, printf("midisynth_close %s %s\n",
1146 m->synth_flags & FREAD ? "M_RX" : "",
1147 m->synth_flags & FWRITE ? "M_TX" : ""));
1153 mtx_lock(&m->qlock);
1155 if ((m->synth_flags & FREAD && !(m->flags & M_RX)) ||
1156 (m->synth_flags & FWRITE && !(m->flags & M_TX))) {
1162 oldflags = m->flags;
1164 if (m->synth_flags & FREAD)
1165 m->flags &= ~(M_RX | M_RXEN);
1166 if (m->synth_flags & FWRITE)
1169 if ((m->flags & (M_TXEN | M_RXEN)) != (oldflags & (M_RXEN | M_TXEN)))
1170 MPU_CALLBACK(m, m->cookie, m->flags);
1172 MIDI_DEBUG(1, printf("midi_close: closed, busy = %d.\n", m->busy));
1174 mtx_unlock(&m->qlock);
1175 mtx_unlock(&m->lock);
1185 midisynth_writeraw(void *n, uint8_t *buf, size_t len)
1187 struct snd_midi *m = ((struct synth_midi *)n)->m;
1192 MIDI_DEBUG(4, printf("midisynth_writeraw\n"));
1200 mtx_lock(&m->qlock);
1202 if (!(m->flags & M_TX))
1206 printf("midi dump: ");
1209 while (MIDIQ_AVAIL(m->outq) == 0) {
1210 if (!(m->flags & M_TXEN)) {
1212 MPU_CALLBACK(m, m->cookie, m->flags);
1214 mtx_unlock(&m->lock);
1216 MIDI_DEBUG(3, printf("midisynth_writeraw msleep\n"));
1217 retval = msleep(&m->wchan, &m->qlock,
1218 PCATCH | PDROP, "midi TX", 0);
1220 * We slept, maybe things have changed since last
1223 if (retval == EINTR)
1229 mtx_lock(&m->qlock);
1236 * We are certain than data can be placed on the queue
1239 used = MIN(MIDIQ_AVAIL(m->outq), len);
1240 used = MIN(used, MIDI_WSIZE);
1242 printf("midi_synth: resid %zu len %jd avail %jd\n",
1243 len, (intmax_t)MIDIQ_LEN(m->outq),
1244 (intmax_t)MIDIQ_AVAIL(m->outq)));
1247 for (i = 0; i < used; i++)
1248 printf("%x ", buf[i]);
1250 MIDIQ_ENQ(m->outq, buf, used);
1254 * Inform the bottom half that data can be written
1256 if (!(m->flags & M_TXEN)) {
1258 MPU_CALLBACK(m, m->cookie, m->flags);
1262 * If we Made it here then transfer is good
1268 err1: mtx_unlock(&m->qlock);
1269 mtx_unlock(&m->lock);
1270 err0: return retval;
1274 midisynth_killnote(void *n, uint8_t chn, uint8_t note, uint8_t vel)
1279 if (note > 127 || chn > 15)
1286 c[0] = 0x90 | (chn & 0x0f); /* Note on. */
1287 c[1] = (u_char)note;
1290 c[0] = 0x80 | (chn & 0x0f); /* Note off. */
1291 c[1] = (u_char)note;
1295 return midisynth_writeraw(n, c, 3);
1299 midisynth_setinstr(void *n, uint8_t chn, uint16_t instr)
1303 if (instr > 127 || chn > 15)
1306 c[0] = 0xc0 | (chn & 0x0f); /* Progamme change. */
1307 c[1] = instr + midi_instroff;
1309 return midisynth_writeraw(n, c, 2);
1313 midisynth_startnote(void *n, uint8_t chn, uint8_t note, uint8_t vel)
1317 if (note > 127 || chn > 15)
1323 c[0] = 0x90 | (chn & 0x0f); /* Note on. */
1324 c[1] = (u_char)note;
1327 return midisynth_writeraw(n, c, 3);
1330 midisynth_alloc(void *n, uint8_t chan, uint8_t note)
1336 midisynth_controller(void *n, uint8_t chn, uint8_t ctrlnum, uint16_t val)
1340 if (ctrlnum > 127 || chn > 15)
1343 c[0] = 0xb0 | (chn & 0x0f); /* Control Message. */
1346 return midisynth_writeraw(n, c, 3);
1350 midisynth_bender(void *n, uint8_t chn, uint16_t val)
1355 if (val > 16383 || chn > 15)
1358 c[0] = 0xe0 | (chn & 0x0f); /* Pitch bend. */
1359 c[1] = (u_char)val & 0x7f;
1360 c[2] = (u_char)(val >> 7) & 0x7f;
1362 return midisynth_writeraw(n, c, 3);
1366 * Single point of midi destructions.
1369 midi_destroy(struct snd_midi *m, int midiuninit)
1372 mtx_assert(&midistat_lock, MA_OWNED);
1373 mtx_assert(&m->lock, MA_OWNED);
1375 MIDI_DEBUG(3, printf("midi_destroy\n"));
1376 m->dev->si_drv1 = NULL;
1377 mtx_unlock(&m->lock); /* XXX */
1378 destroy_dev(m->dev);
1379 TAILQ_REMOVE(&midi_devs, m, link);
1381 MPU_UNINIT(m, m->cookie);
1382 free(MIDIQ_BUF(m->inq), M_MIDI);
1383 free(MIDIQ_BUF(m->outq), M_MIDI);
1384 mtx_destroy(&m->qlock);
1385 mtx_destroy(&m->lock);
1391 * Load and unload functions, creates the /dev/midistat device
1397 mtx_init(&midistat_lock, "midistat lock", NULL, 0);
1398 TAILQ_INIT(&midi_devs); /* Initialize the queue. */
1400 midistat_dev = make_dev(&midistat_cdevsw,
1401 MIDIMKMINOR(0, MIDI_DEV_MIDICTL, 0),
1402 UID_ROOT, GID_WHEEL, 0666, "midistat");
1413 MIDI_DEBUG(1, printf("midi_unload()\n"));
1415 mtx_lock(&midistat_lock);
1416 if (midistat_isopen)
1419 TAILQ_FOREACH(m, &midi_devs, link) {
1424 retval = midi_destroy(m, 1);
1429 mtx_unlock(&midistat_lock); /* XXX */
1431 destroy_dev(midistat_dev);
1433 * Made it here then unload is complete
1435 mtx_destroy(&midistat_lock);
1439 mtx_unlock(&m->lock);
1441 mtx_unlock(&midistat_lock);
1443 MIDI_DEBUG(2, printf("midi_unload: failed\n"));
1447 extern int seq_modevent(module_t mod, int type, void *data);
1450 midi_modevent(module_t mod, int type, void *data)
1458 retval = midi_load();
1461 retval = seq_modevent(mod, type, data);
1466 retval = midi_unload();
1469 retval = seq_modevent(mod, type, data);
1481 midimapper_addseq(void *arg1, int *unit, void **cookie)
1485 return (kobj_t)arg1;
1489 midimapper_open(void *arg1, void **cookie)
1494 mtx_lock(&midistat_lock);
1496 TAILQ_FOREACH(m, &midi_devs, link) {
1500 mtx_unlock(&midistat_lock);
1505 midimapper_close(void *arg1, void *cookie)
1511 midimapper_fetch_synth(void *arg, void *cookie, int unit)
1516 mtx_lock(&midistat_lock);
1518 TAILQ_FOREACH(m, &midi_devs, link) {
1519 if (unit == retval) {
1520 mtx_unlock(&midistat_lock);
1521 return (kobj_t)m->synth;
1526 mtx_unlock(&midistat_lock);
1530 DEV_MODULE(midi, midi_modevent, NULL);
1531 MODULE_VERSION(midi, 1);