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