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