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
28 * $FreeBSD: head/sys/dev/sound/pci/emu10kx-midi.c 246128 2013-01-30 18:01:20Z sbz $
31 #include <sys/param.h>
32 #include <sys/types.h>
35 #include <sys/systm.h>
37 #include <sys/queue.h>
40 #ifdef HAVE_KERNEL_OPTION_HEADERS
44 #include <dev/sound/chip.h>
45 #include <dev/sound/pcm/sound.h>
47 #include <dev/sound/midi/midi.h>
48 #include <dev/sound/midi/mpu401.h>
49 #include "mpufoi_if.h"
51 #include <dev/sound/pci/emuxkireg.h>
52 #include <dev/sound/pci/emu10kx.h>
54 struct emu_midi_softc
{
58 mpu401_intr_t
*mpu_intr
;
59 struct emu_sc_info
*card
;
60 int port
; /* I/O port or I/O ptr reg */
62 int fflags
; /* File flags */
63 int ihandle
; /* interrupt manager handle */
66 static uint32_t emu_midi_card_intr(void *p
, uint32_t arg
);
67 static devclass_t emu_midi_devclass
;
70 emu_mread(struct mpu401
*arg __unused
, void *cookie
, int reg
)
72 struct emu_midi_softc
*sc
= cookie
;
77 d
= emu_rd(sc
->card
, 0x18 + reg
, 1);
79 d
= emu_rdptr(sc
->card
, 0, sc
->port
+ reg
);
85 emu_mwrite(struct mpu401
*arg __unused
, void *cookie
, int reg
, unsigned char b
)
87 struct emu_midi_softc
*sc
= cookie
;
90 emu_wr(sc
->card
, 0x18 + reg
, b
, 1);
92 emu_wrptr(sc
->card
, 0, sc
->port
+ reg
, b
);
96 emu_muninit(struct mpu401
*arg __unused
, void *cookie
)
98 struct emu_midi_softc
*sc
= cookie
;
100 lockmgr(&sc
->lock
, LK_EXCLUSIVE
);
102 lockmgr(&sc
->lock
, LK_RELEASE
);
107 static kobj_method_t emu_mpu_methods
[] = {
108 KOBJMETHOD(mpufoi_read
, emu_mread
),
109 KOBJMETHOD(mpufoi_write
, emu_mwrite
),
110 KOBJMETHOD(mpufoi_uninit
, emu_muninit
),
113 static DEFINE_CLASS(emu_mpu
, emu_mpu_methods
, 0);
116 emu_midi_card_intr(void *p
, uint32_t intr_status
)
118 struct emu_midi_softc
*sc
= (struct emu_midi_softc
*)p
;
120 (sc
->mpu_intr
) (sc
->mpu
);
121 if (sc
->mpu_intr
== NULL
) {
122 /* We should read MIDI event to unlock card after
123 * interrupt. XXX - check, why this happens. */
125 device_printf(sc
->dev
, "midi interrupt %08x without interrupt handler, force mread!\n", intr_status
);
126 (void)emu_mread((void *)(NULL
), sc
, 0);
128 return (intr_status
); /* Acknowledge everything */
132 emu_midi_intr(void *p
)
134 (void)emu_midi_card_intr(p
, 0);
138 emu_midi_probe(device_t dev
)
140 struct emu_midi_softc
*scp
;
141 uintptr_t func
, r
, is_emu10k1
;
143 r
= BUS_READ_IVAR(device_get_parent(dev
), dev
, 0, &func
);
144 if (func
!= SCF_MIDI
)
147 scp
= device_get_softc(dev
);
148 bzero(scp
, sizeof(*scp
));
149 r
= BUS_READ_IVAR(device_get_parent(dev
), dev
, EMU_VAR_ISEMU10K1
, &is_emu10k1
);
150 scp
->is_emu10k1
= is_emu10k1
? 1 : 0;
152 device_set_desc(dev
, "EMU10Kx MIDI Interface");
157 emu_midi_attach(device_t dev
)
159 struct emu_midi_softc
* scp
;
160 struct sndcard_func
*func
;
161 struct emu_midiinfo
*midiinfo
;
162 uint32_t inte_val
, ipr_val
;
164 scp
= device_get_softc(dev
);
165 func
= device_get_ivars(dev
);
168 midiinfo
= (struct emu_midiinfo
*)func
->varinfo
;
169 scp
->port
= midiinfo
->port
;
170 scp
->card
= midiinfo
->card
;
172 lockinit(&scp
->lock
, device_get_nameunit(dev
), 0, LK_CANRECURSE
);
174 if (scp
->is_emu10k1
) {
175 /* SB Live! - only one MIDI device here */
177 /* inte_val |= EMU_INTE_MIDITXENABLE;*/
178 inte_val
|= EMU_INTE_MIDIRXENABLE
;
179 ipr_val
= EMU_IPR_MIDITRANSBUFE
;
180 ipr_val
|= EMU_IPR_MIDIRECVBUFE
;
182 if (scp
->port
== EMU_A_MUDATA1
) {
183 /* EXTERNAL MIDI (AudigyDrive) */
185 /* inte_val |= A_EMU_INTE_MIDITXENABLE1;*/
186 inte_val
|= EMU_INTE_MIDIRXENABLE
;
187 ipr_val
= EMU_IPR_MIDITRANSBUFE
;
188 ipr_val
|= EMU_IPR_MIDIRECVBUFE
;
190 /* MIDI hw config port 2 */
192 /* inte_val |= A_EMU_INTE_MIDITXENABLE2;*/
193 inte_val
|= EMU_INTE_A_MIDIRXENABLE2
;
194 ipr_val
= EMU_IPR_A_MIDITRANSBUFE2
;
195 ipr_val
|= EMU_IPR_A_MIDIRECBUFE2
;
199 scp
->ihandle
= emu_intr_register(scp
->card
, inte_val
, ipr_val
, &emu_midi_card_intr
, scp
);
200 /* Init the interface. */
201 scp
->mpu
= mpu401_init(&emu_mpu_class
, scp
, emu_midi_intr
, &scp
->mpu_intr
);
202 if (scp
->mpu
== NULL
) {
203 emu_intr_unregister(scp
->card
, scp
->ihandle
);
204 lockuninit(&scp
->lock
);
208 * XXX I don't know how to check for Live!Drive / AudigyDrive
209 * presence. Let's hope that IR enabling code will not harm if
213 emu_enable_ir(scp
->card
);
215 if (scp
->port
== EMU_A_MUDATA1
)
216 emu_enable_ir(scp
->card
);
224 emu_midi_detach(device_t dev
)
226 struct emu_midi_softc
*scp
;
228 scp
= device_get_softc(dev
);
229 mpu401_uninit(scp
->mpu
);
230 emu_intr_unregister(scp
->card
, scp
->ihandle
);
231 lockuninit(&scp
->lock
);
235 static device_method_t emu_midi_methods
[] = {
236 DEVMETHOD(device_probe
, emu_midi_probe
),
237 DEVMETHOD(device_attach
, emu_midi_attach
),
238 DEVMETHOD(device_detach
, emu_midi_detach
),
243 static driver_t emu_midi_driver
= {
246 sizeof(struct emu_midi_softc
),
248 DRIVER_MODULE(snd_emu10kx_midi
, emu10kx
, emu_midi_driver
, emu_midi_devclass
,
250 MODULE_DEPEND(snd_emu10kx_midi
, snd_emu10kx
, SND_EMU10KX_MINVER
, SND_EMU10KX_PREFVER
, SND_EMU10KX_MAXVER
);
251 MODULE_DEPEND(snd_emu10kx_midi
, sound
, SOUND_MINVER
, SOUND_PREFVER
, SOUND_MAXVER
);
252 MODULE_VERSION(snd_emu10kx_midi
, SND_EMU10KX_PREFVER
);