2 * Copyright (c) 1999 Seigo Tanimura
3 * Copyright (c) 2003 Mathew Kanner
4 * Copyright (c) 2003-2006 Yuriy Tsibizov <yuriy.tsibizov@gfk.ru>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/param.h>
32 #include <sys/types.h>
34 #include <machine/bus.h>
36 #include <sys/systm.h>
38 #include <sys/queue.h>
40 #include <sys/mutex.h>
42 #ifdef HAVE_KERNEL_OPTION_HEADERS
46 #include <dev/sound/chip.h>
47 #include <dev/sound/pcm/sound.h>
49 #include <dev/sound/midi/midi.h>
50 #include <dev/sound/midi/mpu401.h>
51 #include "mpufoi_if.h"
53 #include <dev/sound/pci/emu10kx.h>
54 #include "emu10k1-alsa%diked.h"
56 struct emu_midi_softc {
60 mpu401_intr_t *mpu_intr;
61 struct emu_sc_info *card;
62 int port; /* I/O port or I/O ptr reg */
64 int fflags; /* File flags */
65 int ihandle; /* interrupt manager handle */
68 static uint32_t emu_midi_card_intr(void *p, uint32_t arg);
69 static devclass_t emu_midi_devclass;
72 emu_mread(struct mpu401 *arg __unused, void *cookie, int reg)
74 struct emu_midi_softc *sc = cookie;
79 d = emu_rd(sc->card, 0x18 + reg, 1);
81 d = emu_rdptr(sc->card, 0, sc->port + reg);
87 emu_mwrite(struct mpu401 *arg __unused, void *cookie, int reg, unsigned char b)
89 struct emu_midi_softc *sc = cookie;
92 emu_wr(sc->card, 0x18 + reg, b, 1);
94 emu_wrptr(sc->card, 0, sc->port + reg, b);
98 emu_muninit(struct mpu401 *arg __unused, void *cookie)
100 struct emu_midi_softc *sc = cookie;
104 mtx_unlock(&sc->mtx);
109 static kobj_method_t emu_mpu_methods[] = {
110 KOBJMETHOD(mpufoi_read, emu_mread),
111 KOBJMETHOD(mpufoi_write, emu_mwrite),
112 KOBJMETHOD(mpufoi_uninit, emu_muninit),
115 static DEFINE_CLASS(emu_mpu, emu_mpu_methods, 0);
118 emu_midi_card_intr(void *p, uint32_t intr_status)
120 struct emu_midi_softc *sc = (struct emu_midi_softc *)p;
122 (sc->mpu_intr) (sc->mpu);
123 if (sc->mpu_intr == NULL) {
124 /* We should read MIDI event to unlock card after
125 * interrupt. XXX - check, why this happens. */
127 device_printf(sc->dev, "midi interrupt %08x without interrupt handler, force mread!\n", intr_status);
128 (void)emu_mread((void *)(NULL), sc, 0);
130 return (intr_status); /* Acknowledge everything */
134 emu_midi_intr(void *p)
136 (void)emu_midi_card_intr(p, 0);
140 emu_midi_probe(device_t dev)
142 struct emu_midi_softc *scp;
143 uintptr_t func, r, is_emu10k1;
145 r = BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func);
146 if (func != SCF_MIDI)
149 scp = device_get_softc(dev);
150 bzero(scp, sizeof(*scp));
151 r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ISEMU10K1, &is_emu10k1);
152 scp->is_emu10k1 = is_emu10k1 ? 1 : 0;
154 device_set_desc(dev, "EMU10Kx MIDI Interface");
159 emu_midi_attach(device_t dev)
161 struct emu_midi_softc * scp;
162 struct sndcard_func *func;
163 struct emu_midiinfo *midiinfo;
164 uint32_t inte_val, ipr_val;
166 scp = device_get_softc(dev);
167 func = device_get_ivars(dev);
170 midiinfo = (struct emu_midiinfo *)func->varinfo;
171 scp->port = midiinfo->port;
172 scp->card = midiinfo->card;
174 mtx_init(&scp->mtx, device_get_nameunit(dev), "midi softc", MTX_DEF);
176 if (scp->is_emu10k1) {
177 /* SB Live! - only one MIDI device here */
179 /* inte_val |= INTE_MIDITXENABLE;*/
180 inte_val |= INTE_MIDIRXENABLE;
181 ipr_val = IPR_MIDITRANSBUFEMPTY;
182 ipr_val |= IPR_MIDIRECVBUFEMPTY;
184 if (scp->port == A_MUDATA1) {
185 /* EXTERNAL MIDI (AudigyDrive) */
187 /* inte_val |= A_INTE_MIDITXENABLE1;*/
188 inte_val |= INTE_MIDIRXENABLE;
189 ipr_val = IPR_MIDITRANSBUFEMPTY;
190 ipr_val |= IPR_MIDIRECVBUFEMPTY;
192 /* MIDI hw config port 2 */
194 /* inte_val |= A_INTE_MIDITXENABLE2;*/
195 inte_val |= INTE_A_MIDIRXENABLE2;
196 ipr_val = IPR_A_MIDITRANSBUFEMPTY2;
197 ipr_val |= IPR_A_MIDIRECVBUFEMPTY2;
201 scp->ihandle = emu_intr_register(scp->card, inte_val, ipr_val, &emu_midi_card_intr, scp);
202 /* Init the interface. */
203 scp->mpu = mpu401_init(&emu_mpu_class, scp, emu_midi_intr, &scp->mpu_intr);
204 if (scp->mpu == NULL) {
205 emu_intr_unregister(scp->card, scp->ihandle);
206 mtx_destroy(&scp->mtx);
210 * XXX I don't know how to check for Live!Drive / AudigyDrive
211 * presence. Let's hope that IR enabling code will not harm if
215 emu_enable_ir(scp->card);
217 if (scp->port == A_MUDATA1)
218 emu_enable_ir(scp->card);
226 emu_midi_detach(device_t dev)
228 struct emu_midi_softc *scp;
230 scp = device_get_softc(dev);
231 mpu401_uninit(scp->mpu);
232 emu_intr_unregister(scp->card, scp->ihandle);
233 mtx_destroy(&scp->mtx);
237 static device_method_t emu_midi_methods[] = {
238 DEVMETHOD(device_probe, emu_midi_probe),
239 DEVMETHOD(device_attach, emu_midi_attach),
240 DEVMETHOD(device_detach, emu_midi_detach),
245 static driver_t emu_midi_driver = {
248 sizeof(struct emu_midi_softc),
250 DRIVER_MODULE(snd_emu10kx_midi, emu10kx, emu_midi_driver, emu_midi_devclass, 0, 0);
251 MODULE_DEPEND(snd_emu10kx_midi, snd_emu10kx, SND_EMU10KX_MINVER, SND_EMU10KX_PREFVER, SND_EMU10KX_MAXVER);
252 MODULE_DEPEND(snd_emu10kx_midi, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
253 MODULE_VERSION(snd_emu10kx_midi, SND_EMU10KX_PREFVER);