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