2 * Copyright (c) 2003 Mathew Kanner
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: head/sys/dev/sound/midi/mpu401.c 193979 2009-06-11 09:06:09Z ariff $");
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/kernel.h>
36 #include <sys/mutex.h>
38 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/bus.h> /* to get driver_intr_t */
43 #ifdef HAVE_KERNEL_OPTION_HEADERS
47 #include <dev/sound/midi/mpu401.h>
48 #include <dev/sound/midi/midi.h>
51 #include "mpufoi_if.h"
53 #ifndef KOBJMETHOD_END
54 #define KOBJMETHOD_END { NULL, NULL }
57 #define MPU_DATAPORT 0
59 #define MPU_STATPORT 1
60 #define MPU_RESET 0xff
63 #define MPU_STATMASK 0xc0
64 #define MPU_OUTPUTBUSY 0x40
65 #define MPU_INPUTBUSY 0x80
66 #define MPU_TRYDATA 50
67 #define MPU_DELAY 2500
69 #define CMD(m,d) MPUFOI_WRITE(m, m->cookie, MPU_CMDPORT,d)
70 #define STATUS(m) MPUFOI_READ(m, m->cookie, MPU_STATPORT)
71 #define READ(m) MPUFOI_READ(m, m->cookie, MPU_DATAPORT)
72 #define WRITE(m,d) MPUFOI_WRITE(m, m->cookie, MPU_DATAPORT,d)
83 static void mpu401_timeout(void *m
);
84 static mpu401_intr_t mpu401_intr
;
86 static int mpu401_minit(struct snd_midi
*, void *);
87 static int mpu401_muninit(struct snd_midi
*, void *);
88 static int mpu401_minqsize(struct snd_midi
*, void *);
89 static int mpu401_moutqsize(struct snd_midi
*, void *);
90 static void mpu401_mcallback(struct snd_midi
*, void *, int);
91 static void mpu401_mcallbackp(struct snd_midi
*, void *, int);
92 static const char *mpu401_mdescr(struct snd_midi
*, void *, int);
93 static const char *mpu401_mprovider(struct snd_midi
*, void *);
95 static kobj_method_t mpu401_methods
[] = {
96 KOBJMETHOD(mpu_init
, mpu401_minit
),
97 KOBJMETHOD(mpu_uninit
, mpu401_muninit
),
98 KOBJMETHOD(mpu_inqsize
, mpu401_minqsize
),
99 KOBJMETHOD(mpu_outqsize
, mpu401_moutqsize
),
100 KOBJMETHOD(mpu_callback
, mpu401_mcallback
),
101 KOBJMETHOD(mpu_callbackp
, mpu401_mcallbackp
),
102 KOBJMETHOD(mpu_descr
, mpu401_mdescr
),
103 KOBJMETHOD(mpu_provider
, mpu401_mprovider
),
107 DEFINE_CLASS(mpu401
, mpu401_methods
, 0);
110 mpu401_timeout(void *a
)
112 struct mpu401
*m
= (struct mpu401
*)a
;
119 mpu401_intr(struct mpu401
*m
)
121 #define MPU_INTR_BUF 16
122 MIDI_TYPE b
[MPU_INTR_BUF
];
127 kprintf("mpu401_intr\n");
129 #define RXRDY(m) ( (STATUS(m) & MPU_INPUTBUSY) == 0)
130 #define TXRDY(m) ( (STATUS(m) & MPU_OUTPUTBUSY) == 0)
132 #define D(x,l) kprintf("mpu401_intr %d %x %s %s\n",l, x, x&MPU_INPUTBUSY?"RX":"", x&MPU_OUTPUTBUSY?"TX":"")
139 while ((s
& MPU_INPUTBUSY
) == 0 && i
< MPU_INTR_BUF
) {
142 kprintf("mpu401_intr in i %d d %d\n", i, b[i]);
148 midi_in(m
->mid
, b
, i
);
150 while (!(s
& MPU_OUTPUTBUSY
) && i
< MPU_INTR_BUF
) {
151 if (midi_out(m
->mid
, b
, 1)) {
153 kprintf("mpu401_intr out i %d d %d\n", i, b[0]);
159 kprintf("mpu401_intr write: no output\n");
168 if ((m
->flags
& M_TXEN
) && (m
->si
)) {
169 callout_reset(&m
->timer
, 1, mpu401_timeout
, m
);
171 return (m
->flags
& M_TXEN
) == M_TXEN
;
175 mpu401_init(kobj_class_t cls
, void *cookie
, driver_intr_t softintr
,
181 m
= kmalloc(sizeof(*m
), M_MIDI
, M_WAITOK
| M_ZERO
);
183 kobj_init((kobj_t
)m
, cls
);
185 callout_init_mp(&m
->timer
);
191 m
->mid
= midi_init(&mpu401_class
, 0, 0, m
);
197 kprintf("mpu401_init error\n");
203 mpu401_uninit(struct mpu401
*m
)
208 retval
= midi_uninit(m
->mid
);
216 mpu401_minit(struct snd_midi
*sm
, void *arg
)
218 struct mpu401
*m
= arg
;
227 if (READ(m
) == MPU_ACK
)
235 kprintf("mpu401_minit failed active sensing\n");
241 mpu401_muninit(struct snd_midi
*sm
, void *arg
)
243 struct mpu401
*m
= arg
;
245 return MPUFOI_UNINIT(m
, m
->cookie
);
249 mpu401_minqsize(struct snd_midi
*sm
, void *arg
)
255 mpu401_moutqsize(struct snd_midi
*sm
, void *arg
)
261 mpu401_mcallback(struct snd_midi
*sm
, void *arg
, int flags
)
263 struct mpu401
*m
= arg
;
265 kprintf("mpu401_callback %s %s %s %s\n",
266 flags
& M_RX
? "M_RX" : "",
267 flags
& M_TX
? "M_TX" : "",
268 flags
& M_RXEN
? "M_RXEN" : "",
269 flags
& M_TXEN
? "M_TXEN" : "");
271 if (flags
& M_TXEN
&& m
->si
) {
272 callout_reset(&m
->timer
, 1, mpu401_timeout
, m
);
278 mpu401_mcallbackp(struct snd_midi
*sm
, void *arg
, int flags
)
280 /* kprintf("mpu401_callbackp\n"); */
281 mpu401_mcallback(sm
, arg
, flags
);
285 mpu401_mdescr(struct snd_midi
*sm
, void *arg
, int verbosity
)
288 return "descr mpu401";
292 mpu401_mprovider(struct snd_midi
*m
, void *arg
)
294 return "provider mpu401";