2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015-2018 Tai-hwa Liang <avatar@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
30 #include <sys/types.h>
32 #include <machine/bus.h>
34 #include <sys/systm.h>
37 #include <sys/mutex.h>
39 #ifdef HAVE_KERNEL_OPTION_HEADERS
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
46 #include <dev/sound/chip.h>
47 #include <dev/sound/pcm/sound.h>
49 #include <dev/sound/midi/midi.h>
50 #include <dev/sound/midi/mpu401.h>
52 #include <dev/sound/pci/csareg.h>
53 #include <dev/sound/pci/csavar.h>
55 #include "mpufoi_if.h"
57 SND_DECLARE_FILE("$FreeBSD$");
59 /* pulled from mpu401.c */
60 #define MPU_DATAPORT 0
62 #define MPU_STATPORT 1
63 #define MPU_RESET 0xff
66 #define MPU_STATMASK 0xc0
67 #define MPU_OUTPUTBUSY 0x40
68 #define MPU_INPUTBUSY 0x80
70 /* device private data */
71 struct csa_midi_softc {
72 /* hardware resources */
73 int io_rid; /* io rid */
74 struct resource *io; /* io */
79 mpu401_intr_t *mpu_intr;
80 int mflags; /* MIDI flags */
83 static struct kobj_class csamidi_mpu_class;
84 static devclass_t midicsa_devclass;
87 csamidi_readio(struct csa_midi_softc *scp, u_long offset)
89 if (offset < BA0_AC97_RESET)
90 return bus_space_read_4(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), offset) & 0xffffffff;
96 csamidi_writeio(struct csa_midi_softc *scp, u_long offset, u_int32_t data)
98 if (offset < BA0_AC97_RESET)
99 bus_space_write_4(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), offset, data);
103 csamidi_midi_intr(void *arg)
105 struct csa_midi_softc *scp = (struct csa_midi_softc *)arg;
108 (scp->mpu_intr)(scp->mpu);
112 csamidi_mread(struct mpu401 *arg __unused, void *cookie, int reg)
114 struct csa_midi_softc *scp = cookie;
116 unsigned int uart_stat;
119 /* hacks to convert hardware status to MPU compatible ones */
122 uart_stat = csamidi_readio(scp, BA0_MIDSR);
123 if (uart_stat & MIDSR_TBF)
124 rc |= MPU_OUTPUTBUSY; /* Tx buffer full */
125 if (uart_stat & MIDSR_RBE)
129 rc = csamidi_readio(scp, BA0_MIDRP);
132 printf("csamidi_mread: unknown register %d\n", reg);
139 csamidi_mwrite(struct mpu401 *arg __unused, void *cookie, int reg, unsigned char b)
141 struct csa_midi_softc *scp = cookie;
149 /* preserve current operation mode */
150 val = csamidi_readio(scp, BA0_MIDCR);
151 /* reset the MIDI port */
152 csamidi_writeio(scp, BA0_MIDCR, MIDCR_MRST);
153 csamidi_writeio(scp, BA0_MIDCR, MIDCR_MLB);
154 csamidi_writeio(scp, BA0_MIDCR, 0x00);
155 /* restore previous operation mode */
156 csamidi_writeio(scp, BA0_MIDCR, val);
159 /* switch to UART mode, no-op */
165 /* put the MIDI databyte in the write port */
166 csamidi_writeio(scp, BA0_MIDWP, b);
169 printf("csamidi_mwrite: unknown register %d\n", reg);
175 csamidi_muninit(struct mpu401 *arg __unused, void *cookie)
177 struct csa_midi_softc *scp = cookie;
180 scp->mpu_intr = NULL;
181 mtx_unlock(&scp->mtx);
187 midicsa_probe(device_t dev)
189 struct sndcard_func *func;
191 /* The parent device has already been probed. */
193 func = device_get_ivars(dev);
194 if (func == NULL || func->func != SCF_MIDI)
197 device_set_desc(dev, "CS461x MIDI");
202 midicsa_attach(device_t dev)
204 struct csa_midi_softc *scp;
205 struct sndcard_func *func;
208 scp = device_get_softc(dev);
209 func = device_get_ivars(dev);
211 bzero(scp, sizeof(struct csa_midi_softc));
214 /* allocate the required resources */
215 scp->io_rid = PCIR_BAR(0);
216 scp->io = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
217 &scp->io_rid, RF_ACTIVE);
221 /* init the fake MPU401 interface. */
222 scp->mpu = mpu401_init(&csamidi_mpu_class, scp, csamidi_midi_intr,
224 if (scp->mpu == NULL) {
229 mtx_init(&scp->mtx, device_get_nameunit(dev), "csamidi softc",
232 /* reset the MIDI port */
233 csamidi_writeio(scp, BA0_MIDCR, MIDCR_MRST);
234 /* MIDI transmit enable, no interrupt */
235 csamidi_writeio(scp, BA0_MIDCR, MIDCR_TXE | MIDCR_RXE);
236 csamidi_writeio(scp, BA0_HICR, HICR_IEV | HICR_CHGM);
240 bus_release_resource(dev, SYS_RES_MEMORY, scp->io_rid, scp->io);
247 midicsa_detach(device_t dev)
249 struct csa_midi_softc *scp;
252 scp = device_get_softc(dev);
253 rc = mpu401_uninit(scp->mpu);
256 if (scp->io != NULL) {
257 bus_release_resource(dev, SYS_RES_MEMORY, scp->io_rid,
261 mtx_destroy(&scp->mtx);
265 static kobj_method_t csamidi_mpu_methods[] = {
266 KOBJMETHOD(mpufoi_read, csamidi_mread),
267 KOBJMETHOD(mpufoi_write, csamidi_mwrite),
268 KOBJMETHOD(mpufoi_uninit, csamidi_muninit),
272 static DEFINE_CLASS(csamidi_mpu, csamidi_mpu_methods, 0);
274 static device_method_t midicsa_methods[] = {
275 DEVMETHOD(device_probe, midicsa_probe),
276 DEVMETHOD(device_attach, midicsa_attach),
277 DEVMETHOD(device_detach, midicsa_detach),
282 static driver_t midicsa_driver = {
285 sizeof(struct csa_midi_softc),
287 DRIVER_MODULE(snd_csa_midi, csa, midicsa_driver, midicsa_devclass, 0, 0);
288 MODULE_DEPEND(snd_csa_midi, snd_csa, 1, 1, 1);
289 MODULE_DEPEND(snd_csa_midi, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
290 MODULE_VERSION(snd_csa_midi, 1);