xref: /dragonfly/sys/dev/sound/midi/mpu401.c (revision 7b1120e5)
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/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 #define MPU_DATAPORT   0
53 #define MPU_CMDPORT    1
54 #define MPU_STATPORT   1
55 #define MPU_RESET      0xff
56 #define MPU_UART       0x3f
57 #define MPU_ACK        0xfe
58 #define MPU_STATMASK   0xc0
59 #define MPU_OUTPUTBUSY 0x40
60 #define MPU_INPUTBUSY  0x80
61 #define MPU_TRYDATA 50
62 #define MPU_DELAY   2500
63 
64 #define CMD(m,d)	MPUFOI_WRITE(m, m->cookie, MPU_CMDPORT,d)
65 #define STATUS(m)	MPUFOI_READ(m, m->cookie, MPU_STATPORT)
66 #define READ(m)		MPUFOI_READ(m, m->cookie, MPU_DATAPORT)
67 #define WRITE(m,d)	MPUFOI_WRITE(m, m->cookie, MPU_DATAPORT,d)
68 
69 struct mpu401 {
70 	KOBJ_FIELDS;
71 	struct snd_midi *mid;
72 	int	flags;
73 	driver_intr_t *si;
74 	void   *cookie;
75 	struct callout timer;
76 };
77 
78 static void mpu401_timeout(void *m);
79 static mpu401_intr_t mpu401_intr;
80 
81 static int mpu401_minit(struct snd_midi *, void *);
82 static int mpu401_muninit(struct snd_midi *, void *);
83 static int mpu401_minqsize(struct snd_midi *, void *);
84 static int mpu401_moutqsize(struct snd_midi *, void *);
85 static void mpu401_mcallback(struct snd_midi *, void *, int);
86 static void mpu401_mcallbackp(struct snd_midi *, void *, int);
87 static const char *mpu401_mdescr(struct snd_midi *, void *, int);
88 static const char *mpu401_mprovider(struct snd_midi *, void *);
89 
90 static kobj_method_t mpu401_methods[] = {
91 	KOBJMETHOD(mpu_init, mpu401_minit),
92 	KOBJMETHOD(mpu_uninit, mpu401_muninit),
93 	KOBJMETHOD(mpu_inqsize, mpu401_minqsize),
94 	KOBJMETHOD(mpu_outqsize, mpu401_moutqsize),
95 	KOBJMETHOD(mpu_callback, mpu401_mcallback),
96 	KOBJMETHOD(mpu_callbackp, mpu401_mcallbackp),
97 	KOBJMETHOD(mpu_descr, mpu401_mdescr),
98 	KOBJMETHOD(mpu_provider, mpu401_mprovider),
99 	KOBJMETHOD_END
100 };
101 
102 DEFINE_CLASS(mpu401, mpu401_methods, 0);
103 
104 void
105 mpu401_timeout(void *a)
106 {
107 	struct mpu401 *m = (struct mpu401 *)a;
108 
109 	if (m->si)
110 		(m->si)(m->cookie);
111 
112 }
113 static int
114 mpu401_intr(struct mpu401 *m)
115 {
116 #define MPU_INTR_BUF	16
117 	MIDI_TYPE b[MPU_INTR_BUF];
118 	int i;
119 	int s;
120 
121 /*
122 	kprintf("mpu401_intr\n");
123 */
124 #define RXRDY(m) ( (STATUS(m) & MPU_INPUTBUSY) == 0)
125 #define TXRDY(m) ( (STATUS(m) & MPU_OUTPUTBUSY) == 0)
126 #if 0
127 #define D(x,l) kprintf("mpu401_intr %d %x %s %s\n",l, x, x&MPU_INPUTBUSY?"RX":"", x&MPU_OUTPUTBUSY?"TX":"")
128 #else
129 #define D(x,l)
130 #endif
131 	i = 0;
132 	s = STATUS(m);
133 	D(s, 1);
134 	while ((s & MPU_INPUTBUSY) == 0 && i < MPU_INTR_BUF) {
135 		b[i] = READ(m);
136 /*
137 		kprintf("mpu401_intr in i %d d %d\n", i, b[i]);
138 */
139 		i++;
140 		s = STATUS(m);
141 	}
142 	if (i)
143 		midi_in(m->mid, b, i);
144 	i = 0;
145 	while (!(s & MPU_OUTPUTBUSY) && i < MPU_INTR_BUF) {
146 		if (midi_out(m->mid, b, 1)) {
147 /*
148 			kprintf("mpu401_intr out i %d d %d\n", i, b[0]);
149 */
150 
151 			WRITE(m, *b);
152 		} else {
153 /*
154 			kprintf("mpu401_intr write: no output\n");
155 */
156 			return 0;
157 		}
158 		i++;
159 		/* DELAY(100); */
160 		s = STATUS(m);
161 	}
162 
163 	if ((m->flags & M_TXEN) && (m->si)) {
164 		callout_reset(&m->timer, 1, mpu401_timeout, m);
165 	}
166 	return (m->flags & M_TXEN) == M_TXEN;
167 }
168 
169 struct mpu401 *
170 mpu401_init(kobj_class_t cls, void *cookie, driver_intr_t softintr,
171     mpu401_intr_t ** cb)
172 {
173 	struct mpu401 *m;
174 
175 	*cb = NULL;
176 	m = kmalloc(sizeof(*m), M_MIDI, M_WAITOK | M_ZERO);
177 
178 	kobj_init((kobj_t)m, cls);
179 
180 	callout_init_mp(&m->timer);
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 	kprintf("mpu401_init error\n");
193 	kfree(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 	kfree(m, M_MIDI);
207 	return 0;
208 }
209 
210 static int
211 mpu401_minit(struct snd_midi *sm, void *arg)
212 {
213 	struct mpu401 *m = arg;
214 	int i;
215 
216 	CMD(m, MPU_RESET);
217 	CMD(m, MPU_UART);
218 	return 0;
219 	i = 0;
220 	while (++i < 2000) {
221 		if (RXRDY(m))
222 			if (READ(m) == MPU_ACK)
223 				break;
224 	}
225 
226 	if (i < 2000) {
227 		CMD(m, MPU_UART);
228 		return 0;
229 	}
230 	kprintf("mpu401_minit failed active sensing\n");
231 	return 1;
232 }
233 
234 
235 int
236 mpu401_muninit(struct snd_midi *sm, void *arg)
237 {
238 	struct mpu401 *m = arg;
239 
240 	return MPUFOI_UNINIT(m, m->cookie);
241 }
242 
243 int
244 mpu401_minqsize(struct snd_midi *sm, void *arg)
245 {
246 	return 128;
247 }
248 
249 int
250 mpu401_moutqsize(struct snd_midi *sm, void *arg)
251 {
252 	return 128;
253 }
254 
255 static void
256 mpu401_mcallback(struct snd_midi *sm, void *arg, int flags)
257 {
258 	struct mpu401 *m = arg;
259 #if 0
260 	kprintf("mpu401_callback %s %s %s %s\n",
261 	    flags & M_RX ? "M_RX" : "",
262 	    flags & M_TX ? "M_TX" : "",
263 	    flags & M_RXEN ? "M_RXEN" : "",
264 	    flags & M_TXEN ? "M_TXEN" : "");
265 #endif
266 	if (flags & M_TXEN && m->si) {
267 		callout_reset(&m->timer, 1, mpu401_timeout, m);
268 	}
269 	m->flags = flags;
270 }
271 
272 static void
273 mpu401_mcallbackp(struct snd_midi *sm, void *arg, int flags)
274 {
275 /*	kprintf("mpu401_callbackp\n"); */
276 	mpu401_mcallback(sm, arg, flags);
277 }
278 
279 static const char *
280 mpu401_mdescr(struct snd_midi *sm, void *arg, int verbosity)
281 {
282 
283 	return "descr mpu401";
284 }
285 
286 static const char *
287 mpu401_mprovider(struct snd_midi *m, void *arg)
288 {
289 	return "provider mpu401";
290 }
291