xref: /freebsd/sys/dev/sound/pci/emu10kx-midi.c (revision 32137c41065c3fa101bf0056b76889b98a7492cb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1999 Seigo Tanimura
5  * Copyright (c) 2003 Mathew Kanner
6  * Copyright (c) 2003-2006 Yuriy Tsibizov <yuriy.tsibizov@gfk.ru>
7  * All rights reserved
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/bus.h>
34 #include <machine/bus.h>
35 #include <sys/rman.h>
36 #include <sys/systm.h>
37 #include <sys/sbuf.h>
38 #include <sys/queue.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 
42 #ifdef HAVE_KERNEL_OPTION_HEADERS
43 #include "opt_snd.h"
44 #endif
45 
46 #include <dev/sound/pcm/sound.h>
47 
48 #include <dev/sound/midi/midi.h>
49 #include <dev/sound/midi/mpu401.h>
50 #include "mpufoi_if.h"
51 
52 #include <dev/sound/pci/emuxkireg.h>
53 #include <dev/sound/pci/emu10kx.h>
54 
55 struct emu_midi_softc {
56 	struct mtx	mtx;
57 	device_t	dev;
58 	struct mpu401	*mpu;
59 	mpu401_intr_t	*mpu_intr;
60 	struct emu_sc_info *card;
61 	int		port;			/* I/O port or I/O ptr reg */
62 	int		is_emu10k1;
63 	int		fflags;			/* File flags */
64 	int		ihandle;		/* interrupt manager handle */
65 };
66 
67 static uint32_t	emu_midi_card_intr(void *p, uint32_t arg);
68 
69 static unsigned char
70 emu_mread(struct mpu401 *arg __unused, void *cookie, int reg)
71 {
72 	struct emu_midi_softc *sc = cookie;
73 	unsigned int d;
74 
75 	d = 0;
76 	if (sc->is_emu10k1)
77 		d = emu_rd(sc->card, 0x18 + reg, 1);
78 	else
79 		d = emu_rdptr(sc->card, 0, sc->port + reg);
80 
81 	return (d);
82 }
83 
84 static void
85 emu_mwrite(struct mpu401 *arg __unused, void *cookie, int reg, unsigned char b)
86 {
87 	struct emu_midi_softc *sc = cookie;
88 
89 	if (sc->is_emu10k1)
90 		emu_wr(sc->card, 0x18 + reg, b, 1);
91 	else
92 		emu_wrptr(sc->card, 0, sc->port + reg, b);
93 }
94 
95 static int
96 emu_muninit(struct mpu401 *arg __unused, void *cookie)
97 {
98 	struct emu_midi_softc *sc = cookie;
99 
100 	mtx_lock(&sc->mtx);
101 	sc->mpu_intr = NULL;
102 	mtx_unlock(&sc->mtx);
103 
104 	return (0);
105 }
106 
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),
111 	KOBJMETHOD_END
112 };
113 static DEFINE_CLASS(emu_mpu, emu_mpu_methods, 0);
114 
115 static uint32_t
116 emu_midi_card_intr(void *p, uint32_t intr_status)
117 {
118 	struct emu_midi_softc *sc = (struct emu_midi_softc *)p;
119 	if (sc->mpu_intr)
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.  */
124 		if (bootverbose)
125 			device_printf(sc->dev, "midi interrupt %08x without interrupt handler, force mread!\n", intr_status);
126 		(void)emu_mread((void *)(NULL), sc, 0);
127 	}
128 	return (intr_status); /* Acknowledge everything */
129 }
130 
131 static void
132 emu_midi_intr(void *p)
133 {
134 	(void)emu_midi_card_intr(p, 0);
135 }
136 
137 static int
138 emu_midi_probe(device_t dev)
139 {
140 	struct emu_midi_softc *scp;
141 	uintptr_t is_emu10k1;
142 
143 	scp = device_get_softc(dev);
144 	bzero(scp, sizeof(*scp));
145 	BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ISEMU10K1, &is_emu10k1);
146 	scp->is_emu10k1 = is_emu10k1 ? 1 : 0;
147 
148 	device_set_desc(dev, "EMU10Kx MIDI Interface");
149 	return (0);
150 }
151 
152 static int
153 emu_midi_attach(device_t dev)
154 {
155 	struct emu_midi_softc * scp;
156 	struct emu_midiinfo *midiinfo;
157 	uint32_t inte_val, ipr_val;
158 
159 	scp = device_get_softc(dev);
160 
161 	scp->dev = dev;
162 	midiinfo = (struct emu_midiinfo *)device_get_ivars(dev);
163 	scp->port = midiinfo->port;
164 	scp->card = midiinfo->card;
165 
166 	mtx_init(&scp->mtx, device_get_nameunit(dev), "midi softc", MTX_DEF);
167 
168 	if (scp->is_emu10k1) {
169 		/* SB Live! - only one MIDI device here */
170 		inte_val = 0;
171 		/* inte_val |= EMU_INTE_MIDITXENABLE;*/
172 		inte_val |= EMU_INTE_MIDIRXENABLE;
173 		ipr_val = EMU_IPR_MIDITRANSBUFE;
174 		ipr_val |= EMU_IPR_MIDIRECVBUFE;
175 	} else {
176 		if (scp->port == EMU_A_MUDATA1) {
177 			/* EXTERNAL MIDI (AudigyDrive) */
178 			inte_val = 0;
179 			/* inte_val |= A_EMU_INTE_MIDITXENABLE1;*/
180 			inte_val |= EMU_INTE_MIDIRXENABLE;
181 			ipr_val = EMU_IPR_MIDITRANSBUFE;
182 			ipr_val |= EMU_IPR_MIDIRECVBUFE;
183 		} else {
184 			/* MIDI hw config port 2 */
185 			inte_val = 0;
186 			/* inte_val |= A_EMU_INTE_MIDITXENABLE2;*/
187 			inte_val |= EMU_INTE_A_MIDIRXENABLE2;
188 			ipr_val = EMU_IPR_A_MIDITRANSBUFE2;
189 			ipr_val |= EMU_IPR_A_MIDIRECBUFE2;
190 		}
191 	}
192 
193 	scp->ihandle = emu_intr_register(scp->card, inte_val, ipr_val, &emu_midi_card_intr, scp);
194 	/* Init the interface. */
195 	scp->mpu = mpu401_init(&emu_mpu_class, scp, emu_midi_intr, &scp->mpu_intr);
196 	if (scp->mpu == NULL) {
197 		emu_intr_unregister(scp->card, scp->ihandle);
198 		mtx_destroy(&scp->mtx);
199 		return (ENOMEM);
200 	}
201 	/*
202 	 * XXX I don't know how to check for Live!Drive / AudigyDrive
203 	 * presence. Let's hope that IR enabling code will not harm if
204 	 * it is not present.
205 	 */
206 	if (scp->is_emu10k1)
207 		emu_enable_ir(scp->card);
208 	else {
209 		if (scp->port == EMU_A_MUDATA1)
210 			emu_enable_ir(scp->card);
211 	}
212 
213 	return (0);
214 }
215 
216 static int
217 emu_midi_detach(device_t dev)
218 {
219 	struct emu_midi_softc *scp;
220 
221 	scp = device_get_softc(dev);
222 	mpu401_uninit(scp->mpu);
223 	emu_intr_unregister(scp->card, scp->ihandle);
224 	mtx_destroy(&scp->mtx);
225 	return (0);
226 }
227 
228 static device_method_t emu_midi_methods[] = {
229 	DEVMETHOD(device_probe, emu_midi_probe),
230 	DEVMETHOD(device_attach, emu_midi_attach),
231 	DEVMETHOD(device_detach, emu_midi_detach),
232 
233 	DEVMETHOD_END
234 };
235 
236 static driver_t emu_midi_driver = {
237 	"midi",
238 	emu_midi_methods,
239 	sizeof(struct emu_midi_softc),
240 };
241 DRIVER_MODULE(snd_emu10kx_midi, emu10kx, emu_midi_driver, 0, 0);
242 MODULE_DEPEND(snd_emu10kx_midi, snd_emu10kx, SND_EMU10KX_MINVER, SND_EMU10KX_PREFVER, SND_EMU10KX_MAXVER);
243 MODULE_DEPEND(snd_emu10kx_midi, sound, 1, 1, 1);
244 MODULE_VERSION(snd_emu10kx_midi, SND_EMU10KX_PREFVER);
245