xref: /freebsd/sys/dev/sound/midi/mpu401.c (revision 92336491ccc2115a3c81a2c6442ddb6c53c3e378)
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 
92 static kobj_method_t mpu401_methods[] = {
93 	KOBJMETHOD(mpu_init, mpu401_minit),
94 	KOBJMETHOD(mpu_uninit, mpu401_muninit),
95 	KOBJMETHOD(mpu_inqsize, mpu401_minqsize),
96 	KOBJMETHOD(mpu_outqsize, mpu401_moutqsize),
97 	KOBJMETHOD(mpu_callback, mpu401_mcallback),
98 	KOBJMETHOD(mpu_callbackp, mpu401_mcallbackp),
99 	KOBJMETHOD_END
100 };
101 
102 DEFINE_CLASS(mpu401, mpu401_methods, 0);
103 
104 void
mpu401_timeout(void * a)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
mpu401_intr(struct mpu401 * m)114 mpu401_intr(struct mpu401 *m)
115 {
116 #define MPU_INTR_BUF	16
117 	uint8_t b[MPU_INTR_BUF];
118 	int i;
119 	int s;
120 
121 #define RXRDY(m) ( (STATUS(m) & MPU_INPUTBUSY) == 0)
122 #define TXRDY(m) ( (STATUS(m) & MPU_OUTPUTBUSY) == 0)
123 	i = 0;
124 	s = STATUS(m);
125 	while ((s & MPU_INPUTBUSY) == 0 && i < MPU_INTR_BUF) {
126 		b[i] = READ(m);
127 		i++;
128 		s = STATUS(m);
129 	}
130 	if (i)
131 		midi_in(m->mid, b, i);
132 	i = 0;
133 	while (!(s & MPU_OUTPUTBUSY) && i < MPU_INTR_BUF) {
134 		if (midi_out(m->mid, b, 1)) {
135 
136 			WRITE(m, *b);
137 		} else {
138 			return 0;
139 		}
140 		i++;
141 		/* DELAY(100); */
142 		s = STATUS(m);
143 	}
144 
145 	if ((m->flags & M_TXEN) && (m->si)) {
146 		callout_reset(&m->timer, 1, mpu401_timeout, m);
147 	}
148 	return (m->flags & M_TXEN) == M_TXEN;
149 }
150 
151 struct mpu401 *
mpu401_init(kobj_class_t cls,void * cookie,driver_intr_t softintr,mpu401_intr_t ** cb)152 mpu401_init(kobj_class_t cls, void *cookie, driver_intr_t softintr,
153     mpu401_intr_t ** cb)
154 {
155 	struct mpu401 *m;
156 
157 	*cb = NULL;
158 	m = malloc(sizeof(*m), M_MIDI, M_NOWAIT | M_ZERO);
159 
160 	if (!m)
161 		return NULL;
162 
163 	kobj_init((kobj_t)m, cls);
164 
165 	callout_init(&m->timer, 1);
166 
167 	m->si = softintr;
168 	m->cookie = cookie;
169 	m->flags = 0;
170 
171 	m->mid = midi_init(&mpu401_class, 0, 0, m);
172 	if (!m->mid)
173 		goto err;
174 	*cb = mpu401_intr;
175 	return m;
176 err:
177 	printf("mpu401_init error\n");
178 	free(m, M_MIDI);
179 	return NULL;
180 }
181 
182 int
mpu401_uninit(struct mpu401 * m)183 mpu401_uninit(struct mpu401 *m)
184 {
185 	int retval;
186 
187 	CMD(m, MPU_RESET);
188 	retval = midi_uninit(m->mid);
189 	if (retval)
190 		return retval;
191 	free(m, M_MIDI);
192 	return 0;
193 }
194 
195 static int
mpu401_minit(struct snd_midi * sm,void * arg)196 mpu401_minit(struct snd_midi *sm, void *arg)
197 {
198 	struct mpu401 *m = arg;
199 	int i;
200 
201 	CMD(m, MPU_RESET);
202 	CMD(m, MPU_UART);
203 	return 0;
204 	i = 0;
205 	while (++i < 2000) {
206 		if (RXRDY(m))
207 			if (READ(m) == MPU_ACK)
208 				break;
209 	}
210 
211 	if (i < 2000) {
212 		CMD(m, MPU_UART);
213 		return 0;
214 	}
215 	printf("mpu401_minit failed active sensing\n");
216 	return 1;
217 }
218 
219 int
mpu401_muninit(struct snd_midi * sm,void * arg)220 mpu401_muninit(struct snd_midi *sm, void *arg)
221 {
222 	struct mpu401 *m = arg;
223 
224 	return MPUFOI_UNINIT(m, m->cookie);
225 }
226 
227 int
mpu401_minqsize(struct snd_midi * sm,void * arg)228 mpu401_minqsize(struct snd_midi *sm, void *arg)
229 {
230 	return 128;
231 }
232 
233 int
mpu401_moutqsize(struct snd_midi * sm,void * arg)234 mpu401_moutqsize(struct snd_midi *sm, void *arg)
235 {
236 	return 128;
237 }
238 
239 static void
mpu401_mcallback(struct snd_midi * sm,void * arg,int flags)240 mpu401_mcallback(struct snd_midi *sm, void *arg, int flags)
241 {
242 	struct mpu401 *m = arg;
243 
244 	if (flags & M_TXEN && m->si) {
245 		callout_reset(&m->timer, 1, mpu401_timeout, m);
246 	}
247 	m->flags = flags;
248 }
249 
250 static void
mpu401_mcallbackp(struct snd_midi * sm,void * arg,int flags)251 mpu401_mcallbackp(struct snd_midi *sm, void *arg, int flags)
252 {
253 	mpu401_mcallback(sm, arg, flags);
254 }
255