xref: /freebsd/sys/dev/sound/pci/emu10kx-midi.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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/chip.h>
47 #include <dev/sound/pcm/sound.h>
48 
49 #include <dev/sound/midi/midi.h>
50 #include <dev/sound/midi/mpu401.h>
51 #include "mpufoi_if.h"
52 
53 #include <dev/sound/pci/emuxkireg.h>
54 #include <dev/sound/pci/emu10kx.h>
55 
56 struct emu_midi_softc {
57 	struct mtx	mtx;
58 	device_t	dev;
59 	struct mpu401	*mpu;
60 	mpu401_intr_t	*mpu_intr;
61 	struct emu_sc_info *card;
62 	int		port;			/* I/O port or I/O ptr reg */
63 	int		is_emu10k1;
64 	int		fflags;			/* File flags */
65 	int		ihandle;		/* interrupt manager handle */
66 };
67 
68 static uint32_t	emu_midi_card_intr(void *p, uint32_t arg);
69 
70 static unsigned char
71 emu_mread(struct mpu401 *arg __unused, void *cookie, int reg)
72 {
73 	struct emu_midi_softc *sc = cookie;
74 	unsigned int d;
75 
76 	d = 0;
77 	if (sc->is_emu10k1)
78 		d = emu_rd(sc->card, 0x18 + reg, 1);
79 	else
80 		d = emu_rdptr(sc->card, 0, sc->port + reg);
81 
82 	return (d);
83 }
84 
85 static void
86 emu_mwrite(struct mpu401 *arg __unused, void *cookie, int reg, unsigned char b)
87 {
88 	struct emu_midi_softc *sc = cookie;
89 
90 	if (sc->is_emu10k1)
91 		emu_wr(sc->card, 0x18 + reg, b, 1);
92 	else
93 		emu_wrptr(sc->card, 0, sc->port + reg, b);
94 }
95 
96 static int
97 emu_muninit(struct mpu401 *arg __unused, void *cookie)
98 {
99 	struct emu_midi_softc *sc = cookie;
100 
101 	mtx_lock(&sc->mtx);
102 	sc->mpu_intr = NULL;
103 	mtx_unlock(&sc->mtx);
104 
105 	return (0);
106 }
107 
108 static kobj_method_t emu_mpu_methods[] = {
109 	KOBJMETHOD(mpufoi_read, emu_mread),
110 	KOBJMETHOD(mpufoi_write, emu_mwrite),
111 	KOBJMETHOD(mpufoi_uninit, emu_muninit),
112 	KOBJMETHOD_END
113 };
114 static DEFINE_CLASS(emu_mpu, emu_mpu_methods, 0);
115 
116 static uint32_t
117 emu_midi_card_intr(void *p, uint32_t intr_status)
118 {
119 	struct emu_midi_softc *sc = (struct emu_midi_softc *)p;
120 	if (sc->mpu_intr)
121 		(sc->mpu_intr) (sc->mpu);
122 	if (sc->mpu_intr == NULL) {
123 		/* We should read MIDI event to unlock card after
124 		 * interrupt. XXX - check, why this happens.  */
125 		if (bootverbose)
126 			device_printf(sc->dev, "midi interrupt %08x without interrupt handler, force mread!\n", intr_status);
127 		(void)emu_mread((void *)(NULL), sc, 0);
128 	}
129 	return (intr_status); /* Acknowledge everything */
130 }
131 
132 static void
133 emu_midi_intr(void *p)
134 {
135 	(void)emu_midi_card_intr(p, 0);
136 }
137 
138 static int
139 emu_midi_probe(device_t dev)
140 {
141 	struct emu_midi_softc *scp;
142 	uintptr_t func, is_emu10k1;
143 
144 	BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func);
145 	if (func != SCF_MIDI)
146 		return (ENXIO);
147 
148 	scp = device_get_softc(dev);
149 	bzero(scp, sizeof(*scp));
150 	BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ISEMU10K1, &is_emu10k1);
151 	scp->is_emu10k1 = is_emu10k1 ? 1 : 0;
152 
153 	device_set_desc(dev, "EMU10Kx MIDI Interface");
154 	return (0);
155 }
156 
157 static int
158 emu_midi_attach(device_t dev)
159 {
160 	struct emu_midi_softc * scp;
161 	struct sndcard_func *func;
162 	struct emu_midiinfo *midiinfo;
163 	uint32_t inte_val, ipr_val;
164 
165 	scp = device_get_softc(dev);
166 	func = device_get_ivars(dev);
167 
168 	scp->dev = dev;
169 	midiinfo = (struct emu_midiinfo *)func->varinfo;
170 	scp->port = midiinfo->port;
171 	scp->card = midiinfo->card;
172 
173 	mtx_init(&scp->mtx, device_get_nameunit(dev), "midi softc", MTX_DEF);
174 
175 	if (scp->is_emu10k1) {
176 		/* SB Live! - only one MIDI device here */
177 		inte_val = 0;
178 		/* inte_val |= EMU_INTE_MIDITXENABLE;*/
179 		inte_val |= EMU_INTE_MIDIRXENABLE;
180 		ipr_val = EMU_IPR_MIDITRANSBUFE;
181 		ipr_val |= EMU_IPR_MIDIRECVBUFE;
182 	} else {
183 		if (scp->port == EMU_A_MUDATA1) {
184 			/* EXTERNAL MIDI (AudigyDrive) */
185 			inte_val = 0;
186 			/* inte_val |= A_EMU_INTE_MIDITXENABLE1;*/
187 			inte_val |= EMU_INTE_MIDIRXENABLE;
188 			ipr_val = EMU_IPR_MIDITRANSBUFE;
189 			ipr_val |= EMU_IPR_MIDIRECVBUFE;
190 		} else {
191 			/* MIDI hw config port 2 */
192 			inte_val = 0;
193 			/* inte_val |= A_EMU_INTE_MIDITXENABLE2;*/
194 			inte_val |= EMU_INTE_A_MIDIRXENABLE2;
195 			ipr_val = EMU_IPR_A_MIDITRANSBUFE2;
196 			ipr_val |= EMU_IPR_A_MIDIRECBUFE2;
197 		}
198 	}
199 
200 	scp->ihandle = emu_intr_register(scp->card, inte_val, ipr_val, &emu_midi_card_intr, scp);
201 	/* Init the interface. */
202 	scp->mpu = mpu401_init(&emu_mpu_class, scp, emu_midi_intr, &scp->mpu_intr);
203 	if (scp->mpu == NULL) {
204 		emu_intr_unregister(scp->card, scp->ihandle);
205 		mtx_destroy(&scp->mtx);
206 		return (ENOMEM);
207 	}
208 	/*
209 	 * XXX I don't know how to check for Live!Drive / AudigyDrive
210 	 * presence. Let's hope that IR enabling code will not harm if
211 	 * it is not present.
212 	 */
213 	if (scp->is_emu10k1)
214 		emu_enable_ir(scp->card);
215 	else {
216 		if (scp->port == EMU_A_MUDATA1)
217 			emu_enable_ir(scp->card);
218 	}
219 
220 	return (0);
221 }
222 
223 static int
224 emu_midi_detach(device_t dev)
225 {
226 	struct emu_midi_softc *scp;
227 
228 	scp = device_get_softc(dev);
229 	mpu401_uninit(scp->mpu);
230 	emu_intr_unregister(scp->card, scp->ihandle);
231 	mtx_destroy(&scp->mtx);
232 	return (0);
233 }
234 
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),
239 
240 	DEVMETHOD_END
241 };
242 
243 static driver_t emu_midi_driver = {
244 	"midi",
245 	emu_midi_methods,
246 	sizeof(struct emu_midi_softc),
247 };
248 DRIVER_MODULE(snd_emu10kx_midi, emu10kx, emu_midi_driver, 0, 0);
249 MODULE_DEPEND(snd_emu10kx_midi, snd_emu10kx, SND_EMU10KX_MINVER, SND_EMU10KX_PREFVER, SND_EMU10KX_MAXVER);
250 MODULE_DEPEND(snd_emu10kx_midi, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
251 MODULE_VERSION(snd_emu10kx_midi, SND_EMU10KX_PREFVER);
252