FreeBSD kernel sound device code
mpu401.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#include <sys/types.h>
34#include <sys/param.h>
35#include <sys/queue.h>
36#include <sys/kernel.h>
37#include <sys/lock.h>
38#include <sys/mutex.h>
39#include <sys/proc.h>
40#include <sys/systm.h>
41#include <sys/kobj.h>
42#include <sys/malloc.h>
43#include <sys/bus.h> /* to get driver_intr_t */
44
45#ifdef HAVE_KERNEL_OPTION_HEADERS
46#include "opt_snd.h"
47#endif
48
50#include <dev/sound/midi/midi.h>
51
52#include "mpu_if.h"
53#include "mpufoi_if.h"
54
55#ifndef KOBJMETHOD_END
56#define KOBJMETHOD_END { NULL, NULL }
57#endif
58
59#define MPU_DATAPORT 0
60#define MPU_CMDPORT 1
61#define MPU_STATPORT 1
62#define MPU_RESET 0xff
63#define MPU_UART 0x3f
64#define MPU_ACK 0xfe
65#define MPU_STATMASK 0xc0
66#define MPU_OUTPUTBUSY 0x40
67#define MPU_INPUTBUSY 0x80
68#define MPU_TRYDATA 50
69#define MPU_DELAY 2500
70
71#define CMD(m,d) MPUFOI_WRITE(m, m->cookie, MPU_CMDPORT,d)
72#define STATUS(m) MPUFOI_READ(m, m->cookie, MPU_STATPORT)
73#define READ(m) MPUFOI_READ(m, m->cookie, MPU_DATAPORT)
74#define WRITE(m,d) MPUFOI_WRITE(m, m->cookie, MPU_DATAPORT,d)
75
76struct mpu401 {
78 struct snd_midi *mid;
79 int flags;
80 driver_intr_t *si;
81 void *cookie;
82 struct callout timer;
83};
84
85static void mpu401_timeout(void *m);
87
88static int mpu401_minit(struct snd_midi *, void *);
89static int mpu401_muninit(struct snd_midi *, void *);
90static int mpu401_minqsize(struct snd_midi *, void *);
91static int mpu401_moutqsize(struct snd_midi *, void *);
92static void mpu401_mcallback(struct snd_midi *, void *, int);
93static void mpu401_mcallbackp(struct snd_midi *, void *, int);
94static const char *mpu401_mdescr(struct snd_midi *, void *, int);
95static const char *mpu401_mprovider(struct snd_midi *, void *);
96
97static kobj_method_t mpu401_methods[] = {
98 KOBJMETHOD(mpu_init, mpu401_minit),
99 KOBJMETHOD(mpu_uninit, mpu401_muninit),
100 KOBJMETHOD(mpu_inqsize, mpu401_minqsize),
101 KOBJMETHOD(mpu_outqsize, mpu401_moutqsize),
102 KOBJMETHOD(mpu_callback, mpu401_mcallback),
103 KOBJMETHOD(mpu_callbackp, mpu401_mcallbackp),
104 KOBJMETHOD(mpu_descr, mpu401_mdescr),
105 KOBJMETHOD(mpu_provider, mpu401_mprovider),
107};
108
110
111void
113{
114 struct mpu401 *m = (struct mpu401 *)a;
115
116 if (m->si)
117 (m->si)(m->cookie);
118
119}
120static int
122{
123#define MPU_INTR_BUF 16
125 int i;
126 int s;
127
128/*
129 printf("mpu401_intr\n");
130*/
131#define RXRDY(m) ( (STATUS(m) & MPU_INPUTBUSY) == 0)
132#define TXRDY(m) ( (STATUS(m) & MPU_OUTPUTBUSY) == 0)
133#if 0
134#define D(x,l) printf("mpu401_intr %d %x %s %s\n",l, x, x&MPU_INPUTBUSY?"RX":"", x&MPU_OUTPUTBUSY?"TX":"")
135#else
136#define D(x,l)
137#endif
138 i = 0;
139 s = STATUS(m);
140 D(s, 1);
141 while ((s & MPU_INPUTBUSY) == 0 && i < MPU_INTR_BUF) {
142 b[i] = READ(m);
143/*
144 printf("mpu401_intr in i %d d %d\n", i, b[i]);
145*/
146 i++;
147 s = STATUS(m);
148 }
149 if (i)
150 midi_in(m->mid, b, i);
151 i = 0;
152 while (!(s & MPU_OUTPUTBUSY) && i < MPU_INTR_BUF) {
153 if (midi_out(m->mid, b, 1)) {
154/*
155 printf("mpu401_intr out i %d d %d\n", i, b[0]);
156*/
157
158 WRITE(m, *b);
159 } else {
160/*
161 printf("mpu401_intr write: no output\n");
162*/
163 return 0;
164 }
165 i++;
166 /* DELAY(100); */
167 s = STATUS(m);
168 }
169
170 if ((m->flags & M_TXEN) && (m->si)) {
171 callout_reset(&m->timer, 1, mpu401_timeout, m);
172 }
173 return (m->flags & M_TXEN) == M_TXEN;
174}
175
176struct mpu401 *
177mpu401_init(kobj_class_t cls, void *cookie, driver_intr_t softintr,
178 mpu401_intr_t ** cb)
179{
180 struct mpu401 *m;
181
182 *cb = NULL;
183 m = malloc(sizeof(*m), M_MIDI, M_NOWAIT | M_ZERO);
184
185 if (!m)
186 return NULL;
187
188 kobj_init((kobj_t)m, cls);
189
190 callout_init(&m->timer, 1);
191
192 m->si = softintr;
193 m->cookie = cookie;
194 m->flags = 0;
195
196 m->mid = midi_init(&mpu401_class, 0, 0, m);
197 if (!m->mid)
198 goto err;
199 *cb = mpu401_intr;
200 return m;
201err:
202 printf("mpu401_init error\n");
203 free(m, M_MIDI);
204 return NULL;
205}
206
207int
209{
210 int retval;
211
212 CMD(m, MPU_RESET);
213 retval = midi_uninit(m->mid);
214 if (retval)
215 return retval;
216 free(m, M_MIDI);
217 return 0;
218}
219
220static int
221mpu401_minit(struct snd_midi *sm, void *arg)
222{
223 struct mpu401 *m = arg;
224 int i;
225
226 CMD(m, MPU_RESET);
227 CMD(m, MPU_UART);
228 return 0;
229 i = 0;
230 while (++i < 2000) {
231 if (RXRDY(m))
232 if (READ(m) == MPU_ACK)
233 break;
234 }
235
236 if (i < 2000) {
237 CMD(m, MPU_UART);
238 return 0;
239 }
240 printf("mpu401_minit failed active sensing\n");
241 return 1;
242}
243
244int
245mpu401_muninit(struct snd_midi *sm, void *arg)
246{
247 struct mpu401 *m = arg;
248
249 return MPUFOI_UNINIT(m, m->cookie);
250}
251
252int
253mpu401_minqsize(struct snd_midi *sm, void *arg)
254{
255 return 128;
256}
257
258int
259mpu401_moutqsize(struct snd_midi *sm, void *arg)
260{
261 return 128;
262}
263
264static void
265mpu401_mcallback(struct snd_midi *sm, void *arg, int flags)
266{
267 struct mpu401 *m = arg;
268#if 0
269 printf("mpu401_callback %s %s %s %s\n",
270 flags & M_RX ? "M_RX" : "",
271 flags & M_TX ? "M_TX" : "",
272 flags & M_RXEN ? "M_RXEN" : "",
273 flags & M_TXEN ? "M_TXEN" : "");
274#endif
275 if (flags & M_TXEN && m->si) {
276 callout_reset(&m->timer, 1, mpu401_timeout, m);
277 }
278 m->flags = flags;
279}
280
281static void
282mpu401_mcallbackp(struct snd_midi *sm, void *arg, int flags)
283{
284/* printf("mpu401_callbackp\n"); */
285 mpu401_mcallback(sm, arg, flags);
286}
287
288static const char *
289mpu401_mdescr(struct snd_midi *sm, void *arg, int verbosity)
290{
291
292 return "descr mpu401";
293}
294
295static const char *
296mpu401_mprovider(struct snd_midi *m, void *arg)
297{
298 return "provider mpu401";
299}
METHOD int free
Definition: channel_if.m:110
struct pcmchan_matrix * m
Definition: channel_if.m:232
struct snd_dbuf * b
Definition: channel_if.m:105
int midi_in(struct snd_midi *m, MIDI_TYPE *buf, int size)
Definition: midi.c:453
int midi_uninit(struct snd_midi *m)
Definition: midi.c:401
struct snd_midi * midi_init(kobj_class_t cls, int unit, int channel, void *cookie)
Definition: midi.c:281
int midi_out(struct snd_midi *m, MIDI_TYPE *buf, int size)
Definition: midi.c:583
#define M_RX
Definition: midi.h:39
#define M_TXEN
Definition: midi.h:42
#define M_TX
Definition: midi.h:40
#define MIDI_TYPE
Definition: midi.h:44
#define M_RXEN
Definition: midi.h:41
static int mpu401_moutqsize(struct snd_midi *, void *)
Definition: mpu401.c:259
static kobj_method_t mpu401_methods[]
Definition: mpu401.c:97
#define STATUS(m)
Definition: mpu401.c:72
static int mpu401_minqsize(struct snd_midi *, void *)
Definition: mpu401.c:253
static void mpu401_timeout(void *m)
Definition: mpu401.c:112
#define MPU_UART
Definition: mpu401.c:63
static int mpu401_minit(struct snd_midi *, void *)
Definition: mpu401.c:221
static void mpu401_mcallbackp(struct snd_midi *, void *, int)
Definition: mpu401.c:282
#define MPU_OUTPUTBUSY
Definition: mpu401.c:66
#define KOBJMETHOD_END
Definition: mpu401.c:56
#define CMD(m, d)
Definition: mpu401.c:71
#define MPU_INPUTBUSY
Definition: mpu401.c:67
__FBSDID("$FreeBSD$")
static mpu401_intr_t mpu401_intr
Definition: mpu401.c:86
DEFINE_CLASS(mpu401, mpu401_methods, 0)
#define MPU_INTR_BUF
static const char * mpu401_mdescr(struct snd_midi *, void *, int)
Definition: mpu401.c:289
#define WRITE(m, d)
Definition: mpu401.c:74
int mpu401_uninit(struct mpu401 *m)
Definition: mpu401.c:208
static void mpu401_mcallback(struct snd_midi *, void *, int)
Definition: mpu401.c:265
#define MPU_ACK
Definition: mpu401.c:64
#define RXRDY(m)
#define MPU_RESET
Definition: mpu401.c:62
struct mpu401 * mpu401_init(kobj_class_t cls, void *cookie, driver_intr_t softintr, mpu401_intr_t **cb)
Definition: mpu401.c:177
#define D(x, l)
static int mpu401_muninit(struct snd_midi *, void *)
Definition: mpu401.c:245
#define READ(m)
Definition: mpu401.c:73
static const char * mpu401_mprovider(struct snd_midi *, void *)
Definition: mpu401.c:296
int mpu401_intr_t(struct mpu401 *_obj)
Definition: mpu401.h:36
Definition: mpu401.c:76
driver_intr_t * si
Definition: mpu401.c:80
void * cookie
Definition: mpu401.c:81
KOBJ_FIELDS
Definition: mpu401.c:77
struct snd_midi * mid
Definition: mpu401.c:78
struct callout timer
Definition: mpu401.c:82
int flags
Definition: mpu401.c:79
Definition: midi.c:101