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