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