xref: /freebsd/sys/dev/sound/midi/mpu401.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*-
2  * Copyright (c) 2003 Mathew Kanner
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/proc.h>
38 #include <sys/systm.h>
39 #include <sys/kobj.h>
40 #include <sys/malloc.h>
41 #include <sys/bus.h>			/* to get driver_intr_t */
42 
43 #include <dev/sound/midi/mpu401.h>
44 #include <dev/sound/midi/midi.h>
45 
46 #include "mpu_if.h"
47 #include "mpufoi_if.h"
48 
49 #define MPU_DATAPORT   0
50 #define MPU_CMDPORT    1
51 #define MPU_STATPORT   1
52 #define MPU_RESET      0xff
53 #define MPU_UART       0x3f
54 #define MPU_ACK        0xfe
55 #define MPU_STATMASK   0xc0
56 #define MPU_OUTPUTBUSY 0x40
57 #define MPU_INPUTBUSY  0x80
58 #define MPU_TRYDATA 50
59 #define MPU_DELAY   2500
60 
61 #define CMD(m,d)	MPUFOI_WRITE(m, m->cookie, MPU_CMDPORT,d)
62 #define STATUS(m)	MPUFOI_READ(m, m->cookie, MPU_STATPORT)
63 #define READ(m)		MPUFOI_READ(m, m->cookie, MPU_DATAPORT)
64 #define WRITE(m,d)	MPUFOI_WRITE(m, m->cookie, MPU_DATAPORT,d)
65 
66 struct mpu401 {
67 	KOBJ_FIELDS;
68 	struct snd_midi *mid;
69 	int	flags;
70 	driver_intr_t *si;
71 	void   *cookie;
72 	struct callout timer;
73 };
74 
75 static void mpu401_timeout(void *m);
76 static mpu401_intr_t mpu401_intr;
77 
78 static int mpu401_minit(kobj_t obj, struct mpu401 *m);
79 static int mpu401_muninit(kobj_t obj, struct mpu401 *m);
80 static int mpu401_minqsize(kobj_t obj, struct mpu401 *m);
81 static int mpu401_moutqsize(kobj_t obj, struct mpu401 *m);
82 static void mpu401_mcallback(kobj_t obj, struct mpu401 *m, int flags);
83 static void mpu401_mcallbackp(kobj_t obj, struct mpu401 *m, int flags);
84 static const char *mpu401_mdescr(kobj_t obj, struct mpu401 *m, int verbosity);
85 static const char *mpu401_mprovider(kobj_t obj, struct mpu401 *m);
86 
87 static kobj_method_t mpu401_methods[] = {
88 	KOBJMETHOD(mpu_init, mpu401_minit),
89 	KOBJMETHOD(mpu_uninit, mpu401_muninit),
90 	KOBJMETHOD(mpu_inqsize, mpu401_minqsize),
91 	KOBJMETHOD(mpu_outqsize, mpu401_moutqsize),
92 	KOBJMETHOD(mpu_callback, mpu401_mcallback),
93 	KOBJMETHOD(mpu_callbackp, mpu401_mcallbackp),
94 	KOBJMETHOD(mpu_descr, mpu401_mdescr),
95 	KOBJMETHOD(mpu_provider, mpu401_mprovider),
96 	{0, 0}
97 };
98 
99 DEFINE_CLASS(mpu401, mpu401_methods, 0);
100 
101 void
102 mpu401_timeout(void *a)
103 {
104 	struct mpu401 *m = (struct mpu401 *)a;
105 
106 	if (m->si)
107 		(m->si)(m->cookie);
108 
109 }
110 static int
111 mpu401_intr(struct mpu401 *m)
112 {
113 #define MPU_INTR_BUF	16
114 	MIDI_TYPE b[MPU_INTR_BUF];
115 	int i;
116 	int s;
117 
118 /*
119 	printf("mpu401_intr\n");
120 */
121 #define RXRDY(m) ( (STATUS(m) & MPU_INPUTBUSY) == 0)
122 #define TXRDY(m) ( (STATUS(m) & MPU_OUTPUTBUSY) == 0)
123 #if 0
124 #define D(x,l) printf("mpu401_intr %d %x %s %s\n",l, x, x&MPU_INPUTBUSY?"RX":"", x&MPU_OUTPUTBUSY?"TX":"")
125 #else
126 #define D(x,l)
127 #endif
128 	i = 0;
129 	s = STATUS(m);
130 	D(s, 1);
131 	while ((s & MPU_INPUTBUSY) == 0 && i < MPU_INTR_BUF) {
132 		b[i] = READ(m);
133 /*
134 		printf("mpu401_intr in i %d d %d\n", i, b[i]);
135 */
136 		i++;
137 		s = STATUS(m);
138 	}
139 	if (i)
140 		midi_in(m->mid, b, i);
141 	i = 0;
142 	while (!(s & MPU_OUTPUTBUSY) && i < MPU_INTR_BUF) {
143 		if (midi_out(m->mid, b, 1)) {
144 /*
145 			printf("mpu401_intr out i %d d %d\n", i, b[0]);
146 */
147 
148 			WRITE(m, *b);
149 		} else {
150 /*
151 			printf("mpu401_intr write: no output\n");
152 */
153 			return 0;
154 		}
155 		i++;
156 		/* DELAY(100); */
157 		s = STATUS(m);
158 	}
159 
160 	if ((m->flags & M_TXEN) && (m->si)) {
161 		callout_reset(&m->timer, 1, mpu401_timeout, m);
162 	}
163 	return (m->flags & M_TXEN) == M_TXEN;
164 }
165 
166 struct mpu401 *
167 mpu401_init(kobj_class_t cls, void *cookie, driver_intr_t softintr,
168     mpu401_intr_t ** cb)
169 {
170 	struct mpu401 *m;
171 
172 	*cb = NULL;
173 	m = malloc(sizeof(*m), M_MIDI, M_NOWAIT | M_ZERO);
174 
175 	if (!m)
176 		return NULL;
177 
178 	kobj_init((kobj_t)m, cls);
179 
180 	callout_init(&m->timer, 1);
181 
182 	m->si = softintr;
183 	m->cookie = cookie;
184 	m->flags = 0;
185 
186 	m->mid = midi_init(&mpu401_class, 0, 0, m);
187 	if (!m->mid)
188 		goto err;
189 	*cb = mpu401_intr;
190 	return m;
191 err:
192 	printf("mpu401_init error\n");
193 	free(m, M_MIDI);
194 	return NULL;
195 }
196 
197 int
198 mpu401_uninit(struct mpu401 *m)
199 {
200 	int retval;
201 
202 	CMD(m, MPU_RESET);
203 	retval = midi_uninit(m->mid);
204 	if (retval)
205 		return retval;
206 	free(m, M_MIDI);
207 	return 0;
208 }
209 
210 static int
211 mpu401_minit(kobj_t obj, struct mpu401 *m)
212 {
213 	int i;
214 
215 	CMD(m, MPU_RESET);
216 	CMD(m, MPU_UART);
217 	return 0;
218 	i = 0;
219 	while (++i < 2000) {
220 		if (RXRDY(m))
221 			if (READ(m) == MPU_ACK)
222 				break;
223 	}
224 
225 	if (i < 2000) {
226 		CMD(m, MPU_UART);
227 		return 0;
228 	}
229 	printf("mpu401_minit failed active sensing\n");
230 	return 1;
231 }
232 
233 
234 int
235 mpu401_muninit(kobj_t obj, struct mpu401 *m)
236 {
237 
238 	return MPUFOI_UNINIT(m, m->cookie);
239 }
240 
241 int
242 mpu401_minqsize(kobj_t obj, struct mpu401 *m)
243 {
244 	return 128;
245 }
246 
247 int
248 mpu401_moutqsize(kobj_t obj, struct mpu401 *m)
249 {
250 	return 128;
251 }
252 
253 static void
254 mpu401_mcallback(kobj_t obj, struct mpu401 *m, int flags)
255 {
256 #if 0
257 	printf("mpu401_callback %s %s %s %s\n",
258 	    flags & M_RX ? "M_RX" : "",
259 	    flags & M_TX ? "M_TX" : "",
260 	    flags & M_RXEN ? "M_RXEN" : "",
261 	    flags & M_TXEN ? "M_TXEN" : "");
262 #endif
263 	if (flags & M_TXEN && m->si) {
264 		callout_reset(&m->timer, 1, mpu401_timeout, m);
265 	}
266 	m->flags = flags;
267 }
268 
269 static void
270 mpu401_mcallbackp(kobj_t obj, struct mpu401 *m, int flags)
271 {
272 /*	printf("mpu401_callbackp\n"); */
273 	mpu401_mcallback(obj, m, flags);
274 }
275 
276 static const char *
277 mpu401_mdescr(kobj_t obj, struct mpu401 *m, int verbosity)
278 {
279 
280 	return "descr mpu401";
281 }
282 
283 static const char *
284 mpu401_mprovider(kobj_t obj, struct mpu401 *m)
285 {
286 	return "provider mpu401";
287 }
288