xref: /freebsd/sys/dev/sound/midi/mpu401.c (revision 8ccc0d235c226d84112561d453c49904398d085c)
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/systm.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/kobj.h>
34 
35 #ifdef HAVE_KERNEL_OPTION_HEADERS
36 #include "opt_snd.h"
37 #endif
38 
39 #include <dev/sound/midi/mpu401.h>
40 #include <dev/sound/midi/midi.h>
41 
42 #include "mpu_if.h"
43 #include "mpufoi_if.h"
44 
45 #define MPU_DATAPORT   0
46 #define MPU_CMDPORT    1
47 #define MPU_STATPORT   1
48 #define MPU_RESET      0xff
49 #define MPU_UART       0x3f
50 #define MPU_ACK        0xfe
51 #define MPU_STATMASK   0xc0
52 #define MPU_OUTPUTBUSY 0x40
53 #define MPU_INPUTBUSY  0x80
54 #define MPU_TRYDATA 50
55 #define MPU_DELAY   2500
56 
57 #define CMD(m,d)	MPUFOI_WRITE(m, m->cookie, MPU_CMDPORT,d)
58 #define STATUS(m)	MPUFOI_READ(m, m->cookie, MPU_STATPORT)
59 #define READ(m)		MPUFOI_READ(m, m->cookie, MPU_DATAPORT)
60 #define WRITE(m,d)	MPUFOI_WRITE(m, m->cookie, MPU_DATAPORT,d)
61 
62 struct mpu401 {
63 	KOBJ_FIELDS;
64 	struct snd_midi *mid;
65 	int	flags;
66 	driver_intr_t *si;
67 	void   *cookie;
68 	struct callout timer;
69 };
70 
71 static void mpu401_timeout(void *m);
72 static mpu401_intr_t mpu401_intr;
73 
74 static int mpu401_minit(struct snd_midi *, void *);
75 static int mpu401_muninit(struct snd_midi *, void *);
76 static int mpu401_minqsize(struct snd_midi *, void *);
77 static int mpu401_moutqsize(struct snd_midi *, void *);
78 static void mpu401_mcallback(struct snd_midi *, void *, int);
79 static void mpu401_mcallbackp(struct snd_midi *, void *, int);
80 
81 static kobj_method_t mpu401_methods[] = {
82 	KOBJMETHOD(mpu_init, mpu401_minit),
83 	KOBJMETHOD(mpu_uninit, mpu401_muninit),
84 	KOBJMETHOD(mpu_inqsize, mpu401_minqsize),
85 	KOBJMETHOD(mpu_outqsize, mpu401_moutqsize),
86 	KOBJMETHOD(mpu_callback, mpu401_mcallback),
87 	KOBJMETHOD(mpu_callbackp, mpu401_mcallbackp),
88 	KOBJMETHOD_END
89 };
90 
91 DEFINE_CLASS(mpu401, mpu401_methods, 0);
92 
93 void
94 mpu401_timeout(void *a)
95 {
96 	struct mpu401 *m = (struct mpu401 *)a;
97 
98 	if (m->si)
99 		(m->si)(m->cookie);
100 
101 }
102 static int
103 mpu401_intr(struct mpu401 *m)
104 {
105 #define MPU_INTR_BUF	16
106 	uint8_t b[MPU_INTR_BUF];
107 	int i;
108 	int s;
109 
110 #define RXRDY(m) ( (STATUS(m) & MPU_INPUTBUSY) == 0)
111 #define TXRDY(m) ( (STATUS(m) & MPU_OUTPUTBUSY) == 0)
112 	i = 0;
113 	s = STATUS(m);
114 	while ((s & MPU_INPUTBUSY) == 0 && i < MPU_INTR_BUF) {
115 		b[i] = READ(m);
116 		i++;
117 		s = STATUS(m);
118 	}
119 	if (i)
120 		midi_in(m->mid, b, i);
121 	i = 0;
122 	while (!(s & MPU_OUTPUTBUSY) && i < MPU_INTR_BUF) {
123 		if (midi_out(m->mid, b, 1)) {
124 
125 			WRITE(m, *b);
126 		} else {
127 			return 0;
128 		}
129 		i++;
130 		/* DELAY(100); */
131 		s = STATUS(m);
132 	}
133 
134 	if ((m->flags & M_TXEN) && (m->si)) {
135 		callout_reset(&m->timer, 1, mpu401_timeout, m);
136 	}
137 	return (m->flags & M_TXEN) == M_TXEN;
138 }
139 
140 struct mpu401 *
141 mpu401_init(kobj_class_t cls, void *cookie, driver_intr_t softintr,
142     mpu401_intr_t ** cb)
143 {
144 	struct mpu401 *m;
145 
146 	*cb = NULL;
147 	m = malloc(sizeof(*m), M_MIDI, M_NOWAIT | M_ZERO);
148 
149 	if (!m)
150 		return NULL;
151 
152 	kobj_init((kobj_t)m, cls);
153 
154 	callout_init(&m->timer, 1);
155 
156 	m->si = softintr;
157 	m->cookie = cookie;
158 	m->flags = 0;
159 
160 	m->mid = midi_init(&mpu401_class, 0, 0, m);
161 	if (!m->mid)
162 		goto err;
163 	*cb = mpu401_intr;
164 	return m;
165 err:
166 	printf("mpu401_init error\n");
167 	free(m, M_MIDI);
168 	return NULL;
169 }
170 
171 int
172 mpu401_uninit(struct mpu401 *m)
173 {
174 	int retval;
175 
176 	CMD(m, MPU_RESET);
177 	retval = midi_uninit(m->mid);
178 	if (retval)
179 		return retval;
180 	free(m, M_MIDI);
181 	return 0;
182 }
183 
184 static int
185 mpu401_minit(struct snd_midi *sm, void *arg)
186 {
187 	struct mpu401 *m = arg;
188 	int i;
189 
190 	CMD(m, MPU_RESET);
191 	CMD(m, MPU_UART);
192 	return 0;
193 	i = 0;
194 	while (++i < 2000) {
195 		if (RXRDY(m))
196 			if (READ(m) == MPU_ACK)
197 				break;
198 	}
199 
200 	if (i < 2000) {
201 		CMD(m, MPU_UART);
202 		return 0;
203 	}
204 	printf("mpu401_minit failed active sensing\n");
205 	return 1;
206 }
207 
208 int
209 mpu401_muninit(struct snd_midi *sm, void *arg)
210 {
211 	struct mpu401 *m = arg;
212 
213 	return MPUFOI_UNINIT(m, m->cookie);
214 }
215 
216 int
217 mpu401_minqsize(struct snd_midi *sm, void *arg)
218 {
219 	return 128;
220 }
221 
222 int
223 mpu401_moutqsize(struct snd_midi *sm, void *arg)
224 {
225 	return 128;
226 }
227 
228 static void
229 mpu401_mcallback(struct snd_midi *sm, void *arg, int flags)
230 {
231 	struct mpu401 *m = arg;
232 
233 	if (flags & M_TXEN && m->si) {
234 		callout_reset(&m->timer, 1, mpu401_timeout, m);
235 	}
236 	m->flags = flags;
237 }
238 
239 static void
240 mpu401_mcallbackp(struct snd_midi *sm, void *arg, int flags)
241 {
242 	mpu401_mcallback(sm, arg, flags);
243 }
244