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