2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003 Mathew Kanner
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/queue.h>
36 #include <sys/kernel.h>
38 #include <sys/mutex.h>
40 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/bus.h> /* to get driver_intr_t */
45 #ifdef HAVE_KERNEL_OPTION_HEADERS
49 #include <dev/sound/midi/mpu401.h>
50 #include <dev/sound/midi/midi.h>
53 #include "mpufoi_if.h"
55 #ifndef KOBJMETHOD_END
56 #define KOBJMETHOD_END { NULL, NULL }
59 #define MPU_DATAPORT 0
61 #define MPU_STATPORT 1
62 #define MPU_RESET 0xff
65 #define MPU_STATMASK 0xc0
66 #define MPU_OUTPUTBUSY 0x40
67 #define MPU_INPUTBUSY 0x80
68 #define MPU_TRYDATA 50
69 #define MPU_DELAY 2500
71 #define CMD(m,d) MPUFOI_WRITE(m, m->cookie, MPU_CMDPORT,d)
72 #define STATUS(m) MPUFOI_READ(m, m->cookie, MPU_STATPORT)
73 #define READ(m) MPUFOI_READ(m, m->cookie, MPU_DATAPORT)
74 #define WRITE(m,d) MPUFOI_WRITE(m, m->cookie, MPU_DATAPORT,d)
85 static void mpu401_timeout(void *m);
86 static mpu401_intr_t mpu401_intr;
88 static int mpu401_minit(struct snd_midi *, void *);
89 static int mpu401_muninit(struct snd_midi *, void *);
90 static int mpu401_minqsize(struct snd_midi *, void *);
91 static int mpu401_moutqsize(struct snd_midi *, void *);
92 static void mpu401_mcallback(struct snd_midi *, void *, int);
93 static void mpu401_mcallbackp(struct snd_midi *, void *, int);
94 static const char *mpu401_mdescr(struct snd_midi *, void *, int);
95 static const char *mpu401_mprovider(struct snd_midi *, void *);
97 static kobj_method_t mpu401_methods[] = {
98 KOBJMETHOD(mpu_init, mpu401_minit),
99 KOBJMETHOD(mpu_uninit, mpu401_muninit),
100 KOBJMETHOD(mpu_inqsize, mpu401_minqsize),
101 KOBJMETHOD(mpu_outqsize, mpu401_moutqsize),
102 KOBJMETHOD(mpu_callback, mpu401_mcallback),
103 KOBJMETHOD(mpu_callbackp, mpu401_mcallbackp),
104 KOBJMETHOD(mpu_descr, mpu401_mdescr),
105 KOBJMETHOD(mpu_provider, mpu401_mprovider),
109 DEFINE_CLASS(mpu401, mpu401_methods, 0);
112 mpu401_timeout(void *a)
114 struct mpu401 *m = (struct mpu401 *)a;
121 mpu401_intr(struct mpu401 *m)
123 #define MPU_INTR_BUF 16
124 MIDI_TYPE b[MPU_INTR_BUF];
129 printf("mpu401_intr\n");
131 #define RXRDY(m) ( (STATUS(m) & MPU_INPUTBUSY) == 0)
132 #define TXRDY(m) ( (STATUS(m) & MPU_OUTPUTBUSY) == 0)
134 #define D(x,l) printf("mpu401_intr %d %x %s %s\n",l, x, x&MPU_INPUTBUSY?"RX":"", x&MPU_OUTPUTBUSY?"TX":"")
141 while ((s & MPU_INPUTBUSY) == 0 && i < MPU_INTR_BUF) {
144 printf("mpu401_intr in i %d d %d\n", i, b[i]);
150 midi_in(m->mid, b, i);
152 while (!(s & MPU_OUTPUTBUSY) && i < MPU_INTR_BUF) {
153 if (midi_out(m->mid, b, 1)) {
155 printf("mpu401_intr out i %d d %d\n", i, b[0]);
161 printf("mpu401_intr write: no output\n");
170 if ((m->flags & M_TXEN) && (m->si)) {
171 callout_reset(&m->timer, 1, mpu401_timeout, m);
173 return (m->flags & M_TXEN) == M_TXEN;
177 mpu401_init(kobj_class_t cls, void *cookie, driver_intr_t softintr,
183 m = malloc(sizeof(*m), M_MIDI, M_NOWAIT | M_ZERO);
188 kobj_init((kobj_t)m, cls);
190 callout_init(&m->timer, 1);
196 m->mid = midi_init(&mpu401_class, 0, 0, m);
202 printf("mpu401_init error\n");
208 mpu401_uninit(struct mpu401 *m)
213 retval = midi_uninit(m->mid);
221 mpu401_minit(struct snd_midi *sm, void *arg)
223 struct mpu401 *m = arg;
232 if (READ(m) == MPU_ACK)
240 printf("mpu401_minit failed active sensing\n");
246 mpu401_muninit(struct snd_midi *sm, void *arg)
248 struct mpu401 *m = arg;
250 return MPUFOI_UNINIT(m, m->cookie);
254 mpu401_minqsize(struct snd_midi *sm, void *arg)
260 mpu401_moutqsize(struct snd_midi *sm, void *arg)
266 mpu401_mcallback(struct snd_midi *sm, void *arg, int flags)
268 struct mpu401 *m = arg;
270 printf("mpu401_callback %s %s %s %s\n",
271 flags & M_RX ? "M_RX" : "",
272 flags & M_TX ? "M_TX" : "",
273 flags & M_RXEN ? "M_RXEN" : "",
274 flags & M_TXEN ? "M_TXEN" : "");
276 if (flags & M_TXEN && m->si) {
277 callout_reset(&m->timer, 1, mpu401_timeout, m);
283 mpu401_mcallbackp(struct snd_midi *sm, void *arg, int flags)
285 /* printf("mpu401_callbackp\n"); */
286 mpu401_mcallback(sm, arg, flags);
290 mpu401_mdescr(struct snd_midi *sm, void *arg, int verbosity)
293 return "descr mpu401";
297 mpu401_mprovider(struct snd_midi *m, void *arg)
299 return "provider mpu401";