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