xref: /freebsd/sys/dev/sound/midi/mpu401.c (revision 06c3fb27)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003 Mathew Kanner
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/proc.h>
37 #include <sys/systm.h>
38 #include <sys/kobj.h>
39 #include <sys/malloc.h>
40 #include <sys/bus.h>			/* to get driver_intr_t */
41 
42 #ifdef HAVE_KERNEL_OPTION_HEADERS
43 #include "opt_snd.h"
44 #endif
45 
46 #include <dev/sound/midi/mpu401.h>
47 #include <dev/sound/midi/midi.h>
48 
49 #include "mpu_if.h"
50 #include "mpufoi_if.h"
51 
52 #ifndef KOBJMETHOD_END
53 #define KOBJMETHOD_END	{ NULL, NULL }
54 #endif
55 
56 #define MPU_DATAPORT   0
57 #define MPU_CMDPORT    1
58 #define MPU_STATPORT   1
59 #define MPU_RESET      0xff
60 #define MPU_UART       0x3f
61 #define MPU_ACK        0xfe
62 #define MPU_STATMASK   0xc0
63 #define MPU_OUTPUTBUSY 0x40
64 #define MPU_INPUTBUSY  0x80
65 #define MPU_TRYDATA 50
66 #define MPU_DELAY   2500
67 
68 #define CMD(m,d)	MPUFOI_WRITE(m, m->cookie, MPU_CMDPORT,d)
69 #define STATUS(m)	MPUFOI_READ(m, m->cookie, MPU_STATPORT)
70 #define READ(m)		MPUFOI_READ(m, m->cookie, MPU_DATAPORT)
71 #define WRITE(m,d)	MPUFOI_WRITE(m, m->cookie, MPU_DATAPORT,d)
72 
73 struct mpu401 {
74 	KOBJ_FIELDS;
75 	struct snd_midi *mid;
76 	int	flags;
77 	driver_intr_t *si;
78 	void   *cookie;
79 	struct callout timer;
80 };
81 
82 static void mpu401_timeout(void *m);
83 static mpu401_intr_t mpu401_intr;
84 
85 static int mpu401_minit(struct snd_midi *, void *);
86 static int mpu401_muninit(struct snd_midi *, void *);
87 static int mpu401_minqsize(struct snd_midi *, void *);
88 static int mpu401_moutqsize(struct snd_midi *, void *);
89 static void mpu401_mcallback(struct snd_midi *, void *, int);
90 static void mpu401_mcallbackp(struct snd_midi *, void *, int);
91 static const char *mpu401_mdescr(struct snd_midi *, void *, int);
92 static const char *mpu401_mprovider(struct snd_midi *, void *);
93 
94 static kobj_method_t mpu401_methods[] = {
95 	KOBJMETHOD(mpu_init, mpu401_minit),
96 	KOBJMETHOD(mpu_uninit, mpu401_muninit),
97 	KOBJMETHOD(mpu_inqsize, mpu401_minqsize),
98 	KOBJMETHOD(mpu_outqsize, mpu401_moutqsize),
99 	KOBJMETHOD(mpu_callback, mpu401_mcallback),
100 	KOBJMETHOD(mpu_callbackp, mpu401_mcallbackp),
101 	KOBJMETHOD(mpu_descr, mpu401_mdescr),
102 	KOBJMETHOD(mpu_provider, mpu401_mprovider),
103 	KOBJMETHOD_END
104 };
105 
106 DEFINE_CLASS(mpu401, mpu401_methods, 0);
107 
108 void
109 mpu401_timeout(void *a)
110 {
111 	struct mpu401 *m = (struct mpu401 *)a;
112 
113 	if (m->si)
114 		(m->si)(m->cookie);
115 
116 }
117 static int
118 mpu401_intr(struct mpu401 *m)
119 {
120 #define MPU_INTR_BUF	16
121 	MIDI_TYPE b[MPU_INTR_BUF];
122 	int i;
123 	int s;
124 
125 /*
126 	printf("mpu401_intr\n");
127 */
128 #define RXRDY(m) ( (STATUS(m) & MPU_INPUTBUSY) == 0)
129 #define TXRDY(m) ( (STATUS(m) & MPU_OUTPUTBUSY) == 0)
130 #if 0
131 #define D(x,l) printf("mpu401_intr %d %x %s %s\n",l, x, x&MPU_INPUTBUSY?"RX":"", x&MPU_OUTPUTBUSY?"TX":"")
132 #else
133 #define D(x,l)
134 #endif
135 	i = 0;
136 	s = STATUS(m);
137 	D(s, 1);
138 	while ((s & MPU_INPUTBUSY) == 0 && i < MPU_INTR_BUF) {
139 		b[i] = READ(m);
140 /*
141 		printf("mpu401_intr in i %d d %d\n", i, b[i]);
142 */
143 		i++;
144 		s = STATUS(m);
145 	}
146 	if (i)
147 		midi_in(m->mid, b, i);
148 	i = 0;
149 	while (!(s & MPU_OUTPUTBUSY) && i < MPU_INTR_BUF) {
150 		if (midi_out(m->mid, b, 1)) {
151 /*
152 			printf("mpu401_intr out i %d d %d\n", i, b[0]);
153 */
154 
155 			WRITE(m, *b);
156 		} else {
157 /*
158 			printf("mpu401_intr write: no output\n");
159 */
160 			return 0;
161 		}
162 		i++;
163 		/* DELAY(100); */
164 		s = STATUS(m);
165 	}
166 
167 	if ((m->flags & M_TXEN) && (m->si)) {
168 		callout_reset(&m->timer, 1, mpu401_timeout, m);
169 	}
170 	return (m->flags & M_TXEN) == M_TXEN;
171 }
172 
173 struct mpu401 *
174 mpu401_init(kobj_class_t cls, void *cookie, driver_intr_t softintr,
175     mpu401_intr_t ** cb)
176 {
177 	struct mpu401 *m;
178 
179 	*cb = NULL;
180 	m = malloc(sizeof(*m), M_MIDI, M_NOWAIT | M_ZERO);
181 
182 	if (!m)
183 		return NULL;
184 
185 	kobj_init((kobj_t)m, cls);
186 
187 	callout_init(&m->timer, 1);
188 
189 	m->si = softintr;
190 	m->cookie = cookie;
191 	m->flags = 0;
192 
193 	m->mid = midi_init(&mpu401_class, 0, 0, m);
194 	if (!m->mid)
195 		goto err;
196 	*cb = mpu401_intr;
197 	return m;
198 err:
199 	printf("mpu401_init error\n");
200 	free(m, M_MIDI);
201 	return NULL;
202 }
203 
204 int
205 mpu401_uninit(struct mpu401 *m)
206 {
207 	int retval;
208 
209 	CMD(m, MPU_RESET);
210 	retval = midi_uninit(m->mid);
211 	if (retval)
212 		return retval;
213 	free(m, M_MIDI);
214 	return 0;
215 }
216 
217 static int
218 mpu401_minit(struct snd_midi *sm, void *arg)
219 {
220 	struct mpu401 *m = arg;
221 	int i;
222 
223 	CMD(m, MPU_RESET);
224 	CMD(m, MPU_UART);
225 	return 0;
226 	i = 0;
227 	while (++i < 2000) {
228 		if (RXRDY(m))
229 			if (READ(m) == MPU_ACK)
230 				break;
231 	}
232 
233 	if (i < 2000) {
234 		CMD(m, MPU_UART);
235 		return 0;
236 	}
237 	printf("mpu401_minit failed active sensing\n");
238 	return 1;
239 }
240 
241 int
242 mpu401_muninit(struct snd_midi *sm, void *arg)
243 {
244 	struct mpu401 *m = arg;
245 
246 	return MPUFOI_UNINIT(m, m->cookie);
247 }
248 
249 int
250 mpu401_minqsize(struct snd_midi *sm, void *arg)
251 {
252 	return 128;
253 }
254 
255 int
256 mpu401_moutqsize(struct snd_midi *sm, void *arg)
257 {
258 	return 128;
259 }
260 
261 static void
262 mpu401_mcallback(struct snd_midi *sm, void *arg, int flags)
263 {
264 	struct mpu401 *m = arg;
265 #if 0
266 	printf("mpu401_callback %s %s %s %s\n",
267 	    flags & M_RX ? "M_RX" : "",
268 	    flags & M_TX ? "M_TX" : "",
269 	    flags & M_RXEN ? "M_RXEN" : "",
270 	    flags & M_TXEN ? "M_TXEN" : "");
271 #endif
272 	if (flags & M_TXEN && m->si) {
273 		callout_reset(&m->timer, 1, mpu401_timeout, m);
274 	}
275 	m->flags = flags;
276 }
277 
278 static void
279 mpu401_mcallbackp(struct snd_midi *sm, void *arg, int flags)
280 {
281 /*	printf("mpu401_callbackp\n"); */
282 	mpu401_mcallback(sm, arg, flags);
283 }
284 
285 static const char *
286 mpu401_mdescr(struct snd_midi *sm, void *arg, int verbosity)
287 {
288 
289 	return "descr mpu401";
290 }
291 
292 static const char *
293 mpu401_mprovider(struct snd_midi *m, void *arg)
294 {
295 	return "provider mpu401";
296 }
297